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;
}
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);
}
}
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;
}
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;
}
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));
}
Aggregations