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);
}
}
}
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);
}
}
}
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();
}
};
}
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);
}
}
}
}
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());
}
}
Aggregations