Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): don't toss the entire index after each note change

perfectra1n1c148f407cc8
diff --git a/apps/server/src/becca/becca-interface.ts b/apps/server/src/becca/becca-interface.ts
index 6619ed30b9..1a95408d9b 100644
--- a/apps/server/src/becca/becca-interface.ts
+++ b/apps/server/src/becca/becca-interface.ts
@@ -34,14 +34,19 @@ export default class Becca {
/**
* Pre-built parallel arrays for fast flat text scanning in search.
* Avoids per-note property access overhead when iterating 50K+ notes.
- * Dirtied when notes change (along with allNoteSetCache).
+ * Supports incremental updates: when individual notes change, only their
+ * entries are rebuilt rather than the entire index.
*/
- flatTextIndex: { notes: BNote[], flatTexts: string[] } | null;
+ flatTextIndex: { notes: BNote[], flatTexts: string[], noteIdToIdx: Map<string, number> } | null;
+
+ /** NoteIds whose flat text needs to be recomputed in the index. */
+ dirtyFlatTextNoteIds: Set<string>;
constructor() {
- this.reset();
+ this.dirtyFlatTextNoteIds = new Set();
this.allNoteSetCache = null;
this.flatTextIndex = null;
+ this.reset();
}
reset() {
@@ -247,25 +252,56 @@ export default class Becca {
/** Should be called when the set of all non-skeleton notes changes (added/removed) */
dirtyNoteSetCache() {
this.allNoteSetCache = null;
+ // Full rebuild needed since the note set itself changed
this.flatTextIndex = null;
+ this.dirtyFlatTextNoteIds.clear();
+ }
+
+ /** Mark a single note's flat text as needing recomputation in the index. */
+ dirtyNoteFlatText(noteId: string) {
+ if (this.flatTextIndex) {
+ // Index exists — schedule an incremental update
+ this.dirtyFlatTextNoteIds.add(noteId);
+ }
+ // If flatTextIndex is null, full rebuild will happen on next access anyway
}
/**
* Returns pre-built parallel arrays of notes and their flat texts for fast scanning.
* The flat texts are already normalized (lowercase, diacritics removed).
+ * Supports incremental updates: when individual notes are dirtied, only their
+ * entries are recomputed rather than rebuilding the entire index.
*/
- getFlatTextIndex(): { notes: BNote[], flatTexts: string[] } {
+ getFlatTextIndex(): { notes: BNote[], flatTexts: string[], noteIdToIdx: Map<string, number> } {
if (!this.flatTextIndex) {
const allNoteSet = this.getAllNoteSet();
const notes: BNote[] = [];
const flatTexts: string[] = [];
+ const noteIdToIdx = new Map<string, number>();
for (const note of allNoteSet.notes) {
+ noteIdToIdx.set(note.noteId, notes.length);
notes.push(note);
flatTexts.push(note.getFlatText());
}
- this.flatTextIndex = { notes, flatTexts };
+ this.flatTextIndex = { notes, flatTexts, noteIdToIdx };
+ this.dirtyFlatTextNoteIds.clear();
+ } else if (this.dirtyFlatTextNoteIds.size > 0) {
+ // Incremental update: only recompute flat texts for dirtied notes
+ const { flatTexts, noteIdToIdx } = this.flatTextIndex;
+
+ for (const noteId of this.dirtyFlatTextNoteIds) {
+ const idx = noteIdToIdx.get(noteId);
+ if (idx !== undefined) {
+ const note = this.notes[noteId];
+ if (note) {
+ flatTexts[idx] = note.getFlatText();
+ }
+ }
+ }
+
+ this.dirtyFlatTextNoteIds.clear();
}
return this.flatTextIndex;
diff --git a/apps/server/src/becca/entities/battribute.ts b/apps/server/src/becca/entities/battribute.ts
index 6ff1246fcf..77a15c2fd1 100644
--- a/apps/server/src/becca/entities/battribute.ts
+++ b/apps/server/src/becca/entities/battribute.ts
@@ -6,6 +6,7 @@ import dateUtils from "../../services/date_utils.js";
import promotedAttributeDefinitionParser from "../../services/promoted_attribute_definition_parser.js";
import sanitizeAttributeName from "../../services/sanitize_attribute_name.js";
import type { AttributeRow, AttributeType } from "@triliumnext/commons";
+import { normalize } from "../../services/utils.js";
interface SavingOpts {
skipValidation?: boolean;
@@ -34,6 +35,11 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {
value!: string;
isInheritable!: boolean;
+ /** Pre-normalized (lowercase, diacritics removed) name for search. */
+ normalizedName!: string;
+ /** Pre-normalized (lowercase, diacritics removed) value for search. */
+ normalizedValue!: string;
+
constructor(row?: AttributeRow) {
super();
@@ -59,6 +65,10 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {
this.isInheritable = !!isInheritable;
this.utcDateModified = utcDateModified;
+ // Pre-compute normalized forms for search (avoids repeated normalize() calls in hot loops)
+ this.normalizedName = normalize(this.name);
+ this.normalizedValue = normalize(this.value);
+
return this;
}
diff --git a/apps/server/src/becca/entities/bnote.ts b/apps/server/src/becca/entities/bnote.ts
index 4e78974b4e..10750efc32 100644
--- a/apps/server/src/becca/entities/bnote.ts
+++ b/apps/server/src/becca/entities/bnote.ts
@@ -791,8 +791,8 @@ class BNote extends AbstractBeccaEntity<BNote> {
this.__inheritableAttributeCache = null;
this.__ancestorCache = null;
- // Dirty the becca-level flat text index since this note's flat text may have changed
- this.becca.flatTextIndex = null;
+ // Mark only this note's flat text as dirty for incremental index update
+ this.becca.dirtyNoteFlatText(this.noteId);
}
invalidateSubTree(path: string[] = []) {
diff --git a/apps/server/src/services/search/expressions/note_flat_text.ts b/apps/server/src/services/search/expressions/note_flat_text.ts
index eff3622a76..ff54287d91 100644
--- a/apps/server/src/services/search/expressions/note_flat_text.ts
+++ b/apps/server/src/services/search/expressions/note_flat_text.ts
@@ -7,7 +7,7 @@ import Expression from "./expression.js";
import NoteSet from "../note_set.js";
import becca from "../../../becca/becca.js";
import { normalize } from "../../utils.js";
-import { normalizeSearchText, fuzzyMatchWord, fuzzyMatchWordWithResult } from "../utils/text_utils.js";
+import { normalizeSearchText, fuzzyMatchWordWithResult } from "../utils/text_utils.js";
import beccaService from "../../../becca/becca_service.js";
class NoteFlatTextExp extends Expression {
@@ -67,11 +67,8 @@ class NoteFlatTextExp extends Expression {
}
for (const attribute of note.getOwnedAttributes()) {
- const normalizedName = normalizeSearchText(attribute.name);
- const normalizedValue = normalizeSearchText(attribute.value);
-
for (const token of remainingTokens) {
- if (normalizedName.includes(token) || normalizedValue.includes(token)) {
+ if (attribute.normalizedName.includes(token) || attribute.normalizedValue.includes(token)) {
foundAttrTokens.push(token);
}
}
@@ -134,7 +131,7 @@ class NoteFlatTextExp extends Expression {
}
for (const attribute of note.ownedAttributes) {
- if (normalizeSearchText(attribute.name).includes(token) || normalizeSearchText(attribute.value).includes(token)) {
+ if (attribute.normalizedName.includes(token) || attribute.normalizedValue.includes(token)) {
foundAttrTokens.push(token);
}
}