PersistentMap now requires only a path instead of a DiskStorage
This makes the PersistentMap easier to use.
This commit is contained in:
@@ -3,6 +3,7 @@ package org.lucares.pdb.map;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -16,7 +17,7 @@ import org.lucares.utils.Preconditions;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class PersistentMap<K, V> {
|
||||
public class PersistentMap<K, V> implements AutoCloseable{
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PersistentMap.class);
|
||||
|
||||
@@ -81,13 +82,18 @@ public class PersistentMap<K, V> {
|
||||
|
||||
private final EncoderDecoder<V> valueEncoder;
|
||||
|
||||
public PersistentMap(final DiskStorage diskStore, final EncoderDecoder<K> keyEncoder,
|
||||
public PersistentMap(final Path path, final EncoderDecoder<K> keyEncoder,
|
||||
final EncoderDecoder<V> valueEncoder) throws IOException {
|
||||
this.diskStore = diskStore;
|
||||
this.diskStore = new DiskStorage(path);
|
||||
this.keyEncoder = keyEncoder;
|
||||
this.valueEncoder = valueEncoder;
|
||||
initIfNew();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
diskStore.close();
|
||||
}
|
||||
|
||||
public void setMaxEntriesInNode(final int maxEntriesInNode) {
|
||||
this.maxEntriesInNode = maxEntriesInNode;
|
||||
|
||||
@@ -15,7 +15,6 @@ import java.util.Random;
|
||||
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;
|
||||
@@ -43,9 +42,8 @@ public class PersistentMapTest {
|
||||
final String value = "value1";
|
||||
final String key = "key1";
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
|
||||
Assert.assertNull(map.getValue(key));
|
||||
|
||||
@@ -53,9 +51,8 @@ public class PersistentMapTest {
|
||||
|
||||
Assert.assertEquals(map.getValue(key), value);
|
||||
}
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
|
||||
Assert.assertEquals(map.getValue(key), value);
|
||||
}
|
||||
@@ -67,9 +64,8 @@ public class PersistentMapTest {
|
||||
|
||||
final Random rnd = new Random(1);
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
map.setMaxEntriesInNode(2);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
@@ -102,9 +98,8 @@ public class PersistentMapTest {
|
||||
}
|
||||
}
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
// map.print(PersistentMap.STRING_DECODER, PersistentMap.STRING_DECODER);
|
||||
final AtomicInteger maxDepth = new AtomicInteger();
|
||||
map.visitNodeEntriesPreOrder(
|
||||
@@ -132,9 +127,8 @@ public class PersistentMapTest {
|
||||
final SecureRandom rnd = new SecureRandom();
|
||||
rnd.setSeed(1);
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<Long, Long> map = new PersistentMap<>(ds, PersistentMap.LONG_CODER,
|
||||
PersistentMap.LONG_CODER);
|
||||
try (final PersistentMap<Long, Long> map = new PersistentMap<>(file, PersistentMap.LONG_CODER,
|
||||
PersistentMap.LONG_CODER)) {
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
// System.out.println("\n\ninserting: " + i);
|
||||
@@ -165,9 +159,8 @@ public class PersistentMapTest {
|
||||
}
|
||||
}
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<Long, Long> map = new PersistentMap<>(ds, PersistentMap.LONG_CODER,
|
||||
PersistentMap.LONG_CODER);
|
||||
try (final PersistentMap<Long, Long> map = new PersistentMap<>(file, PersistentMap.LONG_CODER,
|
||||
PersistentMap.LONG_CODER)) {
|
||||
// map.print(PersistentMap.LONG_DECODER, PersistentMap.LONG_DECODER);
|
||||
final AtomicInteger counter = new AtomicInteger();
|
||||
map.visitNodeEntriesPreOrder(
|
||||
@@ -194,9 +187,8 @@ public class PersistentMapTest {
|
||||
|
||||
final Queue<Integer> numbers = new LinkedList<>(Arrays.asList(1, 15, 11, 4, 16, 3, 13));
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
|
||||
final int numbersSize = numbers.size();
|
||||
for (int i = 0; i < numbersSize; i++) {
|
||||
@@ -223,9 +215,8 @@ public class PersistentMapTest {
|
||||
}
|
||||
}
|
||||
|
||||
try (final DiskStorage ds = new DiskStorage(file)) {
|
||||
final PersistentMap<String, String> map = new PersistentMap<>(ds, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
// map.print(PersistentMap.STRING_DECODER, PersistentMap.STRING_DECODER);
|
||||
|
||||
final AtomicInteger counter = new AtomicInteger();
|
||||
@@ -258,16 +249,14 @@ public class PersistentMapTest {
|
||||
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);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, 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);
|
||||
try (final PersistentMap<String, String> map = new PersistentMap<>(file, PersistentMap.STRING_CODER,
|
||||
PersistentMap.STRING_CODER)) {
|
||||
|
||||
{
|
||||
final LinkedHashMap<String, String> actualBar = new LinkedHashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user