Search in sources :

Example 1 with KeyValueEntry

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry 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)

Example 2 with KeyValueEntry

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry in project titan by thinkaurelius.

the class BerkeleyJEKeyValueStore method getSlice.

@Override
public RecordIterator<KeyValueEntry> getSlice(StaticBuffer keyStart, StaticBuffer keyEnd, KeySelector selector, StoreTransaction txh) throws StorageException {
    log.trace("Get slice query");
    Transaction tx = getTransaction(txh);
    Cursor cursor = null;
    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, LockMode.DEFAULT);
        // 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, LockMode.DEFAULT);
        }
        log.trace("Retrieved: {}", 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 PermanentStorageException(e);
    } finally {
        try {
            if (cursor != null)
                cursor.close();
        } catch (Exception e) {
            throw new PermanentStorageException(e);
        }
    }
}
Also used : RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) ArrayList(java.util.ArrayList) StorageException(com.thinkaurelius.titan.diskstorage.StorageException) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) NoSuchElementException(java.util.NoSuchElementException) 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)

Example 3 with KeyValueEntry

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry in project titan by thinkaurelius.

the class PersistitKeyValueStore method getSlice.

/**
 * Runs all getSlice queries
 *
 * The keyStart & keyEnd are not guaranteed to exist
 * if keyStart is after keyEnd, an empty list is returned
 *
 * @param keyStart
 * @param keyEnd
 * @param selector
 * @param limit
 * @param txh
 * @return
 * @throws StorageException
 */
private RecordIterator<KeyValueEntry> getSlice(final StaticBuffer keyStart, final StaticBuffer keyEnd, final KeySelector selector, final Integer limit, StoreTransaction txh) throws StorageException {
    PersistitTransaction tx = (PersistitTransaction) txh;
    final List<KeyValueEntry> results = new ArrayList<KeyValueEntry>();
    synchronized (tx) {
        tx.assign();
        Exchange exchange = tx.getExchange(name);
        try {
            byte[] start = getArray(keyStart);
            byte[] end = getArray(keyEnd);
            // bail out if the start key comes after the end
            if (compare(start, end) > 0) {
                return KVUtil.EMPTY_ITERATOR;
            }
            KeyFilter.Term[] terms = { KeyFilter.rangeTerm(start, end, true, false, null) };
            KeyFilter keyFilter = new KeyFilter(terms);
            int i = 0;
            while (exchange.next(keyFilter)) {
                StaticBuffer k = getKey(exchange);
                // check the key against the selector, and that is has a corresponding value
                if (exchange.getValue().isDefined() && (selector == null || selector.include(k))) {
                    StaticBuffer v = getValue(exchange);
                    KeyValueEntry kv = new KeyValueEntry(k, v);
                    results.add(kv);
                    i++;
                    if (limit != null && limit >= 0 && i >= limit)
                        break;
                    if (selector != null && selector.reachedLimit())
                        break;
                }
            }
        } catch (PersistitException ex) {
            throw new PermanentStorageException(ex);
        } finally {
            tx.releaseExchange(exchange);
        }
    }
    // on heap for at least 2 collections.
    return new RecordIterator<KeyValueEntry>() {

        private final Iterator<KeyValueEntry> entries = results.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();
        }
    };
}
Also used : RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) PermanentStorageException(com.thinkaurelius.titan.diskstorage.PermanentStorageException) RecordIterator(com.thinkaurelius.titan.diskstorage.util.RecordIterator) PersistitException(com.persistit.exception.PersistitException) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) KeyValueEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)

Example 4 with KeyValueEntry

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry in project titan by thinkaurelius.

the class BerkeleyJEStoreManager method mutateMany.

@Override
public void mutateMany(Map<String, KVMutation> mutations, StoreTransaction txh) throws BackendException {
    for (Map.Entry<String, KVMutation> muts : mutations.entrySet()) {
        BerkeleyJEKeyValueStore store = openDatabase(muts.getKey());
        KVMutation mut = muts.getValue();
        if (!mut.hasAdditions() && !mut.hasDeletions()) {
            log.debug("Empty mutation set for {}, doing nothing", muts.getKey());
        } else {
            log.debug("Mutating {}", muts.getKey());
        }
        if (mut.hasAdditions()) {
            for (KeyValueEntry entry : mut.getAdditions()) {
                store.insert(entry.getKey(), entry.getValue(), txh);
                log.trace("Insertion on {}: {}", muts.getKey(), entry);
            }
        }
        if (mut.hasDeletions()) {
            for (StaticBuffer del : mut.getDeletions()) {
                store.delete(del, txh);
                log.trace("Deletion on {}: {}", muts.getKey(), del);
            }
        }
    }
}
Also used : KVMutation(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KVMutation) HashMap(java.util.HashMap) Map(java.util.Map) KeyValueEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)

Example 5 with KeyValueEntry

use of com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry in project titan by thinkaurelius.

the class KeyValueStoreTest method checkSlice.

public void checkSlice(String[] values, Set<Integer> removed, int start, int end, int limit) throws StorageException {
    List<KeyValueEntry> entries;
    if (limit <= 0)
        entries = KVUtil.getSlice(store, KeyValueStoreUtil.getBuffer(start), KeyValueStoreUtil.getBuffer(end), tx);
    else
        entries = KVUtil.getSlice(store, KeyValueStoreUtil.getBuffer(start), KeyValueStoreUtil.getBuffer(end), limit, tx);
    int pos = 0;
    for (int i = start; i < end; i++) {
        if (removed.contains(i))
            continue;
        if (pos < limit) {
            KeyValueEntry entry = entries.get(pos);
            int id = KeyValueStoreUtil.getID(entry.getKey());
            String str = KeyValueStoreUtil.getString(entry.getValue());
            Assert.assertEquals(i, id);
            Assert.assertEquals(values[i], str);
        }
        pos++;
    }
    if (limit > 0 && pos >= limit)
        Assert.assertEquals(limit, entries.size());
    else {
        Assert.assertNotNull(entries);
        Assert.assertEquals(pos, entries.size());
    }
}
Also used : KeyValueEntry(com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)

Aggregations

KeyValueEntry (com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeyValueEntry)5 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)3 RecordIterator (com.thinkaurelius.titan.diskstorage.util.RecordIterator)3 PermanentStorageException (com.thinkaurelius.titan.diskstorage.PermanentStorageException)2 StoreTransaction (com.thinkaurelius.titan.diskstorage.keycolumnvalue.StoreTransaction)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 PersistitException (com.persistit.exception.PersistitException)1 BackendException (com.thinkaurelius.titan.diskstorage.BackendException)1 PermanentBackendException (com.thinkaurelius.titan.diskstorage.PermanentBackendException)1 StorageException (com.thinkaurelius.titan.diskstorage.StorageException)1 KVMutation (com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KVMutation)1 KeySelector (com.thinkaurelius.titan.diskstorage.keycolumnvalue.keyvalue.KeySelector)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1