diff --git a/convex-core/src/main/java/convex/core/data/AHashMap.java b/convex-core/src/main/java/convex/core/data/AHashMap.java index 4472eb928..7e71cb0f6 100644 --- a/convex-core/src/main/java/convex/core/data/AHashMap.java +++ b/convex-core/src/main/java/convex/core/data/AHashMap.java @@ -175,6 +175,12 @@ public AVector getKeys() { return Vectors.wrap(keys); } + /** + * Gets the Hash for the first entry. Useful for prefix comparisons etc. + * @return + */ + protected abstract Hash getFirstHash(); + @Override public HashSet> entrySet() { int len = size(); diff --git a/convex-core/src/main/java/convex/core/data/MapLeaf.java b/convex-core/src/main/java/convex/core/data/MapLeaf.java index 80917042a..b19047e7d 100644 --- a/convex-core/src/main/java/convex/core/data/MapLeaf.java +++ b/convex-core/src/main/java/convex/core/data/MapLeaf.java @@ -758,4 +758,9 @@ public MapLeaf slice(long start, long end) { return new MapLeaf(nrefs); } + @Override + protected Hash getFirstHash() { + return entries[0].getKeyHash(); + } + } diff --git a/convex-core/src/main/java/convex/core/data/MapTree.java b/convex-core/src/main/java/convex/core/data/MapTree.java index df15f3556..db3131ebf 100644 --- a/convex-core/src/main/java/convex/core/data/MapTree.java +++ b/convex-core/src/main/java/convex/core/data/MapTree.java @@ -933,5 +933,14 @@ public boolean isCVMValue() { return true; } + // Cache of first hash, we don't want to descend tree repeatedly to find this + private Hash firstHash; + + @Override + protected Hash getFirstHash() { + if (firstHash==null) firstHash=children[0].getValue().getFirstHash(); + return firstHash; + } + }