Search in sources :

Example 1 with RecordIterator

use of com.thinkaurelius.titan.diskstorage.util.RecordIterator in project titan by thinkaurelius.

the class VertexIterable method iterator.

@Override
public Iterator<InternalVertex> iterator() {
    return new Iterator<InternalVertex>() {

        RecordIterator<Long> iterator = graph.getVertexIDs(tx.getTxHandle());

        InternalVertex nextVertex = nextVertex();

        private InternalVertex nextVertex() {
            InternalVertex v = null;
            while (v == null && iterator.hasNext()) {
                long nextId = iterator.next().longValue();
                //Filter out invisible vertices
                if (IDManager.VertexIDType.Invisible.is(nextId))
                    continue;
                v = tx.getInternalVertex(nextId);
                //Filter out deleted vertices and types
                if (v.isRemoved())
                    v = null;
            }
            return v;
        }

        @Override
        public boolean hasNext() {
            return nextVertex != null;
        }

        @Override
        public InternalVertex next() {
            if (!hasNext())
                throw new NoSuchElementException();
            InternalVertex returnVertex = nextVertex;
            nextVertex = nextVertex();
            return returnVertex;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) Iterator(java.util.Iterator) InternalVertex(com.thinkaurelius.titan.graphdb.internal.InternalVertex) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with RecordIterator

use of com.thinkaurelius.titan.diskstorage.util.RecordIterator in project titan by thinkaurelius.

the class BerkeleyJEKeyValueStore method getSlice.

@Override
public RecordIterator<KeyValueEntry> getSlice(KVQuery query, StoreTransaction txh) throws BackendException {
    log.trace("beginning db={}, op=getSlice, tx={}", name, txh);
    Transaction tx = getTransaction(txh);
    Cursor cursor = null;
    final StaticBuffer keyStart = query.getStart();
    final StaticBuffer keyEnd = query.getEnd();
    final KeySelector selector = query.getKeySelector();
    final List<KeyValueEntry> result = new ArrayList<KeyValueEntry>();
    try {
        DatabaseEntry foundKey = keyStart.as(ENTRY_FACTORY);
        DatabaseEntry foundData = new DatabaseEntry();
        cursor = db.openCursor(tx, null);
        OperationStatus status = cursor.getSearchKeyRange(foundKey, foundData, getLockMode(txh));
        //Iterate until given condition is satisfied or end of records
        while (status == OperationStatus.SUCCESS) {
            StaticBuffer key = getBuffer(foundKey);
            if (key.compareTo(keyEnd) >= 0)
                break;
            if (selector.include(key)) {
                result.add(new KeyValueEntry(key, getBuffer(foundData)));
            }
            if (selector.reachedLimit())
                break;
            status = cursor.getNext(foundKey, foundData, getLockMode(txh));
        }
        log.trace("db={}, op=getSlice, tx={}, resultcount={}", name, txh, result.size());
        return new RecordIterator<KeyValueEntry>() {

            private final Iterator<KeyValueEntry> entries = result.iterator();

            @Override
            public boolean hasNext() {
                return entries.hasNext();
            }

            @Override
            public KeyValueEntry next() {
                return entries.next();
            }

            @Override
            public void close() {
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    } catch (Exception e) {
        throw new PermanentBackendException(e);
    } finally {
        try {
            if (cursor != null)
                cursor.close();
        } catch (Exception e) {
            throw new PermanentBackendException(e);
        }
    }
}
Also used : RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) ArrayList(java.util.ArrayList) KeySelector(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeySelector) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) PermanentBackendException(com.thinkaurelius.titan.diskstorage.PermanentBackendException) StoreTransaction(com.thinkaurelius.titan.diskstorage.keycolumnvalue.StoreTransaction) RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) Iterator(java.util.Iterator) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KeyValueEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)

Aggregations

RecordIterator (com.thinkaurelius.titan.diskstorage.util.RecordIterator)2 Iterator (java.util.Iterator)2 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)1 PermanentBackendException (com.thinkaurelius.titan.diskstorage.PermanentBackendException)1 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)1 StoreTransaction (com.thinkaurelius.titan.diskstorage.keycolumnvalue.StoreTransaction)1 KeySelector (com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeySelector)1 KeyValueEntry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)1 InternalVertex (com.thinkaurelius.titan.graphdb.internal.InternalVertex)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1