Search in sources :

Example 1 with OEmptyIterator

use of com.orientechnologies.orient.core.iterator.OEmptyIterator 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 2 with OEmptyIterator

use of com.orientechnologies.orient.core.iterator.OEmptyIterator in project orientdb by orientechnologies.

the class OHashTableIndexEngine method cursor.

@Override
public OIndexCursor cursor(final ValuesTransformer valuesTransformer) {
    return new OIndexAbstractCursor() {

        private int nextEntriesIndex;

        private OHashIndexBucket.Entry<Object, Object>[] entries;

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

        private Object currentKey;

        {
            OHashIndexBucket.Entry<Object, Object> firstEntry = hashTable.firstEntry();
            if (firstEntry == null)
                entries = OCommonConst.EMPTY_BUCKET_ENTRY_ARRAY;
            else
                entries = hashTable.ceilingEntries(firstEntry.key);
            if (entries.length == 0)
                currentIterator = null;
        }

        @Override
        public Map.Entry<Object, OIdentifiable> nextEntry() {
            if (currentIterator == null)
                return null;
            if (currentIterator.hasNext())
                return nextCursorValue();
            while (currentIterator != null && !currentIterator.hasNext()) {
                if (entries.length == 0) {
                    currentIterator = null;
                    return null;
                }
                final OHashIndexBucket.Entry<Object, Object> bucketEntry = entries[nextEntriesIndex];
                currentKey = bucketEntry.key;
                Object value = bucketEntry.value;
                if (valuesTransformer != null)
                    currentIterator = valuesTransformer.transformFromValue(value).iterator();
                else
                    currentIterator = Collections.singletonList((OIdentifiable) value).iterator();
                nextEntriesIndex++;
                if (nextEntriesIndex >= entries.length) {
                    entries = hashTable.higherEntries(entries[entries.length - 1].key);
                    nextEntriesIndex = 0;
                }
            }
            if (currentIterator != null && !currentIterator.hasNext())
                return nextCursorValue();
            currentIterator = null;
            return null;
        }

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

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

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

                @Override
                public OIdentifiable setValue(OIdentifiable value) {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : Iterator(java.util.Iterator) OEmptyIterator(com.orientechnologies.orient.core.iterator.OEmptyIterator) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Map(java.util.Map)

Example 3 with OEmptyIterator

use of com.orientechnologies.orient.core.iterator.OEmptyIterator in project orientdb by orientechnologies.

the class OHashTableIndexEngine method descCursor.

@Override
public OIndexCursor descCursor(final ValuesTransformer valuesTransformer) {
    return new OIndexAbstractCursor() {

        private int nextEntriesIndex;

        private OHashIndexBucket.Entry<Object, Object>[] entries;

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

        private Object currentKey;

        {
            OHashIndexBucket.Entry<Object, Object> lastEntry = hashTable.lastEntry();
            if (lastEntry == null)
                entries = OCommonConst.EMPTY_BUCKET_ENTRY_ARRAY;
            else
                entries = hashTable.floorEntries(lastEntry.key);
            if (entries.length == 0)
                currentIterator = null;
        }

        @Override
        public Map.Entry<Object, OIdentifiable> nextEntry() {
            if (currentIterator == null)
                return null;
            if (currentIterator.hasNext())
                return nextCursorValue();
            while (currentIterator != null && !currentIterator.hasNext()) {
                if (entries.length == 0) {
                    currentIterator = null;
                    return null;
                }
                final OHashIndexBucket.Entry<Object, Object> bucketEntry = entries[nextEntriesIndex];
                currentKey = bucketEntry.key;
                Object value = bucketEntry.value;
                if (valuesTransformer != null) {
                    currentIterator = valuesTransformer.transformFromValue(value).iterator();
                } else
                    currentIterator = Collections.singletonList((OIdentifiable) value).iterator();
                nextEntriesIndex--;
                if (nextEntriesIndex < 0) {
                    entries = hashTable.lowerEntries(entries[0].key);
                    nextEntriesIndex = entries.length - 1;
                }
            }
            if (currentIterator != null && !currentIterator.hasNext())
                return nextCursorValue();
            currentIterator = null;
            return null;
        }

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

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

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

                @Override
                public OIdentifiable setValue(OIdentifiable value) {
                    throw new UnsupportedOperationException();
                }
            };
        }
    };
}
Also used : Iterator(java.util.Iterator) OEmptyIterator(com.orientechnologies.orient.core.iterator.OEmptyIterator) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Map(java.util.Map)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 OEmptyIterator (com.orientechnologies.orient.core.iterator.OEmptyIterator)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 OTransactionIndexChanges (com.orientechnologies.orient.core.tx.OTransactionIndexChanges)1 OTransactionIndexEntry (com.orientechnologies.orient.core.tx.OTransactionIndexChangesPerKey.OTransactionIndexEntry)1