Search in sources :

Example 11 with Sized

use of herddb.utils.Sized in project herddb by diennea.

the class BlockRangeIndexTest method testDelete.

@Test
public void testDelete() {
    BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(10));
    index.put(Sized.valueOf(3), Sized.valueOf("c"));
    index.put(Sized.valueOf(2), Sized.valueOf("b"));
    index.put(Sized.valueOf(1), Sized.valueOf("a"));
    index.delete(Sized.valueOf(1), Sized.valueOf("a"));
    assertTrue(index.search(Sized.valueOf(1)).isEmpty());
    assertEquals(Sized.valueOf("b"), index.search(Sized.valueOf(2)).get(0));
    assertEquals(Sized.valueOf("c"), index.search(Sized.valueOf(3)).get(0));
    index.delete(Sized.valueOf(2), Sized.valueOf("b"));
    assertTrue(index.search(Sized.valueOf(2)).isEmpty());
    assertEquals(Sized.valueOf("c"), index.search(Sized.valueOf(3)).get(0));
    index.delete(Sized.valueOf(3), Sized.valueOf("c"));
    assertTrue(index.search(Sized.valueOf(3)).isEmpty());
}
Also used : Sized(herddb.utils.Sized) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Test(org.junit.Test)

Example 12 with Sized

use of herddb.utils.Sized in project herddb by diennea.

the class BLinkTest method testScanHeadNotExistent.

@Test
public void testScanHeadNotExistent() 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 headNonExistent = 100;
        final long inserts = 100;
        for (long l = headNonExistent; l < inserts + headNonExistent; 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 = 0; l < headNonExistent - 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 13 with Sized

use of herddb.utils.Sized 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 14 with Sized

use of herddb.utils.Sized 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 15 with Sized

use of herddb.utils.Sized in project herddb by diennea.

the class ConcurrentUpdatesBLinkTest method concurrentUpdatesTests.

@Test
public void concurrentUpdatesTests() throws Exception {
    int DATA_SIZE = 50000;
    int THREADS = 4;
    int KEY_SIZE = 25;
    int ITERATIONS = 100000;
    BLinkTest.DummyBLinkIndexDataStorage<Sized<String>, Long> storage = new BLinkTest.DummyBLinkIndexDataStorage<>();
    try (BLink<Sized<String>, Long> blink = new BLink<>(2048L, new BLinkTest.StringSizeEvaluator(), new ClockProPolicy(30), storage)) {
        Random random = new Random();
        RandomString rs = new RandomString(random);
        ConcurrentHashMap<String, Long> expectedValues = new ConcurrentHashMap<>();
        List<String> keys = new ArrayList<>();
        for (int i = 0; i < DATA_SIZE; ++i) {
            String key = rs.nextString(KEY_SIZE);
            keys.add(key);
            long value = random.nextInt(Integer.MAX_VALUE) + 1;
            // zero means null
            assertTrue(value > 0);
            blink.insert(Sized.valueOf(key), value);
            expectedValues.put(key, value);
        }
        int numKeys = keys.size();
        ExecutorService threadpool = Executors.newFixedThreadPool(THREADS);
        System.out.println("generated " + numKeys + " keys");
        AtomicLong updates = new AtomicLong();
        AtomicLong inserts = new AtomicLong();
        AtomicLong skipped = new AtomicLong();
        AtomicLong deletes = new AtomicLong();
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < ITERATIONS; i++) {
            String key = keys.get(random.nextInt(numKeys));
            long value = random.nextLong();
            // 10 % deletes
            boolean delete = random.nextInt(100) < 10;
            futures.add(threadpool.submit(new Runnable() {

                @Override
                public void run() {
                    Long current = expectedValues.remove(key);
                    if (current == null) {
                        skipped.incrementAndGet();
                        return;
                    }
                    if (delete) {
                        blink.delete(Sized.valueOf(key));
                        expectedValues.put(key, 0L);
                        deletes.incrementAndGet();
                    } else {
                        blink.insert(Sized.valueOf(key), value);
                        if (current == 0L) {
                            inserts.incrementAndGet();
                        } else {
                            updates.incrementAndGet();
                        }
                        expectedValues.put(key, value);
                    }
                }
            }));
        }
        int progress = 0;
        for (Future f : futures) {
            f.get();
            if (++progress % 10000 == 0) {
                System.out.println("done " + progress + "/" + ITERATIONS);
            }
        }
        int nulls = 0;
        for (String key : keys) {
            Long value = blink.search(Sized.valueOf(key));
            Long expected = expectedValues.get(key);
            if (expected == 0) {
                assertNull(value);
                nulls++;
            } else {
                assertEquals(expected, value);
            }
        }
        System.out.println("total swapin " + storage.swapIn);
        System.out.println("inserts " + inserts);
        System.out.println("updates " + updates);
        System.out.println("skipped " + skipped);
        System.out.println("deletes " + deletes);
        System.out.println("iterations " + ITERATIONS + " (" + (inserts.intValue() + updates.intValue() + deletes.intValue() + skipped.intValue()) + ")");
        System.out.println("nulls " + nulls);
        threadpool.shutdown();
    }
}
Also used : Sized(herddb.utils.Sized) RandomString(herddb.utils.RandomString) ArrayList(java.util.ArrayList) RandomString(herddb.utils.RandomString) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ClockProPolicy(herddb.core.ClockProPolicy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

Sized (herddb.utils.Sized)28 RandomPageReplacementPolicy (herddb.core.RandomPageReplacementPolicy)26 Test (org.junit.Test)26 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 Holder (herddb.utils.Holder)5 Entry (java.util.Map.Entry)5 PageReplacementPolicy (herddb.core.PageReplacementPolicy)4 ExecutorService (java.util.concurrent.ExecutorService)4 List (java.util.List)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Map (java.util.Map)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 ClockProPolicy (herddb.core.ClockProPolicy)1 Block (herddb.index.brin.BlockRangeIndex.Block)1 RandomString (herddb.utils.RandomString)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Future (java.util.concurrent.Future)1