Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Read only the keys. Don't see any value add here, but well...

  • Loading branch information...
commit 7f06caeafbc12a3e0e7279a2f0419b090546b6b6 1 parent 83fcac4
@rsumbaly authored
View
3  src/java/voldemort/store/StoreUtils.java
@@ -76,6 +76,9 @@ public static void assertValidKeys(Iterable<?> keys) {
public static <K, V, T> Map<K, Boolean> hasKeys(Store<K, V, T> storageEngine, Iterable<K> keys) {
Map<K, List<Versioned<V>>> result = storageEngine.getAll(keys, null);
Map<K, Boolean> returnResult = new HashMap<K, Boolean>();
+ for(K key: keys) {
+ returnResult.put(key, false);
+ }
for(Map.Entry<K, List<Versioned<V>>> entry: result.entrySet()) {
if(entry.getValue() != null && entry.getValue().size() > 0)
returnResult.put(entry.getKey(), true);
View
2  src/java/voldemort/store/bdb/BdbStorageEngine.java
@@ -291,6 +291,8 @@ private Database getBdbDatabase() {
if(status == OperationStatus.SUCCESS) {
result.put(key, true);
+ } else {
+ result.put(key, false);
}
if(logger.isTraceEnabled()) {
View
9 src/java/voldemort/store/readonly/ReadOnlyStorageEngine.java
@@ -525,11 +525,10 @@ public void truncate() {
Collections.sort(keysAndValueLocations);
for(KeyValueLocation keyVal: keysAndValueLocations) {
- byte[] value = fileSet.readValue(keyVal.getKey().get(),
- keyVal.getChunk(),
- keyVal.getValueLocation());
- if(value.length > 0)
- results.put(keyVal.getKey(), true);
+ results.put(keyVal.getKey(),
+ fileSet.keyExists(keyVal.getKey().get(),
+ keyVal.getChunk(),
+ keyVal.getValueLocation()));
}
}
return results;
View
63 src/java/voldemort/store/readonly/chunk/ChunkedFileSet.java
@@ -463,6 +463,69 @@ public int getChunkForKey(byte[] key) {
}
+ public boolean keyExists(byte[] key, int chunk, int valueLocation) {
+ FileChannel dataFile = dataFileFor(chunk);
+ try {
+ switch(storageFormat) {
+ case READONLY_V0:
+ case READONLY_V1: {
+ return true;
+ }
+ case READONLY_V2: {
+
+ // Buffer for 'numKeyValues', 'keySize' and 'valueSize'
+ int headerSize = ByteUtils.SIZE_OF_SHORT + (2 * ByteUtils.SIZE_OF_INT);
+ ByteBuffer sizeBuffer = ByteBuffer.allocate(headerSize);
+ dataFile.read(sizeBuffer, valueLocation);
+ valueLocation += headerSize;
+
+ // Read the number of key-values
+ short numKeyValues = sizeBuffer.getShort(0);
+
+ // Read the key size
+ int keySize = sizeBuffer.getInt(ByteUtils.SIZE_OF_SHORT);
+
+ // Read the value size
+ int valueSize = sizeBuffer.getInt(ByteUtils.SIZE_OF_SHORT
+ + ByteUtils.SIZE_OF_INT);
+
+ do {
+ if(keySize == -1 && valueSize == -1) {
+ sizeBuffer.clear();
+ // Reads an extra short, but that is fine since
+ // collisions are rare. Also we save the unnecessary
+ // overhead of allocating a new byte-buffer
+ dataFile.read(sizeBuffer, valueLocation);
+ keySize = sizeBuffer.getInt(0);
+ valueSize = sizeBuffer.getInt(ByteUtils.SIZE_OF_INT);
+ valueLocation += (2 * ByteUtils.SIZE_OF_INT);
+ }
+
+ // Read key
+ ByteBuffer buffer = ByteBuffer.allocate(keySize);
+ dataFile.read(buffer, valueLocation);
+
+ // Compare key
+ if(ByteUtils.compare(key, buffer.array(), 0, keySize) == 0) {
+ return true;
+ }
+ valueLocation += (keySize + valueSize);
+ keySize = valueSize = -1;
+
+ } while(--numKeyValues > 0);
+ // Could not find key, return value of no size
+ return false;
+ }
+
+ default: {
+ throw new VoldemortException("Storage format not supported ");
+ }
+ }
+ } catch(IOException e) {
+ throw new VoldemortException(e);
+ }
+ }
+
public byte[] readValue(byte[] key, int chunk, int valueLocation) {
FileChannel dataFile = dataFileFor(chunk);
try {
View
28 test/unit/voldemort/store/AbstractStorageEngineTest.java
@@ -16,6 +16,7 @@
package voldemort.store;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -39,6 +40,33 @@
public abstract StorageEngine<ByteArray, byte[], byte[]> getStorageEngine();
+ public void testHasKeys() {
+ StorageEngine<ByteArray, byte[], byte[]> store = getStorageEngine();
+ StorageEngine<String, String, String> stringStore = new SerializingStorageEngine<String, String, String>(store,
+ new StringSerializer(),
+ new StringSerializer(),
+ new StringSerializer());
+ Map<String, String> vals = new HashMap<String, String>();
+ for(int i = 0; i < 100; i++) {
+ vals.put("" + i, "" + i);
+ stringStore.put("" + i, new Versioned<String>("" + i), null);
+ }
+
+ for(int i = 100; i < 200; i++) {
+ vals.put("" + i, "" + i);
+ }
+
+ Map<String, Boolean> results = stringStore.hasKeys(vals.keySet(), true);
+
+ for(int i = 0; i < 100; i++) {
+ assertEquals(results.get("" + i), new Boolean(true));
+ }
+
+ for(int i = 100; i < 200; i++) {
+ assertEquals(results.get("" + i), new Boolean(false));
+ }
+ }
+
public void testGetNoEntries() {
ClosableIterator<Pair<ByteArray, Versioned<byte[]>>> it = null;
try {
Please sign in to comment.
Something went wrong with that request. Please try again.