Search in sources :

Example 46 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class ORecordSerializerSchemaAware2CSV method toString.

@Override
protected StringBuilder toString(ORecord iRecord, final StringBuilder iOutput, final String iFormat, OUserObject2RecordHandler iObjHandler, final boolean iOnlyDelta, final boolean autoDetectCollectionType) {
    if (iRecord == null)
        throw new OSerializationException("Expected a record but was null");
    if (!(iRecord instanceof ODocument))
        throw new OSerializationException("Cannot marshall a record of type " + iRecord.getClass().getSimpleName());
    final ODocument record = (ODocument) iRecord;
    if (!iOnlyDelta && ODocumentInternal.getImmutableSchemaClass(record) != null) {
        iOutput.append(ODocumentInternal.getImmutableSchemaClass(record).getStreamableName());
        iOutput.append(OStringSerializerHelper.CLASS_SEPARATOR);
    }
    OProperty prop;
    OType type;
    OClass linkedClass;
    OType linkedType;
    String fieldClassName;
    int i = 0;
    final String[] fieldNames = iOnlyDelta && record.isTrackingChanges() ? record.getDirtyFields() : record.fieldNames();
    // MARSHALL ALL THE FIELDS OR DELTA IF TRACKING IS ENABLED
    for (String fieldName : fieldNames) {
        Object fieldValue = record.rawField(fieldName);
        if (i > 0)
            iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
        // SEARCH FOR A CONFIGURED PROPERTY
        prop = ODocumentInternal.getImmutableSchemaClass(record) != null ? ODocumentInternal.getImmutableSchemaClass(record).getProperty(fieldName) : null;
        fieldClassName = getClassName(fieldValue);
        type = record.fieldType(fieldName);
        if (type == OType.ANY)
            type = null;
        linkedClass = null;
        linkedType = null;
        if (prop != null && prop.getType() != OType.ANY) {
            // RECOGNIZED PROPERTY
            type = prop.getType();
            linkedClass = prop.getLinkedClass();
            linkedType = prop.getLinkedType();
        } else if (fieldValue != null) {
            // NOT FOUND: TRY TO DETERMINE THE TYPE FROM ITS CONTENT
            if (type == null) {
                if (fieldValue.getClass() == byte[].class)
                    type = OType.BINARY;
                else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && fieldValue instanceof ORecord) {
                    if (type == null)
                        // DETERMINE THE FIELD TYPE
                        if (fieldValue instanceof ODocument && ((ODocument) fieldValue).hasOwners())
                            type = OType.EMBEDDED;
                        else
                            type = OType.LINK;
                    linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), fieldClassName);
                } else if (fieldValue instanceof ORID)
                    // DETERMINE THE FIELD TYPE
                    type = OType.LINK;
                else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(fieldClassName) != null) {
                    // DETERMINE THE FIELD TYPE
                    type = OType.LINK;
                    linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), fieldClassName);
                } else if (fieldValue instanceof Date)
                    type = OType.DATETIME;
                else if (fieldValue instanceof String)
                    type = OType.STRING;
                else if (fieldValue instanceof Integer || fieldValue instanceof BigInteger)
                    type = OType.INTEGER;
                else if (fieldValue instanceof Long)
                    type = OType.LONG;
                else if (fieldValue instanceof Float)
                    type = OType.FLOAT;
                else if (fieldValue instanceof Short)
                    type = OType.SHORT;
                else if (fieldValue instanceof Byte)
                    type = OType.BYTE;
                else if (fieldValue instanceof Double)
                    type = OType.DOUBLE;
                else if (fieldValue instanceof BigDecimal)
                    type = OType.DECIMAL;
                else if (fieldValue instanceof ORidBag)
                    type = OType.LINKBAG;
                if (fieldValue instanceof OMultiCollectionIterator<?>) {
                    type = ((OMultiCollectionIterator<?>) fieldValue).isEmbedded() ? OType.EMBEDDEDLIST : OType.LINKLIST;
                    linkedType = ((OMultiCollectionIterator<?>) fieldValue).isEmbedded() ? OType.EMBEDDED : OType.LINK;
                } else if (fieldValue instanceof Collection<?> || fieldValue.getClass().isArray()) {
                    final int size = OMultiValue.getSize(fieldValue);
                    Boolean autoConvertLinks = null;
                    if (fieldValue instanceof ORecordLazyMultiValue) {
                        autoConvertLinks = ((ORecordLazyMultiValue) fieldValue).isAutoConvertToRecord();
                        if (autoConvertLinks)
                            // DISABLE AUTO CONVERT
                            ((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(false);
                    }
                    if (autoDetectCollectionType)
                        if (size > 0) {
                            final Object firstValue = OMultiValue.getFirstValue(fieldValue);
                            if (firstValue != null) {
                                if (firstValue instanceof ORID) {
                                    linkedClass = null;
                                    linkedType = OType.LINK;
                                    if (fieldValue instanceof Set<?>)
                                        type = OType.LINKSET;
                                    else
                                        type = OType.LINKLIST;
                                } else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && (firstValue instanceof ODocument && !((ODocument) firstValue).isEmbedded()) && (firstValue instanceof ORecord || (ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
                                    linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), getClassName(firstValue));
                                    if (type == null) {
                                        // LINK: GET THE CLASS
                                        linkedType = OType.LINK;
                                        if (fieldValue instanceof Set<?>)
                                            type = OType.LINKSET;
                                        else
                                            type = OType.LINKLIST;
                                    } else
                                        linkedType = OType.EMBEDDED;
                                } else {
                                    // EMBEDDED COLLECTION
                                    if (firstValue instanceof ODocument && ((((ODocument) firstValue).hasOwners()) || type == OType.EMBEDDEDSET || type == OType.EMBEDDEDLIST || type == OType.EMBEDDEDMAP))
                                        linkedType = OType.EMBEDDED;
                                    else if (firstValue instanceof Enum<?>)
                                        linkedType = OType.STRING;
                                    else {
                                        linkedType = OType.getTypeByClass(firstValue.getClass());
                                        if (linkedType != OType.LINK)
                                            // EMBEDDED FOR SURE DON'T USE THE LINKED TYPE
                                            linkedType = null;
                                    }
                                    if (type == null)
                                        if (fieldValue instanceof ORecordLazySet)
                                            type = OType.LINKSET;
                                        else if (fieldValue instanceof Set<?>)
                                            type = OType.EMBEDDEDSET;
                                        else
                                            type = OType.EMBEDDEDLIST;
                                }
                            }
                        } else if (type == null)
                            type = OType.EMBEDDEDLIST;
                    if (fieldValue instanceof ORecordLazyMultiValue && autoConvertLinks) {
                        // REPLACE PREVIOUS SETTINGS
                        ((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(true);
                    }
                } else if (fieldValue instanceof Map<?, ?> && type == null) {
                    final int size = OMultiValue.getSize(fieldValue);
                    Boolean autoConvertLinks = null;
                    if (fieldValue instanceof ORecordLazyMap) {
                        autoConvertLinks = ((ORecordLazyMap) fieldValue).isAutoConvertToRecord();
                        if (autoConvertLinks)
                            // DISABLE AUTO CONVERT
                            ((ORecordLazyMap) fieldValue).setAutoConvertToRecord(false);
                    }
                    if (size > 0) {
                        final Object firstValue = OMultiValue.getFirstValue(fieldValue);
                        if (firstValue != null) {
                            if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && (firstValue instanceof ODocument && !((ODocument) firstValue).isEmbedded()) && (firstValue instanceof ORecord || (ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
                                linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), getClassName(firstValue));
                                // LINK: GET THE CLASS
                                linkedType = OType.LINK;
                                type = OType.LINKMAP;
                            }
                        }
                    }
                    if (type == null)
                        type = OType.EMBEDDEDMAP;
                    if (fieldValue instanceof ORecordLazyMap && autoConvertLinks)
                        // REPLACE PREVIOUS SETTINGS
                        ((ORecordLazyMap) fieldValue).setAutoConvertToRecord(true);
                }
            }
        }
        if (type == OType.TRANSIENT)
            // TRANSIENT FIELD
            continue;
        if (type == null)
            type = OType.EMBEDDED;
        iOutput.append(fieldName);
        iOutput.append(FIELD_VALUE_SEPARATOR);
        fieldToStream(record, iOutput, iObjHandler, type, linkedClass, linkedType, fieldName, fieldValue, true);
        i++;
    }
    // GET THE OVERSIZE IF ANY
    final float overSize;
    if (ODocumentInternal.getImmutableSchemaClass(record) != null)
        // GET THE CONFIGURED OVERSIZE SETTED PER CLASS
        overSize = ODocumentInternal.getImmutableSchemaClass(record).getOverSize();
    else
        overSize = 0;
    // APPEND BLANKS IF NEEDED
    final int newSize;
    if (record.hasOwners())
        // EMBEDDED: GET REAL SIZE
        newSize = iOutput.length();
    else if (record.getSize() == iOutput.length())
        // IDENTICAL! DO NOTHING
        newSize = record.getSize();
    else if (record.getSize() > iOutput.length() && !OGlobalConfiguration.RECORD_DOWNSIZING_ENABLED.getValueAsBoolean()) {
        // APPEND EXTRA SPACES TO FILL ALL THE AVAILABLE SPACE AND AVOID FRAGMENTATION
        newSize = record.getSize();
    } else if (overSize > 0) {
        // APPEND EXTRA SPACES TO GET A LARGER iOutput
        newSize = (int) (iOutput.length() * overSize);
    } else
        // NO OVERSIZE
        newSize = iOutput.length();
    if (newSize > iOutput.length()) {
        iOutput.ensureCapacity(newSize);
        for (int b = iOutput.length(); b < newSize; ++b) iOutput.append(' ');
    }
    return iOutput;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) HashSet(java.util.HashSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) Date(java.util.Date) BigDecimal(java.math.BigDecimal) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue) BigInteger(java.math.BigInteger) ORecord(com.orientechnologies.orient.core.record.ORecord) BigInteger(java.math.BigInteger) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map)

