@ocap/statedb-sqlite 1.31.7 → 1.31.9

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.
package/esm/kysely.d.mts CHANGED
@@ -92,8 +92,25 @@ interface SqliteConfig {
92
92
  * Disabled by default (0 / undefined).
93
93
  */
94
94
  walCheckpointIntervalMs?: number;
95
+ /**
96
+ * `PRAGMA mmap_size` in bytes, applied to every better-sqlite3 / bun:sqlite connection (readonly included).
97
+ * Memory-mapped reads skip the pread syscall + copy into the small page cache on large databases.
98
+ * SQLite clamps the value to its compile-time `SQLITE_MAX_MMAP_SIZE` (better-sqlite3 11.x: 2147418112).
99
+ * `0` disables. Falls back to the `SQLITE_STATEDB_MMAP_SIZE` env var, then 8 GiB.
100
+ * Invalid values (negative, fractional, non-numeric) are ignored with a warning.
101
+ */
102
+ mmapSize?: number;
95
103
  }
96
104
  declare function resolveWalCheckpointIntervalMs(config: SqliteConfig): number | undefined;
105
+ /** Default `PRAGMA mmap_size` (8 GiB). */
106
+ declare const DEFAULT_MMAP_SIZE: number;
107
+ /** Env var overriding the default `PRAGMA mmap_size` (bytes, `0` disables). */
108
+ declare const MMAP_SIZE_ENV = "SQLITE_STATEDB_MMAP_SIZE";
109
+ /**
110
+ * Resolve `PRAGMA mmap_size`: valid `config.mmapSize` wins, then a valid `SQLITE_STATEDB_MMAP_SIZE`, then
111
+ * `DEFAULT_MMAP_SIZE`. Invalid input never becomes `0` (an empty env var is not "disable").
112
+ */
113
+ declare function resolveMmapSize(config: SqliteConfig, env?: Record<string, string | undefined>): number;
97
114
  /**
98
115
  * Create a Kysely instance with SQLite dialect
99
116
  * Automatically uses DurableObjectSqliteDialect when `config.doStorage` is provided,
@@ -112,6 +129,11 @@ declare function resolveWalCheckpointIntervalMs(config: SqliteConfig): number |
112
129
  * ```
113
130
  */
114
131
  declare function createKysely(config: SqliteConfig): Kysely<Database>;
132
+ /**
133
+ * Open a better-sqlite3 connection and apply connection pragmas (Node.js runtime path).
134
+ * Exported so tests running under bun can exercise this path with a fake driver.
135
+ */
136
+ declare function openBetterSqlite3(BetterSqlite3: new (filename: string, options: Record<string, unknown>) => any, filename: string, config: SqliteConfig): any;
115
137
  /**
116
138
  * Get the underlying SQLite database instance from Kysely
117
139
  * Useful for executing raw SQL or pragmas
@@ -119,4 +141,4 @@ declare function createKysely(config: SqliteConfig): Kysely<Database>;
119
141
  */
120
142
  declare function getRawDatabase(db: Kysely<Database>): any;
121
143
  //#endregion
122
- export { DurableObjectSqliteDialect, DurableObjectSqliteDialectConfig, IDurableObjectSqlStorage, IDurableObjectSqlStorageCursor, SqliteConfig, createKysely, getDoStorageWriteEpoch, getRawDatabase, resolveWalCheckpointIntervalMs };
144
+ export { DEFAULT_MMAP_SIZE, DurableObjectSqliteDialect, DurableObjectSqliteDialectConfig, IDurableObjectSqlStorage, IDurableObjectSqlStorageCursor, MMAP_SIZE_ENV, SqliteConfig, createKysely, getDoStorageWriteEpoch, getRawDatabase, openBetterSqlite3, resolveMmapSize, resolveWalCheckpointIntervalMs };
package/esm/kysely.mjs CHANGED
@@ -1,7 +1,11 @@
1
1
  import { __require } from "./_virtual/rolldown_runtime.mjs";
2
2
  import { Kysely, SqliteAdapter, SqliteDialect, SqliteIntrospector, SqliteQueryCompiler } from "kysely";
3
+ import { instrumentSqlTiming } from "@ocap/util/lib/sql-timing";
3
4
 
4
5
  //#region src/kysely.ts
