Search in sources :

Example 1 with RandomPageReplacementPolicy

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

the class BlockRangeIndexBench method testHuge.

@Test
public // @Ignore
void testHuge() {
    final int testSize = 1_000_000;
    long _start = System.currentTimeMillis();
    BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(10000, new RandomPageReplacementPolicy(10000));
    for (int i = 0; i < testSize; i++) {
        index.put(Sized.valueOf(i), Sized.valueOf("test_" + i));
    }
    long _stop = System.currentTimeMillis();
    System.out.println("time w: " + (_stop - _start));
    System.out.println("num segments: " + index.getNumBlocks());
    for (int i = 0; i < testSize; i++) {
        index.search(Sized.valueOf(i));
    // index.lookUpRange(i, i + 1000);
    }
    _start = _stop;
    _stop = System.currentTimeMillis();
    System.out.println("time r: " + (_stop - _start));
    index.clear();
}
Also used : Sized(herddb.utils.Sized) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Test(org.junit.Test)

Example 2 with RandomPageReplacementPolicy

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

the class BlockRangeIndexConcurrentTest method testConcurrentReadsWritesDeletesWithSplits.

@Test
public void testConcurrentReadsWritesDeletesWithSplits() throws Exception {
    int testSize = 1000;
    int parallelism = 6;
    BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(3));
    ExecutorService threadpool = Executors.newFixedThreadPool(parallelism);
    CountDownLatch l = new CountDownLatch(testSize);
    ConcurrentLinkedQueue<Sized<String>> results = new ConcurrentLinkedQueue<>();
    ConcurrentLinkedQueue<Sized<String>> results2 = new ConcurrentLinkedQueue<>();
    try {
        for (int i = 0; i < testSize; i++) {
            int _i = i;
            threadpool.submit(() -> {
                try {
                    index.put(Sized.valueOf(_i), Sized.valueOf("a" + _i));
                    List<Sized<String>> search = index.search(Sized.valueOf(_i));
                    results.addAll(search);
                    if (search.isEmpty()) {
                        throw new IllegalStateException("Empty Search! i " + _i);
                    }
                    index.delete(Sized.valueOf(_i), Sized.valueOf("a" + _i));
                    List<Sized<String>> search2 = index.search(Sized.valueOf(_i));
                    results2.addAll(search2);
                    l.countDown();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            });
        }
    } finally {
        threadpool.shutdown();
    }
    assertTrue(l.await(10, TimeUnit.SECONDS));
    dumpIndex(index);
    verifyIndex(index);
    List<Sized<String>> result = index.lookUpRange(Sized.valueOf(0), Sized.valueOf(testSize + 1));
    assertTrue(result.isEmpty());
    System.out.println(results);
    for (int i = 0; i < testSize; i++) {
        assertTrue("cannot find a" + i, results.contains(Sized.valueOf("a" + i)));
    }
    assertTrue(results2.isEmpty());
}
Also used : Sized(herddb.utils.Sized) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) Test(org.junit.Test)

Example 3 with RandomPageReplacementPolicy

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

the class BlockRangeIndexConcurrentTest method testConcurrentReadsWritesWithSplits.

@Test
public void testConcurrentReadsWritesWithSplits() throws Exception {
    int testSize = 1000;
    int parallelism = 6;
    BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(3));
    ExecutorService threadpool = Executors.newFixedThreadPool(parallelism);
    CountDownLatch l = new CountDownLatch(testSize);
    ConcurrentLinkedQueue<Sized<String>> results = new ConcurrentLinkedQueue<>();
    try {
        for (int i = 0; i < testSize; i++) {
            int _i = i;
            threadpool.submit(() -> {
                try {
                    index.put(Sized.valueOf(_i), Sized.valueOf("a" + _i));
                    List<Sized<String>> search = index.search(Sized.valueOf(_i));
                    results.addAll(search);
                    if (search.isEmpty()) {
                        throw new IllegalStateException("Empty Search! i " + _i);
                    }
                    l.countDown();
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            });
        }
    } finally {
        threadpool.shutdown();
    }
    assertTrue(l.await(10, TimeUnit.SECONDS));
    dumpIndex(index);
    verifyIndex(index);
    List<Sized<String>> result = index.lookUpRange(Sized.valueOf(0), Sized.valueOf(testSize + 1));
    for (Sized<String> res : result) {
        System.out.println("res " + res.dummy);
    }
    for (int i = 0; i < testSize; i++) {
        assertTrue("cannot find " + i, index.containsKey(Sized.valueOf(i)));
        assertTrue("cannot find a" + i, results.contains(Sized.valueOf("a" + i)));
    }
}
Also used : Sized(herddb.utils.Sized) CountDownLatch(java.util.concurrent.CountDownLatch) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 4 with RandomPageReplacementPolicy

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

