Search in sources :

Example 11 with OTransactionIndexChanges

use of com.orientechnologies.orient.core.tx.OTransactionIndexChanges in project orientdb by orientechnologies.

the class OIndexTxAwareOneValue method iterateEntries.

@Override
public OIndexCursor iterateEntries(Collection<?> keys, boolean ascSortOrder) {
    final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
    if (indexChanges == null)
        return super.iterateEntries(keys, ascSortOrder);
    final List<Object> sortedKeys = new ArrayList<Object>(keys.size());
    for (Object key : keys) sortedKeys.add(getCollatingValue(key));
    if (ascSortOrder)
        Collections.sort(sortedKeys, ODefaultComparator.INSTANCE);
    else
        Collections.sort(sortedKeys, Collections.reverseOrder(ODefaultComparator.INSTANCE));
    final OIndexCursor txCursor = new OIndexAbstractCursor() {

        private Iterator<Object> keysIterator = sortedKeys.iterator();

        @Override
        public Map.Entry<Object, OIdentifiable> nextEntry() {
            if (keysIterator == null)
                return null;
            Map.Entry<Object, OIdentifiable> entry = null;
            while (entry == null && keysIterator.hasNext()) {
                final Object key = keysIterator.next();
                entry = calculateTxIndexEntry(key, null, indexChanges);
            }
            if (entry == null) {
                keysIterator = null;
                return null;
            }
            return entry;
        }
    };
    if (indexChanges.cleared)
        return txCursor;
    final OIndexCursor backedCursor = super.iterateEntries(keys, ascSortOrder);
    return new OIndexTxCursor(txCursor, backedCursor, ascSortOrder, indexChanges);
}
Also used : OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 12 with OTransactionIndexChanges

use of com.orientechnologies.orient.core.tx.OTransactionIndexChanges 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 13 with OTransactionIndexChanges

use of com.orientechnologies.orient.core.tx.OTransactionIndexChanges in project orientdb by orientechnologies.

the class OIndexTxAwareMultiValue method iterateEntriesMinor.

@Override
public OIndexCursor iterateEntriesMinor(Object toKey, boolean toInclusive, boolean ascOrder) {
    final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
    if (indexChanges == null)
        return super.iterateEntriesMinor(toKey, toInclusive, ascOrder);
    toKey = getCollatingValue(toKey);
    final OIndexCursor txCursor;
    final Object firstKey = indexChanges.getFirstKey();
    if (ascOrder)
        txCursor = new PureTxBetweenIndexForwardCursor(firstKey, true, toKey, toInclusive, indexChanges);
    else
        txCursor = new PureTxBetweenIndexBackwardCursor(firstKey, true, toKey, toInclusive, indexChanges);
    if (indexChanges.cleared)
        return txCursor;
    final OIndexCursor backedCursor = super.iterateEntriesMinor(toKey, toInclusive, ascOrder);
    return new OIndexTxCursor(txCursor, backedCursor, ascOrder, indexChanges);
}
Also used : OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges)

Example 14 with OTransactionIndexChanges

use of com.orientechnologies.orient.core.tx.OTransactionIndexChanges in project orientdb by orientechnologies.

the class OTransactionOptimisticProxy method fillIndexOperations.

private void fillIndexOperations(final ODocument remoteIndexEntries) {
    for (Entry<String, Object> indexEntry : remoteIndexEntries) {
        final String indexName = indexEntry.getKey();
        final ODocument indexDoc = (ODocument) indexEntry.getValue();
        if (indexDoc == null)
            continue;
        OTransactionIndexChanges transactionIndexChanges = indexEntries.get(indexEntry.getKey());
        if (transactionIndexChanges == null) {
            transactionIndexChanges = new OTransactionIndexChanges();
            indexEntries.put(indexEntry.getKey(), transactionIndexChanges);
        }
        final Boolean clearAll = indexDoc.field("clear");
        if (clearAll != null && clearAll)
            transactionIndexChanges.setCleared();
        final Collection<ODocument> entries = indexDoc.field("entries");
        if (entries == null)
            continue;
        for (final ODocument entry : entries) {
            final List<ODocument> operations = entry.field("ops");
            if (operations == null)
                continue;
            final Object key;
            try {
                ODocument keyContainer;
                if (protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_24) {
                    final String serializedKey = OStringSerializerHelper.decode((String) entry.field("k"));
                    if (serializedKey.equals("*"))
                        keyContainer = null;
                    else {
                        keyContainer = new ODocument();
                        keyContainer.setLazyLoad(false);
                        ORecordSerializerSchemaAware2CSV.INSTANCE.fromString(serializedKey, keyContainer, null);
                    }
                } else {
                    keyContainer = entry.field("k");
                }
                if (keyContainer != null) {
                    final Object storedKey = keyContainer.field("key");
                    if (storedKey instanceof List)
                        key = new OCompositeKey((List<? extends Comparable<?>>) storedKey);
                    else if (Boolean.TRUE.equals(keyContainer.field("binary"))) {
                        key = OStreamSerializerAnyStreamable.INSTANCE.fromStream((byte[]) storedKey);
                    } else
                        key = storedKey;
                } else
                    key = null;
            } catch (IOException ioe) {
                throw OException.wrapException(new OTransactionException("Error during index changes deserialization. "), ioe);
            }
            for (final ODocument op : operations) {
                final int operation = (Integer) op.rawField("o");
                final OTransactionIndexChanges.OPERATION indexOperation = OTransactionIndexChanges.OPERATION.values()[operation];
                final OIdentifiable value = op.field("v");
                transactionIndexChanges.getChangesPerKey(key).add(value, indexOperation);
                if (value == null)
                    continue;
                final ORID rid = value.getIdentity();
                List<OTransactionRecordIndexOperation> txIndexOperations = recordIndexOperations.get(rid);
                if (txIndexOperations == null) {
                    txIndexOperations = new ArrayList<OTransactionRecordIndexOperation>();
                    recordIndexOperations.put(rid, txIndexOperations);
                }
                txIndexOperations.add(new OTransactionRecordIndexOperation(indexName, key, indexOperation));
            }
        }
    }
}
Also used : OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges) IOException(java.io.IOException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ORID(com.orientechnologies.orient.core.id.ORID) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OTransactionIndexChanges (com.orientechnologies.orient.core.tx.OTransactionIndexChanges)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)5 OTransactionIndexEntry (com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry)4 OTransactionIndexChangesPerKey (com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey)3 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)1 OEmptyIterator (com.orientechnologies.orient.core.iterator.OEmptyIterator)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 IOException (java.io.IOException)1