Search in sources :

Example 1 with OTransactionIndexChanges

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

the class OIndexTxAware method getSize.

@Override
public long getSize() {
    long tot = delegate.getSize();
    final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
    if (indexChanges != null) {
        if (indexChanges.cleared)
            // BEGIN FROM 0
            tot = 0;
        for (final Entry<Object, OTransactionIndexChangesPerKey> entry : indexChanges.changesPerKey.entrySet()) {
            for (final OTransactionIndexEntry e : entry.getValue().entries) {
                if (e.operation == OPERATION.REMOVE) {
                    if (e.value == null)
                        // KEY REMOVED
                        tot--;
                }
            }
        }
        for (final OTransactionIndexEntry e : indexChanges.nullKeyChanges.entries) {
            if (e.operation == OPERATION.REMOVE) {
                if (e.value == null)
                    // KEY REMOVED
                    tot--;
            }
        }
    }
    return tot;
}
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 2 with OTransactionIndexChanges

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

the class OIndexTxAware method getFirstKey.

@Override
public Object getFirstKey() {
    final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
    if (indexChanges == null)
        return delegate.getFirstKey();
    Object indexFirstKey;
    if (indexChanges.cleared)
        indexFirstKey = null;
    else
        indexFirstKey = delegate.getFirstKey();
    Object firstKey = indexChanges.getFirstKey();
    while (true) {
        OTransactionIndexChangesPerKey changesPerKey = indexChanges.getChangesPerKey(firstKey);
        for (OTransactionIndexEntry indexEntry : changesPerKey.entries) {
            if (indexEntry.operation.equals(OPERATION.REMOVE))
                firstKey = null;
            else
                firstKey = changesPerKey.key;
        }
        if (changesPerKey.key.equals(indexFirstKey))
            indexFirstKey = firstKey;
        if (firstKey != null) {
            if (indexFirstKey != null && ((Comparable) indexFirstKey).compareTo(firstKey) < 0)
                return indexFirstKey;
            return firstKey;
        }
        firstKey = indexChanges.getHigherKey(changesPerKey.key);
        if (firstKey == null)
            return indexFirstKey;
    }
}
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 3 with OTransactionIndexChanges

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

the class OIndexTxAwareMultiValue method iterateEntriesBetween.

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

Example 4 with OTransactionIndexChanges

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

the class OIndexTxAwareMultiValue 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();

        private Iterator<OIdentifiable> valuesIterator = new OEmptyIterator<OIdentifiable>();

        private Object key;

        @Override
        public Map.Entry<Object, OIdentifiable> nextEntry() {
            if (valuesIterator.hasNext())
                return nextEntryInternal();
            if (keysIterator == null)
                return null;
            Set<OIdentifiable> result = null;
            while (result == null && keysIterator.hasNext()) {
                key = keysIterator.next();
                result = calculateTxValue(key, indexChanges);
                if (result != null && result.isEmpty())
                    result = null;
            }
            if (result == null) {
                keysIterator = null;
                return null;
            }
            valuesIterator = result.iterator();
            return nextEntryInternal();
        }

        private Map.Entry<Object, OIdentifiable> nextEntryInternal() {
            final OIdentifiable identifiable = valuesIterator.next();
            return new Map.Entry<Object, OIdentifiable>() {

                @Override
                public Object getKey() {
                    return key;
                }

                @Override
                public OIdentifiable getValue() {
                    return identifiable;
                }

                @Override
                public OIdentifiable setValue(OIdentifiable value) {
                    throw new UnsupportedOperationException("setValue");
                }
            };
        }
    };
    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) OTransactionIndexEntry(com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry) OEmptyIterator(com.orientechnologies.orient.core.iterator.OEmptyIterator)

Example 5 with OTransactionIndexChanges

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

the class OIndexTxAwareMultiValue method get.

@Override
public Set<OIdentifiable> get(Object key) {
    final OTransactionIndexChanges indexChanges = database.getTransaction().getIndexChanges(delegate.getName());
    if (indexChanges == null)
        return super.get(key);
    key = getCollatingValue(key);
    final Set<OIdentifiable> result = new HashSet<OIdentifiable>();
    if (!indexChanges.cleared) {
        // BEGIN FROM THE UNDERLYING RESULT SET
        final Collection<OIdentifiable> subResult = super.get(key);
        if (subResult != null)
            for (OIdentifiable oid : subResult) result.add(oid);
    }
    final Set<OIdentifiable> processed = new HashSet<OIdentifiable>();
    for (OIdentifiable identifiable : result) {
        Map.Entry<Object, OIdentifiable> entry = calculateTxIndexEntry(key, identifiable, indexChanges);
        if (entry != null)
            processed.add(entry.getValue());
    }
    Set<OIdentifiable> txChanges = calculateTxValue(key, indexChanges);
    if (txChanges != null)
        processed.addAll(txChanges);
    if (!processed.isEmpty())
        return processed;
    return null;
}
Also used : OTransactionIndexChanges(com.orientechnologies.orient.core.tx.OTransactionIndexChanges) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

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