Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions engine/src/main/java/com/arcadedb/database/DocumentIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.arcadedb.index.Index;
import com.arcadedb.index.IndexException;
import com.arcadedb.index.IndexInternal;
import com.arcadedb.index.IndexKeyEquality;
import com.arcadedb.schema.DocumentType;
import com.arcadedb.schema.Schema;

Expand All @@ -35,7 +36,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

Expand Down Expand Up @@ -544,27 +544,23 @@ private static boolean containsTuple(final List<Object[]> tuples, final Object[]
/**
* The one equality every "did this key change?" decision in this class goes through: two tuples are the same key when
* every element is {@link #sameKeyValue(Object, Object) the same key value}. Kept as the single policy so a new update
* path cannot pick a third notion of equality (issue #7109).
* path cannot pick a third notion of equality (issue #7109), and shared with the transaction-side overlay through
* {@link IndexKeyEquality} (issue #7881).
*/
private static boolean sameKeyTuple(final Object[] a, final Object[] b) {
if (a.length != b.length)
return false;
for (int i = 0; i < a.length; i++)
if (!sameKeyValue(a[i], b[i]))
return false;
return true;
return IndexKeyEquality.sameTuple(a, b);
}

/**
* Content-aware equality for a single key value: array-typed keys (the {@code float[]} of a vector index, a
* {@code BINARY} property) do not override {@code Object.equals()}, so two independently deserialized-but-identical
* arrays would compare unequal and trigger a spurious remove()+put() on every record update (issues #5318 and #7109).
* {@link Objects#deepEquals} uses {@code Object.equals()} for scalar keys, the element-wise {@code Arrays.equals()}
* overload of the matching primitive array type ({@code float[]}, {@code byte[]}, ...) for primitive arrays, and
* {@code Arrays.deepEquals()} for {@code Object[]}.
* <p>
* Delegated to {@link IndexKeyEquality}, which is where the transaction-side overlay reads the same policy from -
* the two used to have their own copies of it and disagreed on exactly these keys (issue #7881).
*/
private static boolean sameKeyValue(final Object a, final Object b) {
return Objects.deepEquals(a, b);
return IndexKeyEquality.sameValue(a, b);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.arcadedb.index.Index;
import com.arcadedb.index.IndexCursor;
import com.arcadedb.index.IndexInternal;
import com.arcadedb.index.IndexKeyEquality;
import com.arcadedb.index.TypeIndex;
import com.arcadedb.index.lsm.LSMTreeIndexAbstract;
import com.arcadedb.index.vector.LSMVectorIndex;
Expand Down Expand Up @@ -240,15 +241,15 @@ public boolean equals(final Object o) {
if (!(o instanceof IndexKey indexKey))
return false;
if (unique)
return Arrays.equals(keyValues, indexKey.keyValues);
return Objects.equals(rid, indexKey.rid) && Arrays.equals(keyValues, indexKey.keyValues);
return IndexKeyEquality.sameTuple(keyValues, indexKey.keyValues);
return Objects.equals(rid, indexKey.rid) && IndexKeyEquality.sameTuple(keyValues, indexKey.keyValues);
}

@Override
public int hashCode() {
if (unique)
return Objects.hash(Arrays.hashCode(keyValues));
return Objects.hash(rid, Arrays.hashCode(keyValues));
return Objects.hash(IndexKeyEquality.hashTuple(keyValues));
return Objects.hash(rid, IndexKeyEquality.hashTuple(keyValues));
}

@Override
Expand Down Expand Up @@ -305,12 +306,12 @@ public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass())
return false;
final ComparableKey that = (ComparableKey) o;
return Arrays.equals(values, that.values);
return IndexKeyEquality.sameTuple(values, that.values);
}

@Override
public int hashCode() {
return Arrays.hashCode(values);
return IndexKeyEquality.hashTuple(values);
}

