Search in sources :

Example 1 with ORecordLazyMap

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

the class ORecordSerializerCSVAbstract method embeddedMapFromStream.

public Map<String, Object> embeddedMapFromStream(final ODocument iSourceDocument, final OType iLinkedType, final String iValue, final String iName) {
    if (iValue.length() == 0)
        return null;
    // REMOVE BEGIN & END MAP CHARACTERS
    String value = iValue.substring(1, iValue.length() - 1);
    @SuppressWarnings("rawtypes") Map map;
    if (iLinkedType == OType.LINK || iLinkedType == OType.EMBEDDED)
        map = new ORecordLazyMap(iSourceDocument, ODocument.RECORD_TYPE);
    else
        map = new OTrackedMap<Object>(iSourceDocument);
    if (value.length() == 0)
        return map;
    final List<String> items = OStringSerializerHelper.smartSplit(value, OStringSerializerHelper.RECORD_SEPARATOR, true, false);
    if (map instanceof ORecordElement)
        ((ORecordElement) map).setInternalStatus(STATUS.UNMARSHALLING);
    for (String item : items) {
        if (item != null && !item.isEmpty()) {
            final List<String> entries = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR, true, false);
            if (!entries.isEmpty()) {
                final Object mapValueObject;
                if (entries.size() > 1) {
                    String mapValue = entries.get(1);
                    final OType linkedType;
                    if (iLinkedType == null)
                        if (!mapValue.isEmpty()) {
                            linkedType = getType(mapValue);
                            if ((iName == null || iSourceDocument.fieldType(iName) == null || iSourceDocument.fieldType(iName) != OType.EMBEDDEDMAP) && isConvertToLinkedMap(map, linkedType)) {
                                // CONVERT IT TO A LAZY MAP
                                map = new ORecordLazyMap(iSourceDocument, ODocument.RECORD_TYPE);
                                ((ORecordElement) map).setInternalStatus(STATUS.UNMARSHALLING);
                            } else if (map instanceof ORecordLazyMap && linkedType != OType.LINK) {
                                map = new OTrackedMap<Object>(iSourceDocument, map, null);
                            }
                        } else
                            linkedType = OType.EMBEDDED;
                    else
                        linkedType = iLinkedType;
                    if (linkedType == OType.EMBEDDED && mapValue.length() >= 2)
                        mapValue = mapValue.substring(1, mapValue.length() - 1);
                    mapValueObject = fieldTypeFromStream(iSourceDocument, linkedType, mapValue);
                    if (mapValueObject != null && mapValueObject instanceof ODocument)
                        ODocumentInternal.addOwner((ODocument) mapValueObject, iSourceDocument);
                } else
                    mapValueObject = null;
                final Object key = fieldTypeFromStream(iSourceDocument, OType.STRING, entries.get(0));
                try {
                    map.put(key, mapValueObject);
                } catch (ClassCastException e) {
                    throw OException.wrapException(new OSerializationException("Cannot load map because the type was not the expected: key=" + key + "(type " + key.getClass().toString() + "), value=" + mapValueObject + "(type " + key.getClass() + ")"), e);
                }
            }
        }
    }
    if (map instanceof ORecordElement)
        ((ORecordElement) map).setInternalStatus(STATUS.LOADED);
    return map;
}
Also used : OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ORecordLazyMap

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

the class ORecordSerializerCSVAbstract method fieldToStream.