Example 47 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateProperty method execute.

/**
   * Execute the CREATE PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (type == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocument database = getDatabase();
    final OClassImpl sourceClass = (OClassImpl) database.getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    OPropertyImpl prop = (OPropertyImpl) sourceClass.getProperty(fieldName);
    if (prop != null) {
        if (ifNotExists) {
            return sourceClass.properties().size();
        }
        throw new OCommandExecutionException("Property '" + className + "." + fieldName + "' already exists. Remove it before to retry.");
    }
    // CREATE THE PROPERTY
    OClass linkedClass = null;
    OType linkedType = null;
    if (linked != null) {
        // FIRST SEARCH BETWEEN CLASSES
        linkedClass = database.getMetadata().getSchema().getClass(linked);
        if (linkedClass == null)
            // NOT FOUND: SEARCH BETWEEN TYPES
            linkedType = OType.valueOf(linked.toUpperCase(Locale.ENGLISH));
    }
    // CREATE IT LOCALLY
    OPropertyImpl internalProp = sourceClass.addPropertyInternal(fieldName, type, linkedType, linkedClass, unsafe);
    boolean toSave = false;
    if (readonly) {
        internalProp.setReadonly(true);
        toSave = true;
    }
    if (mandatory) {
        internalProp.setMandatory(true);
        toSave = true;
    }
    if (notnull) {
        internalProp.setNotNull(true);
        toSave = true;
    }
    if (max != null) {
        internalProp.setMax(max);
        toSave = true;
    }
    if (min != null) {
        internalProp.setMin(min);
        toSave = true;
    }
    if (defaultValue != null) {
        internalProp.setDefaultValue(defaultValue);
        toSave = true;
    }
    if (collate != null) {
        internalProp.setCollate(collate);
        toSave = true;
    }
    if (regex != null) {
        internalProp.setRegexp(regex);
        toSave = true;
    }
    if (toSave) {
        internalProp.save();
    }
    return sourceClass.properties().size();
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)

Example 48 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OCompositeKeySerializer method getObjectSize.

public int getObjectSize(OCompositeKey compositeKey, Object... hints) {
    final OType[] types = getKeyTypes(hints);
    final List<Object> keys = compositeKey.getKeys();
    int size = 2 * OIntegerSerializer.INT_SIZE;
    final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance();
    for (int i = 0; i < keys.size(); i++) {
        final Object key = keys.get(i);
        if (key != null) {
            final OType type;
            if (types.length > i)
                type = types[i];
            else
                type = OType.getTypeByClass(key.getClass());
            size += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE + ((OBinarySerializer<Object>) factory.getObjectSerializer(type)).getObjectSize(key);
        } else {
            size += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE + ONullSerializer.INSTANCE.getObjectSize(null);
        }
    }
    return size;
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OBinarySerializer(com.orientechnologies.common.serialization.types.OBinarySerializer) OBinarySerializerFactory(com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)

Example 49 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OCompositeKeySerializer method serializeNativeObject.

public void serializeNativeObject(OCompositeKey compositeKey, byte[] stream, int startPosition, Object... hints) {
    final OType[] types = getKeyTypes(hints);
    final List<Object> keys = compositeKey.getKeys();
    final int keysSize = keys.size();
    final int oldStartPosition = startPosition;
    startPosition += OIntegerSerializer.INT_SIZE;
    OIntegerSerializer.INSTANCE.serializeNative(keysSize, stream, startPosition);
    startPosition += OIntegerSerializer.INT_SIZE;
    final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance();
    for (int i = 0; i < keys.size(); i++) {
        final Object key = keys.get(i);
        OBinarySerializer<Object> binarySerializer;
        if (key != null) {
            final OType type;
            if (types.length > i)
                type = types[i];
            else
                type = OType.getTypeByClass(key.getClass());
            binarySerializer = factory.getObjectSerializer(type);
        } else
            binarySerializer = ONullSerializer.INSTANCE;
        stream[startPosition] = binarySerializer.getId();
        startPosition += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE;
        binarySerializer.serializeNativeObject(key, stream, startPosition);
        startPosition += binarySerializer.getObjectSize(key);
    }
    OIntegerSerializer.INSTANCE.serializeNative((startPosition - oldStartPosition), stream, oldStartPosition);
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OBinarySerializerFactory(com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)

Example 50 with OType

use of com.orientechnologies.orient.core.metadata.schema.OType in project orientdb by orientechnologies.

the class OCompositeKeySerializer method serialize.

public void serialize(OCompositeKey compositeKey, byte[] stream, int startPosition, Object... hints) {
    final OType[] types = getKeyTypes(hints);
    final List<Object> keys = compositeKey.getKeys();
    final int keysSize = keys.size();
    final int oldStartPosition = startPosition;
    startPosition += OIntegerSerializer.INT_SIZE;
    OIntegerSerializer.INSTANCE.serializeLiteral(keysSize, stream, startPosition);
    startPosition += OIntegerSerializer.INT_SIZE;
    final OBinarySerializerFactory factory = OBinarySerializerFactory.getInstance();
    for (int i = 0; i < keys.size(); i++) {
        final Object key = keys.get(i);
        OBinarySerializer<Object> binarySerializer;
        if (key != null) {
            final OType type;
            if (types.length > i)
                type = types[i];
            else
                type = OType.getTypeByClass(key.getClass());
            binarySerializer = factory.getObjectSerializer(type);
        } else
            binarySerializer = ONullSerializer.INSTANCE;
        stream[startPosition] = binarySerializer.getId();
        startPosition += OBinarySerializerFactory.TYPE_IDENTIFIER_SIZE;
        binarySerializer.serialize(key, stream, startPosition);
        startPosition += binarySerializer.getObjectSize(key);
    }
    OIntegerSerializer.INSTANCE.serializeLiteral((startPosition - oldStartPosition), stream, oldStartPosition);
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OBinarySerializerFactory(com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)

Aggregations

OType (com.orientechnologies.orient.core.metadata.schema.OType)65 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)21 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)15 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)12 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)11 Map (java.util.Map)10 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)5 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)5 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)5 OBinarySerializerFactory (com.orientechnologies.orient.core.serialization.serializer.binary.OBinarySerializerFactory)5 Collection (java.util.Collection)5 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)4 ORID (com.orientechnologies.orient.core.id.ORID)4