@ocap/indexdb-sqlite 1.31.6 → 1.31.7
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/db/base.mjs +24 -0
- package/esm/db/tx-ref-query.d.mts +33 -0
- package/esm/db/tx-ref-query.mjs +91 -0
- package/lib/db/base.cjs +24 -0
- package/lib/db/tx-ref-query.cjs +92 -0
- package/lib/db/tx-ref-query.d.cts +33 -0
- package/package.json +6 -6
package/esm/db/base.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { buildTxRefDrivenQuery } from "./tx-ref-query.mjs";
|
|
1
2
|
import { sql } from "kysely";
|
|
2
3
|
import { BaseIndexDB, formatDelegationAfterRead, formatNextPagination, formatPagination, formatSearchResult, parseDateTime } from "@ocap/indexdb";
|
|
3
4
|
import { toChecksumAddress } from "@arcblock/did/lib/type";
|
|
@@ -181,12 +182,32 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
181
182
|
const hasNonJsonFilters = !!(sender || receiver || gasPayer || types.length || txs.length || parsedStart || parsedEnd || validity);
|
|
182
183
|
const isUnionOnly = isUnion && !gasPayer && !types.length && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
183
184
|
const isMultiTypeOnly = types.length > 1 && !sender && !receiver && !gasPayer && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
185
|
+
const txRefDriven = !isMutual && activeJsonFilters.length >= 1 && (hasNonJsonFilters || activeJsonFilters.length > 1);
|
|
186
|
+
const txRefDrivenInput = (limit, offset) => ({
|
|
187
|
+
refFilters: activeJsonFilters,
|
|
188
|
+
sender,
|
|
189
|
+
receiver,
|
|
190
|
+
gasPayer,
|
|
191
|
+
direction,
|
|
192
|
+
types,
|
|
193
|
+
txs,
|
|
194
|
+
validity,
|
|
195
|
+
start: parsedStart || void 0,
|
|
196
|
+
end: parsedEnd || void 0,
|
|
197
|
+
order: pagination.order.type === "desc" ? "desc" : "asc",
|
|
198
|
+
limit,
|
|
199
|
+
offset
|
|
200
|
+
});
|
|
184
201
|
if (isMutual) {
|
|
185
202
|
const [c1, c2] = await Promise.all([this.db.selectFrom("tx").select(this.db.fn.countAll().as("count")).where("sender", "=", sender).where("receiver", "=", receiver).executeTakeFirst(), this.db.selectFrom("tx").select(this.db.fn.countAll().as("count")).where("sender", "=", receiver).where("receiver", "=", sender).executeTakeFirst()]);
|
|
186
203
|
total = (c1?.count ?? 0) + (c2?.count ?? 0);
|
|
187
204
|
} else if (!hasNonJsonFilters && activeJsonFilters.length === 1) {
|
|
188
205
|
const [refType, values] = activeJsonFilters[0];
|
|
189
206
|
total = (await this.db.selectFrom("tx_ref").select(this.db.fn.countAll().as("count")).where("type", "=", refType).where("value", "in", values).executeTakeFirst())?.count ?? 0;
|
|
207
|
+
} else if (txRefDriven) {
|
|
208
|
+
const { count } = buildTxRefDrivenQuery(txRefDrivenInput(0, 0));
|
|
209
|
+
const countRows = await count.execute(this.db);
|
|
210
|
+
total = Number(countRows.rows[0]?.count ?? 0);
|
|
190
211
|
} else if (isUnionOnly) {
|
|
191
212
|
const [senderCount, receiverCount, overlapCount] = await Promise.all([
|
|
192
213
|
this.db.selectFrom("tx").select(this.db.fn.countAll().as("count")).where("sender", "=", sender).executeTakeFirst(),
|
|
@@ -236,6 +257,9 @@ var SqliteBaseIndexDB = class extends BaseIndexDB {
|
|
|
236
257
|
const hashOrder = new Map(hashes.map((h, i) => [h, i]));
|
|
237
258
|
rawResults = [...dataRows].sort((a, b) => (hashOrder.get(a.hash) ?? 0) - (hashOrder.get(b.hash) ?? 0));
|
|
238
259
|
} else rawResults = [];
|
|
260
|
+
} else if (txRefDriven && pagination.order.field === "time") {
|
|
261
|
+
const { page } = buildTxRefDrivenQuery(txRefDrivenInput(pagination.size, pagination.cursor));
|
|
262
|
+
rawResults = (await page.execute(this.db)).rows;
|
|
239
263
|
} else {
|
|
240
264
|
qb = qb.orderBy(pagination.order.field, orderDir).offset(pagination.cursor).limit(pagination.size);
|
|
241
265
|
rawResults = await qb.execute();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RawBuilder } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/db/tx-ref-query.d.ts
|
|
4
|
+
|
|
5
|
+
interface TxRefDrivenInput {
|
|
6
|
+
/** [tx_ref.type, values] pairs; at least one. */
|
|
7
|
+
refFilters: [string, string[]][];
|
|
8
|
+
sender?: string;
|
|
9
|
+
receiver?: string;
|
|
10
|
+
gasPayer?: string;
|
|
11
|
+
direction?: string;
|
|
12
|
+
types?: string[];
|
|
13
|
+
txs?: string[];
|
|
14
|
+
validity?: string;
|
|
15
|
+
/** exclusive lower bound on time (ISO string) */
|
|
16
|
+
start?: string;
|
|
17
|
+
/** inclusive upper bound on time (ISO string) */
|
|
18
|
+
end?: string;
|
|
19
|
+
order: 'asc' | 'desc';
|
|
20
|
+
limit: number;
|
|
21
|
+
offset: number;
|
|
22
|
+
}
|
|
23
|
+
interface TxRefDrivenQuery {
|
|
24
|
+
/** `select tx.* ...` ordered, offset/limit applied */
|
|
25
|
+
page: RawBuilder<Record<string, unknown>>;
|
|
26
|
+
/** `select count(*) as count ...` */
|
|
27
|
+
count: RawBuilder<{
|
|
28
|
+
count: number;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
declare function buildTxRefDrivenQuery(input: TxRefDrivenInput): TxRefDrivenQuery;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { TxRefDrivenInput, TxRefDrivenQuery, buildTxRefDrivenQuery };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/db/tx-ref-query.ts
|
|
4
|
+
/**
|
|
5
|
+
* tx_ref-driven SQL for `listTransactions` (issue #1861).
|
|
6
|
+
*
|
|
7
|
+
* The generic path (`select * from tx where <conds> and hash in (select hash from tx_ref ...) order by time`)
|
|
8
|
+
* makes SQLite walk every tx_ref row of the account, look up every full `tx` row, filter, sort in a temp
|
|
9
|
+
* b-tree and only then apply offset/limit: cost is O(total history of the account), twice (count + page).
|
|
10
|
+
*
|
|
11
|
+
* Here tx_ref is the OUTER loop (`cross join` pins the join order), ordered by `idx_tx_ref_time_lookup
|
|
12
|
+
* (type, value, time DESC)`. `tx` is probed by primary key (`hash = ?`) only for rows that are actually
|
|
13
|
+
* needed, and time filters are pushed down to `tx_ref.time` so they become index range bounds.
|
|
14
|
+
*
|
|
15
|
+
* Correctness notes:
|
|
16
|
+
* - `tx_ref.time` is copied from `tx.time` on insert and rewritten on every update (TransactionTable.syncTxRefs),
|
|
17
|
+
* so ordering/filtering on `r.time` equals ordering/filtering on `tx.time`.
|
|
18
|
+
* Only difference: an empty-string `tx.time` is stored as NULL in tx_ref (never produced by the chain).
|
|
19
|
+
* - Single-value driver: `(hash, type, value)` is unique, so no duplicates are possible; ties on `time` come out
|
|
20
|
+
* in tx_ref index order (same as the pre-existing single-filter fast path).
|
|
21
|
+
* - Multi-value driver (`value in (a, b)`): a tx can match twice. The driver is a `select distinct hash, time`
|
|
22
|
+
* subquery over tx_ref only (index-only, no `tx` lookups), ordered by `(time, hash)`; `tx` rows are looked up
|
|
23
|
+
* lazily, only until offset+limit rows pass the remaining filters.
|
|
24
|
+
* - Additional JSON filters are `exists (select 1 from tx_ref x where x.hash = r.hash ...)` probes on idx_tx_ref_pk.
|
|
25
|
+
*/
|
|
26
|
+
const and = (parts) => sql.join(parts, sql` and `);
|
|
27
|
+
function txConditions(input) {
|
|
28
|
+
const { sender, receiver, gasPayer, direction, types = [], txs = [], validity } = input;
|
|
29
|
+
const conds = [];
|
|
30
|
+
if (sender && receiver) if (direction === "MUTUAL") conds.push(sql`((tx.sender = ${sender} and tx.receiver = ${receiver}) or (tx.sender = ${receiver} and tx.receiver = ${sender}))`);
|
|
31
|
+
else if (direction === "ONE_WAY") conds.push(sql`tx.sender = ${sender}`, sql`tx.receiver = ${receiver}`);
|
|
32
|
+
else conds.push(sql`(tx.sender = ${sender} or tx.receiver = ${receiver})`);
|
|
33
|
+
else if (sender) conds.push(sql`tx.sender = ${sender}`);
|
|
34
|
+
else if (receiver) conds.push(sql`tx.receiver = ${receiver}`);
|
|
35
|
+
if (gasPayer) conds.push(sql`tx.gasPayer = ${gasPayer}`);
|
|
36
|
+
if (types.length) conds.push(sql`tx.type in (${sql.join(types)})`);
|
|
37
|
+
if (txs.length) conds.push(sql`tx.hash in (${sql.join(txs)})`);
|
|
38
|
+
if (validity === "VALID") conds.push(sql`tx.valid = 1`);
|
|
39
|
+
else if (validity === "INVALID") conds.push(sql`tx.valid = 0`);
|
|
40
|
+
return conds;
|
|
41
|
+
}
|
|
42
|
+
function timeConditions(alias, start, end) {
|
|
43
|
+
const col = sql.ref(`${alias}.time`);
|
|
44
|
+
const conds = [];
|
|
45
|
+
if (start) conds.push(sql`${col} > ${start}`);
|
|
46
|
+
if (end) conds.push(sql`${col} <= ${end}`);
|
|
47
|
+
return conds;
|
|
48
|
+
}
|
|
49
|
+
/** Pick the filter that drives the scan: a single-valued one avoids the dedupe step. */
|
|
50
|
+
function pickDriver(refFilters) {
|
|
51
|
+
const single = refFilters.findIndex(([, values]) => values.length === 1);
|
|
52
|
+
return single >= 0 ? single : 0;
|
|
53
|
+
}
|
|
54
|
+
function buildTxRefDrivenQuery(input) {
|
|
55
|
+
const { refFilters, start, end, order, limit, offset } = input;
|
|
56
|
+
if (!refFilters.length) throw new Error("buildTxRefDrivenQuery requires at least one tx_ref filter");
|
|
57
|
+
const driverIndex = pickDriver(refFilters);
|
|
58
|
+
const [driverType, driverValues] = refFilters[driverIndex];
|
|
59
|
+
const dir = sql.raw(order === "desc" ? "desc" : "asc");
|
|
60
|
+
let from;
|
|
61
|
+
let countFrom;
|
|
62
|
+
const where = [];
|
|
63
|
+
if (driverValues.length === 1) {
|
|
64
|
+
from = sql`tx_ref r cross join tx`;
|
|
65
|
+
countFrom = from;
|
|
66
|
+
where.push(sql`r.type = ${driverType}`, sql`r.value = ${driverValues[0]}`, ...timeConditions("r", start, end));
|
|
67
|
+
} else {
|
|
68
|
+
const inner = and([
|
|
69
|
+
sql`type = ${driverType}`,
|
|
70
|
+
sql`value in (${sql.join(driverValues)})`,
|
|
71
|
+
...timeConditions("tx_ref", start, end)
|
|
72
|
+
]);
|
|
73
|
+
from = sql`(${sql`select distinct hash, time from tx_ref where ${inner} order by time ${dir}, hash limit -1`}) r cross join tx`;
|
|
74
|
+
countFrom = sql`(select distinct hash, time from tx_ref where ${inner}) r cross join tx`;
|
|
75
|
+
}
|
|
76
|
+
where.push(sql`tx.hash = r.hash`);
|
|
77
|
+
where.push(...txConditions(input));
|
|
78
|
+
refFilters.forEach(([refType, values], i) => {
|
|
79
|
+
if (i === driverIndex) return;
|
|
80
|
+
where.push(sql`exists (select 1 from tx_ref x where x.hash = r.hash and x.type = ${refType} and x.value in (${sql.join(values)}))`);
|
|
81
|
+
});
|
|
82
|
+
const whereSql = and(where);
|
|
83
|
+
const orderBy = driverValues.length === 1 ? sql`order by r.time ${dir}` : sql``;
|
|
84
|
+
return {
|
|
85
|
+
page: sql`select tx.* from ${from} where ${whereSql} ${orderBy} limit ${limit} offset ${offset}`,
|
|
86
|
+
count: sql`select count(*) as count from ${countFrom} where ${whereSql}`
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
//#endregion
|
|
91
|
+
export { buildTxRefDrivenQuery };
|
package/lib/db/base.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
3
|
+
const require_db_tx_ref_query = require('./tx-ref-query.cjs');
|
|
3
4
|
let kysely = require("kysely");
|
|
4
5
|
let _ocap_indexdb = require("@ocap/indexdb");
|
|
5
6
|
let _arcblock_did_lib_type = require("@arcblock/did/lib/type");
|
|
@@ -185,12 +186,32 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
185
186
|
const hasNonJsonFilters = !!(sender || receiver || gasPayer || types.length || txs.length || parsedStart || parsedEnd || validity);
|
|
186
187
|
const isUnionOnly = isUnion && !gasPayer && !types.length && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
187
188
|
const isMultiTypeOnly = types.length > 1 && !sender && !receiver && !gasPayer && !txs.length && !parsedStart && !parsedEnd && !validity && activeJsonFilters.length === 0;
|
|
189
|
+
const txRefDriven = !isMutual && activeJsonFilters.length >= 1 && (hasNonJsonFilters || activeJsonFilters.length > 1);
|
|
190
|
+
const txRefDrivenInput = (limit, offset) => ({
|
|
191
|
+
refFilters: activeJsonFilters,
|
|
192
|
+
sender,
|
|
193
|
+
receiver,
|
|
194
|
+
gasPayer,
|
|
195
|
+
direction,
|
|
196
|
+
types,
|
|
197
|
+
txs,
|
|
198
|
+
validity,
|
|
199
|
+
start: parsedStart || void 0,
|
|
200
|
+
end: parsedEnd || void 0,
|
|
201
|
+
order: pagination.order.type === "desc" ? "desc" : "asc",
|
|
202
|
+
limit,
|
|
203
|
+
offset
|
|
204
|
+
});
|
|
188
205
|
if (isMutual) {
|
|
189
206
|
const [c1, c2] = await Promise.all([this.db.selectFrom("tx").select(this.db.fn.countAll().as("count")).where("sender", "=", sender).where("receiver", "=", receiver).executeTakeFirst(), this.db.selectFrom("tx").select(this.db.fn.countAll().as("count")).where("sender", "=", receiver).where("receiver", "=", sender).executeTakeFirst()]);
|
|
190
207
|
total = (c1?.count ?? 0) + (c2?.count ?? 0);
|
|
191
208
|
} else if (!hasNonJsonFilters && activeJsonFilters.length === 1) {
|
|
192
209
|
const [refType, values] = activeJsonFilters[0];
|
|
193
210
|
total = (await this.db.selectFrom("tx_ref").select(this.db.fn.countAll().as("count")).where("type", "=", refType).where("value", "in", values).executeTakeFirst())?.count ?? 0;
|
|
211
|
+
} else if (txRefDriven) {
|
|
212
|
+
const { count } = require_db_tx_ref_query.buildTxRefDrivenQuery(txRefDrivenInput(0, 0));
|
|
213
|
+
const countRows = await count.execute(this.db);
|
|
214
|
+
total = Number(countRows.rows[0]?.count ?? 0);
|
|
194
215
|
} else if (isUnionOnly) {
|
|
195
216
|
const [senderCount, receiverCount, overlapCount] = await Promise.all([
|
|
196
217
|
this.db.selectFrom("tx").select(this.db.fn.countAll().as("count")).where("sender", "=", sender).executeTakeFirst(),
|
|
@@ -240,6 +261,9 @@ var SqliteBaseIndexDB = class extends _ocap_indexdb.BaseIndexDB {
|
|
|
240
261
|
const hashOrder = new Map(hashes.map((h, i) => [h, i]));
|
|
241
262
|
rawResults = [...dataRows].sort((a, b) => (hashOrder.get(a.hash) ?? 0) - (hashOrder.get(b.hash) ?? 0));
|
|
242
263
|
} else rawResults = [];
|
|
264
|
+
} else if (txRefDriven && pagination.order.field === "time") {
|
|
265
|
+
const { page } = require_db_tx_ref_query.buildTxRefDrivenQuery(txRefDrivenInput(pagination.size, pagination.cursor));
|
|
266
|
+
rawResults = (await page.execute(this.db)).rows;
|
|
243
267
|
} else {
|
|
244
268
|
qb = qb.orderBy(pagination.order.field, orderDir).offset(pagination.cursor).limit(pagination.size);
|
|
245
269
|
rawResults = await qb.execute();
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let kysely = require("kysely");
|
|
3
|
+
|
|
4
|
+
//#region src/db/tx-ref-query.ts
|
|
5
|
+
/**
|
|
6
|
+
* tx_ref-driven SQL for `listTransactions` (issue #1861).
|
|
7
|
+
*
|
|
8
|
+
* The generic path (`select * from tx where <conds> and hash in (select hash from tx_ref ...) order by time`)
|
|
9
|
+
* makes SQLite walk every tx_ref row of the account, look up every full `tx` row, filter, sort in a temp
|
|
10
|
+
* b-tree and only then apply offset/limit: cost is O(total history of the account), twice (count + page).
|
|
11
|
+
*
|
|
12
|
+
* Here tx_ref is the OUTER loop (`cross join` pins the join order), ordered by `idx_tx_ref_time_lookup
|
|
13
|
+
* (type, value, time DESC)`. `tx` is probed by primary key (`hash = ?`) only for rows that are actually
|
|
14
|
+
* needed, and time filters are pushed down to `tx_ref.time` so they become index range bounds.
|
|
15
|
+
*
|
|
16
|
+
* Correctness notes:
|
|
17
|
+
* - `tx_ref.time` is copied from `tx.time` on insert and rewritten on every update (TransactionTable.syncTxRefs),
|
|
18
|
+
* so ordering/filtering on `r.time` equals ordering/filtering on `tx.time`.
|
|
19
|
+
* Only difference: an empty-string `tx.time` is stored as NULL in tx_ref (never produced by the chain).
|
|
20
|
+
* - Single-value driver: `(hash, type, value)` is unique, so no duplicates are possible; ties on `time` come out
|
|
21
|
+
* in tx_ref index order (same as the pre-existing single-filter fast path).
|
|
22
|
+
* - Multi-value driver (`value in (a, b)`): a tx can match twice. The driver is a `select distinct hash, time`
|
|
23
|
+
* subquery over tx_ref only (index-only, no `tx` lookups), ordered by `(time, hash)`; `tx` rows are looked up
|
|
24
|
+
* lazily, only until offset+limit rows pass the remaining filters.
|
|
25
|
+
* - Additional JSON filters are `exists (select 1 from tx_ref x where x.hash = r.hash ...)` probes on idx_tx_ref_pk.
|
|
26
|
+
*/
|
|
27
|
+
const and = (parts) => kysely.sql.join(parts, kysely.sql` and `);
|
|
28
|
+
function txConditions(input) {
|
|
29
|
+
const { sender, receiver, gasPayer, direction, types = [], txs = [], validity } = input;
|
|
30
|
+
const conds = [];
|
|
31
|
+
if (sender && receiver) if (direction === "MUTUAL") conds.push(kysely.sql`((tx.sender = ${sender} and tx.receiver = ${receiver}) or (tx.sender = ${receiver} and tx.receiver = ${sender}))`);
|
|
32
|
+
else if (direction === "ONE_WAY") conds.push(kysely.sql`tx.sender = ${sender}`, kysely.sql`tx.receiver = ${receiver}`);
|
|
33
|
+
else conds.push(kysely.sql`(tx.sender = ${sender} or tx.receiver = ${receiver})`);
|
|
34
|
+
else if (sender) conds.push(kysely.sql`tx.sender = ${sender}`);
|
|
35
|
+
else if (receiver) conds.push(kysely.sql`tx.receiver = ${receiver}`);
|
|
36
|
+
if (gasPayer) conds.push(kysely.sql`tx.gasPayer = ${gasPayer}`);
|
|
37
|
+
if (types.length) conds.push(kysely.sql`tx.type in (${kysely.sql.join(types)})`);
|
|
38
|
+
if (txs.length) conds.push(kysely.sql`tx.hash in (${kysely.sql.join(txs)})`);
|
|
39
|
+
if (validity === "VALID") conds.push(kysely.sql`tx.valid = 1`);
|
|
40
|
+
else if (validity === "INVALID") conds.push(kysely.sql`tx.valid = 0`);
|
|
41
|
+
return conds;
|
|
42
|
+
}
|
|
43
|
+
function timeConditions(alias, start, end) {
|
|
44
|
+
const col = kysely.sql.ref(`${alias}.time`);
|
|
45
|
+
const conds = [];
|
|
46
|
+
if (start) conds.push(kysely.sql`${col} > ${start}`);
|
|
47
|
+
if (end) conds.push(kysely.sql`${col} <= ${end}`);
|
|
48
|
+
return conds;
|
|
49
|
+
}
|
|
50
|
+
/** Pick the filter that drives the scan: a single-valued one avoids the dedupe step. */
|
|
51
|
+
function pickDriver(refFilters) {
|
|
52
|
+
const single = refFilters.findIndex(([, values]) => values.length === 1);
|
|
53
|
+
return single >= 0 ? single : 0;
|
|
54
|
+
}
|
|
55
|
+
function buildTxRefDrivenQuery(input) {
|
|
56
|
+
const { refFilters, start, end, order, limit, offset } = input;
|
|
57
|
+
if (!refFilters.length) throw new Error("buildTxRefDrivenQuery requires at least one tx_ref filter");
|
|
58
|
+
const driverIndex = pickDriver(refFilters);
|
|
59
|
+
const [driverType, driverValues] = refFilters[driverIndex];
|
|
60
|
+
const dir = kysely.sql.raw(order === "desc" ? "desc" : "asc");
|
|
61
|
+
let from;
|
|
62
|
+
let countFrom;
|
|
63
|
+
const where = [];
|
|
64
|
+
if (driverValues.length === 1) {
|
|
65
|
+
from = kysely.sql`tx_ref r cross join tx`;
|
|
66
|
+
countFrom = from;
|
|
67
|
+
where.push(kysely.sql`r.type = ${driverType}`, kysely.sql`r.value = ${driverValues[0]}`, ...timeConditions("r", start, end));
|
|
68
|
+
} else {
|
|
69
|
+
const inner = and([
|
|
70
|
+
kysely.sql`type = ${driverType}`,
|
|
71
|
+
kysely.sql`value in (${kysely.sql.join(driverValues)})`,
|
|
72
|
+
...timeConditions("tx_ref", start, end)
|
|
73
|
+
]);
|
|
74
|
+
from = kysely.sql`(${kysely.sql`select distinct hash, time from tx_ref where ${inner} order by time ${dir}, hash limit -1`}) r cross join tx`;
|
|
75
|
+
countFrom = kysely.sql`(select distinct hash, time from tx_ref where ${inner}) r cross join tx`;
|
|
76
|
+
}
|
|
77
|
+
where.push(kysely.sql`tx.hash = r.hash`);
|
|
78
|
+
where.push(...txConditions(input));
|
|
79
|
+
refFilters.forEach(([refType, values], i) => {
|
|
80
|
+
if (i === driverIndex) return;
|
|
81
|
+
where.push(kysely.sql`exists (select 1 from tx_ref x where x.hash = r.hash and x.type = ${refType} and x.value in (${kysely.sql.join(values)}))`);
|
|
82
|
+
});
|
|
83
|
+
const whereSql = and(where);
|
|
84
|
+
const orderBy = driverValues.length === 1 ? kysely.sql`order by r.time ${dir}` : kysely.sql``;
|
|
85
|
+
return {
|
|
86
|
+
page: kysely.sql`select tx.* from ${from} where ${whereSql} ${orderBy} limit ${limit} offset ${offset}`,
|
|
87
|
+
count: kysely.sql`select count(*) as count from ${countFrom} where ${whereSql}`
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
//#endregion
|
|
92
|
+
exports.buildTxRefDrivenQuery = buildTxRefDrivenQuery;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { RawBuilder } from "kysely";
|
|
2
|
+
|
|
3
|
+
//#region src/db/tx-ref-query.d.ts
|
|
4
|
+
|
|
5
|
+
interface TxRefDrivenInput {
|
|
6
|
+
/** [tx_ref.type, values] pairs; at least one. */
|
|
7
|
+
refFilters: [string, string[]][];
|
|
8
|
+
sender?: string;
|
|
9
|
+
receiver?: string;
|
|
10
|
+
gasPayer?: string;
|
|
11
|
+
direction?: string;
|
|
12
|
+
types?: string[];
|
|
13
|
+
txs?: string[];
|
|
14
|
+
validity?: string;
|
|
15
|
+
/** exclusive lower bound on time (ISO string) */
|
|
16
|
+
start?: string;
|
|
17
|
+
/** inclusive upper bound on time (ISO string) */
|
|
18
|
+
end?: string;
|
|
19
|
+
order: 'asc' | 'desc';
|
|
20
|
+
limit: number;
|
|
21
|
+
offset: number;
|
|
22
|
+
}
|
|
23
|
+
interface TxRefDrivenQuery {
|
|
24
|
+
/** `select tx.* ...` ordered, offset/limit applied */
|
|
25
|
+
page: RawBuilder<Record<string, unknown>>;
|
|
26
|
+
/** `select count(*) as count ...` */
|
|
27
|
+
count: RawBuilder<{
|
|
28
|
+
count: number;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
declare function buildTxRefDrivenQuery(input: TxRefDrivenInput): TxRefDrivenQuery;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { TxRefDrivenInput, TxRefDrivenQuery, buildTxRefDrivenQuery };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/indexdb-sqlite",
|
|
3
3
|
"description": "OCAP indexdb adapter that uses SQLite as backend",
|
|
4
|
-
"version": "1.31.
|
|
4
|
+
"version": "1.31.7",
|
|
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/indexdb-test": "1.31.
|
|
17
|
+
"@ocap/indexdb-test": "1.31.7"
|
|
18
18
|
},
|
|
19
19
|
"homepage": "https://github.com/ArcBlock/blockchain/tree/master/indexdb/sqlite",
|
|
20
20
|
"keywords": [
|
|
@@ -63,10 +63,10 @@
|
|
|
63
63
|
"clean": "rm -rf lib esm"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@arcblock/did": "1.31.
|
|
67
|
-
"@ocap/indexdb": "1.31.
|
|
68
|
-
"@ocap/types": "1.31.
|
|
69
|
-
"@ocap/util": "1.31.
|
|
66
|
+
"@arcblock/did": "1.31.7",
|
|
67
|
+
"@ocap/indexdb": "1.31.7",
|
|
68
|
+
"@ocap/types": "1.31.7",
|
|
69
|
+
"@ocap/util": "1.31.7",
|
|
70
70
|
"debug": "^4.4.3",
|
|
71
71
|
"better-sqlite3": "^11.8.1",
|
|
72
72
|
"kysely": "^0.28.14",
|