Search in sources :

Example 1 with OTrackedSet

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

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

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

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

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

the class DocumentTrackingTest method testDocumentEmbeddedSetTrackingAfterReplace.

public void testDocumentEmbeddedSetTrackingAfterReplace() {
    final ODocument document = new ODocument("DocumentTrackingTestClass");
    final Set<String> set = new HashSet<String>();
    set.add("value1");
    document.field("embeddedset", set);
    document.field("val", 1);
    document.save();
    Assert.assertFalse(document.isDirty());
    Assert.assertEquals(document.getDirtyFields(), new String[] {});
    final Set<String> trackedSet = document.field("embeddedset");
    trackedSet.add("value2");
    final Set<String> newTrackedSet = new OTrackedSet<String>(document);
    document.field("embeddedset", newTrackedSet);
    newTrackedSet.add("value3");
    Assert.assertTrue(document.isDirty());
    final OMultiValueChangeTimeLine timeLine = document.getCollectionTimeLine("embeddedset");
    Assert.assertNull(timeLine);
    Assert.assertEquals(document.getDirtyFields(), new String[] { "embeddedset" });
}
Also used : OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) OMultiValueChangeTimeLine(com.orientechnologies.orient.core.db.record.OMultiValueChangeTimeLine) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet)

Aggregations

OTrackedSet (com.orientechnologies.orient.core.db.record.OTrackedSet)6 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)5 OTrackedList (com.orientechnologies.orient.core.db.record.OTrackedList)5 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)4 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)3 OType (com.orientechnologies.orient.core.metadata.schema.OType)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)2 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 OObjectLazyList (com.orientechnologies.orient.object.db.OObjectLazyList)2 OObjectLazyMap (com.orientechnologies.orient.object.db.OObjectLazyMap)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2