Search in sources :

Example 16 with RandomPageReplacementPolicy

use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.

the class BLinkTest method testUnknownSizeAndRestore.

@Test
public void testUnknownSizeAndRestore() throws Exception {
    String[] data = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    StringSizeEvaluator evaluator = new StringSizeEvaluator();
    for (String d : data) {
        assertTrue(Sized.valueOf(d).compareTo(evaluator.getPosiviveInfinityKey()) < 0);
    }
    BLinkIndexDataStorage<Sized<String>, Long> storage = new DummyBLinkIndexDataStorage<>();
    BLinkMetadata<Sized<String>> metadata;
    try (BLink<Sized<String>, Long> blink = new BLink<>(2048L, new StringSizeEvaluator(), new RandomPageReplacementPolicy(3), storage)) {
        for (int i = 0; i < data.length; ++i) {
            blink.insert(Sized.valueOf(data[i]), i + 1L);
        }
        assertEquals(data.length, blink.size());
        metadata = blink.checkpoint();
    }
    /* Reset each node size to unknown */
    List<BLinkNodeMetadata<Sized<String>>> unknownSizeNodes = new ArrayList<>(metadata.nodes.size());
    for (BLinkNodeMetadata<Sized<String>> node : metadata.nodes) {
        unknownSizeNodes.add(new BLinkNodeMetadata<>(node.leaf, node.id, node.storeId, node.keys, BLink.UNKNOWN_SIZE, node.outlink, node.rightlink, node.rightsep));
    }
    BLinkMetadata<Sized<String>> unknownSizeMetadata = new BLinkMetadata<>(metadata.nextID, metadata.fast, metadata.fastheight, metadata.top, metadata.topheight, metadata.first, metadata.values, unknownSizeNodes);
    /* Checks that node size has been changed for each node */
    for (int i = 0; i < metadata.nodes.size(); ++i) {
        BLinkNodeMetadata<Sized<String>> node = metadata.nodes.get(i);
        BLinkNodeMetadata<Sized<String>> unknownSizeNode = unknownSizeMetadata.nodes.get(i);
        assertNotNull(unknownSizeNode);
        assertNotEquals(node.bytes, unknownSizeNode.bytes);
    }
    BLinkMetadata<Sized<String>> rebuildMetadata;
    try (BLink<Sized<String>, Long> blinkFromMeta = new BLink<>(2048L, new StringSizeEvaluator(), new RandomPageReplacementPolicy(3), storage, unknownSizeMetadata)) {
        /* Require at least two nodes! */
        assertNotEquals(1, metadata.nodes.size());
        for (int i = 0; i < data.length; ++i) {
            assertEquals(i + 1L, (long) blinkFromMeta.search(Sized.valueOf(data[i])));
        }
        assertEquals(data.length, blinkFromMeta.size());
        rebuildMetadata = blinkFromMeta.checkpoint();
    }
    /* Checks that node size has been restored for each node */
    for (int i = 0; i < metadata.nodes.size(); ++i) {
        BLinkNodeMetadata<Sized<String>> node = metadata.nodes.get(i);
        BLinkNodeMetadata<Sized<String>> rebuildNode = rebuildMetadata.nodes.get(i);
        assertNotNull(rebuildNode);
        assertEquals(node.bytes, rebuildNode.bytes);
    }
}
Also used : Sized(herddb.utils.Sized) BLinkNodeMetadata(herddb.index.blink.BLinkMetadata.BLinkNodeMetadata) ArrayList(java.util.ArrayList) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 17 with RandomPageReplacementPolicy

use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.

the class BLinkTest method testScanTailNotExistent.

@Test
public void testScanTailNotExistent() throws Exception {
    BLinkIndexDataStorage<Sized<Long>, Long> storage = new DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<Long>, Long> blink = new BLink<>(2048L, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage)) {
        final long inserts = 100;
        final long tailNonExistent = 100;
        for (long l = 0; l < inserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        BLinkMetadata<Sized<Long>> metadata = blink.checkpoint();
        /* Require at least two nodes! */
        assertNotEquals(1, metadata.nodes.size());
        long offset = 10;
        for (long l = inserts; l < tailNonExistent - offset; l++) {
            Stream<Entry<Sized<Long>, Long>> stream = blink.scan(Sized.valueOf(l), Sized.valueOf(l + offset));
            Holder<Long> h = new Holder<>(l);
            Holder<Long> count = new Holder<>(0L);
            StringBuilder builder = new StringBuilder();
            /* Check each value */
            stream.forEach(entry -> {
                assertEquals(h.value, entry.getValue());
                h.value++;
                count.value++;
                builder.append(entry.getValue()).append(", ");
            });
            assertEquals(0, (long) count.value);
        }
    }
}
Also used : Sized(herddb.utils.Sized) Holder(herddb.utils.Holder) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Entry(java.util.Map.Entry) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 18 with RandomPageReplacementPolicy

