Search in sources :

Example 21 with TransactionExecutor

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));
        }
    });
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) TransactionFailureException(org.apache.tephra.TransactionFailureException) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 22 with TransactionExecutor

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());
        }
    });
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 23 with TransactionExecutor

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());
        }
    });
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 24 with TransactionExecutor

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());
        }
    });
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Test(org.junit.Test)

Example 25 with TransactionExecutor

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);
    }
}
Also used : Scanner(co.cask.cdap.api.dataset.table.Scanner) TransactionExecutor(org.apache.tephra.TransactionExecutor) Row(co.cask.cdap.api.dataset.table.Row) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Aggregations

TransactionExecutor (org.apache.tephra.TransactionExecutor)79 Test (org.junit.Test)64 TransactionFailureException (org.apache.tephra.TransactionFailureException)31 DatasetId (co.cask.cdap.proto.id.DatasetId)30 Table (co.cask.cdap.api.dataset.table.Table)15 List (java.util.List)15 NoSuchElementException (java.util.NoSuchElementException)15 TransactionAware (org.apache.tephra.TransactionAware)15 IOException (java.io.IOException)12 Row (co.cask.cdap.api.dataset.table.Row)11 DefaultTransactionExecutor (org.apache.tephra.DefaultTransactionExecutor)11 Put (co.cask.cdap.api.dataset.table.Put)10 ImmutableList (com.google.common.collect.ImmutableList)9 ProgramId (co.cask.cdap.proto.id.ProgramId)8 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)6 Scanner (co.cask.cdap.api.dataset.table.Scanner)6 Map (java.util.Map)6 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)5 TypeToken (com.google.common.reflect.TypeToken)5 Iterator (java.util.Iterator)5