@ohos-ports/libsql-client 0.18.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ export class SqlCache {
2
+ #owner;
3
+ #sqls;
4
+ capacity;
5
+ constructor(owner, capacity) {
6
+ this.#owner = owner;
7
+ this.#sqls = new Lru();
8
+ this.capacity = capacity;
9
+ }
10
+ // Replaces SQL strings with cached `hrana.Sql` objects in the statements in `hranaStmts`. After this
11
+ // function returns, we guarantee that all `hranaStmts` refer to valid (not closed) `hrana.Sql` objects,
12
+ // but _we may invalidate any other `hrana.Sql` objects_ (by closing them, thus removing them from the
13
+ // server).
14
+ //
15
+ // In practice, this means that after calling this function, you can use the statements only up to the
16
+ // first `await`, because concurrent code may also use the cache and invalidate those statements.
17
+ apply(hranaStmts) {
18
+ if (this.capacity <= 0) {
19
+ return;
20
+ }
21
+ const usedSqlObjs = new Set();
22
+ for (const hranaStmt of hranaStmts) {
23
+ if (typeof hranaStmt.sql !== "string") {
24
+ continue;
25
+ }
26
+ const sqlText = hranaStmt.sql;
27
+ // Stored SQL cannot exceed 5kb.
28
+ // https://github.com/tursodatabase/libsql/blob/e9d637e051685f92b0da43849507b5ef4232fbeb/libsql-server/src/hrana/http/request.rs#L10
29
+ if (sqlText.length >= 5000) {
30
+ continue;
31
+ }
32
+ let sqlObj = this.#sqls.get(sqlText);
33
+ if (sqlObj === undefined) {
34
+ while (this.#sqls.size + 1 > this.capacity) {
35
+ const [evictSqlText, evictSqlObj] = this.#sqls.peekLru();
36
+ if (usedSqlObjs.has(evictSqlObj)) {
37
+ // The SQL object that we are trying to evict is already in use in this batch, so we
38
+ // must not evict and close it.
39
+ break;
40
+ }
41
+ evictSqlObj.close();
42
+ this.#sqls.delete(evictSqlText);
43
+ }
44
+ if (this.#sqls.size + 1 <= this.capacity) {
45
+ sqlObj = this.#owner.storeSql(sqlText);
46
+ this.#sqls.set(sqlText, sqlObj);
47
+ }
48
+ }
49
+ if (sqlObj !== undefined) {
50
+ hranaStmt.sql = sqlObj;
51
+ usedSqlObjs.add(sqlObj);
52
+ }
53
+ }
54
+ }
55
+ }
56
+ class Lru {
57
+ // This maps keys to the cache values. The entries are ordered by their last use (entires that were used
58
+ // most recently are at the end).
59
+ #cache;
60
+ constructor() {
61
+ this.#cache = new Map();
62
+ }
63
+ get(key) {
64
+ const value = this.#cache.get(key);
65
+ if (value !== undefined) {
66
+ // move the entry to the back of the Map
67
+ this.#cache.delete(key);
68
+ this.#cache.set(key, value);
69
+ }
70
+ return value;
71
+ }
72
+ set(key, value) {
73
+ this.#cache.set(key, value);
74
+ }
75
+ peekLru() {
76
+ for (const entry of this.#cache.entries()) {
77
+ return entry;
78
+ }
79
+ return undefined;
80
+ }
81
+ delete(key) {
82
+ this.#cache.delete(key);
83
+ }
84
+ get size() {
85
+ return this.#cache.size;
86
+ }
87
+ }
@@ -0,0 +1,53 @@
1
+ import Database from "@ohos-ports/libsql";
2
+ import type { Config, IntMode, Client, Transaction, TransactionMode, ResultSet, InStatement, InArgs, Replicated } from "@libsql/core/api";
3
+ import type { ExpandedConfig } from "@libsql/core/config";
4
+ export * from "@libsql/core/api";
5
+ export declare function createClient(config: Config): Client;
6
+ /** @private */
7
+ export declare function _createClient(config: ExpandedConfig): Client;
8
+ /**
9
+ * A client owns a small pool of connections to one database, in the same way
10
+ * that a remote client owns a set of hrana streams: every client operation
11
+ * borrows one for the duration of the call, and a transaction borrows one for
12
+ * its lifetime and gives it back on commit, rollback or close. Nothing is
13
+ * shared between an open transaction and the rest of the client.
14
+ *
15
+ * @private
16
+ */
17
+ export declare class ConnectionPool {
18
+ #private;
19
+ constructor(path: string, options: Database.Options, maxConnections: number);
20
+ acquire(forTransaction?: boolean): Promise<Database.Database>;
21
+ acquireSync(forTransaction?: boolean): Database.Database;
22
+ release(db: Database.Database): void;
23
+ close(): void;
24
+ reopen(): void;
25
+ }
26
+ export declare class Sqlite3Client implements Client {
27
+ #private;
28
+ closed: boolean;
29
+ protocol: "file";
30
+ /** @private */
31
+ constructor(pool: ConnectionPool, intMode: IntMode);
32
+ execute(stmtOrSql: InStatement | string, args?: InArgs): Promise<ResultSet>;
33
+ batch(stmts: Array<InStatement | [string, InArgs?]>, mode?: TransactionMode): Promise<Array<ResultSet>>;
34
+ migrate(stmts: Array<InStatement>): Promise<Array<ResultSet>>;
35
+ transaction(mode?: TransactionMode): Promise<Transaction>;
36
+ executeMultiple(sql: string): Promise<void>;
37
+ sync(): Promise<Replicated>;
38
+ reconnect(): Promise<void>;
39
+ close(): void;
40
+ }
41
+ export declare class Sqlite3Transaction implements Transaction {
42
+ #private;
43
+ /** @private */
44
+ constructor(database: Database.Database, intMode: IntMode, release: (db: Database.Database) => void);
45
+ execute(stmt: InStatement): Promise<ResultSet>;
46
+ execute(sql: string, args?: InArgs): Promise<ResultSet>;
47
+ batch(stmts: Array<InStatement | [string, InArgs?]>): Promise<Array<ResultSet>>;
48
+ executeMultiple(sql: string): Promise<void>;
49
+ rollback(): Promise<void>;
50
+ commit(): Promise<void>;
51
+ close(): void;
52
+ get closed(): boolean;
53
+ }