use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.

the class BLinkTest method testScanDotNotExistent.

@Test
public void testScanDotNotExistent() throws Exception {
    BLinkIndexDataStorage<Sized<Long>, Long> storage = new DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<Long>, Long> blink = new BLink<>(2048L, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage)) {
        final long headInserts = 100;
        final long nonExistents = 10;
        final long tailInserts = 100;
        for (long l = 0; l < headInserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        for (long l = headInserts + nonExistents; l < headInserts + nonExistents + tailInserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        BLinkMetadata<Sized<Long>> metadata = blink.checkpoint();
        /* Require at least two nodes! */
        assertNotEquals(1, metadata.nodes.size());
        long offset = 100;
        for (long l = nonExistents; l < headInserts + nonExistents - offset; l++) {
            Stream<Entry<Sized<Long>, Long>> stream = blink.scan(Sized.valueOf(l), Sized.valueOf(l + offset));
            Holder<Long> h = new Holder<>(l);
            Holder<Long> count = new Holder<>(0L);
            StringBuilder builder = new StringBuilder();
            /* Check each value */
            stream.forEach(entry -> {
                assertEquals(h.value, entry.getValue());
                h.value++;
                count.value++;
                builder.append(entry.getValue()).append(", ");
            });
            builder.setLength(builder.length() - 2);
            System.out.println("start " + l + " end " + (l + offset) + " -> " + builder);
            assertEquals(offset - nonExistents, (long) count.value);
        }
    }
}
Also used : Sized(herddb.utils.Sized) Holder(herddb.utils.Holder) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Entry(java.util.Map.Entry) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 19 with RandomPageReplacementPolicy

use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.

the class BLinkTest method testDelete.

@Test
public void testDelete() throws Exception {
    final long inserts = 100000;
    BLinkIndexDataStorage<Sized<Long>, Long> storage = new DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<Long>, Long> blink = new BLink<>(2048L, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage)) {
        for (long l = 0; l < inserts; l++) {
            blink.insert(Sized.valueOf(l), l);
        }
        BLinkMetadata<Sized<Long>> metadata = blink.checkpoint();
        /* Require at least two nodes! */
        assertNotEquals(1, metadata.nodes.size());
        for (long l = 0; l < inserts; l++) {
            assertEquals(l, (long) blink.delete(Sized.valueOf(l)));
        }
        for (long l = 0; l < inserts; l++) {
            assertEquals(null, blink.search(Sized.valueOf(l)));
        }
    }
}
Also used : Sized(herddb.utils.Sized) AtomicLong(java.util.concurrent.atomic.AtomicLong) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Test(org.junit.Test)

Example 20 with RandomPageReplacementPolicy

use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.

the class BLinkTest method testDataSet.

private void testDataSet(List<Long> data, int maxSize) throws Exception {
    System.out.println("testDataSet " + data.size() + " maxSize:" + maxSize + ": " + data);
    BLinkIndexDataStorage<Sized<Long>, Long> storage = new DummyBLinkIndexDataStorage<>();
    BLinkMetadata<Sized<Long>> metadata;
    try (BLink<Sized<Long>, Long> blink = new BLink<>(maxSize, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage)) {
        for (long l : data) {
            blink.insert(Sized.valueOf(l), l);
        }
        for (long l : data) {
            assertEquals(l, (long) blink.search(Sized.valueOf(l)));
        }
        metadata = blink.checkpoint();
        System.out.println("metadata:" + metadata);
        metadata.nodes.forEach(n -> {
            System.out.println("node:" + n + " rightsep:" + n.rightsep);
        });
    }
    // reboot
    try (BLink<Sized<Long>, Long> blink = new BLink<>(maxSize, new LongSizeEvaluator(), new RandomPageReplacementPolicy(10), storage, metadata)) {
        for (long l : data) {
            assertEquals(l, (long) blink.search(Sized.valueOf(l)));
        }
    }
}
Also used : Sized(herddb.utils.Sized) AtomicLong(java.util.concurrent.atomic.AtomicLong) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy)

Aggregations

RandomPageReplacementPolicy (herddb.core.RandomPageReplacementPolicy)35 Sized (herddb.utils.Sized)35 Test (org.junit.Test)34 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 PageReplacementPolicy (herddb.core.PageReplacementPolicy)8 Entry (java.util.Map.Entry)7 List (java.util.List)6 Holder (herddb.utils.Holder)5 ArrayList (java.util.ArrayList)4 ExecutionException (java.util.concurrent.ExecutionException)3 ExecutorService (java.util.concurrent.ExecutorService)3 Block (herddb.index.brin.BlockRangeIndex.Block)2 Random (java.util.Random)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Future (java.util.concurrent.Future)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 BLinkNodeMetadata (herddb.index.blink.BLinkMetadata.BLinkNodeMetadata)1