use of com.orientechnologies.orient.core.serialization.ODocumentSerializable in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method embeddedMapToStream.
public void embeddedMapToStream(ODatabase<?> iDatabase, final OUserObject2RecordHandler iObjHandler, final StringBuilder iOutput, final OClass iLinkedClass, OType iLinkedType, final Object iValue, final boolean iSaveOnlyDirty) {
iOutput.append(OStringSerializerHelper.MAP_BEGIN);
if (iValue != null) {
int items = 0;
// EMBEDDED OBJECTS
for (Entry<String, Object> o : ((Map<String, Object>) iValue).entrySet()) {
if (items > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
if (o != null) {
fieldTypeToString(iOutput, OType.STRING, o.getKey());
iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
if (o.getValue() instanceof ODocument && ((ODocument) o.getValue()).getIdentity().isValid()) {
fieldTypeToString(iOutput, OType.LINK, o.getValue());
} else if (o.getValue() instanceof ORecord || o.getValue() instanceof ODocumentSerializable) {
final ODocument record;
if (o.getValue() instanceof ODocument)
record = (ODocument) o.getValue();
else if (o.getValue() instanceof ODocumentSerializable) {
record = ((ODocumentSerializable) o.getValue()).toDocument();
record.field(ODocumentSerializable.CLASS_NAME, o.getValue().getClass().getName());
} else {
if (iDatabase == null && ODatabaseRecordThreadLocal.INSTANCE.isDefined())
iDatabase = ODatabaseRecordThreadLocal.INSTANCE.get();
record = OObjectSerializerHelperManager.getInstance().toStream(o.getValue(), new ODocument(o.getValue().getClass().getSimpleName()), iDatabase instanceof ODatabaseObject ? ((ODatabaseObject) iDatabase).getEntityManager() : OEntityManagerInternal.INSTANCE, iLinkedClass, iObjHandler != null ? iObjHandler : new OUserObject2RecordHandler() {
public Object getUserObjectByRecord(OIdentifiable iRecord, final String iFetchPlan) {
return iRecord;
}
public ORecord getRecordByUserObject(Object iPojo, boolean iCreateIfNotAvailable) {
return new ODocument(iLinkedClass);
}
public boolean existsUserObjectByRID(ORID iRID) {
return false;
}
public void registerUserObject(Object iObject, ORecord iRecord) {
}
public void registerUserObjectAfterLinkSave(ORecord iRecord) {
}
}, null, iSaveOnlyDirty);
}
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
toString(record, iOutput, null, iObjHandler, false, true);
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
} else if (o.getValue() instanceof Set<?>) {
// SUB SET
fieldTypeToString(iOutput, OType.EMBEDDEDSET, o.getValue());
} else if (o.getValue() instanceof Collection<?>) {
// SUB LIST
fieldTypeToString(iOutput, OType.EMBEDDEDLIST, o.getValue());
} else if (o.getValue() instanceof Map<?, ?>) {
// SUB MAP
fieldTypeToString(iOutput, OType.EMBEDDEDMAP, o.getValue());
} else {
// EMBEDDED LITERALS
if (iLinkedType == null && o.getValue() != null) {
fieldTypeToString(iOutput, OType.getTypeByClass(o.getValue().getClass()), o.getValue());
} else {
fieldTypeToString(iOutput, iLinkedType, o.getValue());
}
}
}
items++;
}
}
iOutput.append(OStringSerializerHelper.MAP_END);
}
use of com.orientechnologies.orient.core.serialization.ODocumentSerializable in project orientdb by orientechnologies.
the class ORecordSerializerBinaryV0 method deserializeValue.
@Override
public Object deserializeValue(final BytesContainer bytes, final OType type, final ODocument ownerDocument) {
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, ownerDocument);
break;
case EMBEDDEDSET:
value = readEmbeddedSet(bytes, ownerDocument);
break;
case EMBEDDEDLIST:
value = readEmbeddedList(bytes, ownerDocument);
break;
case LINKSET:
value = readLinkCollection(bytes, new ORecordLazySet(ownerDocument));
break;
case LINKLIST:
value = readLinkCollection(bytes, new ORecordLazyList(ownerDocument));
break;
case BINARY:
value = readBinary(bytes);
break;
case LINK:
value = readOptimizedLink(bytes);
break;
case LINKMAP:
value = readLinkMap(bytes, ownerDocument);
break;
case EMBEDDEDMAP:
value = readEmbeddedMap(bytes, ownerDocument);
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(ownerDocument);
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.serialization.ODocumentSerializable in project orientdb by orientechnologies.
the class ORecordSerializerNetworkV0 method serializeValue.
@SuppressWarnings("unchecked")
public int serializeValue(final BytesContainer bytes, Object value, final OType type, final OType linkedType) {
int pointer = 0;
switch(type) {
case INTEGER:
case LONG:
case SHORT:
pointer = OVarIntSerializer.write(bytes, ((Number) value).longValue());
break;
case STRING:
pointer = writeString(bytes, value.toString());
break;
case DOUBLE:
long dg = Double.doubleToLongBits((Double) value);
pointer = bytes.alloc(OLongSerializer.LONG_SIZE);
OLongSerializer.INSTANCE.serializeLiteral(dg, bytes.bytes, pointer);
break;
case FLOAT:
int fg = Float.floatToIntBits((Float) value);
pointer = bytes.alloc(OIntegerSerializer.INT_SIZE);
OIntegerSerializer.INSTANCE.serializeLiteral(fg, bytes.bytes, pointer);
break;
case BYTE:
pointer = bytes.alloc(1);
bytes.bytes[pointer] = (Byte) value;
break;
case BOOLEAN:
pointer = bytes.alloc(1);
bytes.bytes[pointer] = ((Boolean) value) ? (byte) 1 : (byte) 0;
break;
case DATETIME:
if (value instanceof Long) {
pointer = OVarIntSerializer.write(bytes, (Long) value);
} else
pointer = OVarIntSerializer.write(bytes, ((Date) value).getTime());
break;
case DATE:
long dateValue;
if (value instanceof Long) {
dateValue = (Long) value;
} else
dateValue = ((Date) value).getTime();
dateValue = convertDayToTimezone(ODateHelper.getDatabaseTimeZone(), TimeZone.getTimeZone("GMT"), dateValue);
pointer = OVarIntSerializer.write(bytes, dateValue / MILLISEC_PER_DAY);
break;
case EMBEDDED:
pointer = bytes.offset;
if (value instanceof ODocumentSerializable) {
ODocument cur = ((ODocumentSerializable) value).toDocument();
cur.field(ODocumentSerializable.CLASS_NAME, value.getClass().getName());
serialize(cur, bytes, false);
} else {
serialize((ODocument) value, bytes, false);
}
break;
case EMBEDDEDSET:
case EMBEDDEDLIST:
if (value.getClass().isArray())
pointer = writeEmbeddedCollection(bytes, Arrays.asList(OMultiValue.array(value)), linkedType);
else
pointer = writeEmbeddedCollection(bytes, (Collection<?>) value, linkedType);
break;
case DECIMAL:
BigDecimal decimalValue = (BigDecimal) value;
pointer = bytes.alloc(ODecimalSerializer.INSTANCE.getObjectSize(decimalValue));
ODecimalSerializer.INSTANCE.serialize(decimalValue, bytes.bytes, pointer);
break;
case BINARY:
pointer = writeBinary(bytes, (byte[]) (value));
break;
case LINKSET:
case LINKLIST:
Collection<OIdentifiable> ridCollection = (Collection<OIdentifiable>) value;
pointer = writeLinkCollection(bytes, ridCollection);
break;
case LINK:
if (!(value instanceof OIdentifiable))
throw new OValidationException("Value '" + value + "' is not a OIdentifiable");
pointer = writeOptimizedLink(bytes, (OIdentifiable) value);
break;
case LINKMAP:
pointer = writeLinkMap(bytes, (Map<Object, OIdentifiable>) value);
break;
case EMBEDDEDMAP:
pointer = writeEmbeddedMap(bytes, (Map<Object, Object>) value);
break;
case LINKBAG:
pointer = ((ORidBag) value).toStream(bytes);
break;
case CUSTOM:
if (!(value instanceof OSerializableStream))
value = new OSerializableWrapper((Serializable) value);
pointer = writeString(bytes, value.getClass().getName());
writeBinary(bytes, ((OSerializableStream) value).toStream());
break;
case TRANSIENT:
break;
case ANY:
break;
}
return pointer;
}
use of com.orientechnologies.orient.core.serialization.ODocumentSerializable 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.serialization.ODocumentSerializable 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);
}
}
Aggregations