@Override
Expand Down
94 changes: 88 additions & 6 deletions engine/src/main/java/com/arcadedb/graph/olap/DeltaOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ class DeltaOverlay {
// absorbed as the scan swallowed, the next one is genuine and does have to spend a budget.
private final Map<String, Map<Long, Integer>> absorbedDeletedEdgesPerType;

// The mirror image of the map above, on the ADD side: buffered additions the freshly built base CSR had ALREADY
// captured at post-compaction re-application time, per type and packed pair. Non-empty only on that path.
// The add side used to dedup by PRESENCE - it skipped a buffered add whenever the fresh run held ANY occurrence of
// the pair - which drops a real edge as soon as the pair carries parallel ones: with one occurrence of s -> t in
// the fresh run and a buffered add of a SECOND one, the second existed in neither the fresh base nor the overlay
// until the next compaction (issue #7884). The discriminator is the same multiplicity comparison the deletion side
// got under #7042, read the other way round: with `before` the pair's occurrences at compaction start and `fresh`
// its occurrences in the newly scanned base, exactly max(0, fresh - before) of the pair's buffered additions were
// captured by the scan. Those are recorded here and skipped; the ones beyond them committed after the scan crossed
// the bucket and belong in the overlay. Kept as a count for the same reason the deletion side is: the captured
// population is a PREFIX of the pair's buffered additions.
private final Map<String, Map<Long, Integer>> absorbedAddedEdgesPerType;

// Per-node deleted edge counts for O(1) lookup: edgeType -> nodeId -> count
private final Map<String, IntIntHashMap> deletedOutEdgeCounts;
private final Map<String, IntIntHashMap> deletedInEdgeCounts;
Expand Down Expand Up @@ -123,6 +136,7 @@ class DeltaOverlay {
this.deletedEdgesPerType = Collections.emptyMap();
this.deletedEdgeRIDsPerType = Collections.emptyMap();
this.absorbedDeletedEdgesPerType = Collections.emptyMap();
this.absorbedAddedEdgesPerType = Collections.emptyMap();
this.deletedOutEdgeCounts = Collections.emptyMap();
this.deletedInEdgeCounts = Collections.emptyMap();
this.propertyOverrides = Collections.emptyMap();
Expand All @@ -146,6 +160,7 @@ private DeltaOverlay(final int baseNodeCount,
final Map<String, Map<Long, Integer>> deletedEdgesPerType,
final Map<String, Set<RID>> deletedEdgeRIDsPerType,
final Map<String, Map<Long, Integer>> absorbedDeletedEdgesPerType,
final Map<String, Map<Long, Integer>> absorbedAddedEdgesPerType,
final Map<String, IntIntHashMap> deletedOutEdgeCounts,
final Map<String, IntIntHashMap> deletedInEdgeCounts,
final Map<Integer, Map<String, Object>> propertyOverrides,
Expand All @@ -163,6 +178,7 @@ private DeltaOverlay(final int baseNodeCount,
this.deletedEdgesPerType = deletedEdgesPerType;
this.deletedEdgeRIDsPerType = deletedEdgeRIDsPerType;
this.absorbedDeletedEdgesPerType = absorbedDeletedEdgesPerType;
this.absorbedAddedEdgesPerType = absorbedAddedEdgesPerType;
this.deletedOutEdgeCounts = deletedOutEdgeCounts;
this.deletedInEdgeCounts = deletedInEdgeCounts;
this.propertyOverrides = propertyOverrides;
Expand Down Expand Up @@ -207,10 +223,11 @@ interface PreCompactionPairCount {
* <p>
* When {@code baseCsrPerType} is non-null (post-compaction delta re-application, see issue #4588),
* an added edge whose both endpoints are base nodes and that the freshly built base CSR already
* contains is skipped, unless the same edge is masked as deleted (either by an earlier buffered
* captured is skipped, unless the same edge is masked as deleted (either by an earlier buffered
* delta or within this delta). The compaction scan is read-committed and non-atomic, so a buffered
* delta may already be reflected in the new base CSR; re-adding it to the overlay would surface a
* duplicate neighbour and inflate the delta edge counter.
* duplicate neighbour and inflate the delta edge counter. Which of the pair's buffered additions the
* scan captured is a MULTIPLICITY question - see {@link #capturedByFreshScan} and issue #7884.
*/
DeltaOverlay merge(final TxDelta delta, final NodeIdMapping baseMapping,
final Map<String, CSRAdjacencyIndex> baseCsrPerType) {
Expand Down Expand Up @@ -255,6 +272,9 @@ DeltaOverlay merge(final TxDelta delta, final NodeIdMapping baseMapping,
final Map<String, Map<Long, Integer>> newAbsorbedDeletions = new HashMap<>();
for (final var entry : absorbedDeletedEdgesPerType.entrySet())
newAbsorbedDeletions.put(entry.getKey(), new HashMap<>(entry.getValue()));
final Map<String, Map<Long, Integer>> newAbsorbedAdditions = new HashMap<>();
for (final var entry : absorbedAddedEdgesPerType.entrySet())
newAbsorbedAdditions.put(entry.getKey(), new HashMap<>(entry.getValue()));
final Map<Integer, Map<String, Object>> newPropOverrides = new HashMap<>(propertyOverrides.size());
for (final var propEntry : propertyOverrides.entrySet())
newPropOverrides.put(propEntry.getKey(), new HashMap<>(propEntry.getValue()));
Expand Down Expand Up @@ -308,18 +328,19 @@ DeltaOverlay merge(final TxDelta delta, final NodeIdMapping baseMapping,
final int tgtId = resolveNodeId(ed.target, baseMapping, newOverflowIds);
if (srcId < 0 || tgtId < 0)
continue; // endpoint not in view
// Post-compaction re-application: if the freshly built base CSR already contains this edge (the
// Post-compaction re-application: if the freshly built base CSR already captured this edge (the
// read-committed scan crossed its bucket after it committed), re-adding it would duplicate the
// neighbour and inflate the counter. Skip it unless it is masked as deleted, in which case the
// overlay must keep the explicit add to reinstate it. See issue #4588.
// overlay must keep the explicit add to reinstate it. See issue #4588, and capturedByFreshScan() for
// why "already captured" is a multiplicity question and not a presence one (issue #7884).
if (baseCsrPerType != null && srcId < baseNodeCount && tgtId < baseNodeCount) {
final CSRAdjacencyIndex csr = baseCsrPerType.get(ed.edgeType);
if (csr != null && csr.hasForwardEdge(srcId, tgtId)) {
final long packed = packEdge(srcId, tgtId);
final Map<Long, Integer> prevDel = newDeletedEdges.get(ed.edgeType);
final boolean masked = (prevDel != null && prevDel.containsKey(packed))
|| (sameDeltaDeleted != null && sameDeltaDeleted.contains(packed));
if (!masked) {
if (!masked && capturedByFreshScan(ed, srcId, tgtId, packed, csr, preCount, newAbsorbedAdditions)) {
// Remembered for the update loop below: the fresh base CSR already carries this edge, scan and
// all, so a property change to it in this same delta is already in the columns and dirties
// nothing. Without this the ordinary insert - one create and one update of the same edge - would
Expand Down Expand Up @@ -472,11 +493,62 @@ else if (alreadyInFreshBase != null && alreadyInFreshBase.contains(ed.rid)) {
overflowRIDsList.toArray(new RID[0]),
overflowPropsList.toArray(new Map[0]),
newDeleted, newDeletedOverflow, newAddedEdges, newDeletedEdges, newDeletedEdgeRIDs,
newAbsorbedDeletions, newDelOutCounts, newDelInCounts, newPropOverrides,
newAbsorbedDeletions, newAbsorbedAdditions, newDelOutCounts, newDelInCounts, newPropOverrides,
newOutIndex, newInIndex,
newOverflowCount, newDeltaEdgeCount, newDirtyTypes, newAllDirty);
}

/**
* Whether this buffered addition is one the freshly built base CSR already captured, and so must NOT be added to
* the overlay a second time - asked by MULTIPLICITY, the way the deletion side asks its own question (issue #7042).
* <p>
* The compaction scan is read-committed and non-atomic, so a buffered addition may or may not already be reflected
* in the fresh run, and the fresh multiplicity alone cannot say which: it is the RISE over the pre-compaction
* multiplicity that counts the additions the scan captured. With {@code before} the pair's occurrences at
* compaction start and {@code fresh} its occurrences in the newly scanned base, exactly {@code max(0, fresh -
* before)} of the pair's buffered additions are already in the fresh run. Those are recorded in
* {@code absorbedAdditions} and skipped; anything beyond them committed after the scan crossed the bucket and has
* to live in the overlay, because it is in neither the fresh base nor anywhere else (issue #7884).
* <p>
* Dedup by presence - which is what this used to be - answers "captured" for every buffered addition to a pair the
* fresh run holds ANY occurrence of, so a second parallel edge between an already-connected pair was dropped
* outright until the next compaction.
* <p>
* When {@code preCount} is null the reference is unavailable, so no addition can be shown to have been captured and
* the pre-#7884 presence answer stands - which is also what the callers that pass no {@code baseCsrPerType} get,
* since they never reach here.
* <p>
* No identity dedup here, unlike the deletion side. A buffered addition is one commit's report that an edge came
* into existence, and the engine cannot report the same edge RID created twice without the record having been
* deleted and its slot reused in between - at which point it is a DIFFERENT edge (issue #6777) and counting it as
* one is the right answer, not a replay to be absorbed. An addition that does reach the overlay is deduped by RID
* there, which is what absorbs a replay on the ordinary (non-compaction) path.
* <p>
* Which of a pair's buffered additions this skips is therefore arbitrary - the first {@code capturable} of them in
* arrival order, not the ones the scan actually saw, because the base CSR is a run of neighbour ids and cannot say.
* Deliberately so, and harmless: an edge the base run holds has no RID there to be told apart from another
* occurrence of the same pair until the next compaction rebuilds the mapping, so the only thing any reader of the
* two can observe is the pair's total multiplicity - which is what the count is chosen to make right.
*/
private static boolean capturedByFreshScan(final TxDelta.EdgeDelta ed, final int srcId, final int tgtId,
final long packed, final CSRAdjacencyIndex csr, final PreCompactionPairCount preCount,
final Map<String, Map<Long, Integer>> absorbedAdditions) {
if (preCount == null)
return true;

final int capturable = csr.forwardEdgeCount(srcId, tgtId) - preCount.occurrences(ed.edgeType, ed.source, ed.target);
if (capturable <= 0)
return false;

final Map<Long, Integer> capturedForType = absorbedAdditions.computeIfAbsent(ed.edgeType, k -> new HashMap<>());
final int capturedSoFar = capturedForType.getOrDefault(packed, 0);
if (capturedSoFar >= capturable)
return false;

capturedForType.put(packed, capturedSoFar + 1);
return true;
}

// --- Query helpers ---

int resolveNodeId(final RID rid, final NodeIdMapping baseMapping) {
Expand Down Expand Up @@ -567,6 +639,16 @@ int countAbsorbedDeletions(final String edgeType, final int srcId, final int tgt
return absorbed == null ? 0 : absorbed.getOrDefault(packEdge(srcId, tgtId), 0);
}

/**
* Returns how many of the pair's buffered ADDITIONS the freshly built base CSR had already captured at
* post-compaction re-application time, and which were therefore skipped rather than appended to the overlay. Zero
* on every path other than that re-application. See {@link #capturedByFreshScan} and issue #7884.
*/
int countAbsorbedAdditions(final String edgeType, final int srcId, final int tgtId) {
final Map<Long, Integer> absorbed = absorbedAddedEdgesPerType.get(edgeType);
return absorbed == null ? 0 : absorbed.getOrDefault(packEdge(srcId, tgtId), 0);
}

/**
* Counts the number of deleted outgoing edges from {@code nodeId} for the given edge type. O(1).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2594,10 +2594,10 @@ private boolean isConnectedForType(final Snapshot snap, final int nodeA, final i
* sum for the OTHER edge types {@link #countEdgesBetween} adds it to. Issue #7042 closed the one that made
* this reachable in ordinary use - the deletion side of the post-compaction re-application, which used to
* budget for edges the freshly scanned base had already dropped - and the deletion side now spends a budget
* only for a deletion the fresh run still carries. What is left is #6777's RID reuse, and the add side's
* dedup being by presence rather than by multiplicity (a buffered add of a SECOND parallel edge to a pair
* the fresh base already holds one of is skipped, which undercounts rather than overspending). The clamp
* stays: it costs nothing, and it is the last line between either of those and a corrupted cross-type sum.
* only for a deletion the fresh run still carries. Issue #7884 closed the mirror image on the add side, whose
* dedup was by presence rather than by multiplicity, so a buffered add of a SECOND parallel edge to a pair the
* fresh base already held one of used to be dropped outright. What is left is #6777's RID reuse. The clamp
* stays: it costs nothing, and it is the last line between that and a corrupted cross-type sum.
*/
private long countBetweenForType(final Snapshot snap, final int nodeA, final int nodeB,
final Vertex.DIRECTION direction, final String edgeType) {
Expand Down
Loading
Loading