Skip to content

A key-wide tombstone is ignored by the LSM point-lookup path on a NON-UNIQUE index, so index.get() keeps returning entries that remove(keys) deleted #7765

Closed

Description

@ruispereira

Summary

LSMTreeIndexAbstract.lookupInPageAndAddInResultset() is the reader behind Index.get(keys) for
both the mutable pages and the compacted sub-index. It decodes every negative-bucket RID as a
PER-RID tombstone:

if (rid.getBucketId() < 0) {
  final RID originalRID = getOriginalRID(rid);   // ((-bucketId) - 2, position)
  deletedRIDs.add(originalRID);
  if (mainIndex.isUnique())
    removedKeys.add(keys);
  continue;
}

REMOVED_ENTRY_RID - the KEY-WIDE tombstone written by remove(keys) with no RID - is
RID(-1, -1). getOriginalRID(RID(-1,-1)) decodes to RID(-1,-1) again, which matches no real
record, so it lands in deletedRIDs and suppresses nothing. The only thing that would have
suppressed the key is removedKeys, and that is gated on mainIndex.isUnique(). On a NON-UNIQUE
index the key-wide tombstone is therefore a no-op for the point lookup, and every RID written
before it is still returned.

Every other place that resolves the same on-disk history gets it right and says so explicitly:

  • LSMTreeIndexCursor.next() (LSMTreeIndexCursor.java:538): if (rid.getBucketId() == -1 && rid.getPosition() == -1) ridState.clear(); with a comment spelling out the two tombstone
    shapes and that collapsing them "was correct only for unique indexes".
  • LSMTreeIndexCompactor.compactFull() (LSMTreeIndexCompactor.java:294): if (rid.getBucketId() == -1 && rid.getPosition() == -1) live.clear(); - "a key-wide tombstone
    (-1,-1) clears everything before it ... Same semantics as the read path".
  • LSMTreeIndex.get() / HashIndex.get() handle the in-transaction equivalent with an explicit
    keyWideRemove flag (An index range scan inside a transaction ignores the tx overlay REMOVE entries, returning rows deleted or re-keyed in that same transaction #6927).

So the semantics is agreed on in three places and missing in the fourth, and the two read paths
of the same index disagree with each other on the same data.

Consequences beyond the direct API call:

  • LSMTreeFullTextIndex.remove(Object[]) (LSMTreeFullTextIndex.java:1182/1192/1193) deletes a
    term with underlyingIndex.remove(new String[]{k}) - key-wide, on a non-unique LSM index - and
    full-text search reads through underlyingIndex.get(...).
  • LSMTreeGeoIndex.remove(Object[]) (LSMTreeGeoIndex.java:287) does the same per covering-cell
    token, and openCellCursor reads the non-frontier/FULL-tokenization case with
    underlyingIndex.get(new Object[]{token}).
  • Because compactFull() DOES apply the tombstone and drops the entries, the same
    index.get(keys) silently changes its answer from "3 rows" to "0 rows" the first time a full
    compaction runs. The wrong answer is not even stable.

Where (HEAD 417314c)

engine/src/main/java/com/arcadedb/index/lsm/LSMTreeIndexAbstract.java:926

        if (rid.getBucketId() < 0) {
          // This is a deletion marker - convert to original RID
          final RID originalRID = getOriginalRID(rid);
          deletedRIDs.add(originalRID);

          // For unique indexes, also mark the entire key as removed
          if (mainIndex.isUnique()) {
            removedKeys.add(keys);
          }
          continue;
        }

and the gate 6 lines below:

        // For unique indexes, check if the entire key has been removed
        if (mainIndex.isUnique() && removedKeys.contains(keys)) {

REMOVED_ENTRY_RID is defined at LSMTreeIndexAbstract.java:168 as new RID(-1, -1L) and written by
LSMTreeIndexMutable.remove() at LSMTreeIndexMutable.java:571
(final RID removedRID = rid != null ? getRemovedRID(rid) : REMOVED_ENTRY_RID;).

Repro

ReproKeyWide.java, against engine/target/classes of the worktree:

javac -cp "audit/engine/target/classes:$(cat cp.txt)" -d repro/out repro/ReproKeyWide.java
java  -cp "repro/out:audit/engine/target/classes:$(cat cp.txt)" ReproKeyWide

Actual output:

unique=false type=LSM_TREE
BEFORE remove: get()=3 range()=3
AFTER  remove: get()=3 range()=0
AFTER  remove: countEntries()=0
AFTER  remove: SELECT FROM Foo WHERE name = 'a' -> 3 row(s)
MISMATCH: point lookup ignored the key-wide tombstone

Four answers about the same key on the same index at the same moment: the range cursor and
countEntries() (which walks the cursor) say the key is gone, while Index.get() and the SQL
equality predicate - which is answered from the point lookup - still hand back all three rows. So
this is not confined to the Java API: SELECT ... WHERE name = 'a' returns rows the index was
told to forget.

The program creates a type Foo with a NOTUNIQUE LSM_TREE index on name, inserts three
documents with name='a', commits, then in its own transaction calls
index.remove(new Object[]{"a"}) (the documented Index.remove(Object[] keys), "Removes the keys
from the index") and commits. After that range() correctly reports the key as gone and get()
still hands back all three RIDs.

Suggested fix

Detect the key-wide sentinel before decoding, and honour it for every index, not only unique ones:

if (rid.getBucketId() < 0) {
  if (rid.getBucketId() == -1 && rid.getPosition() == -1)
    // KEY-WIDE TOMBSTONE: kills every RID written before it, unique or not
    removedKeys.add(keys);
  else
    deletedRIDs.add(getOriginalRID(rid));
  continue;
}
...
if (removedKeys.contains(keys))     // no isUnique() gate
  continue;

The walk is newest-to-oldest here, so adding the key to removedKeys on the way past the
tombstone suppresses exactly the older entries, and a NEWER insert (visited first) is unaffected -
which is what LSMTreeIndexCursor and compactFull already do.

Scope / confidence

Refutation attempts: (1) checked that remove(keys) really writes REMOVED_ENTRY_RID and not a
per-RID tombstone - it does, LSMTreeIndexMutable.java:571; (2) checked whether the TX overlay
masks it - it does not, the repro removes and commits in one transaction and reads in another, and
the in-transaction path has its own correct keyWideRemove handling (#6927) which is exactly why
this only shows up after commit; (3) checked whether Index.remove(Object[]) is documented as
unsupported for automatic indexes - it is not, it is on the public Index interface and
LSMSparseVectorIndex is the only implementation that explicitly rejects it; (4) checked the
HashIndex, which physically deletes the entries and is therefore correct - this is LSM-specific;
(5) checked that range() is not the one that is wrong - compactFull() resolves the history the
same way range() does and permanently drops the entries, so range() is the intended semantics.
Falsified if the project intends remove(Object[]) on a non-unique automatic index to be a no-op,
but then LSMTreeIndexCursor and compactFull would both be wrong instead.

Activity

  1. added this to the 26.10.1 milestone on Sep 17, 2026
  2. added
    in progress
    on Sep 17, 2026
  3. self-assigned this
    on Sep 17, 2026
  4. added a commit that references this issue on Sep 17, 2026
  5. removed
    in progress
    on Sep 17, 2026
  6. lvca commented on Sep 17, 2026

    @lvca
    Member

    Fixed in 26.10.1 (PR #7845).

    On a non-unique LSM_TREE index, calling remove(keys) to delete all entries for a key did not actually take effect for direct key lookups (index.get(...), and equality queries like WHERE field = value planned through the index): the old entries kept being returned even though a range scan or countEntries() correctly showed the key as gone. This is now fixed, so a removed key stays removed for every read path.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions