Search in sources :

Example 1 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method serializeValue.

@SuppressWarnings("unchecked")
public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
    int pointer = 0;
    switch(type) {
        case INTEGER:
        case LONG:
        case SHORT:
            pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
            break;
        case STRING:
            pointer = writeString(bytes, value.toString());
            break;
        case DOUBLE:
            long dg = Double.doubleToLongBits((Double) value);
            pointer = bytes.alloc(OLongSerializer.LONG_SIZE);
            OLongSerializer.INSTANCE.serializeLiteral(dg, bytes.bytes, pointer);
            break;
        case FLOAT:
            int fg = Float.floatToIntBits((Float) value);
            pointer = bytes.alloc(OIntegerSerializer.INT_SIZE);
            OIntegerSerializer.INSTANCE.serializeLiteral(fg, bytes.bytes, pointer);
            break;
        case BYTE:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = (Byte) value;
            break;
        case BOOLEAN:
            pointer = bytes.alloc(1);
            bytes.bytes[pointer] = ((Boolean) value) ? (byte) 1 : (byte) 0;
            break;
        case DATETIME:
            if (value instanceof Long) {
                pointer = OVarIntSerializer.write(bytes, (Long) value);
            } else
                pointer = OVarIntSerializer.write(bytes, ((Date) value).getTime());
            break;
        case DATE:
            long dateValue;
            if (value instanceof Long) {
                dateValue = (Long) value;
            } else
                dateValue = ((Date) value).getTime();
            dateValue = convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), dateValue);
            pointer = OVarIntSerializer.write(bytes, dateValue / MILLISEC_PER_DAY);
            break;
        case EMBEDDED:
            pointer = bytes.offset;
            if (value instanceof ODocumentSerializable) {
                ODocument cur = ((ODocumentSerializable) value).toDocument();
                cur.field(ODocumentSerializable.CLASS_NAME, value.getClass().getName());
                serialize(cur, bytes, false);
            } else {
                serialize((ODocument) value, bytes, false);
            }
            break;
        case EMBEDDEDSET:
        case EMBEDDEDLIST:
            if (value.getClass().isArray())
                pointer = writeEmbeddedCollection(bytes, Arrays.asList(OMultiValue.array(value)), linkedType);
            else
                pointer = writeEmbeddedCollection(bytes, (Collection<?>) value, linkedType);
            break;
        case DECIMAL:
            BigDecimal decimalValue = (BigDecimal) value;
            pointer = bytes.alloc(ODecimalSerializer.INSTANCE.getObjectSize(decimalValue));
            ODecimalSerializer.INSTANCE.serialize(decimalValue, bytes.bytes, pointer);
            break;
        case BINARY:
            pointer = writeBinary(bytes, (byte[]) (value));
            break;
        case LINKSET:
        case LINKLIST:
            Collection<OIdentifiable> ridCollection = (Collection<OIdentifiable>) value;
            pointer = writeLinkCollection(bytes, ridCollection);
            break;
        case LINK:
            if (!(value instanceof OIdentifiable))
                throw new OValidationException("Value '" + value + "' is not a OIdentifiable");
            pointer = writeOptimizedLink(bytes, (OIdentifiable) value);
            break;
        case LINKMAP:
            pointer = writeLinkMap(bytes, (Map<Object, OIdentifiable>) value);
            break;
        case EMBEDDEDMAP:
            pointer = writeEmbeddedMap(bytes, (Map<Object, Object>) value);
            break;
        case LINKBAG:
            pointer = ((ORidBag) value).toStream(bytes);
            break;
        case CUSTOM:
            if (!(value instanceof OSerializableStream))
                value = new OSerializableWrapper((Serializable) value);
            pointer = writeString(bytes, value.getClass().getName());
            writeBinary(bytes, ((OSerializableStream) value).toStream());
            break;
        case TRANSIENT:
            break;
        case ANY:
            break;
    }
    return pointer;
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) BigDecimal(java.math.BigDecimal) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORecordSerializerNetworkV0 method writeLinkMap.

private int writeLinkMap(final BytesContainer bytes, final Map<Object, OIdentifiable> map) {
    final boolean disabledAutoConversion = map instanceof ORecordLazyMultiValue && ((ORecordLazyMultiValue) map).isAutoConvertToRecord();
    if (disabledAutoConversion)
        // AVOID TO FETCH RECORD
        ((ORecordLazyMultiValue) map).setAutoConvertToRecord(false);
    try {
        final int fullPos = OVarIntSerializer.write(bytes, map.size());
        for (Entry<Object, OIdentifiable> entry : map.entrySet()) {
            // TODO:check skip of complex types
            // FIXME: changed to support only string key on map
            final OType type = OType.STRING;
            writeOType(bytes, bytes.alloc(1), type);
            writeString(bytes, entry.getKey().toString());
            if (entry.getValue() == null)
                writeNullLink(bytes);
            else
                writeOptimizedLink(bytes, entry.getValue());
        }
        return fullPos;
    } finally {
        if (disabledAutoConversion)
            ((ORecordLazyMultiValue) map).setAutoConvertToRecord(true);
    }
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Example 3 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ORestrictedAccessHook method onRecordBeforeCreate.

@Override
public RESULT onRecordBeforeCreate(final ODocument iDocument) {
    final OImmutableClass cls = ODocumentInternal.getImmutableSchemaClass(iDocument);
    if (cls != null && cls.isRestricted()) {
        String fieldNames = cls.getCustom(OSecurityShared.ONCREATE_FIELD);
        if (fieldNames == null)
            fieldNames = ORestrictedOperation.ALLOW_ALL.getFieldName();
        final String[] fields = fieldNames.split(",");
        String identityType = cls.getCustom(OSecurityShared.ONCREATE_IDENTITY_TYPE);
        if (identityType == null)
            identityType = "user";
        OIdentifiable identity = null;
        if (identityType.equals("user")) {
            final OSecurityUser user = database.getUser();
            if (user != null)
                identity = user.getIdentity();
        } else if (identityType.equals("role")) {
            final Set<? extends OSecurityRole> roles = database.getUser().getRoles();
            if (!roles.isEmpty())
                identity = roles.iterator().next().getIdentity();
        } else
            throw new OConfigurationException("Wrong custom field '" + OSecurityShared.ONCREATE_IDENTITY_TYPE + "' in class '" + cls.getName() + "' with value '" + identityType + "'. Supported ones are: 'user', 'role'");
        if (identity != null) {
            for (String f : fields) database.getMetadata().getSecurity().allowIdentity(iDocument, f, identity);
            return RESULT.RECORD_CHANGED;
        }
    }
    return RESULT.RECORD_NOT_CHANGED;
}
Also used : OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) Set(java.util.Set) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 4 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class OSBTreeCollectionManagerShared method loadTree.

@Override
protected OSBTreeBonsai<OIdentifiable, Integer> loadTree(OBonsaiCollectionPointer collectionPointer) {
    String fileName;
    OAtomicOperation atomicOperation = storage.getAtomicOperationsManager().getCurrentOperation();
    if (atomicOperation == null) {
        fileName = storage.getWriteCache().fileNameById(collectionPointer.getFileId());
    } else {
        fileName = atomicOperation.fileNameById(collectionPointer.getFileId());
    }
    OSBTreeBonsaiLocal<OIdentifiable, Integer> tree = new OSBTreeBonsaiLocal<OIdentifiable, Integer>(fileName.substring(0, fileName.length() - DEFAULT_EXTENSION.length()), DEFAULT_EXTENSION, storage);
    if (tree.load(collectionPointer.getRootPointer()))
        return tree;
    else
        return null;
}
Also used : OAtomicOperation(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation) OSBTreeBonsaiLocal(com.orientechnologies.orient.core.index.sbtreebonsai.local.OSBTreeBonsaiLocal) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 5 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable in project orientdb by orientechnologies.

the class ODatabaseCompare method compareIndexes.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void compareIndexes(ODocumentHelper.RIDMapper ridMapper) {
    listener.onMessage("\nStarting index comparison:");
    boolean ok = true;
    final OIndexManager indexManagerOne = makeDbCall(databaseOne, new ODbRelatedCall<OIndexManager>() {

        public OIndexManager call(ODatabaseDocumentInternal database) {
            return database.getMetadata().getIndexManager();
        }
    });
    final OIndexManager indexManagerTwo = makeDbCall(databaseTwo, new ODbRelatedCall<OIndexManager>() {

        public OIndexManager call(ODatabaseDocumentInternal database) {
            return database.getMetadata().getIndexManager();
        }
    });
    final Collection<? extends OIndex<?>> indexesOne = makeDbCall(databaseOne, new ODbRelatedCall<Collection<? extends OIndex<?>>>() {

        public Collection<? extends OIndex<?>> call(ODatabaseDocumentInternal database) {
            return indexManagerOne.getIndexes();
        }
    });
    int indexesSizeOne = makeDbCall(databaseTwo, new ODbRelatedCall<Integer>() {

        public Integer call(ODatabaseDocumentInternal database) {
            return indexesOne.size();
        }
    });
    int indexesSizeTwo = makeDbCall(databaseTwo, new ODbRelatedCall<Integer>() {

        public Integer call(ODatabaseDocumentInternal database) {
            return indexManagerTwo.getIndexes().size();
        }
    });
    if (exportImportHashTable != null)
        indexesSizeTwo--;
    if (indexesSizeOne != indexesSizeTwo) {
        ok = false;
        listener.onMessage("\n- ERR: Amount of indexes are different.");
        listener.onMessage("\n--- DB1: " + indexesSizeOne);
        listener.onMessage("\n--- DB2: " + indexesSizeTwo);
        listener.onMessage("\n");
        ++differences;
    }
    final Iterator<? extends OIndex<?>> iteratorOne = makeDbCall(databaseOne, new ODbRelatedCall<Iterator<? extends OIndex<?>>>() {

        public Iterator<? extends OIndex<?>> call(ODatabaseDocumentInternal database) {
            return indexesOne.iterator();
        }
    });
    while (makeDbCall(databaseOne, new ODbRelatedCall<Boolean>() {

        public Boolean call(ODatabaseDocumentInternal database) {
            return iteratorOne.hasNext();
        }
    })) {
        final OIndex indexOne = makeDbCall(databaseOne, new ODbRelatedCall<OIndex<?>>() {

            public OIndex<?> call(ODatabaseDocumentInternal database) {
                return iteratorOne.next();
            }
        });
        final OIndex<?> indexTwo = makeDbCall(databaseTwo, new ODbRelatedCall<OIndex<?>>() {

            public OIndex<?> call(ODatabaseDocumentInternal database) {
                return indexManagerTwo.getIndex(indexOne.getName());
            }
        });
        if (indexTwo == null) {
            ok = false;
            listener.onMessage("\n- ERR: Index " + indexOne.getName() + " is absent in DB2.");
            ++differences;
            continue;
        }
        if (!indexOne.getType().equals(indexTwo.getType())) {
            ok = false;
            listener.onMessage("\n- ERR: Index types for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOne.getType());
            listener.onMessage("\n--- DB2: " + indexTwo.getType());
            listener.onMessage("\n");
            ++differences;
            continue;
        }
        if (!indexOne.getClusters().equals(indexTwo.getClusters())) {
            ok = false;
            listener.onMessage("\n- ERR: Clusters to index for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOne.getClusters());
            listener.onMessage("\n--- DB2: " + indexTwo.getClusters());
            listener.onMessage("\n");
            ++differences;
            continue;
        }
        if (indexOne.getDefinition() == null && indexTwo.getDefinition() != null) {
            ok = false;
            listener.onMessage("\n- ERR: Index definition for index " + indexOne.getName() + " for DB2 is not null.");
            ++differences;
            continue;
        } else if (indexOne.getDefinition() != null && indexTwo.getDefinition() == null) {
            ok = false;
            listener.onMessage("\n- ERR: Index definition for index " + indexOne.getName() + " for DB2 is null.");
            ++differences;
            continue;
        } else if (indexOne.getDefinition() != null && !indexOne.getDefinition().equals(indexTwo.getDefinition())) {
            ok = false;
            listener.onMessage("\n- ERR: Index definitions for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOne.getDefinition());
            listener.onMessage("\n--- DB2: " + indexTwo.getDefinition());
            listener.onMessage("\n");
            ++differences;
            continue;
        }
        final long indexOneSize = makeDbCall(databaseOne, new ODbRelatedCall<Long>() {

            public Long call(ODatabaseDocumentInternal database) {
                return indexOne.getSize();
            }
        });
        final long indexTwoSize = makeDbCall(databaseTwo, new ODbRelatedCall<Long>() {

            public Long call(ODatabaseDocumentInternal database) {
                return indexTwo.getSize();
            }
        });
        if (indexOneSize != indexTwoSize) {
            ok = false;
            listener.onMessage("\n- ERR: Amount of entries for index " + indexOne.getName() + " are different.");
            listener.onMessage("\n--- DB1: " + indexOneSize);
            listener.onMessage("\n--- DB2: " + indexTwoSize);
            listener.onMessage("\n");
            ++differences;
        }
        if (compareIndexMetadata) {
            final ODocument metadataOne = indexOne.getMetadata();
            final ODocument metadataTwo = indexTwo.getMetadata();
            if (metadataOne == null && metadataTwo != null) {
                ok = false;
                listener.onMessage("\n- ERR: Metadata for index " + indexOne.getName() + " for DB1 is null but for DB2 is not.");
                listener.onMessage("\n");
                ++differences;
            } else if (metadataOne != null && metadataTwo == null) {
                ok = false;
                listener.onMessage("\n- ERR: Metadata for index " + indexOne.getName() + " for DB1 is not null but for DB2 is null.");
                listener.onMessage("\n");
                ++differences;
            } else if (metadataOne != null && metadataTwo != null && !ODocumentHelper.hasSameContentOf(metadataOne, databaseOne, metadataTwo, databaseTwo, ridMapper)) {
                ok = false;
                listener.onMessage("\n- ERR: Metadata for index " + indexOne.getName() + " for DB1 and for DB2 are different.");
                makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                    @Override
                    public Object call(ODatabaseDocumentInternal database) {
                        listener.onMessage("\n--- M1: " + metadataOne);
                        return null;
                    }
                });
                makeDbCall(databaseTwo, new ODbRelatedCall<Object>() {

                    @Override
                    public Object call(ODatabaseDocumentInternal database) {
                        listener.onMessage("\n--- M2: " + metadataTwo);
                        return null;
                    }
                });
                listener.onMessage("\n");
                ++differences;
            }
        }
        if (((compareEntriesForAutomaticIndexes && !indexOne.getType().equals("DICTIONARY")) || !indexOne.isAutomatic())) {
            final OIndexKeyCursor indexKeyCursorOne = makeDbCall(databaseOne, new ODbRelatedCall<OIndexKeyCursor>() {

                public OIndexKeyCursor call(ODatabaseDocumentInternal database) {
                    return indexOne.keyCursor();
                }
            });
            Object key = makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                @Override
                public Object call(ODatabaseDocumentInternal database) {
                    return indexKeyCursorOne.next(-1);
                }
            });
            while (key != null) {
                final Object indexKey = key;
                Object indexOneValue = makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                    public Object call(ODatabaseDocumentInternal database) {
                        return indexOne.get(indexKey);
                    }
                });
                final Object indexTwoValue = makeDbCall(databaseTwo, new ODbRelatedCall<Object>() {

                    public Object call(ODatabaseDocumentInternal database) {
                        return indexTwo.get(indexKey);
                    }
                });
                if (indexTwoValue == null) {
                    ok = false;
                    listener.onMessage("\n- ERR: Entry with key " + key + " is absent in index " + indexOne.getName() + " for DB2.");
                    ++differences;
                } else if (indexOneValue instanceof Set && indexTwoValue instanceof Set) {
                    final Set<Object> indexOneValueSet = (Set<Object>) indexOneValue;
                    final Set<Object> indexTwoValueSet = (Set<Object>) indexTwoValue;
                    if (!ODocumentHelper.compareSets(databaseOne, indexOneValueSet, databaseTwo, indexTwoValueSet, ridMapper)) {
                        ok = false;
                        reportIndexDiff(indexOne, key, indexOneValue, indexTwoValue);
                    }
                } else if (indexOneValue instanceof ORID && indexTwoValue instanceof ORID) {
                    if (ridMapper != null && ((ORID) indexOneValue).isPersistent()) {
                        OIdentifiable identifiable = ridMapper.map((ORID) indexOneValue);
                        if (identifiable != null)
                            indexOneValue = identifiable.getIdentity();
                    }
                    if (!indexOneValue.equals(indexTwoValue)) {
                        ok = false;
                        reportIndexDiff(indexOne, key, indexOneValue, indexTwoValue);
                    }
                } else if (!indexOneValue.equals(indexTwoValue)) {
                    ok = false;
                    reportIndexDiff(indexOne, key, indexOneValue, indexTwoValue);
                }
                key = makeDbCall(databaseOne, new ODbRelatedCall<Object>() {

                    @Override
                    public Object call(ODatabaseDocumentInternal database) {
                        return indexKeyCursorOne.next(-1);
                    }
                });
            }
        }
    }
    if (ok)
        listener.onMessage("OK");
}
Also used : Set(java.util.Set) OIndex(com.orientechnologies.orient.core.index.OIndex) ODbRelatedCall(com.orientechnologies.orient.core.record.impl.ODocumentHelper.ODbRelatedCall) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) Iterator(java.util.Iterator) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OIndexKeyCursor(com.orientechnologies.orient.core.index.OIndexKeyCursor) OIndexManager(com.orientechnologies.orient.core.index.OIndexManager) Collection(java.util.Collection)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)536 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)278 ORecordId (com.orientechnologies.orient.core.id.ORecordId)120 Test (org.testng.annotations.Test)104 HashSet (java.util.HashSet)89 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)79 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)70 ORID (com.orientechnologies.orient.core.id.ORID)56 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)47 Test (org.junit.Test)43 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)42 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)41 ArrayList (java.util.ArrayList)39 ORecord (com.orientechnologies.orient.core.record.ORecord)35 Map (java.util.Map)31 ByteBuffer (java.nio.ByteBuffer)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)22 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)22 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)21