public void fieldToStream(final ODocument iRecord, final StringBuilder iOutput, OUserObject2RecordHandler iObjHandler, final OType iType, final OClass iLinkedClass, final OType iLinkedType, final String iName, final Object iValue, final boolean iSaveOnlyDirty) {
    if (iValue == null)
        return;
    final long timer = PROFILER.startChrono();
    switch(iType) {
        case LINK:
            {
                if (!(iValue instanceof OIdentifiable))
                    throw new OSerializationException("Found an unexpected type during marshalling of a LINK where a OIdentifiable (ORID or any Record) was expected. The string representation of the object is: " + iValue);
                if (!((OIdentifiable) iValue).getIdentity().isValid() && iValue instanceof ODocument && ((ODocument) iValue).isEmbedded()) {
                    // WRONG: IT'S EMBEDDED!
                    fieldToStream(iRecord, iOutput, iObjHandler, OType.EMBEDDED, iLinkedClass, iLinkedType, iName, iValue, iSaveOnlyDirty);
                } else {
                    final Object link = linkToStream(iOutput, iRecord, iValue);
                    if (link != null)
                        // OVERWRITE CONTENT
                        iRecord.field(iName, link);
                    PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.link2string"), "Serialize link to string", timer);
                }
                break;
            }
        case LINKLIST:
            {
                iOutput.append(OStringSerializerHelper.LIST_BEGIN);
                if (iValue instanceof ORecordLazyList && ((ORecordLazyList) iValue).getStreamedContent() != null) {
                    iOutput.append(((ORecordLazyList) iValue).getStreamedContent());
                    PROFILER.updateCounter(PROFILER.getProcessMetric("serializer.record.string.linkList2string.cached"), "Serialize linklist to string in stream mode", +1);
                } else {
                    final ORecordLazyList coll;
                    final Iterator<OIdentifiable> it;
                    if (iValue instanceof OMultiCollectionIterator<?>) {
                        final OMultiCollectionIterator<OIdentifiable> iterator = (OMultiCollectionIterator<OIdentifiable>) iValue;
                        iterator.reset();
                        it = iterator;
                        coll = null;
                    } else if (!(iValue instanceof ORecordLazyList)) {
                        // FIRST TIME: CONVERT THE ENTIRE COLLECTION
                        coll = new ORecordLazyList(iRecord);
                        if (iValue.getClass().isArray()) {
                            Iterable<Object> iterab = OMultiValue.getMultiValueIterable(iValue, false);
                            for (Object i : iterab) {
                                coll.add((OIdentifiable) i);
                            }
                        } else {
                            coll.addAll((Collection<? extends OIdentifiable>) iValue);
                            ((Collection<? extends OIdentifiable>) iValue).clear();
                        }
                        iRecord.field(iName, coll);
                        it = coll.rawIterator();
                    } else {
                        // LAZY LIST
                        coll = (ORecordLazyList) iValue;
                        if (coll.getStreamedContent() != null) {
                            // APPEND STREAMED CONTENT
                            iOutput.append(coll.getStreamedContent());
                            PROFILER.updateCounter(PROFILER.getProcessMetric("serializer.record.string.linkList2string.cached"), "Serialize linklist to string in stream mode", +1);
                            it = coll.newItemsIterator();
                        } else
                            it = coll.rawIterator();
                    }
                    if (it != null && it.hasNext()) {
                        final StringBuilder buffer = new StringBuilder(128);
                        for (int items = 0; it.hasNext(); items++) {
                            if (items > 0)
                                buffer.append(OStringSerializerHelper.RECORD_SEPARATOR);
                            final OIdentifiable item = it.next();
                            final OIdentifiable newRid = linkToStream(buffer, iRecord, item);
                            if (newRid != null)
                                ((OLazyIterator<OIdentifiable>) it).update(newRid);
                        }
                        if (coll != null)
                            coll.convertRecords2Links();
                        iOutput.append(buffer);
                        // UPDATE THE STREAM
                        if (coll != null)
                            coll.setStreamedContent(buffer);
                    }
                }
                iOutput.append(OStringSerializerHelper.LIST_END);
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkList2string"), "Serialize linklist to string", timer);
                break;
            }
        case LINKSET:
            {
                if (!(iValue instanceof OStringBuilderSerializable)) {
                    if (iValue instanceof OAutoConvertToRecord)
                        ((OAutoConvertToRecord) iValue).setAutoConvertToRecord(false);
                    final Collection<OIdentifiable> coll;
                    // FIRST TIME: CONVERT THE ENTIRE COLLECTION
                    if (!(iValue instanceof ORecordLazySet)) {
                        final ORecordLazySet set = new ORecordLazySet(iRecord);
                        set.addAll((Collection<OIdentifiable>) iValue);
                        iRecord.field(iName, set);
                        coll = set;
                    } else
                        coll = (Collection<OIdentifiable>) iValue;
                    serializeSet(coll, iOutput);
                } else {
                    // LAZY SET
                    final OStringBuilderSerializable coll = (OStringBuilderSerializable) iValue;
                    coll.toStream(iOutput);
                }
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkSet2string"), "Serialize linkset to string", timer);
                break;
            }
        case LINKMAP:
            {
                iOutput.append(OStringSerializerHelper.MAP_BEGIN);
                Map<Object, Object> map = (Map<Object, Object>) iValue;
                // LINKED MAP
                if (map instanceof OLazyObjectMapInterface<?>)
                    ((OLazyObjectMapInterface<?>) map).setConvertToRecord(false);
                boolean invalidMap = false;
                try {
                    int items = 0;
                    for (Map.Entry<Object, Object> entry : map.entrySet()) {
                        if (items++ > 0)
                            iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
                        fieldTypeToString(iOutput, OType.STRING, entry.getKey());
                        iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
                        final Object link = linkToStream(iOutput, iRecord, entry.getValue());
                        if (link != null && !invalidMap)
                            // IDENTITY IS CHANGED, RE-SET INTO THE COLLECTION TO RECOMPUTE THE HASH
                            invalidMap = true;
                    }
                } finally {
                    if (map instanceof OLazyObjectMapInterface<?>) {
                        ((OLazyObjectMapInterface<?>) map).setConvertToRecord(true);
                    }
                }
                if (invalidMap) {
                    final ORecordLazyMap newMap = new ORecordLazyMap(iRecord, ODocument.RECORD_TYPE);
                    // REPLACE ALL CHANGED ITEMS
                    for (Map.Entry<Object, Object> entry : map.entrySet()) {
                        newMap.put(entry.getKey(), (OIdentifiable) entry.getValue());
                    }
                    map.clear();
                    iRecord.field(iName, newMap);
                }
                iOutput.append(OStringSerializerHelper.MAP_END);
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkMap2string"), "Serialize linkmap to string", timer);
                break;
            }
        case EMBEDDED:
            if (iValue instanceof ORecord) {
                iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
                toString((ORecord) iValue, iOutput, null, iObjHandler, false, true);
                iOutput.append(OStringSerializerHelper.EMBEDDED_END);
            } else if (iValue instanceof ODocumentSerializable) {
                final ODocument doc = ((ODocumentSerializable) iValue).toDocument();
                doc.field(ODocumentSerializable.CLASS_NAME, iValue.getClass().getName());
                iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
                toString(doc, iOutput, null, iObjHandler, false, true);
                iOutput.append(OStringSerializerHelper.EMBEDDED_END);
            } else if (iValue != null)
                iOutput.append(iValue.toString());
            PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embed2string"), "Serialize embedded to string", timer);
            break;
        case EMBEDDEDLIST:
            embeddedCollectionToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty, false);
            PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedList2string"), "Serialize embeddedlist to string", timer);
            break;
        case EMBEDDEDSET:
            embeddedCollectionToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty, true);
            PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedSet2string"), "Serialize embeddedset to string", timer);
            break;
        case EMBEDDEDMAP:
            {
                embeddedMapToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty);
                PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedMap2string"), "Serialize embeddedmap to string", timer);
                break;
            }
        case LINKBAG:
            {
                iOutput.append(OStringSerializerHelper.BAG_BEGIN);
                ((ORidBag) iValue).toStream(iOutput);
                iOutput.append(OStringSerializerHelper.BAG_END);
                break;
            }
        default:
            fieldTypeToString(iOutput, iType, iValue);
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OStringBuilderSerializable(com.orientechnologies.orient.core.serialization.serializer.string.OStringBuilderSerializable) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) OAutoConvertToRecord(com.orientechnologies.orient.core.db.record.OAutoConvertToRecord) Entry(java.util.Map.Entry) OLazyIterator(com.orientechnologies.common.collection.OLazyIterator) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) ORecord(com.orientechnologies.orient.core.record.ORecord) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OLazyIterator(com.orientechnologies.common.collection.OLazyIterator) Iterator(java.util.Iterator) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OLazyObjectMapInterface(com.orientechnologies.orient.core.db.object.OLazyObjectMapInterface) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with ORecordLazyMap

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

