client.createTable(creds, TABLE_TEST, true, TimeType.MILLIS);
client.addConstraint(creds, TABLE_TEST, NumericValueConstraint.class.getName());
String cwid = client.createConditionalWriter(creds, TABLE_TEST, new ConditionalWriterOptions());
Map<ByteBuffer,ConditionalUpdates> updates = new HashMap<ByteBuffer,ConditionalUpdates>();
updates.put(
s2bb("00345"),
new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq")), Arrays.asList(newColUpdate("meta", "seq", 10, "1"),
newColUpdate("data", "img", "73435435"))));
Map<ByteBuffer,ConditionalStatus> results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.ACCEPTED, results.get(s2bb("00345")));
assertScan(new String[][] { {"00345", "data", "img", "73435435"}, {"00345", "meta", "seq", "1"}}, TABLE_TEST);
// test not setting values on conditions
updates.clear();
updates.put(s2bb("00345"), new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq")), Arrays.asList(newColUpdate("meta", "seq", "2"))));
updates.put(s2bb("00346"), new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq")), Arrays.asList(newColUpdate("meta", "seq", "1"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(2, results.size());
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00345")));
assertEquals(ConditionalStatus.ACCEPTED, results.get(s2bb("00346")));
assertScan(new String[][] { {"00345", "data", "img", "73435435"}, {"00345", "meta", "seq", "1"}, {"00346", "meta", "seq", "1"}}, TABLE_TEST);
// test setting values on conditions
updates.clear();
updates.put(
s2bb("00345"),
new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq", "1")), Arrays.asList(newColUpdate("meta", "seq", 20, "2"),
newColUpdate("data", "img", "567890"))));
updates.put(s2bb("00346"), new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq", "2")), Arrays.asList(newColUpdate("meta", "seq", "3"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(2, results.size());
assertEquals(ConditionalStatus.ACCEPTED, results.get(s2bb("00345")));
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00346")));
assertScan(new String[][] { {"00345", "data", "img", "567890"}, {"00345", "meta", "seq", "2"}, {"00346", "meta", "seq", "1"}}, TABLE_TEST);
// test setting timestamp on condition to a non-existant version
updates.clear();
updates.put(
s2bb("00345"),
new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq", 10, "2")), Arrays.asList(newColUpdate("meta", "seq", 30, "3"),
newColUpdate("data", "img", "1234567890"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00345")));
assertScan(new String[][] { {"00345", "data", "img", "567890"}, {"00345", "meta", "seq", "2"}, {"00346", "meta", "seq", "1"}}, TABLE_TEST);
// test setting timestamp to an existing version
updates.clear();
updates.put(
s2bb("00345"),
new ConditionalUpdates(Arrays.asList(newCondition("meta", "seq", 20, "2")), Arrays.asList(newColUpdate("meta", "seq", 30, "3"),
newColUpdate("data", "img", "1234567890"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.ACCEPTED, results.get(s2bb("00345")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"}}, TABLE_TEST);
// run test w/ condition that has iterators
// following should fail w/o iterator
client.updateAndFlush(creds, TABLE_TEST, Collections.singletonMap(s2bb("00347"), Arrays.asList(newColUpdate("data", "count", "1"))));
client.updateAndFlush(creds, TABLE_TEST, Collections.singletonMap(s2bb("00347"), Arrays.asList(newColUpdate("data", "count", "1"))));
client.updateAndFlush(creds, TABLE_TEST, Collections.singletonMap(s2bb("00347"), Arrays.asList(newColUpdate("data", "count", "1"))));
updates.clear();
updates.put(s2bb("00347"),
new ConditionalUpdates(Arrays.asList(newCondition("data", "count", "3")), Arrays.asList(newColUpdate("data", "img", "1234567890"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00347")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "1"}}, TABLE_TEST);
// following test w/ iterator setup should succeed
Condition iterCond = newCondition("data", "count", "3");
Map<String,String> props = new HashMap<String,String>();
props.put("type", "STRING");
props.put("columns", "data:count");
IteratorSetting is = new IteratorSetting(1, "sumc", SummingCombiner.class.getName(), props);
iterCond.setIterators(Arrays.asList(is));
updates.clear();
updates.put(s2bb("00347"), new ConditionalUpdates(Arrays.asList(iterCond), Arrays.asList(newColUpdate("data", "img", "1234567890"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.ACCEPTED, results.get(s2bb("00347")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "1"}, {"00347", "data", "img", "1234567890"}}, TABLE_TEST);
// test a mutation that violated a constraint
updates.clear();
updates.put(s2bb("00347"),
new ConditionalUpdates(Arrays.asList(newCondition("data", "img", "1234567890")), Arrays.asList(newColUpdate("data", "count", "A"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.VIOLATED, results.get(s2bb("00347")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "1"}, {"00347", "data", "img", "1234567890"}}, TABLE_TEST);
// run test with two conditions
// both conditions should fail
updates.clear();
updates.put(
s2bb("00347"),
new ConditionalUpdates(Arrays.asList(newCondition("data", "img", "565"), newCondition("data", "count", "2")), Arrays.asList(
newColUpdate("data", "count", "3"), newColUpdate("data", "img", "0987654321"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00347")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "1"}, {"00347", "data", "img", "1234567890"}}, TABLE_TEST);
// one condition should fail
updates.clear();
updates.put(
s2bb("00347"),
new ConditionalUpdates(Arrays.asList(newCondition("data", "img", "1234567890"), newCondition("data", "count", "2")), Arrays.asList(
newColUpdate("data", "count", "3"), newColUpdate("data", "img", "0987654321"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00347")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "1"}, {"00347", "data", "img", "1234567890"}}, TABLE_TEST);
// one condition should fail
updates.clear();
updates.put(
s2bb("00347"),
new ConditionalUpdates(Arrays.asList(newCondition("data", "img", "565"), newCondition("data", "count", "1")), Arrays.asList(
newColUpdate("data", "count", "3"), newColUpdate("data", "img", "0987654321"))));
results = client.updateRowsConditionally(cwid, updates);
assertEquals(1, results.size());
assertEquals(ConditionalStatus.REJECTED, results.get(s2bb("00347")));
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "1"}, {"00347", "data", "img", "1234567890"}}, TABLE_TEST);
// both conditions should succeed
ConditionalStatus result = client.updateRowConditionally(
creds,
TABLE_TEST,
s2bb("00347"),
new ConditionalUpdates(Arrays.asList(newCondition("data", "img", "1234567890"), newCondition("data", "count", "1")), Arrays.asList(
newColUpdate("data", "count", "3"), newColUpdate("data", "img", "0987654321"))));
assertEquals(ConditionalStatus.ACCEPTED, result);
assertScan(new String[][] { {"00345", "data", "img", "1234567890"}, {"00345", "meta", "seq", "3"}, {"00346", "meta", "seq", "1"},
{"00347", "data", "count", "3"}, {"00347", "data", "img", "0987654321"}}, TABLE_TEST);
client.closeConditionalWriter(cwid);
try {
client.updateRowsConditionally(cwid, updates);
fail("conditional writer not closed");
} catch (UnknownWriter uk) {}
// run test with colvis
client.createLocalUser(creds, "cwuser", s2bb("bestpasswordever"));
client.changeUserAuthorizations(creds, "cwuser", Collections.singleton(s2bb("A")));
client.grantTablePermission(creds, "cwuser", TABLE_TEST, TablePermission.WRITE);
client.grantTablePermission(creds, "cwuser", TABLE_TEST, TablePermission.READ);
ByteBuffer cwuCreds = client.login("cwuser", Collections.singletonMap("password", "bestpasswordever"));
cwid = client.createConditionalWriter(cwuCreds, TABLE_TEST, new ConditionalWriterOptions().setAuthorizations(Collections.singleton(s2bb("A"))));
updates.clear();
updates.put(
s2bb("00348"),
new ConditionalUpdates(Arrays.asList(new Condition(new Column(s2bb("data"), s2bb("c"), s2bb("A")))), Arrays.asList(newColUpdate("data", "seq", "1"),