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);
}));
}
Aggregations