| 1 | import { Mutex } from "async-mutex"; |
| 2 | import { open } from "sqlite"; |
| 3 | import sqlite3 from "sqlite3"; |
| 4 | |
| 5 | import { |
| 6 | DatabaseConnection, |
| 7 | truncateSqliteLikePattern, |
| 8 | } from "../../indexing/refreshIndex.js"; |
| 9 | import { getTabAutocompleteCacheSqlitePath } from "../../util/paths.js"; |
| 10 | |
| 11 | export class AutocompleteLruCache { |
| 12 | private static capacity = 1000; |
| 13 | private mutex = new Mutex(); |
| 14 | |
| 15 | constructor(private db: DatabaseConnection) {} |
| 16 | |
| 17 | static async get(): Promise<AutocompleteLruCache> { |
| 18 | const db = await open({ |
| 19 | filename: getTabAutocompleteCacheSqlitePath(), |
| 20 | driver: sqlite3.Database, |
| 21 | }); |
| 22 | |
| 23 | await db.exec("PRAGMA busy_timeout = 3000;"); |
| 24 | |
| 25 | await db.run(` |
| 26 | CREATE TABLE IF NOT EXISTS cache ( |
| 27 | key TEXT PRIMARY KEY, |
| 28 | value TEXT NOT NULL, |
| 29 | timestamp INTEGER NOT NULL |
| 30 | ) |
| 31 | `); |
| 32 | |
| 33 | return new AutocompleteLruCache(db); |
| 34 | } |
| 35 | |
| 36 | async get(prefix: string): Promise<string | undefined> { |
| 37 | // NOTE: Right now prompts with different suffixes will be considered the same |
| 38 | |
| 39 | // If the query is "co" and we have "c" -> "ontinue" in the cache, |
| 40 | // we should return "ntinue" as the completion. |
| 41 | // Have to make sure we take the key with shortest length |
| 42 | const truncatedPrefix = truncateSqliteLikePattern(prefix); |
| 43 | try { |
| 44 | const result = await this.db.get( |
| 45 | "SELECT key, value FROM cache WHERE ? LIKE key || '%' ORDER BY LENGTH(key) DESC LIMIT 1", |
| 46 | truncatedPrefix, |
| 47 | ); |
| 48 | // Validate that the cached completion is a valid completion for the prefix |
| 49 | if ( |
| 50 | result && |
| 51 | result.value.startsWith(truncatedPrefix.slice(result.key.length)) |
| 52 | ) { |
| 53 | await this.db.run( |
| 54 | "UPDATE cache SET timestamp = ? WHERE key = ?", |
| 55 | Date.now(), |
| 56 | truncatedPrefix, |
| 57 | ); |
| 58 | // And then truncate so we aren't writing something that's already there |
| 59 | return result.value.slice(truncatedPrefix.length - result.key.length); |
| 60 | } |
| 61 | } catch (e) { |
| 62 | // catches e.g. old SQLITE LIKE OR GLOB PATTERN TOO COMPLEX |
| 63 | console.error(e); |
| 64 | } |
| 65 | |
| 66 | return undefined; |
| 67 | } |
| 68 | |
| 69 | async put(prefix: string, completion: string) { |
| 70 | const release = await this.mutex.acquire(); |
| 71 | |
| 72 | const truncatedPrefix = truncateSqliteLikePattern(prefix); |
| 73 | try { |
| 74 | await this.db.run("BEGIN TRANSACTION"); |
| 75 | |
| 76 | try { |
| 77 | const result = await this.db.get( |
| 78 | "SELECT key FROM cache WHERE key = ?", |
| 79 | truncatedPrefix, |
| 80 | ); |
| 81 | |
| 82 | if (result) { |
| 83 | await this.db.run( |
| 84 | "UPDATE cache SET value = ?, timestamp = ? WHERE key = ?", |
| 85 | completion, |
| 86 | Date.now(), |
| 87 | truncatedPrefix, |
| 88 | ); |
| 89 | } else { |
| 90 | const count = await this.db.get( |
| 91 | "SELECT COUNT(*) as count FROM cache", |
| 92 | ); |
| 93 | |
| 94 | if (count.count >= AutocompleteLruCache.capacity) { |
| 95 | await this.db.run( |
| 96 | "DELETE FROM cache WHERE key = (SELECT key FROM cache ORDER BY timestamp ASC LIMIT 1)", |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | await this.db.run( |
| 101 | "INSERT INTO cache (key, value, timestamp) VALUES (?, ?, ?)", |
| 102 | truncatedPrefix, |
| 103 | completion, |
| 104 | Date.now(), |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | await this.db.run("COMMIT"); |
| 109 | } catch (error) { |
| 110 | await this.db.run("ROLLBACK"); |
| 111 | throw error; |
| 112 | } |
| 113 | } catch (e) { |
| 114 | console.error("Error creating transaction: ", e); |
| 115 | } finally { |
| 116 | release(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | export default AutocompleteLruCache; |