@Test
public void testPrefixLookups() throws Exception {
// create some DBs
babu0.getDatabaseManager().createDatabase("prefixLookup", 1);
Database test0 = babu2.getDatabaseManager().getDatabase("prefixLookup");
Database test1 = babu0.getDatabaseManager().getDatabase("prefixLookup");
Database test2 = babu1.getDatabaseManager().getDatabase("prefixLookup");
// make some inserts on database 'test1'
final AtomicInteger notReady = new AtomicInteger(2);
final Object context0 = new Object();
DatabaseInsertGroup ig = test1.createInsertGroup();
ig.addInsert(0, "bla0".getBytes(), "blub0".getBytes());
ig.addInsert(0, "bla1".getBytes(), "blub1".getBytes());
ig.addInsert(0, "bla2".getBytes(), "blub2".getBytes());
test1.insert(ig, context0).registerListener(new DatabaseRequestListener<Object>() {
@Override
public void finished(Object result, Object context) {
if (notReady.decrementAndGet() == 0) {
synchronized (notReady) {
notReady.notify();
}
}
assertEquals(context0, context);
}
@Override
public void failed(BabuDBException error, Object context) {
if (notReady.decrementAndGet() == 0) {
synchronized (notReady) {
notReady.notify();
}
}
fail("This should not happen.");
}
});
final Object context1 = new Object();
ig = test1.createInsertGroup();
ig.addInsert(0, "yagga0".getBytes(), "yagga0".getBytes());
ig.addInsert(0, "yagga1".getBytes(), "yagga1".getBytes());
ig.addInsert(0, "yagga2".getBytes(), "yagga2".getBytes());
test1.insert(ig, context1).registerListener(new DatabaseRequestListener<Object>() {
@Override
public void finished(Object result, Object context) {
if (notReady.decrementAndGet() == 0) {
synchronized (notReady) {
notReady.notify();
}
}
assertEquals(context1, context);
}
@Override
public void failed(BabuDBException error, Object context) {
if (notReady.decrementAndGet() == 0) {
synchronized (notReady) {
notReady.notify();
}
}
fail("This should not happen.");
}
});
// wait for the inserts to finish
synchronized (notReady) {
if (notReady.get() > 0) {
notReady.wait();
}
}
/*
* prefix
*/
// perform a prefix lookup on database 'test0'
ResultSet<byte[], byte[]> result = test0.prefixLookup(0, "bla".getBytes(), null).get();
assertTrue(result.hasNext());
Entry<byte[], byte[]> next = result.next();
assertEquals("bla0", new String(next.getKey()));
assertEquals("blub0", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla1", new String(next.getKey()));
assertEquals("blub1", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
assertFalse(result.hasNext());
// perform a prefix lookup on database 'test1'
result = test1.prefixLookup(0, "bla".getBytes(), null).get();
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla0", new String(next.getKey()));
assertEquals("blub0", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla1", new String(next.getKey()));
assertEquals("blub1", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
assertFalse(result.hasNext());
// perform a prefix lookup on database 'test2'
result = test2.prefixLookup(0, "bla".getBytes(), null).get();
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla0", new String(next.getKey()));
assertEquals("blub0", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla1", new String(next.getKey()));
assertEquals("blub1", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
assertFalse(result.hasNext());
// perform an empty prefix lookup on database 'test0'
result = test0.prefixLookup(0, null, null).get();
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla0", new String(next.getKey()));
assertEquals("blub0", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla1", new String(next.getKey()));
assertEquals("blub1", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
next = result.next();
assertEquals("yagga0", new String(next.getKey()));
assertEquals("yagga0", new String(next.getValue()));
next = result.next();
assertEquals("yagga1", new String(next.getKey()));
assertEquals("yagga1", new String(next.getValue()));
next = result.next();
assertEquals("yagga2", new String(next.getKey()));
assertEquals("yagga2", new String(next.getValue()));
assertFalse(result.hasNext());
/*
* prefix reverse
*/
// perform a prefix lookup on database 'test0'
result = test0.reversePrefixLookup(0, "bla".getBytes(), null).get();
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla1", new String(next.getKey()));
assertEquals("blub1", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla0", new String(next.getKey()));
assertEquals("blub0", new String(next.getValue()));
assertFalse(result.hasNext());
// perform a prefix lookup on database 'test1'
result = test1.reversePrefixLookup(0, "bla".getBytes(), null).get();
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla1", new String(next.getKey()));
assertEquals("blub1", new String(next.getValue()));
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla0", new String(next.getKey()));
assertEquals("blub0", new String(next.getValue()));
assertFalse(result.hasNext());
// perform a prefix lookup on database 'test2'
result = test2.reversePrefixLookup(0, "bla".getBytes(), null).get();
assertTrue(result.hasNext());
next = result.next();
assertEquals("bla2", new String(next.getKey()));
assertEquals("blub2", new String(next.getValue()));
assertTrue(result.hasNext());