Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Use the same protobuf for both requests and log entries
We are now _really_ using the command-log pattern!
- Loading branch information
Showing
40 changed files
with
3,039 additions
and
2,528 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,49 @@ | ||
package com.cloudata.keyvalue.operation; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import com.cloudata.keyvalue.KeyValueLog.KvAction; | ||
import com.cloudata.keyvalue.KeyValueLog.KvEntry; | ||
import com.cloudata.btree.Keyspace; | ||
import com.cloudata.keyvalue.KeyValueProtocol.ActionType; | ||
import com.cloudata.keyvalue.KeyValueProtocol.KeyValueAction; | ||
import com.cloudata.keyvalue.KeyValueProtocol.ResponseEntry; | ||
import com.cloudata.values.Value; | ||
import com.google.common.base.Preconditions; | ||
import com.google.protobuf.ByteString; | ||
|
||
public class AppendOperation implements KeyOperation<Integer> { | ||
public class AppendOperation extends KeyValueOperationBase { | ||
|
||
private int newLength; | ||
final KvEntry entry; | ||
public AppendOperation(KeyValueAction entry) { | ||
super(entry); | ||
|
||
public AppendOperation(KvEntry entry) { | ||
Preconditions.checkState(entry.getAction() == KvAction.APPEND); | ||
Preconditions.checkState(entry.getAction() == ActionType.APPEND); | ||
Preconditions.checkState(!entry.getIfNotExists()); | ||
Preconditions.checkState(!entry.hasIfValue()); | ||
|
||
this.entry = entry; | ||
} | ||
|
||
@Override | ||
public Value doAction(Value oldValue) { | ||
Value appendValue = Value.deserialize(entry.getValue().asReadOnlyByteBuffer()); | ||
|
||
ResponseEntry.Builder eb = response.addEntryBuilder(); | ||
eb.setKey(entry.getKey()); | ||
|
||
Value newValue; | ||
if (oldValue == null) { | ||
this.newLength = entry.getValue().size(); | ||
return appendValue; | ||
newValue = appendValue; | ||
} else { | ||
Value appended = oldValue.concat(appendValue.asBytes()); | ||
|
||
this.newLength = appended.sizeAsBytes(); | ||
return appended; | ||
newValue = oldValue.concat(appendValue.asBytes()); | ||
} | ||
} | ||
|
||
@Override | ||
public KvEntry serialize() { | ||
return entry; | ||
} | ||
|
||
@Override | ||
public Integer getResult() { | ||
return newLength; | ||
} | ||
eb.setValue(ByteString.copyFrom(newValue.asBytes())); | ||
eb.setChanged(true); | ||
|
||
@Override | ||
public ByteBuffer getKey() { | ||
return entry.getKey().asReadOnlyByteBuffer(); | ||
return newValue; | ||
} | ||
|
||
public static AppendOperation build(long storeId, ByteString qualifiedKey, Value value) { | ||
KvEntry.Builder b = KvEntry.newBuilder(); | ||
b.setAction(KvAction.APPEND); | ||
public static AppendOperation build(long storeId, Keyspace keyspace, ByteString key, Value value) { | ||
KeyValueAction.Builder b = KeyValueAction.newBuilder(); | ||
b.setAction(ActionType.APPEND); | ||
b.setStoreId(storeId); | ||
b.setKey(qualifiedKey); | ||
b.setValue(ByteString.copyFrom(value.serialize())); | ||
b.setKeyspaceId(keyspace.getKeyspaceId()); | ||
b.setKey(key); | ||
b.setValue(ByteString.copyFrom(value.asBytes())); | ||
return new AppendOperation(b.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.cloudata.keyvalue.operation; | ||
|
||
import com.cloudata.btree.Btree; | ||
import com.cloudata.btree.Transaction; | ||
import com.cloudata.btree.operation.ComplexOperation; | ||
import com.cloudata.keyvalue.KeyValueProtocol.ActionResponse; | ||
import com.cloudata.keyvalue.KeyValueProtocol.ActionType; | ||
import com.cloudata.keyvalue.KeyValueProtocol.KeyValueAction; | ||
import com.google.common.base.Preconditions; | ||
|
||
public class CompoundOperation implements KeyValueOperation, ComplexOperation<ActionResponse> { | ||
|
||
private final KeyValueAction entry; | ||
|
||
final ActionResponse.Builder response = ActionResponse.newBuilder(); | ||
|
||
public CompoundOperation(KeyValueAction entry) { | ||
Preconditions.checkState(entry.getAction() == ActionType.COMPOUND); | ||
Preconditions.checkArgument(entry.hasStoreId()); | ||
|
||
this.entry = entry; | ||
} | ||
|
||
@Override | ||
public KeyValueAction serialize() { | ||
return entry; | ||
} | ||
|
||
@Override | ||
public ActionResponse getResult() { | ||
return response.build(); | ||
} | ||
|
||
@Override | ||
public void doAction(Btree btree, Transaction txn) { | ||
for (KeyValueAction child : entry.getChildrenList()) { | ||
if (child.getStoreId() != entry.getStoreId()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
KeyValueOperation op = KeyValueOperations.build(child); | ||
txn.doAction(btree, op); | ||
response.addChildren(op.getResult()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isReadOnly() { | ||
return KeyValueOperations.isReadOnly(entry); | ||
} | ||
|
||
@Override | ||
public long getStoreId() { | ||
return entry.getStoreId(); | ||
} | ||
|
||
} |
Oops, something went wrong.