the class ORecordSerializerCSVAbstract method fieldFromStream.

public Object fieldFromStream(final ORecord iSourceRecord, final OType iType, OClass iLinkedClass, OType iLinkedType, final String iName, final String iValue) {
    if (iValue == null)
        return null;
    switch(iType) {
        case EMBEDDEDLIST:
        case EMBEDDEDSET:
            return embeddedCollectionFromStream((ODocument) iSourceRecord, iType, iLinkedClass, iLinkedType, iValue);
        case LINKSET:
        case LINKLIST:
            {
                if (iValue.length() == 0)
                    return null;
                // REMOVE BEGIN & END COLLECTIONS CHARACTERS IF IT'S A COLLECTION
                final String value = iValue.startsWith("[") || iValue.startsWith("<") ? iValue.substring(1, iValue.length() - 1) : iValue;
                if (iType == OType.LINKLIST) {
                    return new ORecordLazyList((ODocument) iSourceRecord).setStreamedContent(new StringBuilder(value));
                } else {
                    return unserializeSet((ODocument) iSourceRecord, value);
                }
            }
        case LINKMAP:
            {
                if (iValue.length() == 0)
                    return null;
                // REMOVE BEGIN & END MAP CHARACTERS
                String value = iValue.substring(1, iValue.length() - 1);
                @SuppressWarnings("rawtypes") final Map map = new ORecordLazyMap((ODocument) iSourceRecord, ODocument.RECORD_TYPE);
                if (value.length() == 0)
                    return map;
                final List<String> items = OStringSerializerHelper.smartSplit(value, OStringSerializerHelper.RECORD_SEPARATOR, true, false);
                // EMBEDDED LITERALS
                for (String item : items) {
                    if (item != null && !item.isEmpty()) {
                        final List<String> entry = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR);
                        if (!entry.isEmpty()) {
                            String mapValue = entry.get(1);
                            if (mapValue != null && !mapValue.isEmpty())
                                mapValue = mapValue.substring(1);
                            map.put(fieldTypeFromStream((ODocument) iSourceRecord, OType.STRING, entry.get(0)), new ORecordId(mapValue));
                        }
                    }
                }
                return map;
            }
        case EMBEDDEDMAP:
            return embeddedMapFromStream((ODocument) iSourceRecord, iLinkedType, iValue, iName);
        case LINK:
            if (iValue.length() > 1) {
                int pos = iValue.indexOf(OStringSerializerHelper.CLASS_SEPARATOR);
                if (pos > -1)
                    ((OMetadataInternal) ODatabaseRecordThreadLocal.INSTANCE.get().getMetadata()).getImmutableSchemaSnapshot().getClass(iValue.substring(1, pos));
                else
                    pos = 0;
                final String linkAsString = iValue.substring(pos + 1);
                try {
                    return new ORecordId(linkAsString);
                } catch (IllegalArgumentException e) {
                    OLogManager.instance().error(this, "Error on unmarshalling field '%s' of record '%s': value '%s' is not a link", iName, iSourceRecord, linkAsString);
                    return new ORecordId();
                }
            } else
                return null;
        case EMBEDDED:
            if (iValue.length() > 2) {
                // REMOVE BEGIN & END EMBEDDED CHARACTERS
                final String value = iValue.substring(1, iValue.length() - 1);
                final Object embeddedObject = OStringSerializerEmbedded.INSTANCE.fromStream(value);
                if (embeddedObject instanceof ODocument)
                    ODocumentInternal.addOwner((ODocument) embeddedObject, iSourceRecord);
                // RECORD
                return embeddedObject;
            } else
                return null;
        case LINKBAG:
            final String value = iValue.charAt(0) == OStringSerializerHelper.BAG_BEGIN ? iValue.substring(1, iValue.length() - 1) : iValue;
            return ORidBag.fromStream(value);
        default:
            return fieldTypeFromStream((ODocument) iSourceRecord, iType, iValue);
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) List(java.util.List) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with ORecordLazyMap

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

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

the class ORecordSerializerNetworkV0 method readLinkMap.

private Map<Object, OIdentifiable> readLinkMap(final BytesContainer bytes, final ODocument document) {
    int size = OVarIntSerializer.readAsInteger(bytes);
    Map<Object, OIdentifiable> result = new ORecordLazyMap(document);
    while ((size--) > 0) {
        OType keyType = readOType(bytes);
        Object key = deserializeValue(bytes, keyType, document);
        ORecordId value = readOptimizedLink(bytes);
        if (value.equals(NULL_RECORD_ID))
            result.put(key, null);
        else
            result.put(key, value);
    }
    return result;
}
Also used : OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Aggregations

ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)9 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 Map (java.util.Map)6 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)5 OType (com.orientechnologies.orient.core.metadata.schema.OType)5 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)3 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OObjectLazyMap (com.orientechnologies.orient.object.db.OObjectLazyMap)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2