Fix to string for maps with values of type Empty

The MAX_KEY inserted into the tree had a value of one byte. This
triggered an assertion for maps with values of type Empty, because they
expected values to be empty.
Fixed by using an empty array for the value of the MAX_KEY.
This commit is contained in:
2019-08-12 08:35:40 +02:00
parent 427d7818bf
commit 0b3eb97b96
7 changed files with 103 additions and 12 deletions

View File

@@ -48,6 +48,11 @@ public final class ByteArrayKey implements Comparable<ByteArrayKey> {
public static boolean equal(final byte[] key, final byte[] otherKey) {
return compare(key, otherKey) == 0;
}
@Override
public String toString() {
return Arrays.toString(bytes);
}
@Override
public int hashCode() {

View File

@@ -81,15 +81,15 @@ class NodeEntry {
+ valueAsString + "]";
}
public String toString(final Function<byte[], String> keyDecoder, final Function<byte[], String> valueDecoder) {
public <K,V> String toString(final Function<byte[], K> keyDecoder, final Function<byte[], V> valueDecoder) {
final String valueAsString = isInnerNode() ? String.valueOf(VariableByteEncoder.decodeFirstValue(value))
: valueDecoder.apply(value);
: String.valueOf(valueDecoder.apply(value));
final String keyAsString;
if (Arrays.equals(key, PersistentMap.MAX_KEY)) {
keyAsString = "<<<MAX_KEY>>>";
} else {
keyAsString = keyDecoder.apply(key);
keyAsString = String.valueOf(keyDecoder.apply(key));
}
return "NodeEntry [type=" + type + ", key=" + keyAsString + ", value=" + valueAsString + "]";

View File

@@ -7,6 +7,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.Objects;
import java.util.Stack;
import java.util.UUID;
@@ -39,6 +40,14 @@ public class PersistentMap<K, V> implements AutoCloseable {
public byte[] encode(O object);
public O decode(byte[] bytes);
public default Function<byte[], O> asDecoder() {
return bytes -> this.decode(bytes);
}
public default Function<O,byte[]> asEncoder() {
return plain -> this.encode(plain);
}
}
private static final class StringCoder implements EncoderDecoder<String> {
@@ -160,7 +169,7 @@ public class PersistentMap<K, V> implements AutoCloseable {
writeNodeOffsetOfRootNode(blockOffset);
// 5. insert a dummy entry with a 'maximum' key
putValue(MAX_KEY, new byte[] { 0 });
putValue(MAX_KEY, new byte[] { });
}
}
@@ -357,7 +366,9 @@ public class PersistentMap<K, V> implements AutoCloseable {
}
private void writeNode(final PersistentMapDiskNode node) {
LOGGER.trace("writing node {}", node);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("writing node {}", node.toString(keyEncoder.asDecoder(), valueEncoder.asDecoder()));
}
final long nodeOffest = node.getNodeOffset();
// final DiskBlock diskBlock = diskStore.getDiskBlock(nodeOffest, BLOCK_SIZE);
DiskBlock diskBlock = node.getDiskBlock();

View File

@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.lucares.collections.LongList;
@@ -200,6 +201,20 @@ public class PersistentMapDiskNode {
return "@" + nodeOffset + ": "
+ String.join("\n", entries.values().stream().map(NodeEntry::toString).collect(Collectors.toList()));
}
public <K,V> String toString(Function<byte[], K> keyDecoder, Function<byte[], V> valueDecoder) {
StringBuilder result = new StringBuilder();
result.append("@");
result.append(nodeOffset);
result.append(": ");
for (NodeEntry e : entries.values()) {
String s = e.toString(keyDecoder, valueDecoder);
result.append("\n");
result.append(s);
}
return result.toString();
}
public NodeEntry getTopNodeEntry() {
return entries.lastEntry().getValue();