Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(nbstore): fix indexer cache not working (#11922)

EYHN9fbffccc25f5
diff --git a/packages/common/nbstore/src/impls/idb/indexer/data-struct.ts b/packages/common/nbstore/src/impls/idb/indexer/data-struct.ts
index 40a1edd5f2..09e3d9f5f0 100644
--- a/packages/common/nbstore/src/impls/idb/indexer/data-struct.ts
+++ b/packages/common/nbstore/src/impls/idb/indexer/data-struct.ts
@@ -265,40 +265,47 @@ export class DataStruct {
if (cached) {
return cached;
}
- using _ = await this.measure(`query[${query.type}]`);
- if (query.type === 'match') {
- const iidx = this.invertedIndex.get(table)?.get(query.field as string);
- if (!iidx) {
- return new Match();
- }
- return await iidx.match(trx, query.match);
- } else if (query.type === 'boolean') {
- const weights = [];
- for (const q of query.queries) {
- weights.push(await this.queryRaw(trx, table, q, cache));
- }
- if (query.occur === 'must') {
- return weights.reduce((acc, w) => acc.and(w));
- } else if (query.occur === 'must_not') {
- const total = weights.reduce((acc, w) => acc.and(w));
- return (await this.matchAll(trx, table)).exclude(total);
- } else if (query.occur === 'should') {
- return weights.reduce((acc, w) => acc.or(w));
- }
- } else if (query.type === 'all') {
- return await this.matchAll(trx, table);
- } else if (query.type === 'boost') {
- return (await this.queryRaw(trx, table, query.query, cache)).boost(
- query.boost
- );
- } else if (query.type === 'exists') {
- const iidx = this.invertedIndex.get(table)?.get(query.field as string);
- if (!iidx) {
- return new Match();
+
+ const result = await (async () => {
+ using _ = await this.measure(`query[${query.type}]`);
+ if (query.type === 'match') {
+ const iidx = this.invertedIndex.get(table)?.get(query.field as string);
+ if (!iidx) {
+ return new Match();
+ }
+ return await iidx.match(trx, query.match);
+ } else if (query.type === 'boolean') {
+ const weights = [];
+ for (const q of query.queries) {
+ weights.push(await this.queryRaw(trx, table, q, cache));
+ }
+ if (query.occur === 'must') {
+ return weights.reduce((acc, w) => acc.and(w));
+ } else if (query.occur === 'must_not') {
+ const total = weights.reduce((acc, w) => acc.and(w));
+ return (await this.matchAll(trx, table)).exclude(total);
+ } else if (query.occur === 'should') {
+ return weights.reduce((acc, w) => acc.or(w));
+ }
+ } else if (query.type === 'all') {
+ return await this.matchAll(trx, table);
+ } else if (query.type === 'boost') {
+ return (await this.queryRaw(trx, table, query.query, cache)).boost(
+ query.boost
+ );
+ } else if (query.type === 'exists') {
+ const iidx = this.invertedIndex.get(table)?.get(query.field as string);
+ if (!iidx) {
+ return new Match();
+ }
+ return await iidx.all(trx);
}
- return await iidx.all(trx);
- }
- throw new Error(`Query type '${query.type}' not supported`);
+ throw new Error(`Query type '${query.type}' not supported`);
+ })();
+
+ cache.set(query, result);
+
+ return result;
}
async clear(trx: DataStructRWTransaction) {