Search in sources :

Example 1 with ORecordElement

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

the class ORecordSerializerCSVAbstract method embeddedCollectionFromStream.

public Object embeddedCollectionFromStream(final ODocument iDocument, final OType iType, OClass iLinkedClass, final OType iLinkedType, final String iValue) {
    if (iValue.length() == 0)
        return null;
    // REMOVE BEGIN & END COLLECTIONS CHARACTERS IF IT'S A COLLECTION
    final String value = iValue.charAt(0) == OStringSerializerHelper.LIST_BEGIN || iValue.charAt(0) == OStringSerializerHelper.SET_BEGIN ? iValue.substring(1, iValue.length() - 1) : iValue;
    Collection<?> coll;
    if (iLinkedType == OType.LINK) {
        if (iDocument != null)
            coll = (Collection<?>) (iType == OType.EMBEDDEDLIST ? new ORecordLazyList(iDocument).setStreamedContent(new StringBuilder(value)) : unserializeSet(iDocument, value));
        else {
            if (iType == OType.EMBEDDEDLIST)
                coll = (Collection<?>) new ORecordLazyList().setStreamedContent(new StringBuilder(value));
            else {
                return unserializeSet(iDocument, value);
            }
        }
    } else
        coll = iType == OType.EMBEDDEDLIST ? new OTrackedList<Object>(iDocument) : new OTrackedSet<Object>(iDocument);
    if (value.length() == 0)
        return coll;
    OType linkedType;
    if (coll instanceof ORecordElement)
        ((ORecordElement) coll).setInternalStatus(STATUS.UNMARSHALLING);
    final List<String> items = OStringSerializerHelper.smartSplit(value, OStringSerializerHelper.RECORD_SEPARATOR, true, false);
    for (String item : items) {
        Object objectToAdd = null;
        linkedType = null;
        if (item.equals("null"))
            // NULL VALUE
            objectToAdd = null;
        else if (item.length() > 2 && item.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN) {
            // REMOVE EMBEDDED BEGIN/END CHARS
            item = item.substring(1, item.length() - 1);
            if (!item.isEmpty()) {
                // EMBEDDED RECORD, EXTRACT THE CLASS NAME IF DIFFERENT BY THE PASSED (SUB-CLASS OR IT WAS PASSED NULL)
                iLinkedClass = OStringSerializerHelper.getRecordClassName(item, iLinkedClass);
                if (iLinkedClass != null) {
                    ODocument doc = new ODocument();
                    objectToAdd = fromString(item, doc, null);
                    ODocumentInternal.fillClassNameIfNeeded(doc, iLinkedClass.getName());
                } else
                    // EMBEDDED OBJECT
                    objectToAdd = fieldTypeFromStream(iDocument, OType.EMBEDDED, item);
            }
        } else {
            if (linkedType == null) {
                final char begin = item.length() > 0 ? item.charAt(0) : OStringSerializerHelper.LINK;
                // AUTO-DETERMINE LINKED TYPE
                if (begin == OStringSerializerHelper.LINK)
                    linkedType = OType.LINK;
                else
                    linkedType = getType(item);
                if (linkedType == null)
                    throw new IllegalArgumentException("Linked type cannot be null. Probably the serialized type has not stored the type along with data");
            }
            if (iLinkedType == OType.CUSTOM)
                item = item.substring(1, item.length() - 1);
            objectToAdd = fieldTypeFromStream(iDocument, linkedType, item);
        }
        if (objectToAdd != null && objectToAdd instanceof ODocument && coll instanceof ORecordElement)
            ODocumentInternal.addOwner((ODocument) objectToAdd, (ORecordElement) coll);
        ((Collection<Object>) coll).add(objectToAdd);
    }
    if (coll instanceof ORecordElement)
        ((ORecordElement) coll).setInternalStatus(STATUS.LOADED);
    return coll;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ORecordElement

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

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

the class ODirtyManager method internalTrack.

private void internalTrack(ORecord pointing, OIdentifiable pointed) {
    if (pointing instanceof ODocument) {
        if (((ODocument) pointing).isEmbedded()) {
            ORecordElement ele = pointing.getOwner();
            while (!(ele instanceof ODocument) && ele != null && ele.getOwner() != null) ele = ele.getOwner();
            if (ele != null)
                pointing = (ORecord) ele;
        }
    }
    if (pointed.getIdentity().isNew()) {
        if (!(pointed instanceof ODocument) || !((ODocument) pointed).isEmbedded()) {
            if (references == null) {
                references = new IdentityHashMap<ODocument, List<OIdentifiable>>();
            }
            List<OIdentifiable> refs = references.get(pointing);
            if (refs == null) {
                refs = new ArrayList<OIdentifiable>();
                references.put((ODocument) pointing, refs);
            }
            refs.add(pointed);
        } else if (pointed instanceof ODocument) {
            List<OIdentifiable> point = ORecordInternal.getDirtyManager((ORecord) pointed).getPointed((ORecord) pointed);
            if (point != null && point.size() > 0) {
                if (references == null) {
                    references = new IdentityHashMap<ODocument, List<OIdentifiable>>();
                }
                List<OIdentifiable> refs = references.get(pointing);
                if (refs == null) {
                    refs = new ArrayList<OIdentifiable>();
                    references.put((ODocument) pointing, refs);
                }
                for (OIdentifiable embPoint : point) {
                    refs.add(embPoint);
                }
            }
        }
    }
    if (pointed instanceof ORecord) {
        ORecordInternal.setDirtyManager((ORecord) pointed, this);
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement)

Example 4 with ORecordElement

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

the class OTransactionRealAbstract method serializeIndexChangeEntry.

protected ODocument serializeIndexChangeEntry(OTransactionIndexChangesPerKey entry, final ODocument indexDoc) {
    // SERIALIZE KEY
    ODocument keyContainer = new ODocument();
    keyContainer.setTrackingChanges(false);
    try {
        if (entry.key != null) {
            if (entry.key instanceof OCompositeKey) {
                final List<Object> keys = ((OCompositeKey) entry.key).getKeys();
                keyContainer.field("key", keys, OType.EMBEDDEDLIST);
                keyContainer.field("binary", false);
            } else if (!(entry.key instanceof ORecordElement) && (entry.key instanceof OSerializableStream)) {
                keyContainer.field("key", OStreamSerializerAnyStreamable.INSTANCE.toStream(entry.key), OType.BINARY);
                keyContainer.field("binary", true);
            } else {
                keyContainer.field("key", entry.key);
                keyContainer.field("binary", false);
            }
        } else
            keyContainer = null;
    } catch (IOException ioe) {
        throw OException.wrapException(new OTransactionException("Error during index changes serialization. "), ioe);
    }
    final List<ODocument> operations = new ArrayList<ODocument>();
    // SERIALIZE VALUES
    if (entry.entries != null && !entry.entries.isEmpty()) {
        for (OTransactionIndexEntry e : entry.entries) {
            final ODocument changeDoc = new ODocument().setAllowChainedAccess(false);
            ODocumentInternal.addOwner((ODocument) changeDoc, indexDoc);
            // SERIALIZE OPERATION
            changeDoc.field("o", e.operation.ordinal());
            if (e.value instanceof ORecord && e.value.getIdentity().isNew()) {
                final ORecord saved = getRecord(e.value.getIdentity());
                if (saved != null)
                    e.value = saved;
                else
                    ((ORecord) e.value).save();
            }
            changeDoc.field("v", e.value != null ? e.value.getIdentity() : null);
            operations.add(changeDoc);
        }
    }
    ODocument res = new ODocument();
    res.setTrackingChanges(false);
    ODocumentInternal.addOwner(res, indexDoc);
    return res.setAllowChainedAccess(false).field("k", keyContainer, OType.EMBEDDED).field("ops", operations, OType.EMBEDDEDLIST);
}
Also used : OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecord(com.orientechnologies.orient.core.record.ORecord) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OTransactionIndexEntry(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement)

Aggregations

ORecordElement (com.orientechnologies.orient.core.db.record.ORecordElement)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)2 OType (com.orientechnologies.orient.core.metadata.schema.OType)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)1 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)1 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)1 OTransactionIndexEntry (com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Map (java.util.Map)1