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