use of com.orientechnologies.orient.core.db.object.ODatabaseObject in project orientdb by orientechnologies.
the class ORecordSerializerCSVAbstract method embeddedCollectionToStream.
public StringBuilder embeddedCollectionToStream(ODatabase<?> iDatabase, final OUserObject2RecordHandler iObjHandler, final StringBuilder iOutput, final OClass iLinkedClass, final OType iLinkedType, final Object iValue, final boolean iSaveOnlyDirty, final boolean iSet) {
iOutput.append(iSet ? OStringSerializerHelper.SET_BEGIN : OStringSerializerHelper.LIST_BEGIN);
final Iterator<Object> iterator = OMultiValue.getMultiValueIterator(iValue, false);
OType linkedType = iLinkedType;
for (int i = 0; iterator.hasNext(); ++i) {
final Object o = iterator.next();
if (i > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
if (o == null) {
iOutput.append("null");
continue;
}
OIdentifiable id = null;
ODocument doc = null;
final OClass linkedClass;
if (!(o instanceof OIdentifiable)) {
final String fieldBound = OObjectSerializerHelperManager.getInstance().getDocumentBoundField(o.getClass());
if (fieldBound != null) {
OObjectSerializerHelperManager.getInstance().invokeCallback(o, null, OBeforeSerialization.class);
doc = (ODocument) OObjectSerializerHelperManager.getInstance().getFieldValue(o, fieldBound);
OObjectSerializerHelperManager.getInstance().invokeCallback(o, doc, OAfterSerialization.class);
id = doc;
} else if (iLinkedType == null)
linkedType = OType.getTypeByClass(o.getClass());
linkedClass = iLinkedClass;
} else {
id = (OIdentifiable) o;
if (iLinkedType == null)
// AUTO-DETERMINE LINKED TYPE
if (id.getIdentity().isValid())
linkedType = OType.LINK;
else
linkedType = OType.EMBEDDED;
if (id instanceof ODocument) {
doc = (ODocument) id;
if (doc.hasOwners())
linkedType = OType.EMBEDDED;
assert linkedType == OType.EMBEDDED || id.getIdentity().isValid() || (ODatabaseRecordThreadLocal.INSTANCE.get().getStorage() instanceof OStorageProxy) : "Impossible to serialize invalid link " + id.getIdentity();
linkedClass = ODocumentInternal.getImmutableSchemaClass(doc);
} else
linkedClass = null;
}
if (id != null && linkedType != OType.LINK)
iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
if (linkedType == OType.EMBEDDED && o instanceof OIdentifiable)
toString((ORecord) ((OIdentifiable) o).getRecord(), iOutput, null);
else if (linkedType != OType.LINK && (linkedClass != null || doc != null)) {
if (id == null) {
// EMBEDDED OBJECTS
if (iDatabase == null && ODatabaseRecordThreadLocal.INSTANCE.isDefined())
iDatabase = ODatabaseRecordThreadLocal.INSTANCE.get();
id = OObjectSerializerHelperManager.getInstance().toStream(o, new ODocument(o.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(linkedClass);
}
public boolean existsUserObjectByRID(ORID iRID) {
return false;
}
public void registerUserObject(Object iObject, ORecord iRecord) {
}
public void registerUserObjectAfterLinkSave(ORecord iRecord) {
}
}, null, iSaveOnlyDirty);
}
toString(doc, iOutput, null, iObjHandler, false, true);
} else {
// EMBEDDED LITERALS
if (iLinkedType == null) {
if (o != null)
linkedType = OType.getTypeByClass(o.getClass());
} else if (iLinkedType == OType.CUSTOM)
iOutput.append(OStringSerializerHelper.CUSTOM_TYPE);
fieldTypeToString(iOutput, linkedType, o);
}
if (id != null && linkedType != OType.LINK)
iOutput.append(OStringSerializerHelper.EMBEDDED_END);
}
iOutput.append(iSet ? OStringSerializerHelper.SET_END : OStringSerializerHelper.LIST_END);
return iOutput;
}
use of com.orientechnologies.orient.core.db.object.ODatabaseObject in project orientdb by orientechnologies.
the class ORecordSerializerSchemaAware2CSV method toString.
@Override
protected StringBuilder toString(ORecord iRecord, final StringBuilder iOutput, final String iFormat, OUserObject2RecordHandler iObjHandler, final boolean iOnlyDelta, final boolean autoDetectCollectionType) {
if (iRecord == null)
throw new OSerializationException("Expected a record but was null");
if (!(iRecord instanceof ODocument))
throw new OSerializationException("Cannot marshall a record of type " + iRecord.getClass().getSimpleName());
final ODocument record = (ODocument) iRecord;
if (!iOnlyDelta && ODocumentInternal.getImmutableSchemaClass(record) != null) {
iOutput.append(ODocumentInternal.getImmutableSchemaClass(record).getStreamableName());
iOutput.append(OStringSerializerHelper.CLASS_SEPARATOR);
}
OProperty prop;
OType type;
OClass linkedClass;
OType linkedType;
String fieldClassName;
int i = 0;
final String[] fieldNames = iOnlyDelta && record.isTrackingChanges() ? record.getDirtyFields() : record.fieldNames();
// MARSHALL ALL THE FIELDS OR DELTA IF TRACKING IS ENABLED
for (String fieldName : fieldNames) {
Object fieldValue = record.rawField(fieldName);
if (i > 0)
iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
// SEARCH FOR A CONFIGURED PROPERTY
prop = ODocumentInternal.getImmutableSchemaClass(record) != null ? ODocumentInternal.getImmutableSchemaClass(record).getProperty(fieldName) : null;
fieldClassName = getClassName(fieldValue);
type = record.fieldType(fieldName);
if (type == OType.ANY)
type = null;
linkedClass = null;
linkedType = null;
if (prop != null && prop.getType() != OType.ANY) {
// RECOGNIZED PROPERTY
type = prop.getType();
linkedClass = prop.getLinkedClass();
linkedType = prop.getLinkedType();
} else if (fieldValue != null) {
// NOT FOUND: TRY TO DETERMINE THE TYPE FROM ITS CONTENT
if (type == null) {
if (fieldValue.getClass() == byte[].class)
type = OType.BINARY;
else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && fieldValue instanceof ORecord) {
if (type == null)
// DETERMINE THE FIELD TYPE
if (fieldValue instanceof ODocument && ((ODocument) fieldValue).hasOwners())
type = OType.EMBEDDED;
else
type = OType.LINK;
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), fieldClassName);
} else if (fieldValue instanceof ORID)
// DETERMINE THE FIELD TYPE
type = OType.LINK;
else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(fieldClassName) != null) {
// DETERMINE THE FIELD TYPE
type = OType.LINK;
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), fieldClassName);
} else if (fieldValue instanceof Date)
type = OType.DATETIME;
else if (fieldValue instanceof String)
type = OType.STRING;
else if (fieldValue instanceof Integer || fieldValue instanceof BigInteger)
type = OType.INTEGER;
else if (fieldValue instanceof Long)
type = OType.LONG;
else if (fieldValue instanceof Float)
type = OType.FLOAT;
else if (fieldValue instanceof Short)
type = OType.SHORT;
else if (fieldValue instanceof Byte)
type = OType.BYTE;
else if (fieldValue instanceof Double)
type = OType.DOUBLE;
else if (fieldValue instanceof BigDecimal)
type = OType.DECIMAL;
else if (fieldValue instanceof ORidBag)
type = OType.LINKBAG;
if (fieldValue instanceof OMultiCollectionIterator<?>) {
type = ((OMultiCollectionIterator<?>) fieldValue).isEmbedded() ? OType.EMBEDDEDLIST : OType.LINKLIST;
linkedType = ((OMultiCollectionIterator<?>) fieldValue).isEmbedded() ? OType.EMBEDDED : OType.LINK;
} else if (fieldValue instanceof Collection<?> || fieldValue.getClass().isArray()) {
final int size = OMultiValue.getSize(fieldValue);
Boolean autoConvertLinks = null;
if (fieldValue instanceof ORecordLazyMultiValue) {
autoConvertLinks = ((ORecordLazyMultiValue) fieldValue).isAutoConvertToRecord();
if (autoConvertLinks)
// DISABLE AUTO CONVERT
((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(false);
}
if (autoDetectCollectionType)
if (size > 0) {
final Object firstValue = OMultiValue.getFirstValue(fieldValue);
if (firstValue != null) {
if (firstValue instanceof ORID) {
linkedClass = null;
linkedType = OType.LINK;
if (fieldValue instanceof Set<?>)
type = OType.LINKSET;
else
type = OType.LINKLIST;
} else if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && (firstValue instanceof ODocument && !((ODocument) firstValue).isEmbedded()) && (firstValue instanceof ORecord || (ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), getClassName(firstValue));
if (type == null) {
// LINK: GET THE CLASS
linkedType = OType.LINK;
if (fieldValue instanceof Set<?>)
type = OType.LINKSET;
else
type = OType.LINKLIST;
} else
linkedType = OType.EMBEDDED;
} else {
// EMBEDDED COLLECTION
if (firstValue instanceof ODocument && ((((ODocument) firstValue).hasOwners()) || type == OType.EMBEDDEDSET || type == OType.EMBEDDEDLIST || type == OType.EMBEDDEDMAP))
linkedType = OType.EMBEDDED;
else if (firstValue instanceof Enum<?>)
linkedType = OType.STRING;
else {
linkedType = OType.getTypeByClass(firstValue.getClass());
if (linkedType != OType.LINK)
// EMBEDDED FOR SURE DON'T USE THE LINKED TYPE
linkedType = null;
}
if (type == null)
if (fieldValue instanceof ORecordLazySet)
type = OType.LINKSET;
else if (fieldValue instanceof Set<?>)
type = OType.EMBEDDEDSET;
else
type = OType.EMBEDDEDLIST;
}
}
} else if (type == null)
type = OType.EMBEDDEDLIST;
if (fieldValue instanceof ORecordLazyMultiValue && autoConvertLinks) {
// REPLACE PREVIOUS SETTINGS
((ORecordLazyMultiValue) fieldValue).setAutoConvertToRecord(true);
}
} else if (fieldValue instanceof Map<?, ?> && type == null) {
final int size = OMultiValue.getSize(fieldValue);
Boolean autoConvertLinks = null;
if (fieldValue instanceof ORecordLazyMap) {
autoConvertLinks = ((ORecordLazyMap) fieldValue).isAutoConvertToRecord();
if (autoConvertLinks)
// DISABLE AUTO CONVERT
((ORecordLazyMap) fieldValue).setAutoConvertToRecord(false);
}
if (size > 0) {
final Object firstValue = OMultiValue.getFirstValue(fieldValue);
if (firstValue != null) {
if (ODatabaseRecordThreadLocal.INSTANCE.isDefined() && (firstValue instanceof ODocument && !((ODocument) firstValue).isEmbedded()) && (firstValue instanceof ORecord || (ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner() instanceof ODatabaseObject && ((ODatabaseObject) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner()).getEntityManager().getEntityClass(getClassName(firstValue)) != null))) {
linkedClass = getLinkInfo(ODatabaseRecordThreadLocal.INSTANCE.get(), getClassName(firstValue));
// LINK: GET THE CLASS
linkedType = OType.LINK;
type = OType.LINKMAP;
}
}
}
if (type == null)
type = OType.EMBEDDEDMAP;
if (fieldValue instanceof ORecordLazyMap && autoConvertLinks)
// REPLACE PREVIOUS SETTINGS
((ORecordLazyMap) fieldValue).setAutoConvertToRecord(true);
}
}
}
if (type == OType.TRANSIENT)
// TRANSIENT FIELD
continue;
if (type == null)
type = OType.EMBEDDED;
iOutput.append(fieldName);
iOutput.append(FIELD_VALUE_SEPARATOR);
fieldToStream(record, iOutput, iObjHandler, type, linkedClass, linkedType, fieldName, fieldValue, true);
i++;
}
// GET THE OVERSIZE IF ANY
final float overSize;
if (ODocumentInternal.getImmutableSchemaClass(record) != null)
// GET THE CONFIGURED OVERSIZE SETTED PER CLASS
overSize = ODocumentInternal.getImmutableSchemaClass(record).getOverSize();
else
overSize = 0;
// APPEND BLANKS IF NEEDED
final int newSize;
if (record.hasOwners())
// EMBEDDED: GET REAL SIZE
newSize = iOutput.length();
else if (record.getSize() == iOutput.length())
// IDENTICAL! DO NOTHING
newSize = record.getSize();
else if (record.getSize() > iOutput.length() && !OGlobalConfiguration.RECORD_DOWNSIZING_ENABLED.getValueAsBoolean()) {
// APPEND EXTRA SPACES TO FILL ALL THE AVAILABLE SPACE AND AVOID FRAGMENTATION
newSize = record.getSize();
} else if (overSize > 0) {
// APPEND EXTRA SPACES TO GET A LARGER iOutput
newSize = (int) (iOutput.length() * overSize);
} else
// NO OVERSIZE
newSize = iOutput.length();
if (newSize > iOutput.length()) {
iOutput.ensureCapacity(newSize);
for (int b = iOutput.length(); b < newSize; ++b) iOutput.append(' ');
}
return iOutput;
}
Aggregations