Created
May 7, 2010 06:46
-
-
Save zznate/393148 to your computer and use it in GitHub Desktop.
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
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