insert values into root node

This commit is contained in:
2018-10-14 19:53:02 +02:00
parent 3855d03ead
commit bb4514c940
2 changed files with 15 additions and 9 deletions

View File

@@ -35,8 +35,9 @@ public class PersistentMap {
} }
} }
public void put(final String key, final long value) throws IOException { public Long put(final String key, final long value) throws IOException {
put(key.getBytes(UTF8), VariableByteEncoder.encode(value)); final byte[] oldValue = put(key.getBytes(UTF8), VariableByteEncoder.encode(value));
return oldValue == null ? null : VariableByteEncoder.decodeFirstValue(oldValue);
} }
public long getAsLong(final String key) throws IOException { public long getAsLong(final String key) throws IOException {
@@ -44,8 +45,9 @@ public class PersistentMap {
return buffer == null ? null : VariableByteEncoder.decodeFirstValue(buffer); return buffer == null ? null : VariableByteEncoder.decodeFirstValue(buffer);
} }
public void put(final String key, final String value) throws IOException { public String put(final String key, final String value) throws IOException {
put(key.getBytes(UTF8), value.getBytes(UTF8)); final byte[] oldValue = put(key.getBytes(UTF8), value.getBytes(UTF8));
return oldValue == null ? null : new String(oldValue, UTF8);
} }
public String getAsString(final String key) throws IOException { public String getAsString(final String key) throws IOException {

View File

@@ -26,17 +26,21 @@ public class PersistentMapTest {
FileUtils.delete(dataDirectory); FileUtils.delete(dataDirectory);
} }
public void test() throws Exception { public void testSingleValue() throws Exception {
final Path file = dataDirectory.resolve("map.db"); final Path file = dataDirectory.resolve("map.db");
try (final DiskStorage ds = new DiskStorage(file)) { try (final DiskStorage ds = new DiskStorage(file)) {
final PersistentMap map = new PersistentMap(ds); final PersistentMap map = new PersistentMap(ds);
final String value1 = "value1"; final String value = "value1";
map.put("key1", value1); final String key = "key1";
final String actualValue1 = map.getAsString("key1");
Assert.assertEquals(actualValue1, value1); Assert.assertNull(map.getAsString(key));
Assert.assertNull(map.put(key, value));
final String actualValue = map.getAsString(key);
Assert.assertEquals(actualValue, value);
} }
} }
} }