use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class CounterTimeseriesTableTest method testCounter.
@Test
public void testCounter() throws Exception {
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor(table);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
byte[] rowKey1 = Bytes.toBytes("1");
byte[] rowKey2 = Bytes.toBytes("2");
byte[] rowKey3 = Bytes.toBytes("3");
byte[] rowKey4 = Bytes.toBytes("4");
long timestamp1 = System.currentTimeMillis();
long timestamp2 = timestamp1 + 500;
long timestamp3 = timestamp1 + 1000;
long timestamp4 = timestamp1 + 1001;
long timestamp5 = timestamp1 + 1500;
long timestamp6 = timestamp1 + 2000;
byte[] tag1 = Bytes.toBytes('t');
byte[] tag2 = Bytes.toBytes('u');
// Test increment
assertEquals(2L, table.increment(rowKey1, 2L, timestamp1));
assertEquals(2L, table.increment(rowKey1, 0L, timestamp1));
assertEquals(7L, table.increment(rowKey1, 5L, timestamp1));
assertEquals(-2L, table.increment(rowKey2, -2L, timestamp1));
assertEquals(0L, table.increment(rowKey3, 0L, timestamp1));
assertEquals(5L, table.increment(rowKey1, 5L, timestamp2));
assertEquals(10L, table.increment(rowKey1, 5L, timestamp2));
assertEquals(7L, table.increment(rowKey1, 7L, timestamp3));
assertEquals(-2L, table.increment(rowKey2, 0L, timestamp1));
// Test set
table.set(rowKey1, 20L, timestamp4);
Iterator<CounterTimeseriesTable.Counter> result = table.read(rowKey1, timestamp4, timestamp4);
assertCounterEquals(rowKey1, 20L, timestamp4, result.next());
assertFalse(result.hasNext());
// Test read
result = table.read(rowKey1, timestamp1, timestamp4);
assertCounterEquals(rowKey1, 7L, timestamp1, result.next());
assertCounterEquals(rowKey1, 10L, timestamp2, result.next());
assertCounterEquals(rowKey1, 7L, timestamp3, result.next());
assertCounterEquals(rowKey1, 20L, timestamp4, result.next());
assertFalse(result.hasNext());
result = table.read(rowKey1, timestamp1, timestamp3, 1, 2);
assertCounterEquals(rowKey1, 10L, timestamp2, result.next());
assertCounterEquals(rowKey1, 7L, timestamp3, result.next());
assertFalse(result.hasNext());
table.set(rowKey4, 3L, timestamp1, tag1);
table.increment(rowKey4, 5L, timestamp2);
table.increment(rowKey4, 7L, timestamp3);
table.increment(rowKey4, 11L, timestamp3, tag1);
table.increment(rowKey4, 13L, timestamp3);
table.increment(rowKey4, 17L, timestamp4, tag1);
table.increment(rowKey4, 19L, timestamp4);
table.increment(rowKey4, 23L, timestamp5, tag1);
table.increment(rowKey4, 29L, timestamp5, tag1);
table.increment(rowKey4, 44L, timestamp5, tag2, tag1);
table.set(rowKey4, 31L, timestamp5);
table.set(rowKey4, 37L, timestamp6, tag2);
table.set(rowKey3, 41L, timestamp5, tag1);
table.set(rowKey3, 43L, timestamp5);
table.set(rowKey3, 47L, timestamp6, tag1, tag2);
result = table.read(rowKey4, timestamp1, timestamp6, tag1);
assertCounterEquals(rowKey4, 3L, timestamp1, result.next());
assertCounterEquals(rowKey4, 11L, timestamp3, result.next());
assertCounterEquals(rowKey4, 17L, timestamp4, result.next());
assertCounterEquals(rowKey4, 52L, timestamp5, result.next());
assertCounterEquals(rowKey4, 44L, timestamp5, result.next());
assertFalse(result.hasNext());
// Multiple tags
result = table.read(rowKey3, timestamp1, timestamp6, tag2, tag1);
assertCounterEquals(rowKey3, 47L, timestamp6, result.next());
assertFalse(result.hasNext());
result = table.read(rowKey4, timestamp1, timestamp6, tag1, tag2);
assertCounterEquals(rowKey4, 44L, timestamp5, result.next());
assertFalse(result.hasNext());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class DatasetWithArgumentsTest method testPrefixTable.
@Test
public void testPrefixTable() throws Exception {
final PrefixedTable table = dsFrameworkUtil.getInstance(pret, Collections.<String, String>emptyMap());
final PrefixedTable aTable = dsFrameworkUtil.getInstance(pret, Collections.singletonMap("prefix", "a"));
final PrefixedTable bTable = dsFrameworkUtil.getInstance(pret, Collections.singletonMap("prefix", "b"));
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(aTable, bTable, table);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// write some values
table.write("z", "0");
aTable.write("x", "1");
bTable.write("x", "2");
bTable.write("y", "3");
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// read all values without prefix
Assert.assertEquals("0", table.read("z"));
Assert.assertEquals("1", table.read("ax"));
Assert.assertEquals("2", table.read("bx"));
Assert.assertEquals("3", table.read("by"));
// read all values with prefix a
Assert.assertEquals("1", aTable.read("x"));
Assert.assertNull(aTable.read("y"));
Assert.assertNull(aTable.read("z"));
// read all values with prefix b
Assert.assertEquals("2", bTable.read("x"));
Assert.assertEquals("3", bTable.read("y"));
Assert.assertNull(aTable.read("z"));
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class IndexedObjectStoreTest method testIndexPruning.
@Test
public void testIndexPruning() throws Exception {
final IndexedObjectStore<Feed> indexedFeed = dsFrameworkUtil.getInstance(index);
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(indexedFeed);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
List<String> categories = ImmutableList.of("running", "marathon", "drinking");
Feed feed = new Feed("rocknroll", "http://rock'n'roll.com", categories);
byte[] key = Bytes.toBytes(feed.getId());
indexedFeed.write(key, feed, getCategories(categories));
List<Feed> feeds = indexedFeed.readAllByIndex(Bytes.toBytes("drinking"));
Assert.assertEquals(1, feeds.size());
indexedFeed.pruneIndex(key, Bytes.toBytes("drinking"));
feeds = indexedFeed.readAllByIndex(Bytes.toBytes("drinking"));
Assert.assertEquals(0, feeds.size());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class IndexedObjectStoreTest method testIndexNoSecondaryKeyChanges.
@Test
public void testIndexNoSecondaryKeyChanges() throws Exception {
final IndexedObjectStore<Feed> indexedFeed = dsFrameworkUtil.getInstance(index);
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(indexedFeed);
final List<String> categories = ImmutableList.of("C++", "C#");
final Feed feed1 = new Feed("MSFT", "http://microwsoft.com", categories);
final byte[] key1 = Bytes.toBytes(feed1.getId());
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
indexedFeed.write(key1, feed1, getCategories(categories));
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
List<Feed> feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("C++"));
Assert.assertEquals(1, feedResult.size());
}
});
// re-write f1 with updated url but same id
final Feed feed2 = new Feed("MSFT", "http://microsoft.com", categories);
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
indexedFeed.write(key1, feed2, getCategories(categories));
}
});
//Should be still be able to look up by secondary indices
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
List<Feed> feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("C++"));
Assert.assertEquals(1, feedResult.size());
feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("C#"));
Assert.assertEquals(1, feedResult.size());
Assert.assertEquals("http://microsoft.com", feedResult.get(0).getUrl());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class UsageDatasetTest method testOneMapping.
@Test
public void testOneMapping() throws Exception {
final UsageDataset usageDataset = getUsageDataset("testOneMapping");
TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor((TransactionAware) usageDataset);
// Add mapping
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
usageDataset.register(flow11, datasetInstance1);
}
});
// Verify mapping
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals(ImmutableSet.of(datasetInstance1), usageDataset.getDatasets(flow11));
}
});
}
Aggregations