Permalink
Browse files

Add support for BDB to read fast as well

  • Loading branch information...
rsumbaly committed Sep 15, 2012
1 parent 799b25f commit be70d7a23cc8523d969360c7dfe556fddca17479
@@ -262,8 +262,56 @@ private Database getBdbDatabase() {
}
public Map<ByteArray, Boolean> hasKeys(Iterable<ByteArray> keys, boolean exact) {
+
+ long startTimeNs = -1;
+
+ if(logger.isTraceEnabled())
+ startTimeNs = System.nanoTime();
+
StoreUtils.assertValidKeys(keys);
- return StoreUtils.hasKeys(this, keys);
+ Map<ByteArray, Boolean> result = StoreUtils.newEmptyHashMap(keys);
+ Cursor cursor = null;
+
+ String keyStr = "";
+
+ try {
+ cursor = getBdbDatabase().openCursor(null, null);
+ for(ByteArray key: keys) {
+
+ if(logger.isTraceEnabled())
+ keyStr += ByteUtils.toHexString(key.get()) + " ";
+
+ DatabaseEntry keyEntry = new DatabaseEntry(key.get());
+ DatabaseEntry valueEntry = new DatabaseEntry();
+ valueEntry.setPartial(0, 0, true);
+
+ OperationStatus status = cursor.getSearchKey(keyEntry,
+ valueEntry,
+ LockMode.READ_UNCOMMITTED);
+
+ if(status == OperationStatus.SUCCESS) {
+ result.put(key, true);
+ }
+
+ if(logger.isTraceEnabled()) {
+ logger.trace("Completed HASKEY from key " + ByteUtils.toHexString(key.get())
+ + " in " + (System.nanoTime() - startTimeNs) + " ns at "
+ + System.currentTimeMillis());
+ }
+ }
+ } catch(DatabaseException e) {
+ logger.error(e);
+ throw new PersistenceFailureException(e);
+ } finally {
+ attemptClose(cursor);
+ }
+
+ if(logger.isTraceEnabled())
+ logger.trace("Completed HASKEYS from keys " + keyStr + " in "
+ + (System.nanoTime() - startTimeNs) + " ns at "
+ + System.currentTimeMillis());
+
+ return result;
}
public Map<ByteArray, List<Versioned<byte[]>>> getAll(Iterable<ByteArray> keys,
@@ -19,6 +19,7 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
+import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
@@ -40,6 +41,7 @@
import voldemort.versioning.VectorClock;
import voldemort.versioning.Versioned;
+import com.google.common.collect.Lists;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.Durability;
@@ -103,9 +105,16 @@ public void testPersistence() throws Exception {
this.environment = new Environment(this.tempDir, envConfig);
this.database = environment.openDatabase(null, "test", databaseConfig);
this.store = new BdbStorageEngine("test", this.environment, this.database, runtimeConfig);
- List<Versioned<byte[]>> vals = store.get(new ByteArray("abc".getBytes()), null);
+ ByteArray key = new ByteArray("abc".getBytes());
+ ByteArray nonAvailableKey = new ByteArray("cde".getBytes());
+ List<Versioned<byte[]>> vals = store.get(key, null);
assertEquals(1, vals.size());
TestUtils.bytesEqual("cdef".getBytes(), vals.get(0).getValue());
+ Map<ByteArray, Boolean> returnedHasKeys = store.hasKeys(Lists.newArrayList(key,
+ nonAvailableKey),
+ true);
+ assertTrue(returnedHasKeys.get(key));
+ assertTrue(!returnedHasKeys.containsKey(nonAvailableKey));
}
public void testEquals() {

0 comments on commit be70d7a

Please sign in to comment.