use of com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry in project orientdb by orientechnologies.
the class OIndexTxAware method getLastKey.
@Override
public Object getLastKey() {
final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
if (indexChanges == null)
return delegate.getLastKey();
Object indexLastKey;
if (indexChanges.cleared)
indexLastKey = null;
else
indexLastKey = delegate.getLastKey();
Object lastKey = indexChanges.getLastKey();
while (true) {
OTransactionIndexChangesPerKey changesPerKey = indexChanges.getChangesPerKey(lastKey);
for (OTransactionIndexEntry indexEntry : changesPerKey.entries) {
if (indexEntry.operation.equals(OPERATION.REMOVE))
lastKey = null;
else
lastKey = changesPerKey.key;
}
if (changesPerKey.key.equals(indexLastKey))
indexLastKey = lastKey;
if (lastKey != null) {
if (indexLastKey != null && ((Comparable) indexLastKey).compareTo(lastKey) > 0)
return indexLastKey;
return lastKey;
}
lastKey = indexChanges.getLowerKey(changesPerKey.key);
if (lastKey == null)
return indexLastKey;
}
}
use of com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry 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