Search in sources :

Example 41 with TransactionExecutor

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

Example 42 with TransactionExecutor

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

Example 43 with TransactionExecutor

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

Example 44 with TransactionExecutor

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

Example 45 with TransactionExecutor

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));
        }
    });
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) 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