use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class KCVSConfiguration method get.
/**
* Reads the configuration property for this StoreManager
*
* @param key Key identifying the configuration property
* @return Value stored for the key or null if the configuration property has not (yet) been defined.
* @throws org.janusgraph.diskstorage.BackendException
*/
@Override
public <O> O get(final String key, final Class<O> dataType) {
StaticBuffer column = string2StaticBuffer(key);
final KeySliceQuery query = new KeySliceQuery(rowKey, column, BufferUtil.nextBiggerBuffer(column));
StaticBuffer result = BackendOperation.execute(new BackendOperation.Transactional<StaticBuffer>() {
@Override
public StaticBuffer call(StoreTransaction txh) throws BackendException {
List<Entry> entries = store.getSlice(query, txh);
if (entries.isEmpty())
return null;
return entries.get(0).getValueAs(StaticBuffer.STATIC_FACTORY);
}
@Override
public String toString() {
return "getConfiguration";
}
}, txProvider, times, maxOperationWaitTime);
if (result == null)
return null;
return staticBuffer2Object(result, dataType);
}
use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class CQLResultSetKeyIteratorTest method testIterator.
@Test
public void testIterator() throws IOException {
final Array<Row> rows = Array.rangeClosed(1, 100).map(idx -> {
final Row row = mock(Row.class);
when(row.getBytes("key")).thenReturn(ByteBuffer.wrap(Integer.toString(idx / 5).getBytes()));
when(row.getBytes("column1")).thenReturn(ByteBuffer.wrap(Integer.toString(idx % 5).getBytes()));
when(row.getBytes("value")).thenReturn(ByteBuffer.wrap(Integer.toString(idx).getBytes()));
return row;
});
final ResultSet resultSet = mock(ResultSet.class);
when(resultSet.iterator()).thenReturn(rows.iterator());
final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
int i = 0;
while (resultSetKeyIterator.hasNext()) {
final StaticBuffer next = resultSetKeyIterator.next();
final RecordIterator<Entry> entries = resultSetKeyIterator.getEntries();
while (entries.hasNext()) {
final Row row = rows.get(i++);
final Entry entry = entries.next();
assertEquals(row.getBytes("key"), next.asByteBuffer());
assertEquals(row.getBytes("column1"), entry.getColumn().asByteBuffer());
assertEquals(row.getBytes("value"), entry.getValue().asByteBuffer());
}
}
}
}
use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class CQLResultSetKeyIteratorTest method testPartialIterateColumns.
@Test
public void testPartialIterateColumns() throws IOException {
final Random random = new Random();
final Function1<Integer, ByteBuffer> randomLong = idx -> {
final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).putLong(random.nextLong());
buffer.flip();
return buffer;
};
final Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap = Array.range(0, random.nextInt(100) + 100).map(randomLong).map(key -> Tuple.of(key, Array.rangeClosed(0, random.nextInt(100) + 1).map(idx -> Tuple.of(randomLong.apply(idx), randomLong.apply(idx)))));
final Seq<Row> rows = keysMap.flatMap(tuple -> tuple._2.map(columnAndValue -> {
final Row row = mock(Row.class);
when(row.getBytes("key")).thenReturn(tuple._1);
when(row.getBytes("column1")).thenReturn(columnAndValue._1);
when(row.getBytes("value")).thenReturn(columnAndValue._2);
return row;
}));
final ResultSet resultSet = mock(ResultSet.class);
when(resultSet.iterator()).thenReturn(rows.iterator());
final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
final Iterator<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> iterator = keysMap.iterator();
while (resultSetKeyIterator.hasNext()) {
final StaticBuffer next = resultSetKeyIterator.next();
try (final RecordIterator<Entry> entries = resultSetKeyIterator.getEntries()) {
final Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>> current = iterator.next();
final ByteBuffer currentKey = current._1;
final Array<Tuple2<ByteBuffer, ByteBuffer>> columnValues = current._2;
final Iterator<Tuple2<ByteBuffer, ByteBuffer>> columnIterator = columnValues.iterator();
while (entries.hasNext()) {
final Entry entry = entries.next();
final Tuple2<ByteBuffer, ByteBuffer> columnAndValue = columnIterator.next();
assertEquals(currentKey, next.asByteBuffer());
assertEquals(columnAndValue._1, entry.getColumn().asByteBuffer());
assertEquals(columnAndValue._2, entry.getValue().asByteBuffer());
assertEquals(columnIterator.hasNext(), entries.hasNext());
// 10% of the time, don't complete the iteration
if (random.nextInt(10) == 0) {
break;
}
}
}
}
}
}
use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class StandardLockCleanerRunnable method runWithExceptions.
private void runWithExceptions() throws BackendException {
StaticBuffer lockKey = serializer.toLockKey(target.getKey(), target.getColumn());
// TODO reduce LOCK_COL_END based on cutoff
List<Entry> locks = store.getSlice(new KeySliceQuery(lockKey, LOCK_COL_START, LOCK_COL_END), tx);
ImmutableList.Builder<StaticBuffer> b = ImmutableList.builder();
for (Entry lc : locks) {
TimestampRid tr = serializer.fromLockColumn(lc.getColumn(), times);
if (tr.getTimestamp().isBefore(cutoff)) {
log.info("Deleting expired lock on {} by rid {} with timestamp {} (before or at cutoff {})", target, tr.getRid(), tr.getTimestamp(), cutoff);
b.add(lc.getColumn());
} else {
log.debug("Ignoring lock on {} by rid {} with timestamp {} (timestamp is after cutoff {})", target, tr.getRid(), tr.getTimestamp(), cutoff);
}
}
List<StaticBuffer> deletions = b.build();
if (!deletions.isEmpty()) {
store.mutate(lockKey, ImmutableList.of(), deletions, tx);
log.info("Deleted {} expired locks (before or at cutoff {})", deletions.size(), cutoff);
}
}
use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class HashingUtil method hashPrefixKey.
public static StaticBuffer hashPrefixKey(final HashLength hashPrefixLen, final StaticBuffer key) {
final int prefixLen = hashPrefixLen.length();
final StaticBuffer.Factory<HashCode> hashFactory;
switch(hashPrefixLen) {
case SHORT:
hashFactory = SHORT_HASH_FACTORY;
break;
case LONG:
hashFactory = LONG_HASH_FACTORY;
break;
default:
throw new IllegalArgumentException("Unknown hash prefix: " + hashPrefixLen);
}
HashCode hashcode = key.as(hashFactory);
WriteByteBuffer newKey = new WriteByteBuffer(prefixLen + key.length());
assert prefixLen == 4 || prefixLen == 8;
if (prefixLen == 4)
newKey.putInt(hashcode.asInt());
else
newKey.putLong(hashcode.asLong());
newKey.putBytes(key);
return newKey.getStaticBuffer();
}
Aggregations