From d3dbd8b514beff7515cf6207249f430f5bbed843 Mon Sep 17 00:00:00 2001 From: Mikers Date: Tue, 5 May 2026 08:01:58 -1000 Subject: [PATCH] collections: reuse direct primary semantic document IDs --- TreeDB/collections/api.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/TreeDB/collections/api.go b/TreeDB/collections/api.go index 20900492ef..ba2b590cf2 100644 --- a/TreeDB/collections/api.go +++ b/TreeDB/collections/api.go @@ -9223,15 +9223,23 @@ func applyDirectBufferedRootEntries(table memtable.Table, entries []directBuffer }) } -func buildIndexedSemanticUpdateRecords(collectionName string, runtimes []indexRuntime, updates []preparedBatchUpdate) []indexedSemanticRecord { +func buildIndexedSemanticUpdateRecords(collectionName string, runtimes []indexRuntime, updates []preparedBatchUpdate, primaryEntries []directBufferedRootEntry) []indexedSemanticRecord { if len(updates) == 0 { return nil } records := make([]indexedSemanticRecord, 0, len(updates)) - for _, update := range updates { + for i, update := range updates { + var documentID []byte + if i < len(primaryEntries) && len(primaryEntries[i].key) > 0 { + // Direct primary entries are built from the same changed slice and carry + // an owned, staged document ID clone, so the semantic sidecar can share it. + documentID = primaryEntries[i].key + } else { + documentID = bytes.Clone(update.documentID) + } record := indexedSemanticRecord{ kind: indexedSemanticRecordUpdate, - documentID: bytes.Clone(update.documentID), + documentID: documentID, } if update.indexStateChanged && len(runtimes) > 0 { for runtimeIdx, runtime := range runtimes { @@ -10425,7 +10433,7 @@ func (c *Collection) buildUpdateBatchPlan(items []UpdateBatchItem, mode updateBa stats = updateCollectionUpdateStatsCounts(stats, results, len(rootNames)) var semanticRecords []indexedSemanticRecord if c.writeDomain != nil && canBufferIndexedUpdateBatch && meta.Options.BufferedIndexedWrites { - semanticRecords = buildIndexedSemanticUpdateRecords(meta.Name, runtimes, changed) + semanticRecords = buildIndexedSemanticUpdateRecords(meta.Name, runtimes, changed, primaryEntries) } *plan = updateBatchPlan{ results: results, @@ -10629,7 +10637,7 @@ func (c *Collection) buildUpdateBatchPlan(items []UpdateBatchItem, mode updateBa stats = updateCollectionUpdateStatsCounts(stats, results, len(deltaTables)) var semanticRecords []indexedSemanticRecord if c.writeDomain != nil && canBufferIndexedUpdateBatch && meta.Options.BufferedIndexedWrites { - semanticRecords = buildIndexedSemanticUpdateRecords(meta.Name, runtimes, changed) + semanticRecords = buildIndexedSemanticUpdateRecords(meta.Name, runtimes, changed, nil) } *plan = updateBatchPlan{ results: results,