6
+ const timeConnection = async (connection) => {
7
+ instrumentSqlTiming(connection);
8
+ };
5
9
  var ConnectionMutex = class {
6
10
  async lock() {
7
11
  while (this.promise) await this.promise;
@@ -1515,6 +1519,25 @@ function resolveWalCheckpointIntervalMs(config) {
1515
1519
  const v = config.walCheckpointIntervalMs ?? fromEnv;
1516
1520
  return Number.isFinite(v) && v > 0 ? Math.floor(v) : void 0;
1517
1521
  }
1522
+ /** Default `PRAGMA mmap_size` (8 GiB). */
1523
+ const DEFAULT_MMAP_SIZE = 8 * 1024 * 1024 * 1024;
1524
+ /** Env var overriding the default `PRAGMA mmap_size` (bytes, `0` disables). */
1525
+ const MMAP_SIZE_ENV = "SQLITE_STATEDB_MMAP_SIZE";
1526
+ function parseMmapSize(value, source) {
1527
+ if (value === void 0) return void 0;
1528
+ let n;
1529
+ if (typeof value === "number") n = value;
1530
+ else if (typeof value === "string" && /^\d+$/.test(value.trim())) n = Number(value.trim());
1531
+ if (n !== void 0 && Number.isSafeInteger(n) && n >= 0) return n;
1532
+ console.warn(`[sqlite] ignoring invalid mmap_size from ${source}: ${JSON.stringify(String(value))}`);
1533
+ }
1534
+ /**
1535
+ * Resolve `PRAGMA mmap_size`: valid `config.mmapSize` wins, then a valid `SQLITE_STATEDB_MMAP_SIZE`, then
1536
+ * `DEFAULT_MMAP_SIZE`. Invalid input never becomes `0` (an empty env var is not "disable").
1537
+ */
1538
+ function resolveMmapSize(config, env = process.env) {
1539
+ return parseMmapSize(config.mmapSize, "config.mmapSize") ?? parseMmapSize(env[MMAP_SIZE_ENV], MMAP_SIZE_ENV) ?? DEFAULT_MMAP_SIZE;
1540
+ }
1518
1541
  /**
1519
1542
  * Create a Kysely instance with SQLite dialect
1520
1543
  * Automatically uses DurableObjectSqliteDialect when `config.doStorage` is provided,
@@ -1544,21 +1567,35 @@ function createKysely(config) {
1544
1567
  const bunReq = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : typeof __require === "function" ? __require : import.meta.require;
1545
1568
  const { BunSqliteDialect } = bunReq("kysely-bun-sqlite");
1546
1569
  const { Database: BunDatabase } = bunReq("bun:sqlite");
1547
- const sqlite$1 = new BunDatabase(filename, {
1570
+ const sqlite = new BunDatabase(filename, {
1548
1571
  create: config.readonly && filename !== ":memory:" ? false : config.create !== false,
1549
1572
  readonly: config.readonly === true
1550
1573
  });
1551
1574
  if (config.walMode !== false && !config.readonly && filename !== ":memory:") {
1552
- sqlite$1.exec("PRAGMA journal_mode = WAL");
1553
- sqlite$1.exec("PRAGMA synchronous = NORMAL");
1575
+ sqlite.exec("PRAGMA journal_mode = WAL");
1576
+ sqlite.exec("PRAGMA synchronous = NORMAL");
1554
1577
  const walCp = resolveWalAutoCheckpoint(config);
1555
- if (walCp !== void 0) sqlite$1.exec(`PRAGMA wal_autocheckpoint = ${walCp}`);
1578
+ if (walCp !== void 0) sqlite.exec(`PRAGMA wal_autocheckpoint = ${walCp}`);
1556
1579
  }
1557
- sqlite$1.exec("PRAGMA foreign_keys = ON");
1558
- sqlite$1.exec("PRAGMA busy_timeout = 5000");
1559
- return new Kysely({ dialect: new BunSqliteDialect({ database: sqlite$1 }) });
1560
- }
1561
- const sqlite = new (__require("better-sqlite3"))(filename, {
1580
+ sqlite.exec("PRAGMA foreign_keys = ON");
1581
+ sqlite.exec("PRAGMA busy_timeout = 5000");
1582
+ sqlite.exec(`PRAGMA mmap_size = ${resolveMmapSize(config)}`);
1583
+ return new Kysely({ dialect: new BunSqliteDialect({
1584
+ database: sqlite,
1585
+ onCreateConnection: timeConnection
1586
+ }) });
1587
+ }
1588
+ return new Kysely({ dialect: new SqliteDialect({
1589
+ database: openBetterSqlite3(__require("better-sqlite3"), filename, config),
1590
+ onCreateConnection: timeConnection
1591
+ }) });
1592
+ }
1593
+ /**
1594
+ * Open a better-sqlite3 connection and apply connection pragmas (Node.js runtime path).
1595
+ * Exported so tests running under bun can exercise this path with a fake driver.
1596
+ */
1597
+ function openBetterSqlite3(BetterSqlite3, filename, config) {
1598
+ const sqlite = new BetterSqlite3(filename, {
1562
1599
  readonly: config.readonly === true,
1563
1600
  fileMustExist: config.readonly === true || config.create === false
1564
1601
  });
@@ -1570,7 +1607,8 @@ function createKysely(config) {
1570
1607
  }
1571
1608
  sqlite.pragma("foreign_keys = ON");
1572
1609
  sqlite.pragma("busy_timeout = 5000");
1573
- return new Kysely({ dialect: new SqliteDialect({ database: sqlite }) });
1610
+ sqlite.pragma(`mmap_size = ${resolveMmapSize(config)}`);
1611
+ return sqlite;
1574
1612
  }
1575
1613
  /**
1576
1614
  * Get the underlying SQLite database instance from Kysely
@@ -1583,4 +1621,4 @@ function getRawDatabase(db) {
1583
1621
  }
1584
1622
 
1585
1623
  //#endregion
1586
- export { DurableObjectSqliteDialect, createKysely, getDoStorageWriteEpoch, getRawDatabase, resolveWalCheckpointIntervalMs };
1624
+ export { DEFAULT_MMAP_SIZE, DurableObjectSqliteDialect, MMAP_SIZE_ENV, createKysely, getDoStorageWriteEpoch, getRawDatabase, openBetterSqlite3, resolveMmapSize, resolveWalCheckpointIntervalMs };
package/lib/kysely.cjs CHANGED
@@ -1,7 +1,11 @@
1
1
  const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
2
  let kysely = require("kysely");
3
+ let _ocap_util_lib_sql_timing = require("@ocap/util/lib/sql-timing");
3
4
 
4
5
  //#region src/kysely.ts
6
+ const timeConnection = async (connection) => {
7
+ (0, _ocap_util_lib_sql_timing.instrumentSqlTiming)(connection);
8
+ };
5
9
  var ConnectionMutex = class {
6
10
  async lock() {
7
11
  while (this.promise) await this.promise;
@@ -1515,6 +1519,25 @@ function resolveWalCheckpointIntervalMs(config) {
1515
1519
  const v = config.walCheckpointIntervalMs ?? fromEnv;
1516
1520
  return Number.isFinite(v) && v > 0 ? Math.floor(v) : void 0;
1517
1521
  }
1522
+ /** Default `PRAGMA mmap_size` (8 GiB). */
1523
+ const DEFAULT_MMAP_SIZE = 8 * 1024 * 1024 * 1024;
1524
+ /** Env var overriding the default `PRAGMA mmap_size` (bytes, `0` disables). */
1525
+ const MMAP_SIZE_ENV = "SQLITE_STATEDB_MMAP_SIZE";
1526
+ function parseMmapSize(value, source) {
1527
+ if (value === void 0) return void 0;
1528
+ let n;
1529
+ if (typeof value === "number") n = value;
1530
+ else if (typeof value === "string" && /^\d+$/.test(value.trim())) n = Number(value.trim());
1531
+ if (n !== void 0 && Number.isSafeInteger(n) && n >= 0) return n;
1532
+ console.warn(`[sqlite] ignoring invalid mmap_size from ${source}: ${JSON.stringify(String(value))}`);
1533
+ }
1534
+ /**
1535
+ * Resolve `PRAGMA mmap_size`: valid `config.mmapSize` wins, then a valid `SQLITE_STATEDB_MMAP_SIZE`, then
1536
+ * `DEFAULT_MMAP_SIZE`. Invalid input never becomes `0` (an empty env var is not "disable").
1537
+ */
1538
+ function resolveMmapSize(config, env = process.env) {
1539
+ return parseMmapSize(config.mmapSize, "config.mmapSize") ?? parseMmapSize(env[MMAP_SIZE_ENV], MMAP_SIZE_ENV) ?? DEFAULT_MMAP_SIZE;
1540
+ }
1518
1541
  /**
1519
1542
  * Create a Kysely instance with SQLite dialect
1520
1543
  * Automatically uses DurableObjectSqliteDialect when `config.doStorage` is provided,
@@ -1544,21 +1567,35 @@ function createKysely(config) {
1544
1567
  const bunReq = typeof __non_webpack_require__ === "function" ? __non_webpack_require__ : typeof require === "function" ? require : {}.require;
1545
1568
  const { BunSqliteDialect } = bunReq("kysely-bun-sqlite");
1546
1569
  const { Database: BunDatabase } = bunReq("bun:sqlite");
1547
- const sqlite$1 = new BunDatabase(filename, {
1570
+ const sqlite = new BunDatabase(filename, {
1548
1571
  create: config.readonly && filename !== ":memory:" ? false : config.create !== false,
1549
1572
  readonly: config.readonly === true
1550
1573
  });
1551
1574
  if (config.walMode !== false && !config.readonly && filename !== ":memory:") {
1552
- sqlite$1.exec("PRAGMA journal_mode = WAL");
1553
- sqlite$1.exec("PRAGMA synchronous = NORMAL");
1575
+ sqlite.exec("PRAGMA journal_mode = WAL");
1576
+ sqlite.exec("PRAGMA synchronous = NORMAL");
1554
1577
  const walCp = resolveWalAutoCheckpoint(config);
1555
- if (walCp !== void 0) sqlite$1.exec(`PRAGMA wal_autocheckpoint = ${walCp}`);
1578
+ if (walCp !== void 0) sqlite.exec(`PRAGMA wal_autocheckpoint = ${walCp}`);
1556
1579
  }
1557
- sqlite$1.exec("PRAGMA foreign_keys = ON");
1558
- sqlite$1.exec("PRAGMA busy_timeout = 5000");
1559
- return new kysely.Kysely({ dialect: new BunSqliteDialect({ database: sqlite$1 }) });
1560
- }
1561
- const sqlite = new (require("better-sqlite3"))(filename, {
1580
+ sqlite.exec("PRAGMA foreign_keys = ON");
1581
+ sqlite.exec("PRAGMA busy_timeout = 5000");
1582
+ sqlite.exec(`PRAGMA mmap_size = ${resolveMmapSize(config)}`);
1583
+ return new kysely.Kysely({ dialect: new BunSqliteDialect({
1584
+ database: sqlite,
1585
+ onCreateConnection: timeConnection
1586
+ }) });
1587
+ }
1588
+ return new kysely.Kysely({ dialect: new kysely.SqliteDialect({
1589
+ database: openBetterSqlite3(require("better-sqlite3"), filename, config),
1590
+ onCreateConnection: timeConnection
1591
+ }) });
1592
+ }
1593
+ /**
1594
+ * Open a better-sqlite3 connection and apply connection pragmas (Node.js runtime path).
1595
+ * Exported so tests running under bun can exercise this path with a fake driver.
1596
+ */
1597
+ function openBetterSqlite3(BetterSqlite3, filename, config) {
1598
+ const sqlite = new BetterSqlite3(filename, {
1562
1599
  readonly: config.readonly === true,
1563
1600
  fileMustExist: config.readonly === true || config.create === false
1564
1601
  });
@@ -1570,7 +1607,8 @@ function createKysely(config) {
1570
1607
  }
1571
1608
  sqlite.pragma("foreign_keys = ON");
1572
1609
  sqlite.pragma("busy_timeout = 5000");
1573
- return new kysely.Kysely({ dialect: new kysely.SqliteDialect({ database: sqlite }) });
1610
+ sqlite.pragma(`mmap_size = ${resolveMmapSize(config)}`);
1611
+ return sqlite;
1574
1612
  }
1575
1613
  /**
1576
1614
  * Get the underlying SQLite database instance from Kysely
@@ -1583,8 +1621,12 @@ function getRawDatabase(db) {
1583
1621
  }
1584
1622
 
1585
1623
  //#endregion
1624
+ exports.DEFAULT_MMAP_SIZE = DEFAULT_MMAP_SIZE;
1586
1625
  exports.DurableObjectSqliteDialect = DurableObjectSqliteDialect;
1626
+ exports.MMAP_SIZE_ENV = MMAP_SIZE_ENV;
1587
1627
  exports.createKysely = createKysely;
1588
1628
  exports.getDoStorageWriteEpoch = getDoStorageWriteEpoch;
1589
1629
  exports.getRawDatabase = getRawDatabase;
1630
+ exports.openBetterSqlite3 = openBetterSqlite3;
1631
+ exports.resolveMmapSize = resolveMmapSize;
1590
1632
  exports.resolveWalCheckpointIntervalMs = resolveWalCheckpointIntervalMs;
package/lib/kysely.d.cts CHANGED
@@ -92,8 +92,25 @@ interface SqliteConfig {
92
92
  * Disabled by default (0 / undefined).
93
93
  */
94
94
  walCheckpointIntervalMs?: number;
95
+ /**
96
+ * `PRAGMA mmap_size` in bytes, applied to every better-sqlite3 / bun:sqlite connection (readonly included).
97
+ * Memory-mapped reads skip the pread syscall + copy into the small page cache on large databases.
98
+ * SQLite clamps the value to its compile-time `SQLITE_MAX_MMAP_SIZE` (better-sqlite3 11.x: 2147418112).
99
+ * `0` disables. Falls back to the `SQLITE_STATEDB_MMAP_SIZE` env var, then 8 GiB.
100
+ * Invalid values (negative, fractional, non-numeric) are ignored with a warning.
101
+ */
102
+ mmapSize?: number;
95
103
  }
96
104
  declare function resolveWalCheckpointIntervalMs(config: SqliteConfig): number | undefined;
105
+ /** Default `PRAGMA mmap_size` (8 GiB). */
106
+ declare const DEFAULT_MMAP_SIZE: number;
107
+ /** Env var overriding the default `PRAGMA mmap_size` (bytes, `0` disables). */
108
+ declare const MMAP_SIZE_ENV = "SQLITE_STATEDB_MMAP_SIZE";
109
+ /**
110
+ * Resolve `PRAGMA mmap_size`: valid `config.mmapSize` wins, then a valid `SQLITE_STATEDB_MMAP_SIZE`, then
111
+ * `DEFAULT_MMAP_SIZE`. Invalid input never becomes `0` (an empty env var is not "disable").
112
+ */
113
+ declare function resolveMmapSize(config: SqliteConfig, env?: Record<string, string | undefined>): number;
97
114
  /**
98
115
  * Create a Kysely instance with SQLite dialect
99
116
  * Automatically uses DurableObjectSqliteDialect when `config.doStorage` is provided,
@@ -112,6 +129,11 @@ declare function resolveWalCheckpointIntervalMs(config: SqliteConfig): number |
112
129
  * ```
113
130
  */
114
131
  declare function createKysely(config: SqliteConfig): Kysely<Database>;
132
+ /**
133
+ * Open a better-sqlite3 connection and apply connection pragmas (Node.js runtime path).
134
+ * Exported so tests running under bun can exercise this path with a fake driver.
135
+ */
136
+ declare function openBetterSqlite3(BetterSqlite3: new (filename: string, options: Record<string, unknown>) => any, filename: string, config: SqliteConfig): any;
115
137
  /**
116
138
  * Get the underlying SQLite database instance from Kysely
117
139
  * Useful for executing raw SQL or pragmas
@@ -119,4 +141,4 @@ declare function createKysely(config: SqliteConfig): Kysely<Database>;
119
141
  */
120
142
  declare function getRawDatabase(db: Kysely<Database>): any;
121
143
  //#endregion
122
- export { DurableObjectSqliteDialect, DurableObjectSqliteDialectConfig, IDurableObjectSqlStorage, IDurableObjectSqlStorageCursor, SqliteConfig, createKysely, getDoStorageWriteEpoch, getRawDatabase, resolveWalCheckpointIntervalMs };
144
+ export { DEFAULT_MMAP_SIZE, DurableObjectSqliteDialect, DurableObjectSqliteDialectConfig, IDurableObjectSqlStorage, IDurableObjectSqlStorageCursor, MMAP_SIZE_ENV, SqliteConfig, createKysely, getDoStorageWriteEpoch, getRawDatabase, openBetterSqlite3, resolveMmapSize, resolveWalCheckpointIntervalMs };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/statedb-sqlite",
3
3
  "description": "OCAP statedb adapter that uses SQLite as backend",
4
- "version": "1.31.7",
4
+ "version": "1.31.9",
5
5
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ArcBlock/blockchain/issues",
@@ -14,7 +14,7 @@
14
14
  "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"
15
15
  ],
16
16
  "devDependencies": {
17
- "@ocap/statedb-test": "1.31.7"
17
+ "@ocap/statedb-test": "1.31.9"
18
18
  },
19
19
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/statedb/sqlite",
20
20
  "keywords": [
@@ -63,12 +63,12 @@
63
63
  "clean": "rm -rf lib esm"
64
64
  },
65
65
  "dependencies": {
66
- "@arcblock/did": "1.31.7",
67
- "@arcblock/did-util": "1.31.7",
68
- "@ocap/state": "1.31.7",
69
- "@ocap/statedb": "1.31.7",
70
- "@ocap/types": "1.31.7",
71
- "@ocap/util": "1.31.7",
66
+ "@arcblock/did": "1.31.9",
67
+ "@arcblock/did-util": "1.31.9",
68
+ "@ocap/state": "1.31.9",
69
+ "@ocap/statedb": "1.31.9",
70
+ "@ocap/types": "1.31.9",
71
+ "@ocap/util": "1.31.9",
72
72
  "better-sqlite3": "^11.8.1",
73
73
  "kysely": "^0.28.14",
74
74
  "kysely-bun-sqlite": "^0.4.0",