Search in sources :

Example 6 with OTransactionIndexEntry

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;
    }
}
Also used : OTransactionIndexChangesPerKey(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey) OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges) OTransactionIndexEntry(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry)

Example 7 with OTransactionIndexEntry

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);
}
Also used : OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecord(com.orientechnologies.orient.core.record.ORecord) OSerializableStream(com.orientechnologies.orient.core.serialization.OSerializableStream) IOException(java.io.IOException) OTransactionIndexEntry(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordElement(com.orientechnologies.orient.core.db.record.ORecordElement)

Aggregations

OTransactionIndexEntry (com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry)7 OTransactionIndexChangesPerKey (com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey)6 OTransactionIndexChanges (com.orientechnologies.orient.core.tx.OTransactionIndexChanges)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ORecordElement (com.orientechnologies.orient.core.db.record.ORecordElement)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OSerializableStream (com.orientechnologies.orient.core.serialization.OSerializableStream)1 IOException (java.io.IOException)1