// JUST THE REFERENCE
rid = (ORID) iLinked;
if (rid.isValid() && rid.isNew()) {
// SAVE AT THE FLY AND STORE THE NEW RID
final ORecord record = rid.getRecord();
final ODatabaseRecord database = ODatabaseRecordThreadLocal.INSTANCE.get();
if (database.getTransaction().isActive()) {
// USE THE DEFAULT CLUSTER
database.save((ORecord) record);
} else
database.save((ORecord) record);
if (record != null)
rid = record.getIdentity();
resultRid = rid;
}
} else {
if (iLinked instanceof String)
iLinked = new ORecordId((String) iLinked);
else if (!(iLinked instanceof ORecord)) {
// NOT RECORD: TRY TO EXTRACT THE DOCUMENT IF ANY
final String boundDocumentField = OObjectSerializerHelperManager.getInstance().getDocumentBoundField(iLinked.getClass());
if (boundDocumentField != null)
iLinked = OObjectSerializerHelperManager.getInstance().getFieldValue(iLinked, boundDocumentField);
}
if (!(iLinked instanceof OIdentifiable))
throw new IllegalArgumentException("Invalid object received. Expected a OIdentifiable but received type="
+ iLinked.getClass().getName() + " and value=" + iLinked);
// RECORD
ORecord iLinkedRecord = ((OIdentifiable) iLinked).getRecord();
rid = iLinkedRecord.getIdentity();
if ((rid.isNew() && !rid.isTemporary()) || iLinkedRecord.isDirty()) {
final ODatabaseRecordInternal database = ODatabaseRecordThreadLocal.INSTANCE.get();
if (iLinkedRecord instanceof ODocument) {
final OClass schemaClass = ((ODocument) iLinkedRecord).getSchemaClass();
database.save(iLinkedRecord, schemaClass != null ? database.getClusterNameById(schemaClass.getClusterForNewInstance())
: null);
} else
// STORE THE TRAVERSED OBJECT TO KNOW THE RECORD ID. CALL THIS VERSION TO AVOID CLEAR OF STACK IN THREAD-LOCAL
database.save(iLinkedRecord);
final ODatabaseComplex<?> dbOwner = database.getDatabaseOwner();
dbOwner.registerUserObjectAfterLinkSave(iLinkedRecord);
resultRid = iLinkedRecord;
}
final ODatabaseRecord database = ODatabaseRecordThreadLocal.INSTANCE.get();
if (iParentRecord != null && database instanceof ODatabaseRecord) {
final ODatabaseRecord db = database;
if (!db.isRetainRecords())
// REPLACE CURRENT RECORD WITH ITS ID: THIS SAVES A LOT OF MEMORY
resultRid = iLinkedRecord.getIdentity();
}
}
if (rid.isValid())
rid.toString(buffer);