the class BlockRangeIndexStorageTest method testSimple.

@Test
public void testSimple() throws Exception {
    IndexDataStorage<Sized<Integer>, Sized<String>> storage = new IndexDataStorage<Sized<Integer>, Sized<String>>() {

        AtomicLong newPageId = new AtomicLong();

        private ConcurrentHashMap<Long, List<Map.Entry<Sized<Integer>, Sized<String>>>> pages = new ConcurrentHashMap<>();

        @Override
        public List<Map.Entry<Sized<Integer>, Sized<String>>> loadDataPage(long pageId) throws IOException {
            return pages.get(pageId);
        }

        @Override
        public long createDataPage(List<Map.Entry<Sized<Integer>, Sized<String>>> values) throws IOException {
            long newid = newPageId.incrementAndGet();
            pages.put(newid, values);
            return newid;
        }
    };
    BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(400, new RandomPageReplacementPolicy(10), storage);
    index.put(Sized.valueOf(1), Sized.valueOf("a"));
    index.put(Sized.valueOf(2), Sized.valueOf("b"));
    index.put(Sized.valueOf(3), Sized.valueOf("c"));
    BlockRangeIndexMetadata<Sized<Integer>> metadata = index.checkpoint();
    assertEquals(index.getNumBlocks(), metadata.getBlocksMetadata().size());
    BlockRangeIndex<Sized<Integer>, Sized<String>> indexAfterBoot = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(10), storage);
    indexAfterBoot.boot(metadata);
    assertEquals(Sized.valueOf("a"), index.search(Sized.valueOf(1)).get(0));
    assertEquals(Sized.valueOf("b"), index.search(Sized.valueOf(2)).get(0));
    assertEquals(Sized.valueOf("c"), index.search(Sized.valueOf(3)).get(0));
    assertEquals(2, indexAfterBoot.getNumBlocks());
}
Also used : Sized(herddb.utils.Sized) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 5 with RandomPageReplacementPolicy

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

the class BlockRangeIndexStorageTest method testUnload.

@Test
public void testUnload() throws Exception {
    IndexDataStorage<Sized<Integer>, Sized<String>> storage = new IndexDataStorage<Sized<Integer>, Sized<String>>() {

        AtomicLong newPageId = new AtomicLong();

        private ConcurrentHashMap<Long, List<Map.Entry<Sized<Integer>, Sized<String>>>> pages = new ConcurrentHashMap<>();

        @Override
        public List<Map.Entry<Sized<Integer>, Sized<String>>> loadDataPage(long pageId) throws IOException {
            return pages.get(pageId);
        }

        @Override
        public long createDataPage(List<Map.Entry<Sized<Integer>, Sized<String>>> values) throws IOException {
            long newid = newPageId.incrementAndGet();
            pages.put(newid, values);
            return newid;
        }
    };
    PageReplacementPolicy policy = new RandomPageReplacementPolicy(10);
    BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(1024, policy, storage);
    for (int i = 0; i < 100; i++) {
        index.put(Sized.valueOf(i), Sized.valueOf("a"));
    }
    BlockRangeIndexMetadata<Sized<Integer>> metadata = index.checkpoint();
    assertEquals(index.getNumBlocks(), metadata.getBlocksMetadata().size());
    for (int i = 0; i < 100; i++) {
        assertEquals(Sized.valueOf("a"), index.search(Sized.valueOf(i)).get(0));
    }
    assertEquals(10, policy.size());
    index.clear();
    /* No pages should remain in memory after unload!! */
    assertEquals(0, policy.size());
}
Also used : Sized(herddb.utils.Sized) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) PageReplacementPolicy(herddb.core.PageReplacementPolicy) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

RandomPageReplacementPolicy (herddb.core.RandomPageReplacementPolicy)26 Sized (herddb.utils.Sized)26 Test (org.junit.Test)25 AtomicLong (java.util.concurrent.atomic.AtomicLong)11 Holder (herddb.utils.Holder)5 Entry (java.util.Map.Entry)5 PageReplacementPolicy (herddb.core.PageReplacementPolicy)4 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2