use of com.orientechnologies.orient.core.db.record.ORecordLazyList in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method fieldToStream.
public void fieldToStream(final ODocument iRecord, final StringBuilder iOutput, OUserObject2RecordHandler iObjHandler, final OType iType, final OClass iLinkedClass, final OType iLinkedType, final String iName, final Object iValue, final boolean iSaveOnlyDirty) {
if (iValue == null)
return;
final long timer = PROFILER.startChrono();
switch(iType) {
case LINK:
{
if (!(iValue instanceof OIdentifiable))
throw new OSerializationException("Found an unexpected type during marshalling of a LINK where a OIdentifiable (ORID or any Record) was expected. The string representation of the object is: " + iValue);
if (!((OIdentifiable) iValue).getIdentity().isValid() && iValue instanceof ODocument && ((ODocument) iValue).isEmbedded()) {
// WRONG: IT'S EMBEDDED!
fieldToStream(iRecord, iOutput, iObjHandler, OType.EMBEDDED, iLinkedClass, iLinkedType, iName, iValue, iSaveOnlyDirty);
} else {
final Object link = linkToStream(iOutput, iRecord, iValue);
if (link != null)
// OVERWRITE CONTENT
iRecord.field(iName, link);
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.link2string"), "Serialize link to string", timer);
}
break;
}
case LINKLIST:
{
iOutput.append(OStringSerializerHelper.LIST_BEGIN);
if (iValue instanceof ORecordLazyList && ((ORecordLazyList) iValue).getStreamedContent() != null) {
iOutput.append(((ORecordLazyList) iValue).getStreamedContent());
PROFILER.updateCounter(PROFILER.getProcessMetric("serializer.record.string.linkList2string.cached"), "Serialize linklist to string in stream mode", +1);
} else {
final ORecordLazyList coll;
final Iterator<OIdentifiable> it;
if (iValue instanceof OMultiCollectionIterator<?>) {
final OMultiCollectionIterator<OIdentifiable> iterator = (OMultiCollectionIterator<OIdentifiable>) iValue;
iterator.reset();
it = iterator;
coll = null;
} else if (!(iValue instanceof ORecordLazyList)) {
// FIRST TIME: CONVERT THE ENTIRE COLLECTION
coll = new ORecordLazyList(iRecord);
if (iValue.getClass().isArray()) {
Iterable<Object> iterab = OMultiValue.getMultiValueIterable(iValue, false);
for (Object i : iterab) {
coll.add((OIdentifiable) i);
}
} else {
coll.addAll((Collection<? extends OIdentifiable>) iValue);
((Collection<? extends OIdentifiable>) iValue).clear();
}
iRecord.field(iName, coll);
it = coll.rawIterator();
} else {
// LAZY LIST
coll = (ORecordLazyList) iValue;
if (coll.getStreamedContent() != null) {
// APPEND STREAMED CONTENT
iOutput.append(coll.getStreamedContent());
PROFILER.updateCounter(PROFILER.getProcessMetric("serializer.record.string.linkList2string.cached"), "Serialize linklist to string in stream mode", +1);
it = coll.newItemsIterator();
} else
it = coll.rawIterator();
}
if (it != null && it.hasNext()) {
final StringBuilder buffer = new StringBuilder(128);
for (int items = 0; it.hasNext(); items++) {
if (items > 0)
buffer.append(OStringSerializerHelper.RECORD_SEPARATOR);
final OIdentifiable item = it.next();
final OIdentifiable newRid = linkToStream(buffer, iRecord, item);
if (newRid != null)
((OLazyIterator<OIdentifiable>) it).update(newRid);
}
if (coll != null)
coll.convertRecords2Links();
iOutput.append(buffer);
// UPDATE THE STREAM
if (coll != null)
coll.setStreamedContent(buffer);
}
}
iOutput.append(OStringSerializerHelper.LIST_END);
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkList2string"), "Serialize linklist to string", timer);
break;
}
case LINKSET:
{
if (!(iValue instanceof OStringBuilderSerializable)) {
if (iValue instanceof OAutoConvertToRecord)
((OAutoConvertToRecord) iValue).setAutoConvertToRecord(false);
final Collection<OIdentifiable> coll;
// FIRST TIME: CONVERT THE ENTIRE COLLECTION
if (!(iValue instanceof ORecordLazySet)) {
final ORecordLazySet set = new ORecordLazySet(iRecord);
set.addAll((Collection<OIdentifiable>) iValue);
iRecord.field(iName, set);
coll = set;
} else
coll = (Collection<OIdentifiable>) iValue;
serializeSet(coll, iOutput);
} else {
// LAZY SET
final OStringBuilderSerializable coll = (OStringBuilderSerializable) iValue;
coll.toStream(iOutput);
}
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkSet2string"), "Serialize linkset to string", timer);
break;
}
case LINKMAP:
{
iOutput.append(OStringSerializerHelper.MAP_BEGIN);
Map<Object, Object> map = (Map<Object, Object>) iValue;
// LINKED MAP
if (map instanceof OLazyObjectMapInterface<?>)
((OLazyObjectMapInterface<?>) map).setConvertToRecord(false);
boolean invalidMap = false;
try {
int items = 0;
for (Map.Entry<Object, Object> entry : map.entrySet()) {
if (items++ > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
fieldTypeToString(iOutput, OType.STRING, entry.getKey());
iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
final Object link = linkToStream(iOutput, iRecord, entry.getValue());
if (link != null && !invalidMap)
// IDENTITY IS CHANGED, RE-SET INTO THE COLLECTION TO RECOMPUTE THE HASH
invalidMap = true;
}
} finally {
if (map instanceof OLazyObjectMapInterface<?>) {
((OLazyObjectMapInterface<?>) map).setConvertToRecord(true);
}
}
if (invalidMap) {
final ORecordLazyMap newMap = new ORecordLazyMap(iRecord, ODocument.RECORD_TYPE);
// REPLACE ALL CHANGED ITEMS
for (Map.Entry<Object, Object> entry : map.entrySet()) {
newMap.put(entry.getKey(), (OIdentifiable) entry.getValue());
}
map.clear();
iRecord.field(iName, newMap);
}
iOutput.append(OStringSerializerHelper.MAP_END);
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.linkMap2string"), "Serialize linkmap to string", timer);
break;
}
case EMBEDDED:
if (iValue instanceof ORecord) {
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
toString((ORecord) iValue, iOutput, null, iObjHandler, false, true);
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
} else if (iValue instanceof ODocumentSerializable) {
final ODocument doc = ((ODocumentSerializable) iValue).toDocument();
doc.field(ODocumentSerializable.CLASS_NAME, iValue.getClass().getName());
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
toString(doc, iOutput, null, iObjHandler, false, true);
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
} else if (iValue != null)
iOutput.append(iValue.toString());
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embed2string"), "Serialize embedded to string", timer);
break;
case EMBEDDEDLIST:
embeddedCollectionToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty, false);
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedList2string"), "Serialize embeddedlist to string", timer);
break;
case EMBEDDEDSET:
embeddedCollectionToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty, true);
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedSet2string"), "Serialize embeddedset to string", timer);
break;
case EMBEDDEDMAP:
{
embeddedMapToStream(null, iObjHandler, iOutput, iLinkedClass, iLinkedType, iValue, iSaveOnlyDirty);
PROFILER.stopChrono(PROFILER.getProcessMetric("serializer.record.string.embedMap2string"), "Serialize embeddedmap to string", timer);
break;
}
case LINKBAG:
{
iOutput.append(OStringSerializerHelper.BAG_BEGIN);
((ORidBag) iValue).toStream(iOutput);
iOutput.append(OStringSerializerHelper.BAG_END);
break;
}
default:
fieldTypeToString(iOutput, iType, iValue);
}
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyList in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method fieldFromStream.
public Object fieldFromStream(final ORecord iSourceRecord, final OType iType, OClass iLinkedClass, OType iLinkedType, final String iName, final String iValue) {
if (iValue == null)
return null;
switch(iType) {
case EMBEDDEDLIST:
case EMBEDDEDSET:
return embeddedCollectionFromStream((ODocument) iSourceRecord, iType, iLinkedClass, iLinkedType, iValue);
case LINKSET:
case LINKLIST:
{
if (iValue.length() == 0)
return null;
// REMOVE BEGIN & END COLLECTIONS CHARACTERS IF IT'S A COLLECTION
final String value = iValue.startsWith("[") || iValue.startsWith("<") ? iValue.substring(1, iValue.length() - 1) : iValue;
if (iType == OType.LINKLIST) {
return new ORecordLazyList((ODocument) iSourceRecord).setStreamedContent(new StringBuilder(value));
} else {
return unserializeSet((ODocument) iSourceRecord, value);
}
}
case LINKMAP:
{
if (iValue.length() == 0)
return null;
// REMOVE BEGIN & END MAP CHARACTERS
String value = iValue.substring(1, iValue.length() - 1);
@SuppressWarnings("rawtypes") final Map map = new ORecordLazyMap((ODocument) iSourceRecord, ODocument.RECORD_TYPE);
if (value.length() == 0)
return map;
final List<String> items = OStringSerializerHelper.smartSplit(value, OStringSerializerHelper.RECORD_SEPARATOR, true, false);
// EMBEDDED LITERALS
for (String item : items) {
if (item != null && !item.isEmpty()) {
final List<String> entry = OStringSerializerHelper.smartSplit(item, OStringSerializerHelper.ENTRY_SEPARATOR);
if (!entry.isEmpty()) {
String mapValue = entry.get(1);
if (mapValue != null && !mapValue.isEmpty())
mapValue = mapValue.substring(1);
map.put(fieldTypeFromStream((ODocument) iSourceRecord, OType.STRING, entry.get(0)), new ORecordId(mapValue));
}
}
}
return map;
}
case EMBEDDEDMAP:
return embeddedMapFromStream((ODocument) iSourceRecord, iLinkedType, iValue, iName);
case LINK:
if (iValue.length() > 1) {
int pos = iValue.indexOf(OStringSerializerHelper.CLASS_SEPARATOR);
if (pos > -1)
((OMetadataInternal) ODatabaseRecordThreadLocal.INSTANCE.get().getMetadata()).getImmutableSchemaSnapshot().getClass(iValue.substring(1, pos));
else
pos = 0;
final String linkAsString = iValue.substring(pos + 1);
try {
return new ORecordId(linkAsString);
} catch (IllegalArgumentException e) {
OLogManager.instance().error(this, "Error on unmarshalling field '%s' of record '%s': value '%s' is not a link", iName, iSourceRecord, linkAsString);
return new ORecordId();
}
} else
return null;
case EMBEDDED:
if (iValue.length() > 2) {
// REMOVE BEGIN & END EMBEDDED CHARACTERS
final String value = iValue.substring(1, iValue.length() - 1);
final Object embeddedObject = OStringSerializerEmbedded.INSTANCE.fromStream(value);
if (embeddedObject instanceof ODocument)
ODocumentInternal.addOwner((ODocument) embeddedObject, iSourceRecord);
// RECORD
return embeddedObject;
} else
return null;
case LINKBAG:
final String value = iValue.charAt(0) == OStringSerializerHelper.BAG_BEGIN ? iValue.substring(1, iValue.length() - 1) : iValue;
return ORidBag.fromStream(value);
default:
return fieldTypeFromStream((ODocument) iSourceRecord, iType, iValue);
}
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyList in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateLink method execute.
/**
* Execute the CREATE LINK.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (destField == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
final ODatabaseDocumentInternal database = getDatabase();
if (!(database.getDatabaseOwner() instanceof ODatabaseDocument))
throw new OCommandSQLParsingException("This command supports only the database type ODatabaseDocumentTx and type '" + database.getClass() + "' was found");
final ODatabaseDocument db = (ODatabaseDocument) database.getDatabaseOwner();
final OClass sourceClass = database.getMetadata().getSchema().getClass(sourceClassName);
if (sourceClass == null)
throw new OCommandExecutionException("Source class '" + sourceClassName + "' not found");
final OClass destClass = database.getMetadata().getSchema().getClass(destClassName);
if (destClass == null)
throw new OCommandExecutionException("Destination class '" + destClassName + "' not found");
Object value;
String cmd = "select from ";
if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField)) {
cmd = "select from " + destClassName + " where " + destField + " = ";
}
List<ODocument> result;
ODocument target;
Object oldValue;
long total = 0;
if (linkName == null)
// NO LINK NAME EXPRESSED: OVERWRITE THE SOURCE FIELD
linkName = sourceField;
boolean multipleRelationship;
if (linkType != null)
// DETERMINE BASED ON FORCED TYPE
multipleRelationship = linkType == OType.LINKSET || linkType == OType.LINKLIST;
else
multipleRelationship = false;
long totRecords = db.countClass(sourceClass.getName());
long currRecord = 0;
if (progressListener != null)
progressListener.onBegin(this, totRecords, false);
database.declareIntent(new OIntentMassiveInsert());
try {
// BROWSE ALL THE RECORDS OF THE SOURCE CLASS
for (ODocument doc : db.browseClass(sourceClass.getName())) {
value = doc.field(sourceField);
if (value != null) {
if (value instanceof ODocument || value instanceof ORID) {
// ALREADY CONVERTED
} else if (value instanceof Collection<?>) {
// TODO
} else {
// SEARCH THE DESTINATION RECORD
target = null;
if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField) && value instanceof String)
if (((String) value).length() == 0)
value = null;
else
value = "'" + value + "'";
result = database.<OCommandRequest>command(new OSQLSynchQuery<ODocument>(cmd + value)).execute();
if (result == null || result.size() == 0)
value = null;
else if (result.size() > 1)
throw new OCommandExecutionException("Cannot create link because multiple records was found in class '" + destClass.getName() + "' with value " + value + " in field '" + destField + "'");
else {
target = result.get(0);
value = target;
}
if (target != null && inverse) {
// INVERSE RELATIONSHIP
oldValue = target.field(linkName);
if (oldValue != null) {
if (!multipleRelationship)
multipleRelationship = true;
Collection<ODocument> coll;
if (oldValue instanceof Collection) {
// ADD IT IN THE EXISTENT COLLECTION
coll = (Collection<ODocument>) oldValue;
target.setDirty();
} else {
// CREATE A NEW COLLECTION FOR BOTH
coll = new ArrayList<ODocument>(2);
target.field(linkName, coll);
coll.add((ODocument) oldValue);
}
coll.add(doc);
} else {
if (linkType != null)
if (linkType == OType.LINKSET) {
value = new ORecordLazySet(target);
((Set<OIdentifiable>) value).add(doc);
} else if (linkType == OType.LINKLIST) {
value = new ORecordLazyList(target);
((ORecordLazyList) value).add(doc);
} else
// IGNORE THE TYPE, SET IT AS LINK
value = doc;
else
value = doc;
target.field(linkName, value);
}
target.save();
} else {
// SET THE REFERENCE
doc.field(linkName, value);
doc.save();
}
total++;
}
}
if (progressListener != null)
progressListener.onProgress(this, currRecord, currRecord * 100f / totRecords);
}
if (total > 0) {
if (inverse) {
// REMOVE THE OLD PROPERTY IF ANY
OProperty prop = destClass.getProperty(linkName);
if (prop != null)
destClass.dropProperty(linkName);
if (linkType == null)
linkType = multipleRelationship ? OType.LINKSET : OType.LINK;
// CREATE THE PROPERTY
destClass.createProperty(linkName, linkType, sourceClass);
} else {
// REMOVE THE OLD PROPERTY IF ANY
OProperty prop = sourceClass.getProperty(linkName);
if (prop != null)
sourceClass.dropProperty(linkName);
// CREATE THE PROPERTY
sourceClass.createProperty(linkName, OType.LINK, destClass);
}
}
if (progressListener != null)
progressListener.onCompletition(this, true);
} catch (Exception e) {
if (progressListener != null)
progressListener.onCompletition(this, false);
throw OException.wrapException(new OCommandExecutionException("Error on creation of links"), e);
} finally {
database.declareIntent(null);
}
return total;
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyList in project orientdb by orientechnologies.
the class CollectionOfLinkInNestedDocumentTest method nestedLinkList.
@Test
public void nestedLinkList() {
ODocument doc1 = new ODocument();
doc1.field("value", "item 1");
ODocument doc2 = new ODocument();
doc2.field("value", "item 2");
ODocument nested = new ODocument();
ORecordLazyList list = new ORecordLazyList(nested);
list.add(doc1);
list.add(doc2);
nested.field("list", list);
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("list"), nested.field("list"));
}
use of com.orientechnologies.orient.core.db.record.ORecordLazyList 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()));
}
Aggregations