@supalive/core 0.1.4 → 0.1.6
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/README.md +17 -2
- package/dist/bytes-AuEkyxeU.js +19 -0
- package/dist/bytes-AuEkyxeU.js.map +1 -0
- package/dist/codec-BHAmGEIQ.js +62 -0
- package/dist/codec-BHAmGEIQ.js.map +1 -0
- package/dist/helper-CiacMqje.d.ts +14 -0
- package/dist/helper-CiacMqje.d.ts.map +1 -0
- package/dist/helper-wGbzz6zo.js +42 -0
- package/dist/helper-wGbzz6zo.js.map +1 -0
- package/dist/index-CaWMDqq0.d.ts +1754 -0
- package/dist/index-CaWMDqq0.d.ts.map +1 -0
- package/dist/mysql-Cxd4Uq5p.d.ts +104 -0
- package/dist/mysql-Cxd4Uq5p.d.ts.map +1 -0
- package/dist/mysql-bvnjYWvZ.js +570 -0
- package/dist/mysql-bvnjYWvZ.js.map +1 -0
- package/dist/overlap-checker-Ct_HP_iB.js +187 -0
- package/dist/overlap-checker-Ct_HP_iB.js.map +1 -0
- package/dist/postgres-B8u1YN8X.js +824 -0
- package/dist/postgres-B8u1YN8X.js.map +1 -0
- package/dist/postgres-Ck-xt72U.d.ts +108 -0
- package/dist/postgres-Ck-xt72U.d.ts.map +1 -0
- package/dist/procedure-DX87tyef.js +124 -0
- package/dist/procedure-DX87tyef.js.map +1 -0
- package/dist/query-Bh0RNxDz.js +673 -0
- package/dist/query-Bh0RNxDz.js.map +1 -0
- package/dist/realtime_db-DOCgqbwi.js +247 -0
- package/dist/realtime_db-DOCgqbwi.js.map +1 -0
- package/dist/router-DlTYWpop.js +81 -0
- package/dist/router-DlTYWpop.js.map +1 -0
- package/dist/schema-Dd7u0A1L.js +109 -0
- package/dist/schema-Dd7u0A1L.js.map +1 -0
- package/dist/src/client/index.d.ts +2 -17
- package/dist/src/client/index.js +1152 -18
- package/dist/src/client/index.js.map +1 -1
- package/dist/src/exports/mysql.d.ts +2 -2
- package/dist/src/exports/mysql.js +2 -2
- package/dist/src/exports/postgres.d.ts +2 -2
- package/dist/src/exports/postgres.js +2 -2
- package/dist/src/exports/procedure.d.ts +3 -5
- package/dist/src/exports/procedure.js +6 -5
- package/dist/src/exports/schema-sql.d.ts +18 -0
- package/dist/src/exports/schema-sql.d.ts.map +1 -0
- package/dist/src/exports/schema-sql.js +161 -0
- package/dist/src/exports/schema-sql.js.map +1 -0
- package/dist/src/exports/server.d.ts +368 -2
- package/dist/src/exports/server.d.ts.map +1 -1
- package/dist/src/exports/server.js +1346 -2
- package/dist/src/exports/server.js.map +1 -1
- package/dist/src/exports/subscription-manager-worker-entry.d.ts +1 -0
- package/dist/src/exports/subscription-manager-worker-entry.js +115 -0
- package/dist/src/exports/subscription-manager-worker-entry.js.map +1 -0
- package/dist/src/exports/types.d.ts +4 -6
- package/dist/src/exports/types.js +5 -6
- package/dist/subscription-worker-pahX0zxg.js +511 -0
- package/dist/subscription-worker-pahX0zxg.js.map +1 -0
- package/dist/types_client_rpc-CIsjQQGG.js +105 -0
- package/dist/types_client_rpc-CIsjQQGG.js.map +1 -0
- package/dist/types_db-DXj_l9n-.js +168 -0
- package/dist/types_db-DXj_l9n-.js.map +1 -0
- package/dist/types_server-Dk6o_B7N.js +24 -0
- package/dist/types_server-Dk6o_B7N.js.map +1 -0
- package/dist/types_server-TFo6SWDH.d.ts +216 -0
- package/dist/types_server-TFo6SWDH.d.ts.map +1 -0
- package/package.json +7 -3
- package/dist/src/client/caller_client.d.ts +0 -42
- package/dist/src/client/caller_client.d.ts.map +0 -1
- package/dist/src/client/caller_client.js +0 -77
- package/dist/src/client/caller_client.js.map +0 -1
- package/dist/src/client/client.d.ts +0 -149
- package/dist/src/client/client.d.ts.map +0 -1
- package/dist/src/client/client.js +0 -308
- package/dist/src/client/client.js.map +0 -1
- package/dist/src/client/index.d.ts.map +0 -1
- package/dist/src/client/sub_key.d.ts +0 -20
- package/dist/src/client/sub_key.d.ts.map +0 -1
- package/dist/src/client/sub_key.js +0 -37
- package/dist/src/client/sub_key.js.map +0 -1
- package/dist/src/client/ws_client_manager.d.ts +0 -268
- package/dist/src/client/ws_client_manager.d.ts.map +0 -1
- package/dist/src/client/ws_client_manager.js +0 -1013
- package/dist/src/client/ws_client_manager.js.map +0 -1
- package/dist/src/config.d.ts +0 -117
- package/dist/src/config.d.ts.map +0 -1
- package/dist/src/config.js +0 -2
- package/dist/src/config.js.map +0 -1
- package/dist/src/db/bytes.d.ts +0 -4
- package/dist/src/db/bytes.d.ts.map +0 -1
- package/dist/src/db/bytes.js +0 -24
- package/dist/src/db/bytes.js.map +0 -1
- package/dist/src/db/cache.d.ts +0 -30
- package/dist/src/db/cache.d.ts.map +0 -1
- package/dist/src/db/cache.js +0 -153
- package/dist/src/db/cache.js.map +0 -1
- package/dist/src/db/codec.d.ts +0 -10
- package/dist/src/db/codec.d.ts.map +0 -1
- package/dist/src/db/codec.js +0 -73
- package/dist/src/db/codec.js.map +0 -1
- package/dist/src/db/context.d.ts +0 -28
- package/dist/src/db/context.d.ts.map +0 -1
- package/dist/src/db/context.js +0 -147
- package/dist/src/db/context.js.map +0 -1
- package/dist/src/db/db.d.ts +0 -154
- package/dist/src/db/db.d.ts.map +0 -1
- package/dist/src/db/db.js +0 -23
- package/dist/src/db/db.js.map +0 -1
- package/dist/src/db/mysql.d.ts +0 -120
- package/dist/src/db/mysql.d.ts.map +0 -1
- package/dist/src/db/mysql.js +0 -708
- package/dist/src/db/mysql.js.map +0 -1
- package/dist/src/db/postgres.d.ts +0 -116
- package/dist/src/db/postgres.d.ts.map +0 -1
- package/dist/src/db/postgres.js +0 -998
- package/dist/src/db/postgres.js.map +0 -1
- package/dist/src/db/query.d.ts +0 -174
- package/dist/src/db/query.d.ts.map +0 -1
- package/dist/src/db/query.js +0 -790
- package/dist/src/db/query.js.map +0 -1
- package/dist/src/db/query_definition.d.ts +0 -54
- package/dist/src/db/query_definition.d.ts.map +0 -1
- package/dist/src/db/query_definition.js +0 -47
- package/dist/src/db/query_definition.js.map +0 -1
- package/dist/src/db/realtime_db.d.ts +0 -19
- package/dist/src/db/realtime_db.d.ts.map +0 -1
- package/dist/src/db/realtime_db.js +0 -98
- package/dist/src/db/realtime_db.js.map +0 -1
- package/dist/src/db/schema.d.ts +0 -110
- package/dist/src/db/schema.d.ts.map +0 -1
- package/dist/src/db/schema.js +0 -47
- package/dist/src/db/schema.js.map +0 -1
- package/dist/src/db/types_client_rpc.d.ts +0 -157
- package/dist/src/db/types_client_rpc.d.ts.map +0 -1
- package/dist/src/db/types_client_rpc.js +0 -106
- package/dist/src/db/types_client_rpc.js.map +0 -1
- package/dist/src/db/types_db.d.ts +0 -236
- package/dist/src/db/types_db.d.ts.map +0 -1
- package/dist/src/db/types_db.js +0 -188
- package/dist/src/db/types_db.js.map +0 -1
- package/dist/src/exports/mysql.d.ts.map +0 -1
- package/dist/src/exports/mysql.js.map +0 -1
- package/dist/src/exports/postgres.d.ts.map +0 -1
- package/dist/src/exports/postgres.js.map +0 -1
- package/dist/src/exports/procedure.d.ts.map +0 -1
- package/dist/src/exports/procedure.js.map +0 -1
- package/dist/src/exports/types.d.ts.map +0 -1
- package/dist/src/exports/types.js.map +0 -1
- package/dist/src/helper.d.ts +0 -11
- package/dist/src/helper.d.ts.map +0 -1
- package/dist/src/helper.js +0 -45
- package/dist/src/helper.js.map +0 -1
- package/dist/src/logger.d.ts +0 -7
- package/dist/src/logger.d.ts.map +0 -1
- package/dist/src/logger.js +0 -47
- package/dist/src/logger.js.map +0 -1
- package/dist/src/router/index.d.ts +0 -4
- package/dist/src/router/index.d.ts.map +0 -1
- package/dist/src/router/index.js +0 -4
- package/dist/src/router/index.js.map +0 -1
- package/dist/src/router/procedure.d.ts +0 -204
- package/dist/src/router/procedure.d.ts.map +0 -1
- package/dist/src/router/procedure.js +0 -122
- package/dist/src/router/procedure.js.map +0 -1
- package/dist/src/router/router.d.ts +0 -88
- package/dist/src/router/router.d.ts.map +0 -1
- package/dist/src/router/router.js +0 -88
- package/dist/src/router/router.js.map +0 -1
- package/dist/src/server/hash.d.ts +0 -12
- package/dist/src/server/hash.d.ts.map +0 -1
- package/dist/src/server/hash.js +0 -20
- package/dist/src/server/hash.js.map +0 -1
- package/dist/src/server/hash.test.d.ts +0 -2
- package/dist/src/server/hash.test.d.ts.map +0 -1
- package/dist/src/server/hash.test.js +0 -43
- package/dist/src/server/hash.test.js.map +0 -1
- package/dist/src/server/index.d.ts +0 -9
- package/dist/src/server/index.d.ts.map +0 -1
- package/dist/src/server/index.js +0 -8
- package/dist/src/server/index.js.map +0 -1
- package/dist/src/server/overlap-checker.d.ts +0 -18
- package/dist/src/server/overlap-checker.d.ts.map +0 -1
- package/dist/src/server/overlap-checker.js +0 -247
- package/dist/src/server/overlap-checker.js.map +0 -1
- package/dist/src/server/sub_hash.d.ts +0 -23
- package/dist/src/server/sub_hash.d.ts.map +0 -1
- package/dist/src/server/sub_hash.js +0 -38
- package/dist/src/server/sub_hash.js.map +0 -1
- package/dist/src/server/subscription-manager-client.d.ts +0 -120
- package/dist/src/server/subscription-manager-client.d.ts.map +0 -1
- package/dist/src/server/subscription-manager-client.js +0 -207
- package/dist/src/server/subscription-manager-client.js.map +0 -1
- package/dist/src/server/subscription-manager-types.d.ts +0 -168
- package/dist/src/server/subscription-manager-types.d.ts.map +0 -1
- package/dist/src/server/subscription-manager-types.js +0 -94
- package/dist/src/server/subscription-manager-types.js.map +0 -1
- package/dist/src/server/subscription-manager-worker-entry.d.ts +0 -2
- package/dist/src/server/subscription-manager-worker-entry.d.ts.map +0 -1
- package/dist/src/server/subscription-manager-worker-entry.js +0 -127
- package/dist/src/server/subscription-manager-worker-entry.js.map +0 -1
- package/dist/src/server/subscription-manager-worker-protocol.d.ts +0 -40
- package/dist/src/server/subscription-manager-worker-protocol.d.ts.map +0 -1
- package/dist/src/server/subscription-manager-worker-protocol.js +0 -20
- package/dist/src/server/subscription-manager-worker-protocol.js.map +0 -1
- package/dist/src/server/subscription-manager.d.ts +0 -32
- package/dist/src/server/subscription-manager.d.ts.map +0 -1
- package/dist/src/server/subscription-manager.js +0 -249
- package/dist/src/server/subscription-manager.js.map +0 -1
- package/dist/src/server/subscription-manager.test.d.ts +0 -2
- package/dist/src/server/subscription-manager.test.d.ts.map +0 -1
- package/dist/src/server/subscription-manager.test.js +0 -610
- package/dist/src/server/subscription-manager.test.js.map +0 -1
- package/dist/src/server/subscription-worker.d.ts +0 -32
- package/dist/src/server/subscription-worker.d.ts.map +0 -1
- package/dist/src/server/subscription-worker.js +0 -190
- package/dist/src/server/subscription-worker.js.map +0 -1
- package/dist/src/server/supalive-server.d.ts +0 -158
- package/dist/src/server/supalive-server.d.ts.map +0 -1
- package/dist/src/server/supalive-server.js +0 -1048
- package/dist/src/server/supalive-server.js.map +0 -1
- package/dist/src/server/types_server.d.ts +0 -96
- package/dist/src/server/types_server.d.ts.map +0 -1
- package/dist/src/server/types_server.js +0 -36
- package/dist/src/server/types_server.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
package/dist/src/db/mysql.js
DELETED
|
@@ -1,708 +0,0 @@
|
|
|
1
|
-
import mysql from "mysql2/promise";
|
|
2
|
-
import { normalizeIdToBytes, normalizeToBytes, OccConflictError } from "./types_db";
|
|
3
|
-
import { utf8Encode } from "./bytes";
|
|
4
|
-
// mysql2 expects Buffer (not plain Uint8Array) for binary params.
|
|
5
|
-
function toBufferView(bytes) {
|
|
6
|
-
if (Buffer.isBuffer(bytes))
|
|
7
|
-
return bytes;
|
|
8
|
-
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
9
|
-
}
|
|
10
|
-
import { checkLogsAffectReadSet, mapReadSetToRaw } from "../server/overlap-checker";
|
|
11
|
-
import { supaliveStringify } from "../helper";
|
|
12
|
-
/**
|
|
13
|
-
* mysql2 returns `ResultSetHeader` for non-SELECT queries (carrying
|
|
14
|
-
* `affectedRows`) and `RowDataPacket[]` for SELECTs. The previous shim
|
|
15
|
-
* blindly cast to T[] which made `rowCount` zero for UPDATE/DELETE, breaking
|
|
16
|
-
* CAS detection. This helper picks the right shape.
|
|
17
|
-
*/
|
|
18
|
-
function normalizeMysqlResult(raw) {
|
|
19
|
-
if (Array.isArray(raw)) {
|
|
20
|
-
return { rows: raw, rowCount: raw.length };
|
|
21
|
-
}
|
|
22
|
-
if (raw && typeof raw === "object" && "affectedRows" in raw) {
|
|
23
|
-
return { rows: [], rowCount: Number(raw.affectedRows) || 0 };
|
|
24
|
-
}
|
|
25
|
-
return { rows: [], rowCount: 0 };
|
|
26
|
-
}
|
|
27
|
-
export class MySqlDatabase {
|
|
28
|
-
constructor(logger, config, pool = undefined) {
|
|
29
|
-
this.logger = logger;
|
|
30
|
-
const { queryTimeoutMs, ...poolConfig } = config;
|
|
31
|
-
this.queryTimeoutMs = queryTimeoutMs ?? 0;
|
|
32
|
-
this.config = poolConfig;
|
|
33
|
-
if (pool) {
|
|
34
|
-
this.pool = pool;
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
this.pool = mysql.createPool(poolConfig);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
logger;
|
|
41
|
-
config;
|
|
42
|
-
queryTimeoutMs;
|
|
43
|
-
pool;
|
|
44
|
-
dbType = "mysql";
|
|
45
|
-
sqlBuilder = mysqlSql;
|
|
46
|
-
// Lazy per-table column-type cache. The apply path consults this for
|
|
47
|
-
// unnest carriers/casts in batched writes; we never re-query pg_attribute
|
|
48
|
-
// for a table we've already seen unless the cache is explicitly
|
|
49
|
-
// invalidated (e.g. after a migration). Shared with tx clients via
|
|
50
|
-
// adoptRegistries so a single startup load services all transactions.
|
|
51
|
-
columnTypes = new Map();
|
|
52
|
-
// Compiled-SQL cache keyed by (op, table, sorted column signature).
|
|
53
|
-
// Mirrors columnTypes — invalidated together when types change.
|
|
54
|
-
sqlCache = new Map();
|
|
55
|
-
/**
|
|
56
|
-
* Fetches actual column types from MySQL for `table` and returns a
|
|
57
|
-
* map of DB column name → MySQL type string (as produced by `COLUMN_TYPE`,
|
|
58
|
-
* e.g. `bigint`, `integer`, `varchar(32)`, `text`, `json`).
|
|
59
|
-
* No caching here — see {@link getColumnTypes} for the cached path.
|
|
60
|
-
*/
|
|
61
|
-
async loadColumnTypesFromMySql(table) {
|
|
62
|
-
const result = await this.query(`SELECT COLUMN_NAME AS column_name, COLUMN_TYPE AS mysql_type
|
|
63
|
-
FROM INFORMATION_SCHEMA.COLUMNS
|
|
64
|
-
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?
|
|
65
|
-
ORDER BY ORDINAL_POSITION`, [table]);
|
|
66
|
-
// MySQL folds unquoted identifiers to lowercase on the server side, so
|
|
67
|
-
// COLUMN_NAME comes back lowercase. JS-side column keys
|
|
68
|
-
// (`entry.data`) often arrive in camelCase from the schema layer; we
|
|
69
|
-
// lookup via lowercase to bridge the two cases without making callers
|
|
70
|
-
// worry about it.
|
|
71
|
-
const out = new Map();
|
|
72
|
-
for (const row of result.rows)
|
|
73
|
-
out.set(row.column_name.toLowerCase(), row.mysql_type);
|
|
74
|
-
return out;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Synchronous column-type lookup. The apply path calls this on every
|
|
78
|
-
* batched write, so it can't await — populate the cache via
|
|
79
|
-
* {@link bootstrapColumnTypes} at server start. Throws if `table` is
|
|
80
|
-
* missing (programmer error / bootstrap skipped).
|
|
81
|
-
*/
|
|
82
|
-
getColumnTypes(table) {
|
|
83
|
-
const hit = this.columnTypes.get(table);
|
|
84
|
-
if (!hit) {
|
|
85
|
-
this.logger.warn({ table }, "MySqlDatabase: no cached column types fallback to non-batched writes");
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
return hit;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Eagerly load column types into the in-memory cache. With a Redis
|
|
92
|
-
* client supplied, attempts the cache key first and falls through to
|
|
93
|
-
* MySQL on miss (writing the result back to Redis). This is the
|
|
94
|
-
* "one server queries MySQL, the rest read from Redis" pattern for cold
|
|
95
|
-
* starts in a multi-instance deploy. Without Redis, always queries MySQL.
|
|
96
|
-
*
|
|
97
|
-
* Pass `tables` to limit the load to a specific list; otherwise every
|
|
98
|
-
* user table in the connected database is loaded.
|
|
99
|
-
*
|
|
100
|
-
* The Redis cache key should encode whatever invalidates types in your
|
|
101
|
-
* environment (typically a schema version / deploy version). The
|
|
102
|
-
* subscriber in supalive-server.ts is expected to publish a reload
|
|
103
|
-
* signal after migrations and invoke {@link invalidateColumnTypes}.
|
|
104
|
-
*/
|
|
105
|
-
async bootstrapColumnTypes(opts) {
|
|
106
|
-
this.invalidateColumnTypes();
|
|
107
|
-
const { redis, cacheKey, tables } = opts;
|
|
108
|
-
// Redis hit path: deserialize the full table → column → mysqlType map
|
|
109
|
-
// and populate in one shot, skipping any INFORMATION_SCHEMA round-trips.
|
|
110
|
-
if (redis) {
|
|
111
|
-
const cached = await redis.getCachedTableColumnTypes(cacheKey);
|
|
112
|
-
if (cached) {
|
|
113
|
-
for (const [table, cols] of Object.entries(cached)) {
|
|
114
|
-
this.columnTypes.set(table, new Map(Object.entries(cols)));
|
|
115
|
-
}
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
// Cache miss (or no Redis): query MySQL for the listed tables
|
|
120
|
-
// (or enumerate all user tables) and populate.
|
|
121
|
-
const names = tables ?? await this.listUserTables();
|
|
122
|
-
for (const t of names) {
|
|
123
|
-
if (this.columnTypes.has(t))
|
|
124
|
-
continue;
|
|
125
|
-
const fresh = await this.loadColumnTypesFromMySql(t);
|
|
126
|
-
this.columnTypes.set(t, fresh);
|
|
127
|
-
}
|
|
128
|
-
// Write back to Redis so subsequent server instances skip the MySQL hop.
|
|
129
|
-
if (redis) {
|
|
130
|
-
const dump = {};
|
|
131
|
-
for (const [table, cols] of this.columnTypes) {
|
|
132
|
-
dump[table] = Object.fromEntries(Object.entries(cols).sort());
|
|
133
|
-
}
|
|
134
|
-
await redis.setCachedTableColumnsTypes(cacheKey, dump);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Invalidate cached types (and dependent SQL) for `table`, or the entire
|
|
139
|
-
* cache if `table` is omitted. The Redis-pubsub reload signal in
|
|
140
|
-
* supalive-server.ts is expected to call this on schema changes.
|
|
141
|
-
*/
|
|
142
|
-
invalidateColumnTypes(table) {
|
|
143
|
-
if (table) {
|
|
144
|
-
this.columnTypes.delete(table);
|
|
145
|
-
for (const k of [...this.sqlCache.keys()]) {
|
|
146
|
-
if (k.includes(`:${table}:`) || k === `d:${table}`)
|
|
147
|
-
this.sqlCache.delete(k);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
this.columnTypes.clear();
|
|
152
|
-
this.sqlCache.clear();
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
/** Replace this instance's caches with shared references from a parent.
|
|
156
|
-
* Used by getTransactionClient so types loaded once at startup are
|
|
157
|
-
* visible inside transactions. */
|
|
158
|
-
adoptCaches(columnTypes, sqlCache) {
|
|
159
|
-
this.columnTypes = columnTypes;
|
|
160
|
-
this.sqlCache = sqlCache;
|
|
161
|
-
}
|
|
162
|
-
/** Enumerate the user-defined tables in the connected database for mysql — used
|
|
163
|
-
* by bootstrapColumnTypes() when no explicit list is provided. */
|
|
164
|
-
async listUserTables() {
|
|
165
|
-
const result = await this.query(`SELECT table_name FROM information_schema.tables
|
|
166
|
-
WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE'
|
|
167
|
-
AND table_name NOT IN ('commit_logs', 'metadata', 'sequence')`);
|
|
168
|
-
return result.rows.map(r => r.table_name);
|
|
169
|
-
}
|
|
170
|
-
migrationSql = [
|
|
171
|
-
// Commit logs table (per-entry inserts, note 8)
|
|
172
|
-
`CREATE TABLE IF NOT EXISTS commit_logs (
|
|
173
|
-
id BINARY(16) NOT NULL,
|
|
174
|
-
ts BIGINT NOT NULL,
|
|
175
|
-
table_id BINARY(16) NOT NULL,
|
|
176
|
-
data LONGBLOB NOT NULL,
|
|
177
|
-
PRIMARY KEY (ts, table_id, id),
|
|
178
|
-
INDEX commit_logs_by_table_and_id (table_id, id, ts)
|
|
179
|
-
) ENGINE=InnoDB ROW_FORMAT=DYNAMIC`,
|
|
180
|
-
// `CREATE INDEX IF NOT EXISTS commit_logs_by_table_ts_and_id ON commit_logs (table_id, ts, id)`,
|
|
181
|
-
// Timestamp sequence table for MySQL
|
|
182
|
-
`CREATE TABLE IF NOT EXISTS sequence (id INT NOT NULL)`,
|
|
183
|
-
`INSERT IGNORE INTO sequence VALUES (0)`,
|
|
184
|
-
// Metadata table
|
|
185
|
-
`CREATE TABLE IF NOT EXISTS metadata (
|
|
186
|
-
meta_key VARCHAR(255) PRIMARY KEY,
|
|
187
|
-
meta_value BIGINT NOT NULL
|
|
188
|
-
) ENGINE=InnoDB;`,
|
|
189
|
-
`INSERT IGNORE INTO metadata (meta_key, meta_value) VALUES ('latest_committed_ts', 0);`
|
|
190
|
-
];
|
|
191
|
-
async query(sql, params = []) {
|
|
192
|
-
const client = await this.pool.getConnection();
|
|
193
|
-
try {
|
|
194
|
-
const [rows] = this.queryTimeoutMs > 0
|
|
195
|
-
? await client.query({ sql, values: params, timeout: this.queryTimeoutMs })
|
|
196
|
-
: await client.query(sql, params);
|
|
197
|
-
return normalizeMysqlResult(rows);
|
|
198
|
-
}
|
|
199
|
-
catch (err) {
|
|
200
|
-
throw translateMysqlError(err);
|
|
201
|
-
}
|
|
202
|
-
finally {
|
|
203
|
-
client.release();
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
async getClient() {
|
|
207
|
-
const client = await this.pool.getConnection();
|
|
208
|
-
return client;
|
|
209
|
-
}
|
|
210
|
-
async getTransactionClient() {
|
|
211
|
-
const client = await this.pool.getConnection();
|
|
212
|
-
const tx = new MySqlTxDatabase(this.logger, { ...this.config, queryTimeoutMs: this.queryTimeoutMs }, this.pool, client);
|
|
213
|
-
tx.adoptCaches(this.columnTypes, this.sqlCache);
|
|
214
|
-
return tx;
|
|
215
|
-
}
|
|
216
|
-
async getLatestSnapshotTimestamp() {
|
|
217
|
-
const result = await this.query("SELECT meta_value FROM metadata WHERE meta_key = 'latest_committed_ts'");
|
|
218
|
-
return BigInt(result.rows[0]?.meta_value ?? 0) || -1n;
|
|
219
|
-
}
|
|
220
|
-
async updateLatestSnapshotTimestamp(commitTs) {
|
|
221
|
-
await this.query(`UPDATE metadata SET meta_value = ? WHERE meta_key = 'latest_committed_ts' AND meta_value < ?`, [commitTs, commitTs]);
|
|
222
|
-
}
|
|
223
|
-
async getNextTimestamp() {
|
|
224
|
-
const result = await this.query("UPDATE sequence SET id = LAST_INSERT_ID(id + 1); SELECT LAST_INSERT_ID() AS id");
|
|
225
|
-
return BigInt(result.rows[0]?.id ?? 0) || -1n;
|
|
226
|
-
}
|
|
227
|
-
// Runs on the pool, not the tx client — intentionally. This keeps the
|
|
228
|
-
// transaction window short, but it means `prevData` is not snapshot-
|
|
229
|
-
// consistent with the eventual commitTs (a peer may have moved the row
|
|
230
|
-
// between beginTs and this SELECT). That's safe because:
|
|
231
|
-
// 1. The per-row CAS in pgApplyWrites is the real snapshot fence. If a
|
|
232
|
-
// peer committed in our staleness window, the CAS sees a moved
|
|
233
|
-
// commit_ts and throws OccConflictError → retry.
|
|
234
|
-
// 2. The only window where CAS passes with "stale" prevData is when
|
|
235
|
-
// the current row at our SELECT moment matches `expectedCommitTs`;
|
|
236
|
-
// by construction nobody committed between our captured ts and now,
|
|
237
|
-
// so prevData IS consistent with the ts we're CAS-ing against.
|
|
238
|
-
// 3. The downstream consumer (overlap-checker.checkRowWriteAffectsRange)
|
|
239
|
-
// is conservative on missing/odd prevData — "unknown" verdicts still
|
|
240
|
-
// invalidate, so the worst case here is a spurious invalidation,
|
|
241
|
-
// never a missed one.
|
|
242
|
-
// Locked in by mysql.attach-prev-data.test.ts.
|
|
243
|
-
async attachPrevDataToWriteSet(writeSet, readSet) {
|
|
244
|
-
const readTsByKey = new Map();
|
|
245
|
-
for (let i = readSet.length - 1; i >= 0; i--) {
|
|
246
|
-
const r = readSet[i];
|
|
247
|
-
if (r.kind !== "point" || r.expectedCommitTs === undefined)
|
|
248
|
-
continue;
|
|
249
|
-
const key = `${r.table} ${r.id}`;
|
|
250
|
-
if (!readTsByKey.has(key))
|
|
251
|
-
readTsByKey.set(key, r.expectedCommitTs);
|
|
252
|
-
}
|
|
253
|
-
const byTable = new Map();
|
|
254
|
-
for (const entry of writeSet) {
|
|
255
|
-
if (entry.op !== "update" && entry.op !== "delete")
|
|
256
|
-
continue;
|
|
257
|
-
const bucket = byTable.get(entry.table);
|
|
258
|
-
if (bucket)
|
|
259
|
-
bucket.push(entry);
|
|
260
|
-
else
|
|
261
|
-
byTable.set(entry.table, [entry]);
|
|
262
|
-
}
|
|
263
|
-
if (byTable.size === 0)
|
|
264
|
-
return;
|
|
265
|
-
for (const [table, entries] of byTable) {
|
|
266
|
-
const ids = entries.map((e) => e.id);
|
|
267
|
-
const placeholders = ids.map(() => "?").join(", ");
|
|
268
|
-
// No FOR UPDATE — the per-row CAS on the subsequent UPDATE/DELETE is
|
|
269
|
-
// the fence. If a peer mutated the row since our read, the CAS will
|
|
270
|
-
// see a stale expected commit_ts and throw OCC.
|
|
271
|
-
const sql = `SELECT * FROM ${table} WHERE id IN (${placeholders})`;
|
|
272
|
-
const result = await this.query(sql, ids);
|
|
273
|
-
const byId = new Map();
|
|
274
|
-
for (const row of result.rows) {
|
|
275
|
-
byId.set(String(row.id), row);
|
|
276
|
-
}
|
|
277
|
-
for (const entry of entries) {
|
|
278
|
-
const row = byId.get(String(entry.id));
|
|
279
|
-
const key = `${entry.table} ${entry.id}`;
|
|
280
|
-
const priorReadTs = readTsByKey.get(key);
|
|
281
|
-
if (row) {
|
|
282
|
-
entry.prevData = row;
|
|
283
|
-
// Prefer the read-time ts so read-then-update patterns get
|
|
284
|
-
// beginTs-level isolation; fall back to current row ts for
|
|
285
|
-
// blind writes (no prior read).
|
|
286
|
-
entry.expectedCommitTs = priorReadTs !== undefined
|
|
287
|
-
? priorReadTs
|
|
288
|
-
: BigInt(row.commit_ts);
|
|
289
|
-
}
|
|
290
|
-
else if (priorReadTs !== undefined) {
|
|
291
|
-
// Row gone now, but we had read it earlier: carry the read ts —
|
|
292
|
-
// the CAS will then mismatch (row deleted) and throw OCC.
|
|
293
|
-
entry.expectedCommitTs = priorReadTs;
|
|
294
|
-
}
|
|
295
|
-
// else: neither a prior read nor a current row → leave undefined
|
|
296
|
-
// so CAS misses and surfaces OCC.
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
// Prepare BEFORE the conflict scan: each write is a CAS against the row's
|
|
301
|
-
// commit_ts, so any concurrent peer that touched the row produces a
|
|
302
|
-
// 0-row-affected UPDATE/DELETE → OccConflictError. Inserts surface as
|
|
303
|
-
// PK violations, also translated to OCC.
|
|
304
|
-
async prepareCommitWrites(readSet, writeSet, commitTs) {
|
|
305
|
-
const result = [];
|
|
306
|
-
await this.mysqlPrepareBatchWrites(result, writeSet, commitTs);
|
|
307
|
-
await this.mysqlPrepareCommitLogs(result, writeSet, commitTs);
|
|
308
|
-
const hasRangeRead = readSet.some(r => r.kind === "range");
|
|
309
|
-
if (!hasRangeRead) {
|
|
310
|
-
const validateReads = this.mysqlPrepareValidatePointReadsByCommitTs(readSet, writeSet);
|
|
311
|
-
return { commits: result, validateReadPoints: validateReads, hasRangeRead: false };
|
|
312
|
-
}
|
|
313
|
-
// has atleast one range read so use scan, prepare data
|
|
314
|
-
const tableIds = [...new Set(readSet.map(r => r.tableId))];
|
|
315
|
-
const rawReadSet = mapReadSetToRaw(readSet);
|
|
316
|
-
return { commits: result, hasRangeRead: true, tableIds: tableIds, rawReadSet: rawReadSet };
|
|
317
|
-
}
|
|
318
|
-
async mysqlPrepareBatchWrites(result, writeSet, commitTs) {
|
|
319
|
-
let i = 0;
|
|
320
|
-
let counter = 0;
|
|
321
|
-
while (i < writeSet.length) {
|
|
322
|
-
counter++;
|
|
323
|
-
const { table, op } = writeSet[i];
|
|
324
|
-
let j = i + 1;
|
|
325
|
-
while (j < writeSet.length && writeSet[j].table === table && writeSet[j].op === op)
|
|
326
|
-
j++;
|
|
327
|
-
const batch = writeSet.slice(i, j);
|
|
328
|
-
if (op === "insert")
|
|
329
|
-
await this.mysqlPrepareInsertBatch(result, table, batch, commitTs);
|
|
330
|
-
else if (op === "update")
|
|
331
|
-
await this.mysqlPrepareUpdateSingle(result, table, batch, commitTs);
|
|
332
|
-
else
|
|
333
|
-
await this.mysqlPrepareDeleteBatch(result, table, batch);
|
|
334
|
-
i = j;
|
|
335
|
-
if (counter % 50 == 0) {
|
|
336
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
async mysqlPrepareInsertBatch(result, table, entries, commitTs) {
|
|
341
|
-
if (entries.length === 0)
|
|
342
|
-
return;
|
|
343
|
-
const groups = new Map();
|
|
344
|
-
for (const entry of entries) {
|
|
345
|
-
const cols = [...Object.keys(entry.data ?? {}), 'commit_ts'].sort();
|
|
346
|
-
const key = cols.join("|");
|
|
347
|
-
let g = groups.get(key);
|
|
348
|
-
if (!g) {
|
|
349
|
-
g = { cols, rows: [] };
|
|
350
|
-
groups.set(key, g);
|
|
351
|
-
}
|
|
352
|
-
g.rows.push(entry);
|
|
353
|
-
}
|
|
354
|
-
let counter = 0;
|
|
355
|
-
for (const { cols, rows } of groups.values()) {
|
|
356
|
-
counter++;
|
|
357
|
-
const values = [];
|
|
358
|
-
const tuples = [];
|
|
359
|
-
for (const entry of rows) {
|
|
360
|
-
const data = { ...(entry.data ?? {}), commit_ts: commitTs };
|
|
361
|
-
tuples.push(`(${cols.map(() => "?").join(", ")})`);
|
|
362
|
-
for (const col of cols) {
|
|
363
|
-
values.push(data[col]);
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
if (counter % 50 == 0) {
|
|
367
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
368
|
-
}
|
|
369
|
-
result.push({
|
|
370
|
-
sql: `INSERT INTO ${table} (${cols.join(", ")}) VALUES ${tuples.join(", ")}`,
|
|
371
|
-
params: values,
|
|
372
|
-
operation: "insertBatch",
|
|
373
|
-
table: table,
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
async mysqlPrepareUpdateSingle(result, table, entries, commitTs) {
|
|
378
|
-
if (entries.length === 0)
|
|
379
|
-
return;
|
|
380
|
-
let counter = 0;
|
|
381
|
-
for (const entry of entries) {
|
|
382
|
-
counter++;
|
|
383
|
-
const data = { ...(entry.data ?? {}), commit_ts: commitTs };
|
|
384
|
-
const columns = Object.keys(data);
|
|
385
|
-
const sets = columns.map((c) => `${c} = ?`);
|
|
386
|
-
const values = Object.values(data);
|
|
387
|
-
const expected = entry.expectedCommitTs ?? -1n;
|
|
388
|
-
if (counter % 50 == 0) {
|
|
389
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
390
|
-
}
|
|
391
|
-
result.push({
|
|
392
|
-
sql: `UPDATE ${table} SET ${sets.join(", ")} WHERE id = ? AND commit_ts = ?`,
|
|
393
|
-
params: [...values, entry.id, expected],
|
|
394
|
-
operation: 'updateSingle',
|
|
395
|
-
table: table,
|
|
396
|
-
entryId: entry.id,
|
|
397
|
-
expectedTs: expected,
|
|
398
|
-
});
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
async mysqlPrepareDeleteBatch(result, table, entries) {
|
|
402
|
-
if (entries.length === 0)
|
|
403
|
-
return;
|
|
404
|
-
let counter = 0;
|
|
405
|
-
for (const entry of entries) {
|
|
406
|
-
counter++;
|
|
407
|
-
const expected = entry.expectedCommitTs ?? -1n;
|
|
408
|
-
if (counter % 50 == 0) {
|
|
409
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
410
|
-
}
|
|
411
|
-
result.push({
|
|
412
|
-
sql: `DELETE FROM ${table} WHERE id = ? AND commit_ts = ?`,
|
|
413
|
-
params: [entry.id, expected],
|
|
414
|
-
table: table,
|
|
415
|
-
operation: 'deleteSingle',
|
|
416
|
-
entryId: entry.id,
|
|
417
|
-
expectedTs: expected,
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
async mysqlPrepareCommitLogs(result, writeEntries, commitTs) {
|
|
422
|
-
if (writeEntries.length === 0)
|
|
423
|
-
return;
|
|
424
|
-
const values = [];
|
|
425
|
-
const tuples = [];
|
|
426
|
-
let counter = 0;
|
|
427
|
-
for (const entry of writeEntries) {
|
|
428
|
-
counter++;
|
|
429
|
-
tuples.push(`(?, ?, ?, ?)`);
|
|
430
|
-
values.push(toBufferView(normalizeIdToBytes(entry.id)), commitTs, toBufferView(entry.tableId), toBufferView(utf8Encode(supaliveStringify(entry))));
|
|
431
|
-
if (counter % 50 == 0) {
|
|
432
|
-
await new Promise((resolve) => setImmediate(resolve));
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
result.push({
|
|
436
|
-
sql: `INSERT INTO commit_logs (id, ts, table_id, data) VALUES ${tuples.join(", ")}`,
|
|
437
|
-
params: values,
|
|
438
|
-
table: 'commit_logs',
|
|
439
|
-
operation: 'commit_logs',
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Point-only OCC check. For each genuine point read, compare the row's
|
|
444
|
-
* current commit_ts against the value captured at read time. A `null`
|
|
445
|
-
* expectedCommitTs means "row didn't exist when we read"; any row found
|
|
446
|
-
* now is a conflict. Reads whose row is being written in this same txn
|
|
447
|
-
* are skipped — the CAS UPDATE/DELETE already validated those.
|
|
448
|
-
*/
|
|
449
|
-
mysqlPrepareValidatePointReadsByCommitTs(readSet, writeSet) {
|
|
450
|
-
const result = [];
|
|
451
|
-
// Skip reads whose row is being mutated in this same tx — CAS already
|
|
452
|
-
// fenced them. We dedupe per (table, id) so duplicate reads of the same
|
|
453
|
-
// row don't cause extra fetches.
|
|
454
|
-
const writeKeys = new Set();
|
|
455
|
-
for (const w of writeSet)
|
|
456
|
-
writeKeys.add(`${w.table} ${w.id}`);
|
|
457
|
-
const byTable = new Map();
|
|
458
|
-
const seen = new Set();
|
|
459
|
-
for (const entry of readSet) {
|
|
460
|
-
if (entry.kind !== "point")
|
|
461
|
-
continue;
|
|
462
|
-
const key = `${entry.table} ${entry.id}`;
|
|
463
|
-
if (writeKeys.has(key) || seen.has(key))
|
|
464
|
-
continue;
|
|
465
|
-
seen.add(key);
|
|
466
|
-
// expectedCommitTs === undefined → no commit_ts captured at read time
|
|
467
|
-
// (synthetic or pre-migration). Skip — we cannot CAS-validate it. This
|
|
468
|
-
// path should only fire when callers construct PointReads externally.
|
|
469
|
-
if (entry.expectedCommitTs === undefined)
|
|
470
|
-
continue;
|
|
471
|
-
const bucket = byTable.get(entry.table);
|
|
472
|
-
if (bucket)
|
|
473
|
-
bucket.push({ entry, key });
|
|
474
|
-
else
|
|
475
|
-
byTable.set(entry.table, [{ entry, key }]);
|
|
476
|
-
}
|
|
477
|
-
for (const [table, pendings] of byTable) {
|
|
478
|
-
const ids = pendings.map((p) => p.entry.id);
|
|
479
|
-
const placeholders = ids.map(() => "?").join(", ");
|
|
480
|
-
result.push({
|
|
481
|
-
sql: `SELECT id, commit_ts FROM ${table} WHERE id IN (${placeholders})`,
|
|
482
|
-
params: ids,
|
|
483
|
-
table: table,
|
|
484
|
-
operation: 'pointReadValidation',
|
|
485
|
-
pendingsEntry: pendings,
|
|
486
|
-
});
|
|
487
|
-
}
|
|
488
|
-
return result;
|
|
489
|
-
}
|
|
490
|
-
async commitWrites(beginTs, queries, commitTs) {
|
|
491
|
-
await this.mysqlApplyWrites(queries.commits);
|
|
492
|
-
// Fast path: when the readSet has no range reads, we don't need a
|
|
493
|
-
// commit-log scan at all — point reads validate via per-row commit_ts,
|
|
494
|
-
// and writes already self-validated via CAS above. This is the dominant
|
|
495
|
-
// shape (most mutations target known ids) and eliminates the heaviest
|
|
496
|
-
// OCC cost in the previous design.
|
|
497
|
-
if (!queries.hasRangeRead) {
|
|
498
|
-
if ((queries.validateReadPoints?.length ?? 0) > 0) {
|
|
499
|
-
await this.validatePointReadsByCommitTs(queries.validateReadPoints ?? []);
|
|
500
|
-
}
|
|
501
|
-
return { success: true };
|
|
502
|
-
}
|
|
503
|
-
// Slow path: at least one range read present. Fall back to the
|
|
504
|
-
// commit-log scan so phantom inserts and predicate transitions-in are
|
|
505
|
-
// still caught. Point reads in the same set are covered by the same
|
|
506
|
-
// scan, so no separate validation is needed.
|
|
507
|
-
const logs = await this.getCommitLogsSinceTs(beginTs, commitTs, queries.tableIds ?? []);
|
|
508
|
-
const hasConflict = checkLogsAffectReadSet(logs, queries.rawReadSet ?? []);
|
|
509
|
-
if (hasConflict) {
|
|
510
|
-
throw new OccConflictError(`Conflict detected: overlapping commit logs since ${beginTs}`, { logs });
|
|
511
|
-
}
|
|
512
|
-
return { success: true };
|
|
513
|
-
}
|
|
514
|
-
// Walks the writeSet in order and coalesces consecutive entries with the
|
|
515
|
-
// same (table, op) into one batched statement. Order is preserved across
|
|
516
|
-
// batch boundaries so replace-patterns (delete-then-insert of the same
|
|
517
|
-
// id) behave identically to the per-row code path. Single-row batches
|
|
518
|
-
// take a direct INSERT/UPDATE/DELETE fast-path to avoid the unnest+join
|
|
519
|
-
// overhead, which matters under high contention.
|
|
520
|
-
async mysqlApplyWrites(result) {
|
|
521
|
-
for (const q of result) {
|
|
522
|
-
switch (q.operation) {
|
|
523
|
-
case 'insertBatch': {
|
|
524
|
-
try {
|
|
525
|
-
await this.query(q.sql, q.params);
|
|
526
|
-
}
|
|
527
|
-
catch (err) {
|
|
528
|
-
if (err && typeof err === "object" &&
|
|
529
|
-
(err.errno === 1062 ||
|
|
530
|
-
err.code === "ER_DUP_ENTRY")) {
|
|
531
|
-
const msg = err.message ?? "";
|
|
532
|
-
const idMatch = msg.match(/key '[^']+'.*?\(.*?\)=\(?([^,\)]+)/);
|
|
533
|
-
const failedId = idMatch?.[1];
|
|
534
|
-
throw new OccConflictError(failedId !== undefined
|
|
535
|
-
? `Conflict: insert into ${q.table}/${failedId} hit duplicate key`
|
|
536
|
-
: `Conflict: batch insert into ${q.table} hit duplicate key`, err);
|
|
537
|
-
}
|
|
538
|
-
throw err;
|
|
539
|
-
}
|
|
540
|
-
break;
|
|
541
|
-
}
|
|
542
|
-
case 'updateSingle': {
|
|
543
|
-
const result = await this.query(q.sql, q.params);
|
|
544
|
-
if ((result.rowCount ?? 0) === 0) {
|
|
545
|
-
throw new OccConflictError(`Conflict: CAS update of ${q.table}/${q.entryId} (expected commit_ts=${q.expectedTs}) matched no rows`, { table: q.table, id: q.entryId, expected: q.expectedTs });
|
|
546
|
-
}
|
|
547
|
-
break;
|
|
548
|
-
}
|
|
549
|
-
case 'deleteBatch': {
|
|
550
|
-
const result = await this.query(q.sql, q.params);
|
|
551
|
-
if ((result.rowCount ?? 0) === 0) {
|
|
552
|
-
throw new OccConflictError(`Conflict: CAS delete of ${q.table}/${q.entryId} (expected commit_ts=${q.expectedTs}) matched no rows`, { table: q.table, id: q.entryId, expected: q.expectedTs });
|
|
553
|
-
}
|
|
554
|
-
break;
|
|
555
|
-
}
|
|
556
|
-
case 'commit_logs': {
|
|
557
|
-
await this.query(q.sql, q.params);
|
|
558
|
-
break;
|
|
559
|
-
}
|
|
560
|
-
default:
|
|
561
|
-
throw new Error("Unhandled MySQL Operation: " + q.operation);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
async validatePointReadsByCommitTs(queries) {
|
|
566
|
-
for (const q of queries) {
|
|
567
|
-
const result = await this.query(q.sql, q.params);
|
|
568
|
-
const currentById = new Map();
|
|
569
|
-
for (const row of result.rows) {
|
|
570
|
-
currentById.set(String(row.id), BigInt(row.commit_ts));
|
|
571
|
-
}
|
|
572
|
-
for (const { entry } of q.pendingsEntry ?? []) {
|
|
573
|
-
const current = currentById.get(String(entry.id));
|
|
574
|
-
const expected = entry.expectedCommitTs;
|
|
575
|
-
if (expected === null) {
|
|
576
|
-
// We read "no row"; conflict if a row now exists.
|
|
577
|
-
if (current !== undefined) {
|
|
578
|
-
throw new OccConflictError(`Conflict: row ${entry.table}/${entry.id} was inserted concurrently`, { table: entry.table, id: entry.id });
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
else {
|
|
582
|
-
if (current === undefined) {
|
|
583
|
-
throw new OccConflictError(`Conflict: row ${entry.table}/${entry.id} was deleted concurrently`, { table: entry.table, id: entry.id, expected });
|
|
584
|
-
}
|
|
585
|
-
if (current !== expected) {
|
|
586
|
-
throw new OccConflictError(`Conflict: row ${entry.table}/${entry.id} changed from ts=${expected} to ts=${current}`, { table: entry.table, id: entry.id, expected, current });
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
async pruneCommitLogsBefore(ts) {
|
|
593
|
-
const result = await this.query(`DELETE FROM commit_logs WHERE ts < ?`, [ts]);
|
|
594
|
-
return BigInt(result.rowCount ?? 0);
|
|
595
|
-
}
|
|
596
|
-
async getCommitLogsBetweenTs(beginTs, endTs, tableIds) {
|
|
597
|
-
if (tableIds.length === 0)
|
|
598
|
-
return [];
|
|
599
|
-
const placeholders = tableIds.map(() => '?').join(", ");
|
|
600
|
-
const sql = `SELECT id, ts, table_id, data FROM commit_logs WHERE ts >= ? AND ts < ? AND table_id IN (${placeholders})`;
|
|
601
|
-
const result = await this.query(sql, [beginTs, endTs, ...tableIds.map(toBufferView)]);
|
|
602
|
-
return result.rows.map((row) => ({
|
|
603
|
-
id: normalizeToBytes(row.id),
|
|
604
|
-
ts: BigInt(row.ts),
|
|
605
|
-
tableId: normalizeToBytes(row.table_id),
|
|
606
|
-
data: normalizeToBytes(row.data),
|
|
607
|
-
}));
|
|
608
|
-
}
|
|
609
|
-
async getCommitLogsSinceTs(sinceTs, excludeTs, tableIds) {
|
|
610
|
-
if (tableIds.length === 0)
|
|
611
|
-
return [];
|
|
612
|
-
const placeholders = tableIds.map(() => '?').join(", ");
|
|
613
|
-
const sql = `SELECT id, ts, table_id, data FROM commit_logs WHERE ts >= ? AND ts <> ? AND table_id IN (${placeholders})`;
|
|
614
|
-
const result = await this.query(sql, [sinceTs, excludeTs, ...tableIds.map(toBufferView)]);
|
|
615
|
-
return result.rows.map((row) => ({
|
|
616
|
-
id: normalizeToBytes(row.id),
|
|
617
|
-
ts: BigInt(row.ts),
|
|
618
|
-
tableId: normalizeToBytes(row.table_id),
|
|
619
|
-
data: normalizeToBytes(row.data),
|
|
620
|
-
}));
|
|
621
|
-
}
|
|
622
|
-
async close() {
|
|
623
|
-
await this.pool.end();
|
|
624
|
-
}
|
|
625
|
-
}
|
|
626
|
-
export class MySqlTxDatabase extends MySqlDatabase {
|
|
627
|
-
constructor(logger, config, pool, client) {
|
|
628
|
-
super(logger, config, pool);
|
|
629
|
-
this.client = client;
|
|
630
|
-
}
|
|
631
|
-
client;
|
|
632
|
-
async query(sql, params = []) {
|
|
633
|
-
try {
|
|
634
|
-
const [rows] = this.queryTimeoutMs > 0
|
|
635
|
-
? await this.client.query({ sql, values: params, timeout: this.queryTimeoutMs })
|
|
636
|
-
: await this.client.query(sql, params);
|
|
637
|
-
return normalizeMysqlResult(rows);
|
|
638
|
-
}
|
|
639
|
-
catch (err) {
|
|
640
|
-
throw translateMysqlError(err);
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
async begin() {
|
|
644
|
-
await this.client.beginTransaction();
|
|
645
|
-
}
|
|
646
|
-
async commit() {
|
|
647
|
-
await this.client.commit();
|
|
648
|
-
}
|
|
649
|
-
async rollback() {
|
|
650
|
-
await this.client.rollback();
|
|
651
|
-
}
|
|
652
|
-
release() {
|
|
653
|
-
this.client.release();
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
/**
|
|
657
|
-
* MySQL InnoDB deadlock (errno 1213 / SQLSTATE 40001) is transient and
|
|
658
|
-
* semantically equivalent to an OCC conflict from the mutation runner's
|
|
659
|
-
* perspective: re-execute the handler with a fresh snapshot. Wrapping
|
|
660
|
-
* here lets realtime_db.ts stay driver-agnostic — its retry loop already
|
|
661
|
-
* handles OccConflictError.
|
|
662
|
-
*/
|
|
663
|
-
function translateMysqlError(err) {
|
|
664
|
-
if (err && typeof err === "object" &&
|
|
665
|
-
(err.errno === 1213 ||
|
|
666
|
-
err.code === "ER_LOCK_DEADLOCK")) {
|
|
667
|
-
return new OccConflictError("deadlock detected", err);
|
|
668
|
-
}
|
|
669
|
-
return err;
|
|
670
|
-
}
|
|
671
|
-
const mysqlSql = {
|
|
672
|
-
placeholder: (i) => `?`,
|
|
673
|
-
like: (col, i) => `${col} LIKE ?`,
|
|
674
|
-
ilike: (col, i) => `${col} LIKE ?`, // MySQL doesn't have ILIKE
|
|
675
|
-
icontains: (col, i) => `${col} LIKE ?`,
|
|
676
|
-
// CAST(? AS JSON) lets the driver bind the param as a regular string
|
|
677
|
-
// (JSON-serialized client-side) and MySQL re-parses it into a JSON value
|
|
678
|
-
// for containment comparison. When a sub-path is set, JSON_CONTAINS takes
|
|
679
|
-
// the path as its third argument.
|
|
680
|
-
jsonContains: (col, _i, opts) => opts?.pathIndex !== undefined
|
|
681
|
-
? `JSON_CONTAINS(${col}, CAST(? AS JSON), ?)`
|
|
682
|
-
: `JSON_CONTAINS(${col}, CAST(? AS JSON))`,
|
|
683
|
-
jsonContainedBy: (col, _i, opts) => opts?.pathIndex !== undefined
|
|
684
|
-
// For "is the column contained in the candidate at this sub-path of the
|
|
685
|
-
// column" we still need to extract the sub-path from the column side.
|
|
686
|
-
// JSON_CONTAINS's path arg is on the TARGET position, so we flip and use
|
|
687
|
-
// JSON_EXTRACT for the column side instead.
|
|
688
|
-
? `JSON_CONTAINS(CAST(? AS JSON), JSON_EXTRACT(${col}, ?))`
|
|
689
|
-
: `JSON_CONTAINS(CAST(? AS JSON), ${col})`,
|
|
690
|
-
jsonHasKey: (col, _i, opts) => {
|
|
691
|
-
if (opts?.pathCount && opts.pathCount > 0) {
|
|
692
|
-
const mode = opts.mode === "all" ? "'all'" : "'one'";
|
|
693
|
-
const places = new Array(opts.pathCount).fill("?").join(", ");
|
|
694
|
-
return `JSON_CONTAINS_PATH(${col}, ${mode}, ${places})`;
|
|
695
|
-
}
|
|
696
|
-
return opts?.raw
|
|
697
|
-
// Param is already a JSON path expression like `$.foo.bar` or `$."a"."b"`.
|
|
698
|
-
? `JSON_CONTAINS_PATH(${col}, 'one', ?)`
|
|
699
|
-
// Param is a literal top-level key. Wrap it in `$."<key>"` server-side
|
|
700
|
-
// and escape any embedded double-quotes so dotted keys behave as keys
|
|
701
|
-
// and not nested paths.
|
|
702
|
-
: `JSON_CONTAINS_PATH(${col}, 'one', CONCAT('$."', REPLACE(?, '"', '\\\\"'), '"'))`;
|
|
703
|
-
},
|
|
704
|
-
jsonArrayContains: (col, _i, opts) => opts?.pathIndex !== undefined
|
|
705
|
-
? `JSON_CONTAINS(${col}, JSON_ARRAY(CAST(? AS JSON)), ?)`
|
|
706
|
-
: `JSON_CONTAINS(${col}, JSON_ARRAY(CAST(? AS JSON)))`,
|
|
707
|
-
};
|
|
708
|
-
//# sourceMappingURL=mysql.js.map
|