Skip to content

Instantly share code, notes, and snippets.

@zznate
Created May 7, 2010 06:46
Show Gist options
  • Save zznate/393148 to your computer and use it in GitHub Desktop.
Save zznate/393148 to your computer and use it in GitHub Desktop.
The following would work with ExampleDao if you make execute public. Just an example of how you can get real tight encapsulation with a command pattern.
Given the following method on my "DAO" class:
public void batchInsert(final String key, final Map<String, String> columnMap) throws Exception {
BatchMutationCallback batchInsertCallback = new BatchMutationCallback(columnFamilyName, buildTimestamp());
for (String colName : columnMap.keySet()) {
batchInsertCallback.addInsertion(key, colName, columnMap.get(colName));
}
execute(batchInsertCallback);
}
...
I have the following callback class (annonymous inner classes can get too messy sometimes):
public class BatchMutationCallback extends Command<Void> {
private final long timestamp;
private final String columnFamilyName;
private final BatchMutation batchMutation;
BatchMutationCallback(String columnFamilyName, long timestamp) {
this.columnFamilyName = columnFamilyName;
this.timestamp = timestamp;
batchMutation = new BatchMutation();
}
public void addInsertion(String key, String columnName, String columnValue) {
batchMutation.addInsertion(key, Arrays.asList(columnFamilyName),
new Column(bytes(columnName), bytes(columnValue), timestamp));
}
public Void execute(final Keyspace ks) throws Exception {
ks.batchMutate(batchMutation);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment