-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathsqlite.js
More file actions
169 lines (148 loc) Β· 4.76 KB
/
sqlite.js
File metadata and controls
169 lines (148 loc) Β· 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { stat } from 'node:fs'
import Database from 'better-sqlite3'
import bytes from 'bytes'
import { BLOCKS_TABLE, CHECKPOINT_FILES_TABLE, CHECKPOINTS_TABLE, EVALUATIONS_TABLE, MESSAGES_TABLE, MODULES_TABLE, PROCESSES_TABLE } from './db.js'
const createProcesses = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${PROCESSES_TABLE}(
id TEXT PRIMARY KEY,
signature TEXT,
data TEXT,
anchor TEXT,
owner TEXT,
tags JSONB,
block JSONB
) WITHOUT ROWID;`
).run()
const createBlocks = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${BLOCKS_TABLE}(
id INTEGER PRIMARY KEY,
height INTEGER,
timestamp INTEGER
) WITHOUT ROWID;`
).run()
const createModules = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${MODULES_TABLE}(
id TEXT PRIMARY KEY,
owner TEXT,
tags JSONB
) WITHOUT ROWID;`
).run()
const createEvaluations = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${EVALUATIONS_TABLE}(
id TEXT PRIMARY KEY,
processId TEXT,
messageId TEXT,
deepHash TEXT,
nonce INTEGER,
epoch INTEGER,
timestamp INTEGER,
ordinate TEXT,
blockHeight INTEGER,
cron TEXT,
output JSONB,
evaluatedAt INTEGER
) WITHOUT ROWID;`
).run()
const createMessages = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${MESSAGES_TABLE}(
id TEXT,
processId TEXT,
seq TEXT,
PRIMARY KEY (id, processId)
) WITHOUT ROWID;`
).run()
const createCheckpoints = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${CHECKPOINTS_TABLE}(
id TEXT PRIMARY KEY,
processId TEXT,
timestamp INTEGER,
ordinate TEXT,
cron TEXT,
memory TEXT,
evaluation TEXT
) WITHOUT ROWID;`
).run()
const createCheckpointFiles = async (db) => db.prepare(
`CREATE TABLE IF NOT EXISTS ${CHECKPOINT_FILES_TABLE}(
id TEXT PRIMARY KEY,
processId TEXT UNIQUE,
timestamp INTEGER,
ordinate TEXT,
cron TEXT,
file TEXT,
evaluation TEXT,
cachedAt INTEGER
) WITHOUT ROWID;`
).run()
const createBlocksIndexes = async (db) => db.prepare(
`CREATE INDEX IF NOT EXISTS idx_${BLOCKS_TABLE}_height_timestamp
ON ${BLOCKS_TABLE}
(height, timestamp);`
).run()
const createMessagesIndexes = async (db) => db.prepare(
`CREATE INDEX IF NOT EXISTS idx_${MESSAGES_TABLE}_id_processId_seq
ON ${MESSAGES_TABLE}
(id, processId, seq);
`
).run()
const createCheckpointsIndexes = async (db) => db.prepare(
`CREATE INDEX IF NOT EXISTS idx_${CHECKPOINTS_TABLE}_processId_timestamp
ON ${CHECKPOINTS_TABLE}
(processId, timestamp);`
).run()
const createCheckpointFilesIndexes = async (db) => db.prepare(
`CREATE INDEX IF NOT EXISTS idx_${CHECKPOINT_FILES_TABLE}_processId_timestamp
ON ${CHECKPOINTS_TABLE}
(processId, timestamp);`
).run()
const createEvaluationsIndexes = async (db) => db.prepare(
`CREATE INDEX IF NOT EXISTS idx_${EVALUATIONS_TABLE}_id_messageId
ON ${EVALUATIONS_TABLE}
(id, messageId);`
).run()
let internalSqliteDb
export async function createSqliteClient ({ url, bootstrap = false, walLimit = bytes.parse('100mb') }) {
if (internalSqliteDb) return internalSqliteDb
const db = Database(url)
if (bootstrap) {
db.pragma('encoding = "UTF-8"')
db.pragma('journal_mode = WAL')
/**
* https://github.com/WiseLibs/better-sqlite3/blob/master/docs/performance.md#checkpoint-starvation
*/
setInterval(stat.bind(null, `${url}-wal`, (err, stat) => {
if (err && err.code !== 'ENOENT') throw err
if (stat && stat.size > walLimit) db.pragma('wal_checkpoint(RESTART)')
}), 5000).unref()
await Promise.resolve()
.then(() => createProcesses(db))
.then(() => createBlocks(db))
.then(() => createModules(db))
.then(() => createEvaluations(db))
.then(() => createMessages(db))
.then(() => createCheckpoints(db))
.then(() => createCheckpointFiles(db))
.then(() => createBlocksIndexes(db))
.then(() => createMessagesIndexes(db))
.then(() => createCheckpointsIndexes(db))
.then(() => createCheckpointFilesIndexes(db))
.then(() => createEvaluationsIndexes(db))
}
return {
query: async ({ sql, parameters }) => db.prepare(sql).all(...parameters),
run: async ({ sql, parameters }) => db.prepare(sql).run(...parameters),
transaction: async (statements) => db.transaction(
(statements) => statements.map(({ sql, parameters }) => db.prepare(sql).run(...parameters))
)(statements),
db
}
}
/**
* Use a high value unicode character to terminate a range query prefix.
* This will cause only string with a given prefix to match a range query
*/
export const COLLATION_SEQUENCE_MAX_CHAR = '\u{10FFFF}'
/**
* This technically isn't the smallest char, but it's small enough for our needs
*/
export const COLLATION_SEQUENCE_MIN_CHAR = '0'