Search in sources :

Example 1 with BlockStartKey

use of herddb.index.brin.BlockRangeIndex.BlockStartKey in project herddb by diennea.

the class BlockRangeIndexStorageTest method testReloadAfterBlockDeletion.

@Test
public void testReloadAfterBlockDeletion() throws Exception {
    PageReplacementPolicy policy = new RandomPageReplacementPolicy(10);
    IndexDataStorage<Sized<Integer>, Sized<Integer>> storage = new MemoryIndexDataStorage<>();
    BlockRangeIndex<Sized<Integer>, Sized<Integer>> index = new BlockRangeIndex<>(400, policy, storage);
    index.boot(BlockRangeIndexMetadata.empty());
    int i = 0;
    do {
        Sized<Integer> si = Sized.valueOf(i++);
        index.put(si, si);
    } while (index.getNumBlocks() < 4);
    /* First checkpoint after insertion */
    index.checkpoint();
    /* Now we empty middle blocks */
    /* Map without first key */
    ConcurrentNavigableMap<BlockStartKey<Sized<Integer>>, Block<Sized<Integer>, Sized<Integer>>> sub = index.getBlocks().tailMap(index.getBlocks().firstKey(), false);
    /* Second block */
    Block<Sized<Integer>, Sized<Integer>> second = sub.firstEntry().getValue();
    /* Map without second key */
    sub = sub.tailMap(sub.firstKey(), false);
    /* Third block */
    Block<Sized<Integer>, Sized<Integer>> third = sub.firstEntry().getValue();
    /* Copy to avoid concurrent modification */
    List<Entry<Sized<Integer>, Sized<Integer>>> toDelete = new ArrayList<>();
    second.values.forEach((k, vl) -> vl.forEach(v -> toDelete.add(new SimpleEntry<>(k, v))));
    third.values.forEach((k, vl) -> vl.forEach(v -> toDelete.add(new SimpleEntry<>(k, v))));
    /* Delete blocks 2 and 3 */
    toDelete.forEach(e -> index.delete(e.getKey(), e.getValue()));
    /* Checkpoint, should remove a block */
    BlockRangeIndexMetadata<Sized<Integer>> metadata = index.checkpoint();
    assertEquals(2, index.getNumBlocks());
    assertEquals(index.getNumBlocks(), metadata.getBlocksMetadata().size());
    /* Load a new index from data */
    BlockRangeIndex<Sized<Integer>, Sized<Integer>> indexAfterBoot = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(10), storage);
    indexAfterBoot.boot(metadata);
    /* Check data equality between new and old index */
    index.getBlocks().forEach((f, b) -> b.values.forEach((k, vl) -> {
        List<Sized<Integer>> search = indexAfterBoot.search(k);
        Assert.assertEquals(vl, search);
    }));
}
Also used : Sized(herddb.utils.Sized) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) BlockStartKey(herddb.index.brin.BlockRangeIndex.BlockStartKey) ArrayList(java.util.ArrayList) Sized(herddb.utils.Sized) Block(herddb.index.brin.BlockRangeIndex.Block) List(java.util.List) Entry(java.util.Map.Entry) Assert(org.junit.Assert) PageReplacementPolicy(herddb.core.PageReplacementPolicy) SimpleEntry(java.util.AbstractMap.SimpleEntry) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) PageReplacementPolicy(herddb.core.PageReplacementPolicy) RandomPageReplacementPolicy(herddb.core.RandomPageReplacementPolicy) BlockStartKey(herddb.index.brin.BlockRangeIndex.BlockStartKey) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) Block(herddb.index.brin.BlockRangeIndex.Block) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

PageReplacementPolicy (herddb.core.PageReplacementPolicy)1 RandomPageReplacementPolicy (herddb.core.RandomPageReplacementPolicy)1 Block (herddb.index.brin.BlockRangeIndex.Block)1 BlockStartKey (herddb.index.brin.BlockRangeIndex.BlockStartKey)1 Sized (herddb.utils.Sized)1 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Test (org.junit.Test)1