Repository navigation
Description
Background
#5677 made a HASH index able to key on a LINK (a LINK property, or an edge type's @out/@in). The fix stored
those keys as TYPE_COMPRESSED_RID - the varint form the hash bucket already uses for every entry value - rather
than the TYPE_RID the schema declares:
TYPE_RIDisputInt(bucketId)+putLong(position)= 12 bytes, fixedTYPE_COMPRESSED_RIDisputNumber(bucketId)+putNumber(position)= ~2-7 bytes in practice
On the composite (@out, @in) key that motivates an endpoint-keyed unique index that is roughly half the key bytes,
and therefore roughly half the pages the index occupies.
LSM_TREE still uses the declared type. LSMTreeIndexAbstract (~line 138):
this.binaryKeyTypes = new byte[keyTypes.length];
for (...) this.binaryKeyTypes[i] = keyTypes[i].getBinaryType(); // LINK -> TYPE_RID -> 12 bytesSo an LSM index on (@out, @in) spends 24 bytes per entry on the key where ~8-14 would do. Edge de-duplication
indexes are exactly the case that gets large, so this is worth measuring.
Why this is not a straight copy of the HASH fix
A hash index only needs the encoding to be deterministic and injective - it compares serialized key bytes for
equality and the ordering within a page is an internal detail. An LSM index is ordered: range scans, MIN/MAX and
every ordered cursor depend on key order being meaningful.
The encouraging part is that LSM does not compare raw bytes. LSMTreeIndexAbstract.compareKey deserializes and
uses the typed comparator:
final Object keyValue = serializer.deserializeValue(database, currentPageBuffer, binaryKeyTypes[keyIndex], ...);
result = comparator.compare(key, binaryKeyTypes[keyIndex], keyValue, binaryKeyTypes[keyIndex]);so ordering comes from comparing RIDs semantically, not from the byte layout - which suggests a narrower encoding
would not by itself reorder anything. That needs proving, not assuming, before anyone touches it. In particular:
BinaryComparatormust orderTYPE_COMPRESSED_RIDidentically toTYPE_RID(and handle a mixed pair, for indexes
written before and after such a change).- The compacted sub-index (
LSMTreeIndexCompacted) persistsbinaryKeyTypesinto its own page header, so old and
new files would coexist; the reader has to honour whatever each file declares. LSMTreeIndexExternalSorterwritesbinaryKeyTypesinto spill runs and rejects a run whose types differ
(!Arrays.equals(runKeyTypes, binaryKeyTypes)), so a bulk build spanning the change would need care.- Anything that reports key types outward -
getBinaryKeyTypes(),TypeIndex,MultiIndexCursor- should keep
reporting the schema type. HASH index cannot index @out/@in (LINK) keys, and fails at insert time claiming page corruption #5677 splitdeclaredKeyTypes(schema, persisted, reported) frombinaryKeyTypes
(on-page) for exactly this reason: reporting the storage type fromgetBinaryKeyTypes()broke coercing
WHERE link = "#150:5"into a RID and threwClassCastException. The same trap exists here.
Suggested first step
Measure before changing anything. EdgeUniqueIndexLsmVsHashBenchmark - the benchmark whose construction surfaced
#5677, and which could not run its HASH arm until that fix landed - is the natural place: build a large
(@out, @in) unique index both ways and compare on-disk size, page count and lookup throughput. If the saving on
LSM is small relative to the per-entry overhead the LSM format already carries, this is not worth the compatibility
surface described above and the issue should be closed on the numbers.
Activity
fix(#5703): store an LSM_TREE LINK key as a compressed RID (#6021)