Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zznate/390015 to your computer and use it in GitHub Desktop.
Save zznate/390015 to your computer and use it in GitHub Desktop.
@Test
public void testBatchMutate() throws IllegalArgumentException, NoSuchElementException,
IllegalStateException, NotFoundException, TException, Exception {
Map<String, Map<String, List<Mutation>>> outerMutationMap = new HashMap<String, Map<String,List<Mutation>>>();
for (int i = 0; i < 10; i++) {
Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>();
ArrayList<Mutation> mutations = new ArrayList<Mutation>(10);
for (int j = 0; j < 10; j++) {
Column col = new Column(bytes("testBatchMutateColumn_" + j),
bytes("testBatchMutateColumn_value_" + j), keyspace.createTimestamp());
//list.add(col);
ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
cosc.setColumn(col);
Mutation mutation = new Mutation();
mutation.setColumn_or_supercolumn(cosc);
mutations.add(mutation);
}
mutationMap.put("Standard1", mutations);
outerMutationMap.put("testBatchMutateColumn_" + i, mutationMap);
}
keyspace.batchMutate(outerMutationMap);
// re-use later
outerMutationMap.clear();
// get value
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ColumnPath cp = new ColumnPath("Standard1");
cp.setColumn(bytes("testBatchMutateColumn_" + j));
Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp);
assertNotNull(col);
String value = string(col.getValue());
assertEquals("testBatchMutateColumn_value_" + j, value);
}
}
// batch_mutate delete by key
for (int i = 0; i < 10; i++) {
ArrayList<Mutation> mutations = new ArrayList<Mutation>(10);
Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>();
SlicePredicate slicePredicate = new SlicePredicate();
for (int j = 0; j < 10; j++) {
slicePredicate.addToColumn_names(bytes("testBatchMutateColumn_" + j));
}
Mutation mutation = new Mutation();
Deletion deletion = new Deletion(keyspace.createTimestamp());
deletion.setPredicate(slicePredicate);
mutation.setDeletion(deletion);
mutations.add(mutation);
mutationMap.put("Standard1", mutations);
outerMutationMap.put("testBatchMutateColumn_"+i, mutationMap);
}
keyspace.batchMutate(outerMutationMap);
// make sure the values are gone
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ColumnPath cp = new ColumnPath("Standard1");
cp.setColumn(bytes("testBatchMutateColumn_" + j));
try {
keyspace.getColumn("testBatchMutateColumn_" + i, cp);
fail();
} catch (NotFoundException e) {
}
}
}
}
@Test
public void testBatchMutateBatchMutation() throws IllegalArgumentException, NoSuchElementException,
IllegalStateException, NotFoundException, TException, Exception {
BatchMutation batchMutation = new BatchMutation();
List<String> columnFamilies = Arrays.asList("Standard1");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
Column col = new Column(bytes("testBatchMutateColumn_" + j),
bytes("testBatchMutateColumn_value_" + j), keyspace.createTimestamp());
batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col);
}
}
keyspace.batchMutate(batchMutation);
// get value
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ColumnPath cp = new ColumnPath("Standard1");
cp.setColumn(bytes("testBatchMutateColumn_" + j));
Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp);
assertNotNull(col);
String value = string(col.getValue());
assertEquals("testBatchMutateColumn_value_" + j, value);
}
}
batchMutation = new BatchMutation();
// batch_mutate delete by key
for (int i = 0; i < 10; i++) {
SlicePredicate slicePredicate = new SlicePredicate();
for (int j = 0; j < 10; j++) {
slicePredicate.addToColumn_names(bytes("testBatchMutateColumn_" + j));
}
Deletion deletion = new Deletion(keyspace.createTimestamp());
deletion.setPredicate(slicePredicate);
batchMutation.addDeletion("testBatchMutateColumn_" + i, columnFamilies, deletion);
}
keyspace.batchMutate(batchMutation);
// make sure the values are gone
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ColumnPath cp = new ColumnPath("Standard1");
cp.setColumn(bytes("testBatchMutateColumn_" + j));
try {
keyspace.getColumn("testBatchMutateColumn_" + i, cp);
fail();
} catch (NotFoundException e) {
// good, we want this to throw.
}
}
}
}
@Test
public void testBatchUpdateInsertAndDelOnSame() throws IllegalArgumentException, NoSuchElementException,
IllegalStateException, NotFoundException, TException, Exception {
ColumnPath sta1 = new ColumnPath("Standard1");
sta1.setColumn(bytes("deleteThroughInserBatch_col"));
keyspace.insert("deleteThroughInserBatch_key", sta1, bytes("deleteThroughInserBatch_val"));
Column found = keyspace.getColumn("deleteThroughInserBatch_key", sta1);
assertNotNull(found);
BatchMutation batchMutation = new BatchMutation();
List<String> columnFamilies = Arrays.asList("Standard1");
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
Column col = new Column(bytes("testBatchMutateColumn_" + j),
bytes("testBatchMutateColumn_value_" + j), keyspace.createTimestamp());
batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col);
}
}
SlicePredicate slicePredicate = new SlicePredicate();
slicePredicate.addToColumn_names(bytes("deleteThroughInserBatch_col"));
Deletion deletion = new Deletion(keyspace.createTimestamp());
deletion.setPredicate(slicePredicate);
batchMutation.addDeletion("deleteThroughInserBatch_key", columnFamilies, deletion);
keyspace.batchMutate(batchMutation);
try {
keyspace.getColumn("deleteThroughInserBatch_key", sta1);
fail("Should not have found a value here");
} catch (Exception e) {
}
// get value
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
ColumnPath cp = new ColumnPath("Standard1");
cp.setColumn(bytes("testBatchMutateColumn_" + j));
Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp);
assertNotNull(col);
String value = string(col.getValue());
assertEquals("testBatchMutateColumn_value_" + j, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment