Search in sources :

Example 16 with StaticBuffer

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);
}
Also used : StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) ArrayList(java.util.ArrayList) List(java.util.List) BackendOperation(org.janusgraph.diskstorage.util.BackendOperation) BackendException(org.janusgraph.diskstorage.BackendException)

Example 17 with StaticBuffer

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());
            }
        }
    }
}
Also used : Entry(org.janusgraph.diskstorage.Entry) ResultSet(com.datastax.driver.core.ResultSet) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 18 with StaticBuffer

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;
                    }
                }
            }
        }
    }
}
Also used : Tuple(io.vavr.Tuple) EntryMetaData(org.janusgraph.diskstorage.EntryMetaData) Row(com.datastax.driver.core.Row) SliceQuery(org.janusgraph.diskstorage.keycolumnvalue.SliceQuery) Array(io.vavr.collection.Array) IOException(java.io.IOException) Random(java.util.Random) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function1(io.vavr.Function1) ByteBuffer(java.nio.ByteBuffer) RecordIterator(org.janusgraph.diskstorage.util.RecordIterator) ResultSet(com.datastax.driver.core.ResultSet) Tuple2(io.vavr.Tuple2) Assert.assertFalse(org.junit.Assert.assertFalse) Entry(org.janusgraph.diskstorage.Entry) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) BufferUtil(org.janusgraph.diskstorage.util.BufferUtil) Iterator(io.vavr.collection.Iterator) Seq(io.vavr.collection.Seq) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) ByteBuffer(java.nio.ByteBuffer) Array(io.vavr.collection.Array) Entry(org.janusgraph.diskstorage.Entry) Random(java.util.Random) Tuple2(io.vavr.Tuple2) ResultSet(com.datastax.driver.core.ResultSet) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 19 with StaticBuffer

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);
    }
}
Also used : Entry(org.janusgraph.diskstorage.Entry) ImmutableList(com.google.common.collect.ImmutableList) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) KeySliceQuery(org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery)

Example 20 with StaticBuffer

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();
}
Also used : HashCode(com.google.common.hash.HashCode) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer)

Aggregations

StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)45 Test (org.junit.Test)17 Entry (org.janusgraph.diskstorage.Entry)14 ArrayList (java.util.ArrayList)10 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)8 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)8 SliceQuery (org.janusgraph.diskstorage.keycolumnvalue.SliceQuery)7 ByteBuffer (java.nio.ByteBuffer)6 List (java.util.List)6 Map (java.util.Map)6 ResultSet (com.datastax.driver.core.ResultSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 BackendException (org.janusgraph.diskstorage.BackendException)5 Row (com.datastax.driver.core.Row)4 Tuple (io.vavr.Tuple)4 Array (io.vavr.collection.Array)4 IOException (java.io.IOException)4 Instant (java.time.Instant)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4