use of com.orientechnologies.orient.core.db.record.ORecordLazyMap in project orientdb by orientechnologies.
the class CollectionOfLinkInNestedDocumentTest method nestedLinkMap.
@Test
public void nestedLinkMap() {
ODocument doc1 = new ODocument();
doc1.field("value", "item 1");
ODocument doc2 = new ODocument();
doc2.field("value", "item 2");
ODocument nested = new ODocument();
ORecordLazyMap map = new ORecordLazyMap(nested);
map.put("record1", doc1);
map.put("record2", doc2);
nested.field("map", map);
ODocument base = new ODocument();
base.field("nested", nested, OType.EMBEDDED);
OIdentifiable id = db.save(base);
db.getLocalCache().clear();
ODocument base1 = db.load(id.getIdentity());
ODocument nest1 = base1.field("nested");
assertNotNull(nest1);
assertEquals(nest1.field("map"), nested.field("map"));
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyMap in project orientdb by orientechnologies.
the class TestOTypeDetection method testOTypeFromValue.
@Test
public void testOTypeFromValue() {
assertEquals(OType.BOOLEAN, OType.getTypeByValue(true));
assertEquals(OType.LONG, OType.getTypeByValue(2L));
assertEquals(OType.INTEGER, OType.getTypeByValue(2));
assertEquals(OType.SHORT, OType.getTypeByValue((short) 4));
assertEquals(OType.FLOAT, OType.getTypeByValue(0.5f));
assertEquals(OType.DOUBLE, OType.getTypeByValue(0.7d));
assertEquals(OType.BYTE, OType.getTypeByValue((byte) 10));
assertEquals(OType.STRING, OType.getTypeByValue('a'));
assertEquals(OType.STRING, OType.getTypeByValue("yaaahooooo"));
assertEquals(OType.BINARY, OType.getTypeByValue(new byte[] { 0, 1, 2 }));
assertEquals(OType.DATETIME, OType.getTypeByValue(new Date()));
assertEquals(OType.DECIMAL, OType.getTypeByValue(new BigDecimal(10)));
assertEquals(OType.INTEGER, OType.getTypeByValue(new BigInteger("20")));
assertEquals(OType.LINK, OType.getTypeByValue(new ODocument()));
assertEquals(OType.LINK, OType.getTypeByValue(new ORecordId()));
assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new ArrayList<Object>()));
assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new OTrackedList<Object>(new ODocument())));
assertEquals(OType.EMBEDDEDSET, OType.getTypeByValue(new HashSet<Object>()));
assertEquals(OType.EMBEDDEDMAP, OType.getTypeByValue(new HashMap<Object, Object>()));
assertEquals(OType.LINKSET, OType.getTypeByValue(new ORecordLazySet(new ODocument())));
assertEquals(OType.LINKLIST, OType.getTypeByValue(new ORecordLazyList(new ODocument())));
assertEquals(OType.LINKMAP, OType.getTypeByValue(new ORecordLazyMap(new ODocument())));
assertEquals(OType.LINKBAG, OType.getTypeByValue(new ORidBag()));
assertEquals(OType.CUSTOM, OType.getTypeByValue(new CustomClass()));
assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new Object[] {}));
assertEquals(OType.EMBEDDEDLIST, OType.getTypeByValue(new String[] {}));
assertEquals(OType.EMBEDDED, OType.getTypeByValue(new DocumentSer()));
assertEquals(OType.CUSTOM, OType.getTypeByValue(new ClassSerializable()));
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyMap in project orientdb by orientechnologies.
the class OObjectEntitySerializer method multiValueToStream.
@SuppressWarnings("unchecked")
private static Object multiValueToStream(final Object iMultiValue, OType iType, final ODatabaseObject db, final ODocument iRecord) {
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 (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;
if (iType == null) {
// 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 (isToSerialize(firstValue.getClass()))
result = new HashSet<Object>();
else if ((iRecord != null && iType.equals(OType.EMBEDDEDSET)) || OType.isSimpleType(firstValue))
result = new OTrackedSet<Object>(iRecord);
else
result = new ORecordLazySet(iRecord);
} else if (iType.equals(OType.EMBEDDEDLIST) || iType.equals(OType.LINKLIST)) {
if (isToSerialize(firstValue.getClass()))
result = new ArrayList<Object>();
else if ((iRecord != null && iType.equals(OType.EMBEDDEDLIST)) || OType.isSimpleType(firstValue))
result = new OTrackedList<Object>(iRecord);
else
result = new ORecordLazyList(iRecord);
}
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))
if (firstValue instanceof List)
linkedType = OType.EMBEDDEDLIST;
else if (firstValue instanceof Set)
linkedType = OType.EMBEDDEDSET;
else if (firstValue instanceof Map)
linkedType = OType.EMBEDDEDMAP;
else
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) {
((Set<Object>) result).add(typeToStream(o, linkedType, db, null));
}
} else if (iMultiValue instanceof List<?>) {
for (int i = 0; i < sourceValues.size(); i++) {
((List<Object>) result).add(typeToStream(((List<?>) sourceValues).get(i), linkedType, db, null));
}
} else {
if (iMultiValue instanceof OObjectLazyMap<?>) {
result = ((OObjectLazyMap<?>) iMultiValue).getUnderlying();
} else {
if (isToSerialize(firstValue.getClass()))
result = new HashMap<Object, Object>();
else if (iRecord != null && iType.equals(OType.EMBEDDEDMAP))
result = new OTrackedMap<Object>(iRecord);
else
result = new ORecordLazyMap(iRecord);
for (Entry<Object, Object> entry : ((Map<Object, Object>) iMultiValue).entrySet()) {
((Map<Object, Object>) result).put(entry.getKey(), typeToStream(entry.getValue(), linkedType, db, null));
}
}
}
return result;
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyMap in project orientdb by orientechnologies.
the class OObjectProxyMethodHandler method manageMapSave.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object manageMapSave(final Object self, final Field f, Map<?, ?> value, final boolean customSerialization) {
final Class genericType = OReflectionHelper.getGenericMultivalueType(f);
if (customSerialization) {
Map<Object, Object> map = new HashMap<Object, Object>();
setDocFieldValue(f.getName(), map, OType.EMBEDDEDMAP);
value = new OObjectCustomSerializerMap<TYPE>(OObjectEntitySerializer.getSerializedType(f), doc, map, (Map<Object, Object>) value);
} else if (genericType != null && genericType.isEnum()) {
Map<Object, Object> map = new HashMap<Object, Object>();
setDocFieldValue(f.getName(), map, OType.EMBEDDEDMAP);
value = new OObjectEnumLazyMap(genericType, doc, map, (Map<Object, Object>) value);
} else if (!(value instanceof OObjectLazyMultivalueElement)) {
OType type = OObjectEntitySerializer.isEmbeddedField(self.getClass(), f.getName()) ? OType.EMBEDDEDMAP : OType.LINKMAP;
if (doc.fieldType(f.getName()) != type)
doc.field(f.getName(), doc.field(f.getName()), type);
Map<Object, OIdentifiable> docMap = doc.field(f.getName(), type);
if (docMap == null) {
if (OType.EMBEDDEDMAP == type)
docMap = new OTrackedMap<OIdentifiable>(doc);
else
docMap = new ORecordLazyMap(doc);
setDocFieldValue(f.getName(), docMap, type);
}
value = new OObjectLazyMap(self, docMap, value, OObjectEntitySerializer.isCascadeDeleteField(self.getClass(), f.getName()));
}
return value;
}
Aggregations