Search in sources :

Example 1 with ORecordLazySet

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

the class ORecordSerializerNetworkV0 method deserializeValue.

public Object deserializeValue(BytesContainer bytes, OType type, ODocument document) {
    Object value = null;
    switch(type) {
        case INTEGER:
            value = OVarIntSerializer.readAsInteger(bytes);
            break;
        case LONG:
            value = OVarIntSerializer.readAsLong(bytes);
            break;
        case SHORT:
            value = OVarIntSerializer.readAsShort(bytes);
            break;
        case STRING:
            value = readString(bytes);
            break;
        case DOUBLE:
            value = Double.longBitsToDouble(readLong(bytes));
            break;
        case FLOAT:
            value = Float.intBitsToFloat(readInteger(bytes));
            break;
        case BYTE:
            value = readByte(bytes);
            break;
        case BOOLEAN:
            value = readByte(bytes) == 1;
            break;
        case DATETIME:
            value = new Date(OVarIntSerializer.readAsLong(bytes));
            break;
        case DATE:
            long savedTime = OVarIntSerializer.readAsLong(bytes) * MILLISEC_PER_DAY;
            savedTime = convertDayToTimezone(TimeZone.getTimeZone("GMT"), ODateHelper.getDatabaseTimeZone(), savedTime);
            value = new Date(savedTime);
            break;
        case EMBEDDED:
            value = new ODocument();
            deserialize((ODocument) value, bytes);
            if (((ODocument) value).containsField(ODocumentSerializable.CLASS_NAME)) {
                String className = ((ODocument) value).field(ODocumentSerializable.CLASS_NAME);
                try {
                    Class<?> clazz = Class.forName(className);
                    ODocumentSerializable newValue = (ODocumentSerializable) clazz.newInstance();
                    newValue.fromDocument((ODocument) value);
                    value = newValue;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else
                ODocumentInternal.addOwner((ODocument) value, document);
            break;
        case EMBEDDEDSET:
            value = readEmbeddedCollection(bytes, new OTrackedSet<Object>(document), document);
            break;
        case EMBEDDEDLIST:
            value = readEmbeddedCollection(bytes, new OTrackedList<Object>(document), document);
            break;
        case LINKSET:
            value = readLinkCollection(bytes, new ORecordLazySet(document));
            break;
        case LINKLIST:
            value = readLinkCollection(bytes, new ORecordLazyList(document));
            break;
        case BINARY:
            value = readBinary(bytes);
            break;
        case LINK:
            value = readOptimizedLink(bytes);
            break;
        case LINKMAP:
            value = readLinkMap(bytes, document);
            break;
        case EMBEDDEDMAP:
            value = readEmbeddedMap(bytes, document);
            break;
        case DECIMAL:
            value = ODecimalSerializer.INSTANCE.deserialize(bytes.bytes, bytes.offset);
            bytes.skip(ODecimalSerializer.INSTANCE.getObjectSize(bytes.bytes, bytes.offset));
            break;
        case LINKBAG:
            ORidBag bag = new ORidBag();
            bag.fromStream(bytes);
            bag.setOwner(document);
            value = bag;
            break;
        case TRANSIENT:
            break;
        case CUSTOM:
            try {
                String className = readString(bytes);
                Class<?> clazz = Class.forName(className);
                OSerializableStream stream = (OSerializableStream) clazz.newInstance();
                stream.fromStream(readBinary(bytes));
                if (stream instanceof OSerializableWrapper)
                    value = ((OSerializableWrapper) stream).getSerializable();
                else
                    value = stream;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            break;
        case ANY:
            break;
    }
    return value;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OException(com.orientechnologies.common.exception.OException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OValidationException(com.orientechnologies.orient.core.exception.OValidationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ORecordLazySet

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

the class ORecordSerializerJSON method getValueAsCollection.

private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
    // remove square brackets
    iFieldValue = iFieldValue.substring(1, iFieldValue.length() - 1);
    if (iType == OType.LINKBAG) {
        final ORidBag bag = new ORidBag();
        parseCollection(iRecord, iFieldValue, iType, OType.LINK, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {

            @Override
            public void visitItem(Object item) {
                bag.add((OIdentifiable) item);
            }
        });
        return bag;
    } else if (iType == OType.LINKSET) {
        return getValueAsLinkedCollection(new ORecordLazySet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iType == OType.LINKLIST) {
        return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else if (iType == OType.EMBEDDEDSET) {
        return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    } else {
        return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
    }
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 3 with ORecordLazySet

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

the class OObjectSerializerHelper method multiValueToStream.

private static Object multiValueToStream(final Object iMultiValue, OType iType, final OEntityManager iEntityManager, final OUserObject2RecordHandler iObj2RecHandler, final ODatabaseObject db, final ODocument iRecord, final boolean iSaveOnlyDirty) {
    if (iMultiValue == null)
        return null;
    final Collection<Object> sourceValues;
    if (iMultiValue instanceof Collection<?>) {
        sourceValues = (Collection<Object>) iMultiValue;
    } else {
        sourceValues = (Collection<Object>) ((Map<?, ?>) iMultiValue).values();
    }
    if (iType == null) {
        if (sourceValues.size() == 0)
            return iMultiValue;
        // TRY TO UNDERSTAND THE COLLECTION TYPE BY ITS CONTENT
        final Object firstValue = sourceValues.iterator().next();
        if (firstValue == null)
            return iMultiValue;
        // DETERMINE THE RIGHT TYPE BASED ON SOURCE MULTI VALUE OBJECT
        if (OType.isSimpleType(firstValue)) {
            if (iMultiValue instanceof List)
                iType = OType.EMBEDDEDLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.EMBEDDEDSET;
            else
                iType = OType.EMBEDDEDMAP;
        } else {
            if (iMultiValue instanceof List)
                iType = OType.LINKLIST;
            else if (iMultiValue instanceof Set)
                iType = OType.LINKSET;
            else
                iType = OType.LINKMAP;
        }
    }
    Object result = iMultiValue;
    final OType linkedType;
    // CREATE THE RETURN MULTI VALUE OBJECT BASED ON DISCOVERED TYPE
    if (iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.LINKSET)) {
        if (iRecord != null && iType.equals(OType.EMBEDDEDSET))
            result = new OTrackedSet<Object>(iRecord);
        else
            result = new ORecordLazySet(iRecord);
    } else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
        if (iRecord != null && iType.equals(OType.EMBEDDEDLIST))
            result = new OTrackedList<Object>(iRecord);
        else
            result = new ArrayList<Object>();
    }
    if (iType.equals(OType.LINKLIST) || iType.equals(OType.LINKSET) || iType.equals(OType.LINKMAP))
        linkedType = OType.LINK;
    else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.EMBEDDEDSET) || iType.equals(OType.EMBEDDEDMAP))
        linkedType = OType.EMBEDDED;
    else
        throw new IllegalArgumentException("Type " + iType + " must be a multi value type (collection or map)");
    if (iMultiValue instanceof Set<?>) {
        for (Object o : sourceValues) {
            ((Collection<Object>) result).add(typeToStream(o, linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
        }
    } else if (iMultiValue instanceof List<?>) {
        for (int i = 0; i < sourceValues.size(); i++) {
            ((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
        }
    } else {
        if (iMultiValue instanceof OObjectLazyMap<?>) {
            result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
        } else {
            if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
                result = new OTrackedMap<Object>(iRecord);
            else
                result = new HashMap<Object, Object>();
            for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
                ((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, iEntityManager, iObj2RecHandler, db, null, iSaveOnlyDirty));
            }
        }
    }
    return result;
}
Also used : OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Entry(java.util.Map.Entry) Collection(java.util.Collection) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) List(java.util.List) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) ArrayList(java.util.ArrayList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) Map(java.util.Map) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap)

Example 4 with ORecordLazySet

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

the class OObjectProxyMethodHandler method manageCollectionSave.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object manageCollectionSave(final Object self, final Field f, Collection<?> value, final boolean customSerialization, final boolean isFieldUpdate) {
    final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
    if (customSerialization) {
        if (value instanceof List<?>) {
            final List<Object> list = new ArrayList<Object>();
            setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
            value = new OObjectCustomSerializerList(OObjectEntitySerializer.getSerializedType(f), doc, new ArrayList<Object>(), (List<Object>) value);
        } else {
            final Set<Object> set = new HashSet<Object>();
            setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
            value = new OObjectCustomSerializerSet(OObjectEntitySerializer.getSerializedType(f), doc, set, (Set<Object>) value);
        }
    } else if (genericType != null && genericType.isEnum()) {
        if (value instanceof List<?>) {
            final List<Object> list = new ArrayList<Object>();
            setDocFieldValue(f.getName(), list, OType.EMBEDDEDLIST);
            value = new OObjectEnumLazyList(genericType, doc, list, (List<Object>) value);
        } else {
            final Set<Object> set = new HashSet<Object>();
            setDocFieldValue(f.getName(), set, OType.EMBEDDEDSET);
            value = new OObjectEnumLazySet(genericType, doc, set, (Set<Object>) value);
        }
    } else if (!(value instanceof OObjectLazyMultivalueElement)) {
        boolean embedded = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName());
        if (value instanceof List) {
            OType type = embedded ? OType.EMBEDDEDLIST : OType.LINKLIST;
            List<OIdentifiable> docList = doc.field(f.getName(), type);
            if (docList == null) {
                if (embedded)
                    docList = new OTrackedList<OIdentifiable>(doc);
                else
                    docList = new ORecordLazyList(doc);
                setDocFieldValue(f.getName(), docList, type);
            } else if (isFieldUpdate) {
                docList.clear();
            }
            value = new OObjectLazyList(self, docList, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
        } else if (value instanceof Set) {
            OType type = embedded ? OType.EMBEDDEDSET : OType.LINKSET;
            Set<OIdentifiable> docSet = doc.field(f.getName(), type);
            if (docSet == null) {
                if (embedded)
                    docSet = new OTrackedSet<OIdentifiable>(doc);
                else
                    docSet = new ORecordLazySet(doc);
                setDocFieldValue(f.getName(), docSet, type);
            } else if (isFieldUpdate) {
                docSet.clear();
            }
            value = new OObjectLazySet(self, docSet, (Set<?>) value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
        }
    }
    if (!((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).isLazyLoading())
        ((OObjectLazyMultivalueElement) value).detach(false);
    return value;
}
Also used : ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectEnumLazyList(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList) ArrayList(java.util.ArrayList) OObjectEnumLazySet(com.orientechnologies.orient.object.enumerations.OObjectEnumLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ArrayList(java.util.ArrayList) OObjectEnumLazyList(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyList) OObjectCustomSerializerList(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerList) List(java.util.List) OTrackedList(com.orientechnologies.orient.core.db.record.OTrackedList) OObjectLazyList(com.orientechnologies.orient.object.db.OObjectLazyList) OObjectLazySet(com.orientechnologies.orient.object.db.OObjectLazySet) OObjectCustomSerializerSet(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerSet) HashSet(java.util.HashSet) OType(com.orientechnologies.orient.core.metadata.schema.OType) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OObjectLazyMultivalueElement(com.orientechnologies.orient.core.db.object.OObjectLazyMultivalueElement) OObjectCustomSerializerList(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerList) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ProxyObject(javassist.util.proxy.ProxyObject)

Example 5 with ORecordLazySet

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

the class ODirtyManagerTest method testLinkSetNoConvert.

@Test
public void testLinkSetNoConvert() {
    ODocument doc = new ODocument();
    doc.field("test", "ddd");
    Set<OIdentifiable> set = new ORecordLazySet(doc);
    ODocument link = new ODocument();
    set.add(link);
    doc.field("set", set, OType.LINKSET);
    ODirtyManager manager = ORecordInternal.getDirtyManager(doc);
    assertEquals(2, manager.getNewRecords().size());
    assertEquals(1, manager.getPointed(doc).size());
    assertTrue(manager.getPointed(doc).contains(link));
}
Also used : ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) ODirtyManager(com.orientechnologies.orient.core.record.impl.ODirtyManager) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)14 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)8 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)7 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)7 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)6 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)5 OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)4 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)4 OType (com.orientechnologies.orient.core.metadata.schema.OType)4 Collection (java.util.Collection)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Test (org.testng.annotations.Test)4 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)3 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 ORecord (com.orientechnologies.orient.core.record.ORecord)3