Search in sources :

Example 11 with ORecordLazyMultiValue

use of com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue 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 12 with ORecordLazyMultiValue

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

the class ORecordSerializerNetworkV0 method writeLinkCollection.

private int writeLinkCollection(final BytesContainer bytes, final Collection<OIdentifiable> value) {
    final int pos = OVarIntSerializer.write(bytes, value.size());
    final boolean disabledAutoConversion = value instanceof ORecordLazyMultiValue && ((ORecordLazyMultiValue) value).isAutoConvertToRecord();
    if (disabledAutoConversion)
        // AVOID TO FETCH RECORD
        ((ORecordLazyMultiValue) value).setAutoConvertToRecord(false);
    try {
        for (OIdentifiable itemValue : value) {
            // TODO: handle the null links
            if (itemValue == null)
                writeNullLink(bytes);
            else
                writeOptimizedLink(bytes, itemValue);
        }
    } finally {
        if (disabledAutoConversion)
            ((ORecordLazyMultiValue) value).setAutoConvertToRecord(true);
    }
    return pos;
}
Also used : OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Aggregations

ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 OType (com.orientechnologies.orient.core.metadata.schema.OType)5 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)4 ORecord (com.orientechnologies.orient.core.record.ORecord)3 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)2 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)1 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)1