Permalink
Browse files

Use the strategy pattern for db operations

  • Loading branch information...
justinsb committed Dec 13, 2013
1 parent 110fde4 commit 49c32753ec3ed085f08ee792e662f94d40d0d553

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -17,6 +17,10 @@
import com.cloudata.keyvalue.KeyValueProto.KvAction;
import com.cloudata.keyvalue.KeyValueProto.KvEntry;
import com.cloudata.keyvalue.btree.operation.DeleteOperation;
import com.cloudata.keyvalue.btree.operation.IncrementOperation;
import com.cloudata.keyvalue.btree.operation.KeyOperation;
import com.cloudata.keyvalue.btree.operation.SetOperation;
import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
@@ -83,8 +87,31 @@ public Object applyOperation(@Nonnull ByteBuffer op) {
KeyValueStore keyValueStore = getKeyValueStore(storeId);
Object ret = keyValueStore.doAction(entry.getAction(), key != null ? key.asReadOnlyByteBuffer() : null,
value != null ? value.asReadOnlyByteBuffer() : null);
KeyOperation operation;
switch (entry.getAction()) {
case DELETE:
operation = new DeleteOperation();
break;
case INCREMENT: {
long delta = 1;
if (entry.hasIncrementBy()) {
delta = entry.getIncrementBy();
}
operation = new IncrementOperation(delta);
break;
}
case SET:
operation = new SetOperation(value.asReadOnlyByteBuffer());
break;
default:
throw new UnsupportedOperationException();
}
Object ret = keyValueStore.doAction(key != null ? key.asReadOnlyByteBuffer() : null, operation);
return ret;
} catch (InvalidProtocolBufferException e) {
@@ -7,14 +7,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.KeyValueProto.KvAction;
import com.cloudata.keyvalue.btree.Btree;
import com.cloudata.keyvalue.btree.ByteBuffers;
import com.cloudata.keyvalue.btree.EntryListener;
import com.cloudata.keyvalue.btree.MmapPageStore;
import com.cloudata.keyvalue.btree.PageStore;
import com.cloudata.keyvalue.btree.ReadOnlyTransaction;
import com.cloudata.keyvalue.btree.WriteTransaction;
import com.cloudata.keyvalue.btree.operation.KeyOperation;
public class KeyValueStore {
@@ -31,9 +31,9 @@ public KeyValueStore(File dir, boolean uniqueKeys) throws IOException {
this.btree = new Btree(pageStore, uniqueKeys);
}
public Object doAction(KvAction action, ByteBuffer key, ByteBuffer value) {
public Object doAction(ByteBuffer key, KeyOperation operation) {
try (WriteTransaction txn = btree.beginReadWrite()) {
Object ret = txn.doAction(btree, action, key, value);
Object ret = txn.doAction(btree, key, operation);
txn.commit();
return ret;
}
@@ -9,7 +9,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloudata.keyvalue.KeyValueProto.KvAction;
import com.cloudata.keyvalue.btree.operation.KeyOperation;
import com.cloudata.util.Hex;
public class BranchPage extends Page {
@@ -412,7 +412,7 @@ Mutable getMutable() {
}
@Override
public Object doAction(Transaction txn, KvAction action, ByteBuffer key, ByteBuffer value) {
public Object doAction(Transaction txn, ByteBuffer key, KeyOperation operation) {
int pos = findPos(key);
if (pos < 0) {
pos = 0;
@@ -424,7 +424,7 @@ public Object doAction(Transaction txn, KvAction action, ByteBuffer key, ByteBuf
ByteBuffer oldLbound = childPage.getKeyLbound();
Object ret = childPage.doAction(txn, action, key, value);
Object ret = childPage.doAction(txn, key, operation);
ByteBuffer newLbound = childPage.getKeyLbound();
Oops, something went wrong.

0 comments on commit 49c3275

Please sign in to comment.