PersistentMap can store data in multiple nodes
This commit is contained in:
@@ -81,4 +81,29 @@ public class VariableByteEncoderTest {
|
||||
final LongList decodedValues = VariableByteEncoder.decode(buffer);
|
||||
Assert.assertEquals(decodedValues, originalValues);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] providerNededBytes() {
|
||||
return new Object[][] { //
|
||||
{ 0, 1 }, //
|
||||
{ -10, 1 }, //
|
||||
{ 10, 1 }, //
|
||||
{ -63, 1 }, //
|
||||
{ 63, 1 }, //
|
||||
{ -64, 2 }, //
|
||||
{ 64, 2 }, //
|
||||
{ -8191, 2 }, //
|
||||
{ 8191, 2 }, //
|
||||
{ -8192, 3 }, //
|
||||
{ 8192, 3 }, //
|
||||
};
|
||||
}
|
||||
|
||||
@Test(dataProvider = "providerNededBytes")
|
||||
public void testNeededBytes(final long value, final int expectedNeededBytes) {
|
||||
|
||||
final int neededBytes = VariableByteEncoder.neededBytes(value);
|
||||
final byte[] encoded = VariableByteEncoder.encode(value);
|
||||
Assert.assertEquals(encoded.length, neededBytes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package org.lucares.pdb.map;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.lucares.pdb.diskstorage.DiskStorage;
|
||||
import org.lucares.utils.file.FileUtils;
|
||||
@@ -38,9 +40,51 @@ public class PersistentMapTest {
|
||||
Assert.assertNull(map.getAsString(key));
|
||||
|
||||
Assert.assertNull(map.put(key, value));
|
||||
final String actualValue = map.getAsString(key);
|
||||
|
||||
Assert.assertEquals(actualValue, value);
|
||||
Assert.assertEquals(map.getAsString(key), value);
|
||||
}
|
||||
}
|
||||
|
||||
public void testManyValues() throws Exception {
|
||||
final Path file = dataDirectory.resolve("map.db");
|
||||
final var insertedValues = new HashMap<String, String>();
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap map = new PersistentMap(ds);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
|
||||
final String key = UUID.randomUUID().toString() + "__" + i;
|
||||
final String value = "long value to waste some bytes " + i;
|
||||
Assert.assertNull(map.getAsString(key));
|
||||
|
||||
Assert.assertNull(map.put(key, value));
|
||||
|
||||
insertedValues.put(key, value);
|
||||
|
||||
for (final var entry : insertedValues.entrySet()) {
|
||||
final String actualValue = map.getAsString(entry.getKey());
|
||||
Assert.assertEquals(actualValue, entry.getValue(),
|
||||
"value for key " + entry.getKey() + " in the " + i + "th iteration");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap map = new PersistentMap(ds);
|
||||
map.visitPreOrder((nodeEntry, depth) -> {
|
||||
if (nodeEntry.isInnerNode()) {
|
||||
System.out.println(" ".repeat(depth) + nodeEntry);
|
||||
}
|
||||
});
|
||||
|
||||
for (final var entry : insertedValues.entrySet()) {
|
||||
final String actualValue = map.getAsString(entry.getKey());
|
||||
Assert.assertEquals(actualValue, entry.getValue(),
|
||||
"value for key " + entry.getKey() + " after all iterations");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user