Skip to content

Commit

Permalink
cache for insertDataHash calls to prevent duplicate writes
Browse files Browse the repository at this point in the history
  • Loading branch information
karlprieb committed Sep 18, 2024
1 parent 937fe82 commit cd55758
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/database/standalone-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ export class StandaloneSqliteDatabaseWorker {
private bundleFormatIds: { [filter: string]: number } = {};
private filterIds: { [filter: string]: number } = {};

private insertDataHashCache: NodeCache;

// Transactions
resetBundlesToHeightFn: Sqlite.Transaction;
resetCoreToHeightFn: Sqlite.Transaction;
Expand Down Expand Up @@ -762,6 +764,12 @@ export class StandaloneSqliteDatabaseWorker {
});
},
);

this.insertDataHashCache = new NodeCache({
stdTTL: 60 * 7, // 7 minutes
checkperiod: 60, // 1 minute
useClones: false,
});
}

getMaxHeight() {
Expand Down Expand Up @@ -1133,13 +1141,7 @@ export class StandaloneSqliteDatabaseWorker {
cachedAt?: number;
}) {
const hashBuffer = fromB64Url(hash);
this.stmts.data.insertDataHash.run({
hash: hashBuffer,
data_size: dataSize,
original_source_content_type: contentType,
indexed_at: currentUnixTimestamp(),
cached_at: cachedAt,
});

this.stmts.data.insertDataId.run({
id: fromB64Url(id),
contiguous_data_hash: hashBuffer,
Expand All @@ -1152,6 +1154,19 @@ export class StandaloneSqliteDatabaseWorker {
indexed_at: currentUnixTimestamp(),
});
}

if (this.insertDataHashCache.get(id)) {
return;
}
this.insertDataHashCache.set(id, true);

this.stmts.data.insertDataHash.run({
hash: hashBuffer,
data_size: dataSize,
original_source_content_type: contentType,
indexed_at: currentUnixTimestamp(),
cached_at: cachedAt,
});
}

getGqlNewTransactionTags(txId: Buffer) {
Expand Down

0 comments on commit cd55758

Please sign in to comment.