Search in sources :

Example 16 with Session

use of com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session in project incubator-hugegraph by apache.

the class RocksDBSessionTest method testScanByRange.

@Test
public void testScanByRange() throws RocksDBException {
    put("person:1gname", "James");
    put("person:1gage", "19");
    put("person:1gcity", "Beijing");
    put("person:2gname", "Lisa");
    put("person:2gage", "20");
    put("person:2gcity", "Beijing");
    put("person:3gname", "Hebe");
    put("person:3gage", "21");
    put("person:3gcity", "Taipei");
    Map<String, String> results = new HashMap<>();
    Session session = this.rocks.session();
    Iterator<BackendColumn> iter = session.scan(TABLE, getBytes("person:1"), getBytes("person:3"));
    while (iter.hasNext()) {
        BackendColumn col = iter.next();
        results.put(getString(col.name), getString(col.value));
    }
    Assert.assertEquals(6, results.size());
    Assert.assertEquals("James", results.get("person:1gname"));
    Assert.assertEquals("19", results.get("person:1gage"));
    Assert.assertEquals("Beijing", results.get("person:1gcity"));
    Assert.assertEquals("Lisa", results.get("person:2gname"));
    Assert.assertEquals("20", results.get("person:2gage"));
    Assert.assertEquals("Beijing", results.get("person:2gcity"));
    Assert.assertEquals("Hebe", get("person:3gname"));
}
Also used : BackendColumn(com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn) HashMap(java.util.HashMap) Session(com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 17 with Session

use of com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session in project incubator-hugegraph by apache.

the class RocksDBSessionTest method testScanByRangeWithSignedBytes.

@Test
public void testScanByRangeWithSignedBytes() throws RocksDBException {
    Session session = this.rocks.session();
    byte[] key11 = new byte[] { 1, 1 };
    byte[] value11 = getBytes("value-1-1");
    session.put(TABLE, key11, value11);
    byte[] key12 = new byte[] { 1, 2 };
    byte[] value12 = getBytes("value-1-2");
    session.put(TABLE, key12, value12);
    byte[] key13 = new byte[] { 1, -3 };
    byte[] value13 = getBytes("value-1-3");
    session.put(TABLE, key13, value13);
    byte[] key21 = new byte[] { 2, 1 };
    byte[] value21 = getBytes("value-2-1");
    session.put(TABLE, key21, value21);
    this.commit();
    Iterator<BackendColumn> iter;
    iter = session.scan(TABLE, new byte[] { 1, -1 }, new byte[] { 1, 3 });
    Assert.assertFalse(iter.hasNext());
    iter = session.scan(TABLE, new byte[] { 1, 1 }, new byte[] { 1, -1 });
    Map<ByteBuffer, byte[]> results = new HashMap<>();
    while (iter.hasNext()) {
        BackendColumn col = iter.next();
        results.put(ByteBuffer.wrap(col.name), col.value);
    }
    Assert.assertEquals(3, results.size());
    Assert.assertArrayEquals(value11, results.get(ByteBuffer.wrap(key11)));
    Assert.assertArrayEquals(value12, results.get(ByteBuffer.wrap(key12)));
    Assert.assertArrayEquals(value13, results.get(ByteBuffer.wrap(key13)));
    Assert.assertArrayEquals(value21, session.get(TABLE, key21));
}
Also used : BackendColumn(com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Session(com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 18 with Session

use of com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session in project incubator-hugegraph by apache.

the class RocksDBSessionTest method testDeleteByRangeWithBytes.

@Test
public void testDeleteByRangeWithBytes() throws RocksDBException {
    Session session = this.rocks.session();
    byte[] key11 = new byte[] { 1, 1 };
    byte[] value11 = getBytes("value-1-1");
    session.put(TABLE, key11, value11);
    byte[] key12 = new byte[] { 1, 2 };
    byte[] value12 = getBytes("value-1-2");
    session.put(TABLE, key12, value12);
    byte[] key21 = new byte[] { 2, 1 };
    byte[] value21 = getBytes("value-2-1");
    session.put(TABLE, key21, value21);
    session.deleteRange(TABLE, key11, new byte[] { 1, 3 });
    this.commit();
    Assert.assertArrayEquals(null, session.get(TABLE, key11));
    Assert.assertArrayEquals(null, session.get(TABLE, key12));
    Assert.assertArrayEquals(value21, session.get(TABLE, key21));
}
Also used : Session(com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 19 with Session

use of com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session in project incubator-hugegraph by apache.

the class RocksDBStore method query.

@Override
public Iterator<BackendEntry> query(Query query) {
    Lock readLock = this.storeLock.readLock();
    readLock.lock();
    try {
        this.checkOpened();
        HugeType tableType = RocksDBTable.tableType(query);
        RocksDBTable table;
        RocksDBSessions.Session session;
        if (query.olap()) {
            table = this.table(this.olapTableName(tableType));
            session = this.session(HugeType.OLAP);
        } else {
            table = this.table(tableType);
            session = this.session(tableType);
        }
        Iterator<BackendEntry> entries = table.query(session, query);
        // Merge olap results as needed
        Set<Id> olapPks = query.olapPks();
        if (this.isGraphStore && !olapPks.isEmpty()) {
            List<Iterator<BackendEntry>> iterators = new ArrayList<>();
            for (Id pk : olapPks) {
                Query q = query.copy();
                table = this.table(this.olapTableName(pk));
                iterators.add(table.query(this.session(HugeType.OLAP), q));
            }
            entries = new MergeIterator<>(entries, iterators, BackendEntry::mergeable);
        }
        return entries;
    } finally {
        readLock.unlock();
    }
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Query(com.baidu.hugegraph.backend.query.Query) ArrayList(java.util.ArrayList) HugeType(com.baidu.hugegraph.type.HugeType) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) MergeIterator(com.baidu.hugegraph.backend.serializer.MergeIterator) Iterator(java.util.Iterator) Session(com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session) Id(com.baidu.hugegraph.backend.id.Id)

Example 20 with Session

use of com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session in project incubator-hugegraph by apache.

the class RocksDBCountersTest method testCounterWithMutiThreads.

@Test
public void testCounterWithMutiThreads() {
    final int TIMES = 1000;
    AtomicLong times = new AtomicLong(0);
    Map<Id, Boolean> ids = new ConcurrentHashMap<>();
    runWithThreads(THREADS_NUM, () -> {
        Session session = this.rocks.session();
        for (int i = 0; i < TIMES; i++) {
            Id id = nextId(session, HugeType.PROPERTY_KEY);
            Assert.assertFalse(ids.containsKey(id));
            ids.put(id, true);
            times.incrementAndGet();
        }
        this.rocks.close();
    });
    Assert.assertEquals(THREADS_NUM * TIMES, times.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Id(com.baidu.hugegraph.backend.id.Id) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Session(com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session) Test(org.junit.Test)

Aggregations

Session (com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session)26 Test (org.junit.Test)21 BackendColumn (com.baidu.hugegraph.backend.store.BackendEntry.BackendColumn)9 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)8 HashMap (java.util.HashMap)6 Lock (java.util.concurrent.locks.Lock)5 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)5 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)5 Id (com.baidu.hugegraph.backend.id.Id)2 HugeType (com.baidu.hugegraph.type.HugeType)2 ByteBuffer (java.nio.ByteBuffer)2 Query (com.baidu.hugegraph.backend.query.Query)1 MergeIterator (com.baidu.hugegraph.backend.serializer.MergeIterator)1 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)1 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)1 BackendColumnIterator (com.baidu.hugegraph.backend.store.BackendEntry.BackendColumnIterator)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Random (java.util.Random)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1