From cd3d95b7ebf2ddb8f14c7fdb5d11e1365194f2aa Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Thu, 13 Jun 2024 12:56:48 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20use=20transactions=20for?= =?UTF-8?q?=20autocomplete=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/autocomplete/cache.ts | 68 ++++++++++++++++++++++++-------------- core/package-lock.json | 9 +++++ core/package.json | 1 + 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/core/autocomplete/cache.ts b/core/autocomplete/cache.ts index bad86ca38..c3daefb1c 100644 --- a/core/autocomplete/cache.ts +++ b/core/autocomplete/cache.ts @@ -1,3 +1,4 @@ +import { Mutex } from "async-mutex"; import { open } from "sqlite"; import sqlite3 from "sqlite3"; import { DatabaseConnection } from "../indexing/refreshIndex.js"; @@ -5,6 +6,7 @@ import { getTabAutocompleteCacheSqlitePath } from "../util/paths.js"; export class AutocompleteLruCache { private static capacity = 1000; + private mutex = new Mutex(); db: DatabaseConnection; @@ -55,33 +57,51 @@ export class AutocompleteLruCache { } async put(prefix: string, completion: string) { - const result = await this.db.get( - "SELECT key FROM cache WHERE key = ?", - prefix, - ); + const release = await this.mutex.acquire(); + try { + await this.db.run("BEGIN TRANSACTION"); - if (result) { - await this.db.run( - "UPDATE cache SET value = ?, timestamp = ? WHERE key = ?", - completion, - Date.now(), - prefix, - ); - } else { - const count = await this.db.get("SELECT COUNT(*) as count FROM cache"); - - if (count.count >= AutocompleteLruCache.capacity) { - await this.db.run( - "DELETE FROM cache WHERE key = (SELECT key FROM cache ORDER BY timestamp ASC LIMIT 1)", + try { + const result = await this.db.get( + "SELECT key FROM cache WHERE key = ?", + prefix, ); - } - await this.db.run( - "INSERT INTO cache (key, value, timestamp) VALUES (?, ?, ?)", - prefix, - completion, - Date.now(), - ); + if (result) { + await this.db.run( + "UPDATE cache SET value = ?, timestamp = ? WHERE key = ?", + completion, + Date.now(), + prefix, + ); + } else { + const count = await this.db.get( + "SELECT COUNT(*) as count FROM cache", + ); + + if (count.count >= AutocompleteLruCache.capacity) { + await this.db.run( + "DELETE FROM cache WHERE key = (SELECT key FROM cache ORDER BY timestamp ASC LIMIT 1)", + ); + } + + await this.db.run( + "INSERT INTO cache (key, value, timestamp) VALUES (?, ?, ?)", + prefix, + completion, + Date.now(), + ); + } + + await this.db.run("COMMIT"); + } catch (error) { + await this.db.run("ROLLBACK"); + throw error; + } + } catch (e) { + console.error("Error creating transaction: ", e); + } finally { + release(); } } } diff --git a/core/package-lock.json b/core/package-lock.json index 1ff3584a6..028b9be35 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -19,6 +19,7 @@ "@typescript-eslint/parser": "^7.8.0", "@xenova/transformers": "2.14.0", "adf-to-md": "^1.1.0", + "async-mutex": "^0.5.0", "axios": "^1.6.7", "cheerio": "^1.0.0-rc.12", "commander": "^12.0.0", @@ -5389,6 +5390,14 @@ "node": ">=0.8" } }, + "node_modules/async-mutex": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/async-mutex/-/async-mutex-0.5.0.tgz", + "integrity": "sha512-1A94B18jkJ3DYq284ohPxoXbfTA5HsQ7/Mf4DEhcyLx3Bz27Rh59iScbB6EPiP+B+joue6YCxcMXSbFC1tZKwA==", + "dependencies": { + "tslib": "^2.4.0" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", diff --git a/core/package.json b/core/package.json index da77c644d..07287e6ba 100644 --- a/core/package.json +++ b/core/package.json @@ -39,6 +39,7 @@ "@typescript-eslint/parser": "^7.8.0", "@xenova/transformers": "2.14.0", "adf-to-md": "^1.1.0", + "async-mutex": "^0.5.0", "axios": "^1.6.7", "cheerio": "^1.0.0-rc.12", "commander": "^12.0.0",