Search in sources :

Example 1 with DiffStats

use of com.palantir.atlasdb.table.common.TableTasks.DiffStats in project atlasdb by palantir.

the class TableTasksTest method testDiffTask.

@Test
public void testDiffTask() throws InterruptedException {
    TableReference table1 = TableReference.createWithEmptyNamespace("table1");
    TableReference table2 = TableReference.createWithEmptyNamespace("table2");
    Random rand = new Random();
    kvs.createTable(table1, AtlasDbConstants.GENERIC_TABLE_METADATA);
    kvs.createTable(table2, AtlasDbConstants.GENERIC_TABLE_METADATA);
    Multimap<Integer, Integer> keys1 = HashMultimap.create();
    Multimap<Integer, Integer> keys2 = HashMultimap.create();
    int key = 0;
    for (int col = 0; col < 256; col++) {
        int randomInt = rand.nextInt(3);
        if (randomInt >= 1) {
            keys1.put(key, col);
            kvs.put(table1, ImmutableMap.of(Cell.create(new byte[] { (byte) key }, new byte[] { (byte) col }), new byte[] { 0 }), 1);
        }
        if (randomInt <= 1) {
            keys2.put(key, col);
            kvs.put(table2, ImmutableMap.of(Cell.create(new byte[] { (byte) key }, new byte[] { (byte) col }), new byte[] { 0 }), 1);
        }
        if (rand.nextBoolean()) {
            key++;
        }
    }
    TransactionServices.createTransactionService(kvs).putUnlessExists(1, 1);
    AtomicLong rowsOnlyInSource = new AtomicLong();
    AtomicLong rowsPartiallyInCommon = new AtomicLong();
    AtomicLong rowsCompletelyInCommon = new AtomicLong();
    AtomicLong rowsVisited = new AtomicLong();
    AtomicLong cellsOnlyInSource = new AtomicLong();
    AtomicLong cellsInCommon = new AtomicLong();
    DiffStats stats = new TableTasks.DiffStats(rowsOnlyInSource, rowsPartiallyInCommon, rowsCompletelyInCommon, rowsVisited, cellsOnlyInSource, cellsInCommon);
    TableTasks.diff(txManager, MoreExecutors.newDirectExecutorService(), table1, table2, 10, 1, stats, (transaction, partialDiff) -> Iterators.size(partialDiff));
    long sourceOnlyCells = 0;
    long commonCells = 0;
    for (Entry<Integer, Integer> cell : keys1.entries()) {
        if (keys2.containsEntry(cell.getKey(), cell.getValue())) {
            commonCells++;
        } else {
            sourceOnlyCells++;
        }
    }
    long disjointRows = 0;
    long partialRows = 0;
    long commonRows = 0;
    for (int k : keys1.keySet()) {
        if (Collections.disjoint(keys2.get(k), keys1.get(k))) {
            disjointRows++;
        } else if (keys2.get(k).containsAll(keys1.get(k))) {
            commonRows++;
        } else {
            partialRows++;
        }
    }
    Assert.assertEquals(commonCells, cellsInCommon.get());
    Assert.assertEquals(sourceOnlyCells, cellsOnlyInSource.get());
    Assert.assertEquals(disjointRows, rowsOnlyInSource.get());
    Assert.assertEquals(commonRows, rowsCompletelyInCommon.get());
    Assert.assertEquals(partialRows, rowsPartiallyInCommon.get());
    Assert.assertEquals(keys1.keySet().size(), rowsVisited.get());
}
Also used : DiffStats(com.palantir.atlasdb.table.common.TableTasks.DiffStats) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) AbstractTransactionTest(com.palantir.atlasdb.transaction.impl.AbstractTransactionTest) Test(org.junit.Test)

Aggregations

TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)1 DiffStats (com.palantir.atlasdb.table.common.TableTasks.DiffStats)1 AbstractTransactionTest (com.palantir.atlasdb.transaction.impl.AbstractTransactionTest)1 Random (java.util.Random)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Test (org.junit.Test)1