add visitor that find all values by a prefix of the key
This commit is contained in:
@@ -1,7 +1,37 @@
|
||||
package org.lucares.pdb.map;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.lucares.pdb.map.NodeEntry.ValueType;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class NodeEntryTest {
|
||||
@DataProvider
|
||||
public Object[][] providerPrefixCompare() {
|
||||
final List<Object[]> result = new ArrayList<>();
|
||||
|
||||
result.add(new Object[] { "ab", "abc", -1 });
|
||||
result.add(new Object[] { "abb", "abc", -1 });
|
||||
result.add(new Object[] { "abc", "abc", 0 });
|
||||
result.add(new Object[] { "abcd", "abc", 0 });
|
||||
result.add(new Object[] { "abd", "abc", 1 });
|
||||
result.add(new Object[] { "abz", "abc", 23 });
|
||||
|
||||
return result.toArray(Object[][]::new);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "providerPrefixCompare")
|
||||
public void testPrefixCompare(final String key, final String prefix, final int expected) {
|
||||
|
||||
final NodeEntry nodeEntry = new NodeEntry(ValueType.NODE_POINTER, key.getBytes(StandardCharsets.UTF_8),
|
||||
new byte[0]);
|
||||
|
||||
final int actual = nodeEntry.compareKeyPrefix(prefix.getBytes(StandardCharsets.UTF_8));
|
||||
Assert.assertEquals(actual, expected, key + " ? " + prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import java.nio.file.Path;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Queue;
|
||||
import java.util.Random;
|
||||
@@ -14,6 +16,7 @@ import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.lucares.pdb.diskstorage.DiskStorage;
|
||||
import org.lucares.pdb.map.PersistentMap.Visitor;
|
||||
import org.lucares.utils.file.FileUtils;
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
@@ -238,4 +241,42 @@ public class PersistentMapTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAllByPrefix() throws Exception {
|
||||
final Path file = dataDirectory.resolve("map.db");
|
||||
|
||||
final Map<String, String> expectedBar = new HashMap<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
// the value is a little bit longer to make sure that the values don't fit into
|
||||
// a single leaf node
|
||||
expectedBar.put("bar:" + i, "bar:" + i + "__##################################");
|
||||
}
|
||||
|
||||
final Map<String, String> input = new HashMap<>();
|
||||
input.putAll(expectedBar);
|
||||
for (int i = 0; i < 500; i++) {
|
||||
input.put(UUID.randomUUID().toString(), UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
|
||||
map.putAllValues(input);
|
||||
}
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
|
||||
{
|
||||
final LinkedHashMap<String, String> actualBar = new LinkedHashMap<>();
|
||||
final Visitor<String, String> visitor = (key, value) -> actualBar.put(key, value);
|
||||
map.visitValues("bar:", visitor);
|
||||
|
||||
Assert.assertEquals(actualBar, expectedBar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user