use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class KeyValueTableTest method testSyncWriteReadSwapDelete.
@Test
public void testSyncWriteReadSwapDelete() throws Exception {
TransactionExecutor txnl = dsFrameworkUtil.newTransactionExecutor(kvTable);
// this test runs all operations synchronously
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
// write a value and read it back
kvTable.write(KEY1, VAL1);
Assert.assertArrayEquals(VAL1, kvTable.read(KEY1));
// update the value and read it back
kvTable.write(KEY1, VAL2);
Assert.assertArrayEquals(VAL2, kvTable.read(KEY1));
// attempt to swap, expecting old value
Assert.assertFalse(kvTable.compareAndSwap(KEY1, VAL1, VAL3));
Assert.assertArrayEquals(VAL2, kvTable.read(KEY1));
// swap the value and read it back
Assert.assertTrue(kvTable.compareAndSwap(KEY1, VAL2, VAL3));
Assert.assertArrayEquals(VAL3, kvTable.read(KEY1));
// delete the value and verify its gone
kvTable.delete(KEY1);
Assert.assertNull(kvTable.read(KEY1));
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class IndexedObjectStoreTest method testLookupByIndex.
@Test
public void testLookupByIndex() 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> categories1 = ImmutableList.of("racing", "tech");
List<String> categories2 = ImmutableList.of("electronics", "tech");
Feed feed1 = new Feed("f1", "http://f1.com", categories1);
Feed feed2 = new Feed("apple", "http://apple.com", categories2);
byte[] key1 = Bytes.toBytes(feed1.getId());
byte[] key2 = Bytes.toBytes(feed2.getId());
indexedFeed.write(key1, feed1, getCategories(categories1));
indexedFeed.write(key2, feed2, getCategories(categories2));
List<Feed> feedResult1 = indexedFeed.readAllByIndex(Bytes.toBytes("racing"));
Assert.assertEquals(1, feedResult1.size());
List<Feed> feedResult2 = indexedFeed.readAllByIndex(Bytes.toBytes("running"));
Assert.assertEquals(0, feedResult2.size());
List<Feed> feedResult3 = indexedFeed.readAllByIndex(Bytes.toBytes("tech"));
Assert.assertEquals(2, feedResult3.size());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class IndexedObjectStoreTest method testIndexUpdates.
@Test
public void testIndexUpdates() 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> categories1 = ImmutableList.of("big data");
Feed feed1 = new Feed("a1", "http://apple.com", categories1);
byte[] key1 = Bytes.toBytes(feed1.getId());
indexedFeed.write(key1, feed1, getCategories(categories1));
List<Feed> feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("big data"));
Assert.assertEquals(1, feedResult.size());
indexedFeed.updateIndex(key1, Bytes.toBytes("startup"));
feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("startup"));
Assert.assertEquals(1, feedResult.size());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class IndexedObjectStoreTest method testIndexRewrites.
@Test
public void testIndexRewrites() 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> categories1 = ImmutableList.of("big data", "startup");
List<String> categories2 = ImmutableList.of("hadoop");
Feed feed1 = new Feed("c1", "http://abc.com", categories1);
byte[] key1 = Bytes.toBytes(feed1.getId());
indexedFeed.write(key1, feed1, getCategories(categories1));
List<Feed> feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("big data"));
Assert.assertEquals(1, feedResult.size());
// re-write with new index values
indexedFeed.write(key1, feed1, getCategories(categories2));
//Should not return based on old index values
feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("big data"));
Assert.assertEquals(0, feedResult.size());
feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("hadoop"));
//Should return based on new indexValue
Assert.assertEquals(1, feedResult.size());
//Update with no index value. Lookup by any indexValue should not return the old entries
indexedFeed.write(key1, feed1);
feedResult = indexedFeed.readAllByIndex(Bytes.toBytes("hadoop"));
Assert.assertEquals(0, feedResult.size());
}
});
}
use of org.apache.tephra.TransactionExecutor in project cdap by caskdata.
the class IndexedTableTest method testIndexKeyDelimiterHandling.
/**
* Test conditions where the indexed column name or column value may contain the key delimiter.
* @throws Exception
*/
@Test
public void testIndexKeyDelimiterHandling() throws Exception {
DatasetId delimTabInstance = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("delimtab");
dsFrameworkUtil.createInstance("indexedTable", delimTabInstance, DatasetProperties.builder().add(IndexedTable.INDEX_COLUMNS_CONF_KEY, idxColString).build());
final IndexedTable iTable = dsFrameworkUtil.getInstance(delimTabInstance);
final byte[] delim = new byte[] { 0 };
try {
final byte[] valueWithDelimiter = Bytes.concat(idx1, delim, idx2);
TransactionExecutor tx = dsFrameworkUtil.newTransactionExecutor(iTable);
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
iTable.put(keyA, idxCol, idx1);
iTable.put(keyB, idxCol, valueWithDelimiter);
iTable.put(keyC, idxCol, idx2);
}
});
tx.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Scanner scanner = iTable.readByIndex(idxCol, idx1);
try {
Row row = scanner.next();
TableAssert.assertRow(row, keyA, new byte[][] { idxCol }, new byte[][] { idx1 });
assertEmpty(scanner);
} finally {
scanner.close();
}
scanner = iTable.readByIndex(idxCol, idx2);
try {
Row row = scanner.next();
TableAssert.assertRow(row, keyC, new byte[][] { idxCol }, new byte[][] { idx2 });
assertEmpty(scanner);
} finally {
scanner.close();
}
scanner = iTable.readByIndex(idxCol, valueWithDelimiter);
try {
Row row = scanner.next();
TableAssert.assertRow(row, keyB, new byte[][] { idxCol }, new byte[][] { valueWithDelimiter });
assertEmpty(scanner);
} finally {
scanner.close();
}
}
});
} finally {
dsFrameworkUtil.deleteInstance(delimTabInstance);
}
}
Aggregations