Skip to content

LSM_TREE stores a LINK key as a fixed 12 bytes per column; evaluate the compressed RID used by HASH #5703

Closed

Description

@lvca

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_RID is putInt(bucketId) + putLong(position) = 12 bytes, fixed
  • TYPE_COMPRESSED_RID is putNumber(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 bytes

So 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:

  • BinaryComparator must order TYPE_COMPRESSED_RID identically to TYPE_RID (and handle a mixed pair, for indexes
    written before and after such a change).
  • The compacted sub-index (LSMTreeIndexCompacted) persists binaryKeyTypes into its own page header, so old and
    new files would coexist; the reader has to honour whatever each file declares.
  • LSMTreeIndexExternalSorter writes binaryKeyTypes into 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 split declaredKeyTypes (schema, persisted, reported) from binaryKeyTypes
    (on-page) for exactly this reason: reporting the storage type from getBinaryKeyTypes() broke coercing
    WHERE link = "#150:5" into a RID and threw ClassCastException. 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.

Follow-up to #5677 / PR #5685.

Activity

  1. added
    enhancement
    on Aug 1, 2026
  2. modified the milestones: 26.8.1, 26.9.1 on Aug 1, 2026
  3. self-assigned this
    on Aug 9, 2026
  4. added
    in progress
    on Aug 9, 2026
  5. added a commit that references this issue on Aug 9, 2026
  6. removed
    in progress
    on Aug 11, 2026
  7. added
    engine
    storage
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

engineenhancementstorage

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions