use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.
the class BlockRangeIndexTest method testManySegments.
@Test
public void testManySegments() {
BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(10));
index.boot(BlockRangeIndexMetadata.empty());
for (int i = 0; i < 20; i++) {
index.put(Sized.valueOf(i), Sized.valueOf("test_" + i));
}
List<Sized<String>> result = index.search(Sized.valueOf(2), Sized.valueOf(10));
System.out.println("result_" + result);
for (int i = 2; i <= 10; i++) {
assertTrue(result.contains(Sized.valueOf("test_" + i)));
}
assertEquals(9, result.size());
}
use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.
the class BlockRangeIndexTest method testSelfLoop.
/**
* This test was generating a block self relationship
*
* @see GitHub: https://github.com/diennea/herddb/pull/443
*/
@Test
public void testSelfLoop() throws IOException {
Random random = new Random(2431);
BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(400, new RandomPageReplacementPolicy(10, random));
index.boot(BlockRangeIndexMetadata.empty());
int numCheckpoints = 0;
int i = 0;
try {
for (i = 0; i < 60; i++) {
byte[] s = new byte[10];
random.nextBytes(s);
index.put(Sized.valueOf(random.nextInt(200)), Sized.valueOf(new String(s, StandardCharsets.US_ASCII)));
if (random.nextInt(10) == 3) {
index.checkpoint();
numCheckpoints++;
}
if (i % 1000 == 0) {
System.out.println("DONE " + i + " - " + numCheckpoints + " checkpoints");
}
}
} finally {
System.out.println("DONE " + i + " - " + numCheckpoints + " checkpoints");
}
}
use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.
the class BlockRangeIndexTest method testSplitBlockStartKeySorting.
/**
* Test that after split order imposed by BRIN outer tree {@link BlockRangeIndex#blocks} is the same order
* imposed by {@link BlockRangeIndex.Block#next} relationship.
*
* @see GitHub: https://github.com/diennea/herddb/pull/443
*/
@Test
public void testSplitBlockStartKeySorting() throws IOException {
/**
* Expected BRIN tree
* <pre>
* +-----------------+
* | MinKey: 1 |
* ... -> | BlockId: 1 | -> ...
* | Keys: 1, 2, 2 |
* | Occupancy: 100% |
* +-----------------+
*
* Adding key 2 with next block id 2: create a new block (2,-2)
*
* +-----------------+ +-----------------+
* | MinKey: 1 | | MinKey: 2 |
* ... -> | BlockId: 1 | -> | BlockId: -2 | -> ...
* | Keys: 1, 2 | | Keys: 2, 2 |
* | Occupancy: 66% | | Occupancy: 66% |
* +-----------------+ +-----------------+
*
* Adding key 2 with next block id 3: add to block (2,-2)
*
* +-----------------+ +-----------------+
* | MinKey: 1 | | MinKey: 2 |
* ... -> | BlockId: 1 | -> | BlockId: -2 | -> ...
* | Keys: 1, 2 | | Keys: 2, 2, 2 |
* | Occupancy: 66% | | Occupancy: 100% |
* +-----------------+ +-----------------+
*
* Adding key 2 with next block id 3: create a new block (2,3)
*
* +-----------------+ +-----------------+ +-----------------+
* | MinKey: 1 | | MinKey: 2 | | MinKey: 2 |
* ... -> | BlockId: 1 | -> | BlockId: -2 | -> | BlockId: 3 | -> ...
* | Keys: 1, 2 | | Keys: 2, 2 | | Keys: 2, 2 |
* | Occupancy: 66% | | Occupancy: 66% | | Occupancy: 66% |
* +-----------------+ +-----------------+ +-----------------+
* </pre>
*/
BlockRangeIndex<Sized<Integer>, Sized<Integer>> index = new BlockRangeIndex<>(450, new RandomPageReplacementPolicy(1000));
index.boot(BlockRangeIndexMetadata.empty());
Sized<Integer> s1 = Sized.valueOf(1);
Sized<Integer> s2 = Sized.valueOf(2);
index.put(s1, s1);
index.put(s2, s2);
index.put(s2, s2);
assertEquals(1, index.getNumBlocks());
Block<Sized<Integer>, Sized<Integer>> block1 = index.getBlocks().firstEntry().getValue();
block1.ensureBlockLoaded();
assertTrue(block1.values.containsKey(s1));
assertTrue(block1.values.containsKey(s2));
assertEquals(Arrays.asList(s1), block1.values.get(s1));
assertEquals(Arrays.asList(s2, s2), block1.values.get(s2));
index.put(s2, s2);
assertEquals(2, index.getNumBlocks());
Block<Sized<Integer>, Sized<Integer>> block2 = index.getBlocks().lastEntry().getValue();
assertSame(block2, block1.next);
assertTrue(block2.key.blockId < 0);
assertTrue(block1.key.blockId > block2.key.blockId);
assertTrue(block1.key.compareTo(block2.key) < 0);
block1.ensureBlockLoaded();
assertTrue(block1.values.containsKey(s1));
assertTrue(block1.values.containsKey(s2));
assertEquals(Arrays.asList(s1), block1.values.get(s1));
assertEquals(Arrays.asList(s2), block1.values.get(s2));
block2.ensureBlockLoaded();
assertFalse(block2.values.containsKey(s1));
assertTrue(block2.values.containsKey(s2));
assertEquals(Arrays.asList(s2, s2), block2.values.get(s2));
index.put(s2, s2);
assertEquals(2, index.getNumBlocks());
block2.ensureBlockLoaded();
assertTrue(block2.values.containsKey(s2));
assertEquals(Arrays.asList(s2, s2, s2), block2.values.get(s2));
index.put(s2, s2);
assertEquals(3, index.getNumBlocks());
Block<Sized<Integer>, Sized<Integer>> block3 = index.getBlocks().lastEntry().getValue();
assertSame(block3, block2.next);
assertTrue(block3.key.blockId > 0);
assertTrue(block2.key.blockId < block3.key.blockId);
assertTrue(block2.key.compareTo(block3.key) < 0);
block2.ensureBlockLoaded();
assertFalse(block2.values.containsKey(s1));
assertTrue(block2.values.containsKey(s2));
assertEquals(Arrays.asList(s2, s2), block2.values.get(s2));
block3.ensureBlockLoaded();
assertFalse(block3.values.containsKey(s1));
assertTrue(block3.values.containsKey(s2));
assertEquals(Arrays.asList(s2, s2), block3.values.get(s2));
}
use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.
the class BlockRangeIndexTest method testUnboundedSearch.
@Test
public void testUnboundedSearch() {
BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(1024, new RandomPageReplacementPolicy(10));
index.boot(BlockRangeIndexMetadata.empty());
index.put(Sized.valueOf(1), Sized.valueOf("a"));
index.put(Sized.valueOf(2), Sized.valueOf("b"));
index.put(Sized.valueOf(3), Sized.valueOf("c"));
assertEquals(3, index.search(Sized.valueOf(1), null).size());
assertEquals(2, index.search(null, Sized.valueOf(2)).size());
}
use of herddb.core.RandomPageReplacementPolicy in project herddb by diennea.
the class BlockRangeIndexTest method testSimpleSplit.
@Test
public void testSimpleSplit() {
BlockRangeIndex<Sized<Integer>, Sized<String>> index = new BlockRangeIndex<>(400, new RandomPageReplacementPolicy(10));
index.boot(BlockRangeIndexMetadata.empty());
index.put(Sized.valueOf(1), Sized.valueOf("a"));
index.put(Sized.valueOf(2), Sized.valueOf("b"));
index.put(Sized.valueOf(3), Sized.valueOf("c"));
dumpIndex(index);
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, index.getNumBlocks());
}
Aggregations