use of org.apache.accumulo.core.data.Condition in project incubator-rya by apache.
the class PcjTables method makeUpdateCardinalityMutation.
/**
* Creates a {@link ConditionalMutation} that only updates the cardinality
* of the PCJ table if the old value has not changed by the time this mutation
* is committed to Accumulo.
*
* @param current - The current cardinality value.
* @param delta - How much the cardinality will change.
* @return The mutation that will perform the conditional update.
*/
private static ConditionalMutation makeUpdateCardinalityMutation(final long current, final long delta) {
// Try to update the cardinality by the delta.
final ConditionalMutation mutation = new ConditionalMutation(PCJ_METADATA_ROW_ID);
final Condition lastCardinalityStillCurrent = new Condition(PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY);
// Require the old cardinality to be the value we just read.
final byte[] currentCardinalityBytes = longLexicoder.encode(current);
lastCardinalityStillCurrent.setValue(currentCardinalityBytes);
mutation.addCondition(lastCardinalityStillCurrent);
// If that is the case, then update to the new value.
final Value newCardinality = new Value(longLexicoder.encode(current + delta));
mutation.put(PCJ_METADATA_FAMILY, PCJ_METADATA_CARDINALITY, newCardinality);
return mutation;
}
use of org.apache.accumulo.core.data.Condition in project accumulo by apache.
the class ConditionalComparatorTest method testComparator.
@Test
public void testComparator() {
Condition c1 = new Condition("a", "b");
Condition c2 = new Condition("a", "c");
Condition c3 = new Condition("b", "c");
Condition c4 = new Condition("a", "b").setTimestamp(5);
Condition c5 = new Condition("a", "b").setTimestamp(6);
Condition c6 = new Condition("a", "b").setVisibility(new ColumnVisibility("A&B"));
Condition c7 = new Condition("a", "b").setVisibility(new ColumnVisibility("A&C"));
ConditionComparator comparator = new ConditionComparator();
Assert.assertTrue(comparator.compare(c1, c1) == 0);
Assert.assertTrue(comparator.compare(c1, c2) < 0);
Assert.assertTrue(comparator.compare(c2, c1) > 0);
Assert.assertTrue(comparator.compare(c1, c3) < 0);
Assert.assertTrue(comparator.compare(c3, c1) > 0);
Assert.assertTrue(comparator.compare(c1, c4) < 0);
Assert.assertTrue(comparator.compare(c4, c1) > 0);
Assert.assertTrue(comparator.compare(c5, c4) < 0);
Assert.assertTrue(comparator.compare(c4, c5) > 0);
Assert.assertTrue(comparator.compare(c1, c7) < 0);
Assert.assertTrue(comparator.compare(c7, c1) > 0);
Assert.assertTrue(comparator.compare(c6, c7) < 0);
Assert.assertTrue(comparator.compare(c7, c6) > 0);
}
use of org.apache.accumulo.core.data.Condition in project accumulo by apache.
the class ConditionalWriterIT method testTimeout.
@Test
public void testTimeout() throws Exception {
Connector conn = getConnector();
String table = getUniqueNames(1)[0];
conn.tableOperations().create(table);
try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig().setTimeout(3, TimeUnit.SECONDS));
Scanner scanner = conn.createScanner(table, Authorizations.EMPTY)) {
ConditionalMutation cm1 = new ConditionalMutation("r1", new Condition("tx", "seq"));
cm1.put("tx", "seq", "1");
cm1.put("data", "x", "a");
Assert.assertEquals(cw.write(cm1).getStatus(), Status.ACCEPTED);
IteratorSetting is = new IteratorSetting(5, SlowIterator.class);
SlowIterator.setSeekSleepTime(is, 5000);
ConditionalMutation cm2 = new ConditionalMutation("r1", new Condition("tx", "seq").setValue("1").setIterators(is));
cm2.put("tx", "seq", "2");
cm2.put("data", "x", "b");
Assert.assertEquals(cw.write(cm2).getStatus(), Status.UNKNOWN);
for (Entry<Key, Value> entry : scanner) {
String cf = entry.getKey().getColumnFamilyData().toString();
String cq = entry.getKey().getColumnQualifierData().toString();
String val = entry.getValue().toString();
if (cf.equals("tx") && cq.equals("seq"))
Assert.assertEquals("Unexpected value in tx:seq", "1", val);
else if (cf.equals("data") && cq.equals("x"))
Assert.assertEquals("Unexpected value in data:x", "a", val);
else
Assert.fail("Saw unexpected column family and qualifier: " + entry);
}
ConditionalMutation cm3 = new ConditionalMutation("r1", new Condition("tx", "seq").setValue("1"));
cm3.put("tx", "seq", "2");
cm3.put("data", "x", "b");
Assert.assertEquals(cw.write(cm3).getStatus(), Status.ACCEPTED);
}
}
use of org.apache.accumulo.core.data.Condition in project accumulo by apache.
the class ConditionalWriterIT method testTableAndConditionIterators.
@Test
public void testTableAndConditionIterators() throws Exception {
// test w/ table that has iterators configured
Connector conn = getConnector();
String tableName = getUniqueNames(1)[0];
IteratorSetting aiConfig1 = new IteratorSetting(30, "AI1", AddingIterator.class);
aiConfig1.addOption("amount", "2");
IteratorSetting aiConfig2 = new IteratorSetting(35, "MI1", MultiplyingIterator.class);
aiConfig2.addOption("amount", "3");
IteratorSetting aiConfig3 = new IteratorSetting(40, "AI2", AddingIterator.class);
aiConfig3.addOption("amount", "5");
conn.tableOperations().create(tableName);
BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
Mutation m = new Mutation("ACCUMULO-1000");
m.put("count", "comments", "6");
bw.addMutation(m);
m = new Mutation("ACCUMULO-1001");
m.put("count", "comments", "7");
bw.addMutation(m);
m = new Mutation("ACCUMULO-1002");
m.put("count", "comments", "8");
bw.addMutation(m);
bw.close();
conn.tableOperations().attachIterator(tableName, aiConfig1, EnumSet.of(IteratorScope.scan));
conn.tableOperations().offline(tableName, true);
conn.tableOperations().online(tableName, true);
try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig());
Scanner scanner = conn.createScanner(tableName, new Authorizations())) {
ConditionalMutation cm6 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setValue("8"));
cm6.put("count", "comments", "7");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm6).getStatus());
scanner.setRange(new Range("ACCUMULO-1000"));
scanner.fetchColumn(new Text("count"), new Text("comments"));
Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("9", entry.getValue().toString());
ConditionalMutation cm7 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(aiConfig2).setValue("27"));
cm7.put("count", "comments", "8");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm7).getStatus());
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("10", entry.getValue().toString());
ConditionalMutation cm8 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(aiConfig2, aiConfig3).setValue("35"));
cm8.put("count", "comments", "9");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm8).getStatus());
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("11", entry.getValue().toString());
ConditionalMutation cm3 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(aiConfig2).setValue("33"));
cm3.put("count", "comments", "3");
ConditionalMutation cm4 = new ConditionalMutation("ACCUMULO-1001", new Condition("count", "comments").setIterators(aiConfig3).setValue("14"));
cm4.put("count", "comments", "3");
ConditionalMutation cm5 = new ConditionalMutation("ACCUMULO-1002", new Condition("count", "comments").setIterators(aiConfig3).setValue("10"));
cm5.put("count", "comments", "3");
Iterator<Result> results = cw.write(Arrays.asList(cm3, cm4, cm5).iterator());
Map<String, Status> actual = new HashMap<>();
while (results.hasNext()) {
Result result = results.next();
String k = new String(result.getMutation().getRow());
Assert.assertFalse("Did not expect to see multiple resultus for the row: " + k, actual.containsKey(k));
actual.put(k, result.getStatus());
}
Map<String, Status> expected = new HashMap<>();
expected.put("ACCUMULO-1000", Status.ACCEPTED);
expected.put("ACCUMULO-1001", Status.ACCEPTED);
expected.put("ACCUMULO-1002", Status.REJECTED);
Assert.assertEquals(expected, actual);
}
}
use of org.apache.accumulo.core.data.Condition in project accumulo by apache.
the class ConditionalWriterIT method testDeleteTable.
@Test
public void testDeleteTable() throws Exception {
String table = getUniqueNames(1)[0];
Connector conn = getConnector();
try {
conn.createConditionalWriter(table, new ConditionalWriterConfig());
Assert.fail("Creating conditional writer for table that doesn't exist should fail");
} catch (TableNotFoundException e) {
}
conn.tableOperations().create(table);
try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig())) {
conn.tableOperations().delete(table);
ConditionalMutation cm1 = new ConditionalMutation("r1", new Condition("tx", "seq"));
cm1.put("tx", "seq", "1");
cm1.put("data", "x", "a");
Result result = cw.write(cm1);
try {
Status status = result.getStatus();
Assert.fail("Expected exception writing conditional mutation to deleted table. Got status: " + status);
} catch (AccumuloException ae) {
Assert.assertEquals(TableDeletedException.class, ae.getCause().getClass());
}
}
}
Aggregations