envio 3.11.0 → 3.12.1

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.
Files changed (109) hide show
  1. package/index.d.ts +3 -0
  2. package/package.json +6 -6
  3. package/src/Batch.res +111 -78
  4. package/src/Batch.res.mjs +53 -46
  5. package/src/BatchProcessing.res +1 -11
  6. package/src/BatchProcessing.res.mjs +1 -4
  7. package/src/ChainFetching.res +7 -3
  8. package/src/ChainFetching.res.mjs +1 -1
  9. package/src/ChainState.res +82 -20
  10. package/src/ChainState.res.mjs +51 -13
  11. package/src/ChainState.resi +6 -1
  12. package/src/Config.res +58 -37
  13. package/src/Config.res.mjs +63 -53
  14. package/src/CrossChainState.res +50 -53
  15. package/src/CrossChainState.res.mjs +25 -23
  16. package/src/CrossChainState.resi +6 -13
  17. package/src/EffectState.res +13 -4
  18. package/src/EffectState.resi +7 -1
  19. package/src/EventProcessing.res +6 -6
  20. package/src/EventProcessing.res.mjs +6 -6
  21. package/src/FetchState.res +65 -46
  22. package/src/FetchState.res.mjs +68 -36
  23. package/src/FinalizeBackfill.res +16 -12
  24. package/src/FinalizeBackfill.res.mjs +3 -3
  25. package/src/HistoryPolicy.res +46 -0
  26. package/src/HistoryPolicy.res.mjs +50 -0
  27. package/src/InMemoryStore.res +30 -32
  28. package/src/InMemoryStore.res.mjs +33 -28
  29. package/src/InMemoryTable.res +155 -67
  30. package/src/InMemoryTable.res.mjs +151 -60
  31. package/src/IndexerState.res +66 -45
  32. package/src/IndexerState.res.mjs +57 -29
  33. package/src/IndexerState.resi +8 -13
  34. package/src/Internal.res +4 -2
  35. package/src/LoadLayer.res +68 -38
  36. package/src/LoadLayer.res.mjs +50 -64
  37. package/src/LoadLayer.resi +1 -1
  38. package/src/Main.res +25 -24
  39. package/src/Main.res.mjs +24 -25
  40. package/src/Metrics.res +49 -1
  41. package/src/Metrics.res.mjs +26 -3
  42. package/src/Persistence.res +55 -16
  43. package/src/Persistence.res.mjs +18 -3
  44. package/src/PgStorage.res +286 -180
  45. package/src/PgStorage.res.mjs +209 -122
  46. package/src/PruneStaleHistory.res +9 -15
  47. package/src/PruneStaleHistory.res.mjs +11 -20
  48. package/src/Rollback.res +9 -14
  49. package/src/Rollback.res.mjs +5 -3
  50. package/src/Sink.res +7 -5
  51. package/src/Sink.res.mjs +4 -4
  52. package/src/Staging.res +298 -0
  53. package/src/Staging.res.mjs +257 -0
  54. package/src/TestIndexer.res +14 -30
  55. package/src/TestIndexer.res.mjs +11 -9
  56. package/src/UserContext.res +20 -36
  57. package/src/UserContext.res.mjs +6 -9
  58. package/src/Utils.res +10 -4
  59. package/src/Utils.res.mjs +16 -16
  60. package/src/Writing.res +34 -20
  61. package/src/Writing.res.mjs +15 -20
  62. package/src/bindings/ClickHouse.res +41 -34
  63. package/src/bindings/ClickHouse.res.mjs +36 -30
  64. package/src/bindings/ClickHouseSink.res +45 -186
  65. package/src/bindings/ClickHouseSink.res.mjs +21 -174
  66. package/src/db/CheckpointSequence.res +109 -0
  67. package/src/db/CheckpointSequence.res.mjs +99 -0
  68. package/src/db/EntityFilter.res +487 -275
  69. package/src/db/EntityFilter.res.mjs +557 -309
  70. package/src/db/EntityHistory.res +6 -5
  71. package/src/db/EntityHistory.res.mjs +5 -5
  72. package/src/db/Frontier.res +86 -0
  73. package/src/db/Frontier.res.mjs +126 -0
  74. package/src/db/InternalTable.res +145 -27
  75. package/src/db/InternalTable.res.mjs +115 -29
  76. package/src/db/RollbackFloors.res +34 -41
  77. package/src/db/RollbackFloors.res.mjs +27 -71
  78. package/src/db/Table.res +21 -6
  79. package/src/db/Table.res.mjs +13 -4
  80. package/src/sources/BlockStore.res +8 -0
  81. package/src/sources/EvmHyperSyncSource.res +5 -1
  82. package/src/sources/EvmHyperSyncSource.res.mjs +4 -3
  83. package/src/sources/FuelHyperSyncSource.res +1 -0
  84. package/src/sources/FuelHyperSyncSource.res.mjs +1 -1
  85. package/src/sources/HyperSync.res +6 -0
  86. package/src/sources/HyperSync.res.mjs +5 -2
  87. package/src/sources/HyperSync.resi +2 -0
  88. package/src/sources/HyperSyncClient.res +6 -0
  89. package/src/sources/HyperSyncSSE.res +1 -1
  90. package/src/sources/HyperSyncSSE.res.mjs +4 -10
  91. package/src/sources/RequestStat.res +8 -1
  92. package/src/sources/RpcSource.res +1 -0
  93. package/src/sources/RpcSource.res.mjs +1 -1
  94. package/src/sources/SimulateSource.res +1 -0
  95. package/src/sources/SimulateSource.res.mjs +1 -1
  96. package/src/sources/Source.res +11 -3
  97. package/src/sources/SourceManager.res +34 -13
  98. package/src/sources/SourceManager.res.mjs +26 -9
  99. package/src/sources/SourceManager.resi +2 -0
  100. package/src/sources/SvmHyperSyncClient.res +5 -0
  101. package/src/sources/SvmHyperSyncSource.res +3 -0
  102. package/src/sources/SvmHyperSyncSource.res.mjs +4 -2
  103. package/src/tui/Tui.res +56 -143
  104. package/src/tui/Tui.res.mjs +70 -166
  105. package/src/tui/components/TuiData.res +61 -16
  106. package/src/tui/components/TuiData.res.mjs +53 -15
  107. package/svm.schema.json +0 -7
  108. package/src/db/CheckpointBounds.res +0 -53
  109. package/src/db/CheckpointBounds.res.mjs +0 -44
@@ -9,9 +9,16 @@ type historySchema = {
9
9
  checkpointsTable: string,
10
10
  checkpointChainIdColumn: string,
11
11
  checkpointBlockNumberColumn: string,
12
+ chainsTable: string,
13
+ chainsCheckpointIdColumn: string,
12
14
  }
13
15
 
14
- type chainProgressInput = {chainId: string, progressBlockNumber: int}
16
+ type chainProgressInput = {
17
+ chainId: string,
18
+ progressBlockNumber: int,
19
+ // Where the chain's own sequence stands, as Postgres has it committed.
20
+ committedCheckpointId: string,
21
+ }
15
22
 
16
23
  type options = {
17
24
  url: string,
@@ -62,12 +69,14 @@ type initializeInput = {
62
69
  databaseEngine?: string,
63
70
  }
64
71
 
72
+ // A history table and the column naming the chain its rows belong to. Absent
73
+ // only for a cross-chain entity, which no per-chain sequence can produce.
74
+ type historyTableInput = {name: string, chainIdColumn?: string}
75
+
65
76
  type resumeInput = {
66
- checkpointId: string,
77
+ perChain: bool,
67
78
  chainProgress: array<chainProgressInput>,
68
- historyTables: array<string>,
69
- replicated: bool,
70
- databaseEngine?: string,
79
+ historyTables: array<historyTableInput>,
71
80
  }
72
81
 
73
82
  type registeredTable = {
@@ -77,15 +86,6 @@ type registeredTable = {
77
86
  nullable: array<bool>,
78
87
  }
79
88
 
80
- type columnValuesInput = {
81
- numbers?: Float64Array.t,
82
- unsigned64?: BigUint64Array.t,
83
- signed64?: BigInt64Array.t,
84
- texts?: array<string>,
85
- bytes?: array<Uint8Array.t>,
86
- nulls?: Uint8Array.t,
87
- }
88
-
89
89
  @send
90
90
  external registerEntityTable: (t, entitySpec) => registeredTable = "registerEntityTable"
91
91
 
@@ -99,7 +99,30 @@ external registerCheckpointsTable: (t, array<columnSpec>) => registeredTable =
99
99
  external resume: (t, resumeInput) => promise<unit> = "resume"
100
100
 
101
101
  @send
102
- external stage: (t, ~table: int, ~rows: int, ~columns: array<columnValuesInput>) => int = "stage"
102
+ external beginStage: (t, ~table: int, ~rows: int) => Staging.begun = "beginStage"
103
+
104
+ @send
105
+ external growStage: (
106
+ t,
107
+ ~handle: int,
108
+ ~column: int,
109
+ ~needed: int,
110
+ ~stale: ArrayBuffer.t,
111
+ ) => ArrayBuffer.t = "growStage"
112
+
113
+ @send
114
+ external commitStage: (t, ~handle: int, ~buffers: array<ArrayBuffer.t>) => unit = "commitStage"
115
+
116
+ @send
117
+ external abortStage: (t, ~handle: int, ~buffers: array<ArrayBuffer.t>) => unit = "abortStage"
118
+
119
+ let arena = (sink): Staging.arena => {
120
+ beginStage: (~table, ~rows) => sink->beginStage(~table, ~rows),
121
+ growStage: (~handle, ~column, ~needed, ~stale) =>
122
+ sink->growStage(~handle, ~column, ~needed, ~stale),
123
+ commitStage: (~handle, ~buffers) => sink->commitStage(~handle, ~buffers),
124
+ abortStage: (~handle, ~buffers) => sink->abortStage(~handle, ~buffers),
125
+ }
103
126
 
104
127
  @send
105
128
  external writeBatch: (t, ~entities: array<int>, ~checkpoints: Null.t<int>) => promise<unit> =
@@ -116,6 +139,8 @@ let historySchema = (): historySchema => {
116
139
  checkpointsTable: InternalTable.Checkpoints.table.tableName,
117
140
  checkpointChainIdColumn: (#chain_id: InternalTable.Checkpoints.field :> string),
118
141
  checkpointBlockNumberColumn: (#block_number: InternalTable.Checkpoints.field :> string),
142
+ chainsTable: InternalTable.Chains.table.tableName,
143
+ chainsCheckpointIdColumn: (#checkpoint_id: InternalTable.Chains.field :> string),
119
144
  }
120
145
 
121
146
  let make = (~url, ~username, ~password, ~database, ~chainIdMode: ChainId.mode, ~onWarning) =>
@@ -131,45 +156,6 @@ let make = (~url, ~username, ~password, ~database, ~chainIdMode: ChainId.mode, ~
131
156
  onWarning,
132
157
  )
133
158
 
134
- type kind =
135
- | @as(0) F64
136
- | @as(1) U64
137
- | @as(2) I64
138
- | @as(3) Text
139
- | @as(4) Bytes
140
-
141
- let kindOfOrdinal = ordinal =>
142
- switch ordinal {
143
- | 0 => F64
144
- | 1 => U64
145
- | 2 => I64
146
- | 3 => Text
147
- | 4 => Bytes
148
- | unknown => JsError.throwWithMessage(`Unknown ClickHouse column kind ${unknown->Int.toString}`)
149
- }
150
-
151
- external asString: unknown => string = "%identity"
152
- @val external toNumber: unknown => float = "Number"
153
- @val external toBigInt: unknown => bigint = "BigInt"
154
- @val external stringOf: unknown => string = "String"
155
-
156
- // No column holds NaN or Infinity, but nothing downstream refuses them well: a
157
- // list renders one as `null` on the way into JSON text, which the column then
158
- // refuses for a reason naming `null` rather than what the handler wrote, and a
159
- // scalar Float64 takes the raw bytes and stores a value no reader can render.
160
- // Refused here instead, where the value is still itself, with one message for
161
- // both shapes.
162
- %%private(
163
- let finiteOrThrow = (number: float, ~column) =>
164
- if number->Float.isFinite {
165
- number
166
- } else {
167
- JsError.throwWithMessage(
168
- `${number->Float.toString} is not a finite number, so it cannot be stored in the \`${column}\` column. Store a finite number, or keep it out of the entity.`,
169
- )
170
- }
171
- )
172
-
173
159
  // Visits every node on the way into JSON text. A bigint would make
174
160
  // `JSON.stringify` throw and fail the whole batch, so it travels as its digits.
175
161
  %%private(
@@ -177,7 +163,7 @@ external asString: unknown => string = "%identity"
177
163
  (_, value: JSON.t) =>
178
164
  switch value->typeof {
179
165
  | #bigint =>
180
- value->(Utils.magic: JSON.t => unknown)->stringOf->(Utils.magic: string => JSON.t)
166
+ value->(Utils.magic: JSON.t => unknown)->Staging.stringOf->(Utils.magic: string => JSON.t)
181
167
  // A Uint8Array inside a list column travels as its byte values, which the
182
168
  // sink reads back into raw bytes.
183
169
  | #object =>
@@ -190,148 +176,21 @@ external asString: unknown => string = "%identity"
190
176
  | None => value
191
177
  }
192
178
  | #number =>
193
- let _ = value->(Utils.magic: JSON.t => float)->finiteOrThrow(~column)
179
+ let _ = value->(Utils.magic: JSON.t => float)->Staging.finiteOrThrow(~column)
194
180
  value
195
181
  | _ => value
196
182
  }
197
183
  )
198
184
 
199
- // The replacer closes over nothing but the column's name, so it is built once
200
- // with the column rather than once per column per batch.
201
- type column = {name: string, kind: kind, isNullable: bool, replacer: JSON.replacer}
202
- type table = {handle: int, name: string, columns: array<column>}
185
+ type table = {handle: int, name: string, columns: array<Staging.column>}
203
186
 
204
187
  let makeTable = (~name, {handle, names, kinds, nullable}: registeredTable) => {
205
188
  handle,
206
189
  name,
207
- columns: names->Array.mapWithIndex((name, index) => {
190
+ columns: names->Array.mapWithIndex((name, index): Staging.column => {
208
191
  name,
209
- kind: kinds->Array.getUnsafe(index)->kindOfOrdinal,
192
+ kind: kinds->Array.getUnsafe(index)->Staging.kindOfOrdinal,
210
193
  isNullable: nullable->Array.getUnsafe(index),
211
194
  replacer: Replacer(jsonSafe(~column=name)),
212
195
  }),
213
196
  }
214
-
215
- let toText = (value: unknown, ~replacer) =>
216
- switch value->typeof {
217
- | #string => value->asString
218
- | #bigint => value->stringOf
219
- | _ => value->(Utils.magic: unknown => JSON.t)->JSON.stringify(~replacer)
220
- }
221
-
222
- type builder = {
223
- name: string,
224
- kind: kind,
225
- isNullable: bool,
226
- floats: Float64Array.t,
227
- unsigned: BigUint64Array.t,
228
- signed: BigInt64Array.t,
229
- texts: array<string>,
230
- bytes: array<Uint8Array.t>,
231
- replacer: JSON.replacer,
232
- mutable nulls: option<Uint8Array.t>,
233
- rows: int,
234
- }
235
-
236
- %%private(let noFloats = Float64Array.fromLength(0))
237
- %%private(let noUnsigned = BigUint64Array.fromLength(0))
238
- %%private(let noSigned = BigInt64Array.fromLength(0))
239
- %%private(let noBytes = Uint8Array.fromLength(0))
240
-
241
- let makeBuilder = ({name, kind, isNullable, replacer}: column, ~rows) => {
242
- name,
243
- kind,
244
- isNullable,
245
- floats: kind === F64 ? Float64Array.fromLength(rows) : noFloats,
246
- unsigned: kind === U64 ? BigUint64Array.fromLength(rows) : noUnsigned,
247
- signed: kind === I64 ? BigInt64Array.fromLength(rows) : noSigned,
248
- texts: kind === Text ? Array.make(~length=rows, "") : [],
249
- bytes: kind === Bytes ? Array.make(~length=rows, noBytes) : [],
250
- replacer,
251
- nulls: None,
252
- rows,
253
- }
254
-
255
- let markNull = (builder, ~row) => {
256
- let nulls = switch builder.nulls {
257
- | Some(nulls) => nulls
258
- | None =>
259
- let nulls = Uint8Array.fromLength(builder.rows)
260
- builder.nulls = Some(nulls)
261
- nulls
262
- }
263
- nulls->TypedArray.set(row, 1)
264
- }
265
-
266
- // RowBinary carries the raw integer, so a value the column cannot hold is not
267
- // rejected anywhere downstream — and a typed array reduces it modulo 2^64 on
268
- // the way in rather than refusing it, which would leave no trace at all.
269
- %%private(
270
- let checkedBigInt = (value: unknown, ~builder, ~min, ~max) => {
271
- let value = value->toBigInt
272
- if value < min || value > max {
273
- JsError.throwWithMessage(
274
- `${value->BigInt.toString} is out of range for the \`${builder.name}\` column`,
275
- )
276
- }
277
- value
278
- }
279
- )
280
-
281
- %%private(
282
- let writePresent = (builder, ~row, value: unknown) =>
283
- switch builder.kind {
284
- | F64 =>
285
- builder.floats->TypedArray.set(row, value->toNumber->finiteOrThrow(~column=builder.name))
286
- | U64 =>
287
- builder.unsigned->TypedArray.set(
288
- row,
289
- value->checkedBigInt(~builder, ~min=0n, ~max=18446744073709551615n),
290
- )
291
- | I64 =>
292
- builder.signed->TypedArray.set(
293
- row,
294
- value->checkedBigInt(~builder, ~min=-9223372036854775808n, ~max=9223372036854775807n),
295
- )
296
- | Text => builder.texts->Array.setUnsafe(row, value->toText(~replacer=builder.replacer))
297
- | Bytes => builder.bytes->Array.setUnsafe(row, value->(Utils.magic: unknown => Uint8Array.t))
298
- }
299
- )
300
-
301
- %%private(let isAbsent = (value: unknown) => value === %raw(`undefined`) || value === %raw(`null`))
302
-
303
- // Writes one value of a row the handler set. `undefined`/`null` marks the row's
304
- // null bit, which a column that accepts NULL stores as such — and which one that
305
- // does not has no way to store: RowBinary carries no "absent", so the row would
306
- // land holding the type's zero, a value the handler never chose and that nothing
307
- // downstream could tell from one it did.
308
- let writeValue = (builder, ~row, value: unknown) =>
309
- if value->isAbsent {
310
- if builder.isNullable {
311
- builder->markNull(~row)
312
- } else {
313
- JsError.throwWithMessage(
314
- `No value for the \`${builder.name}\` column, which is not nullable. Set the field before saving the entity, or make it optional in the schema.`,
315
- )
316
- }
317
- } else {
318
- builder->writePresent(~row, value)
319
- }
320
-
321
- let writeDeletedValue = (builder, ~row, value: unknown) =>
322
- if value->isAbsent {
323
- builder->markNull(~row)
324
- } else {
325
- builder->writePresent(~row, value)
326
- }
327
-
328
- let builderPayload = (builder): columnValuesInput => {
329
- let base = {nulls: ?builder.nulls}
330
- switch builder.kind {
331
- | F64 => {...base, numbers: builder.floats}
332
- | U64 => {...base, unsigned64: builder.unsigned}
333
- | I64 => {...base, signed64: builder.signed}
334
- | Text => {...base, texts: builder.texts}
335
- | Bytes => {...base, bytes: builder.bytes}
336
- }
337
- }
@@ -3,10 +3,22 @@
3
3
  import * as Core from "../Core.res.mjs";
4
4
  import * as Table from "../db/Table.res.mjs";
5
5
  import * as Utils from "../Utils.res.mjs";
6
- import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
6
+ import * as Staging from "../Staging.res.mjs";
7
7
  import * as EntityHistory from "../db/EntityHistory.res.mjs";
8
8
  import * as InternalTable from "../db/InternalTable.res.mjs";
9
- import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
9
+
10
+ function arena(sink) {
11
+ return {
12
+ beginStage: (table, rows) => sink.beginStage(table, rows),
13
+ growStage: (handle, column, needed, stale) => sink.growStage(handle, column, needed, stale),
14
+ commitStage: (handle, buffers) => {
15
+ sink.commitStage(handle, buffers);
16
+ },
17
+ abortStage: (handle, buffers) => {
18
+ sink.abortStage(handle, buffers);
19
+ }
20
+ };
21
+ }
10
22
 
11
23
  function historySchema() {
12
24
  return {
@@ -17,7 +29,9 @@ function historySchema() {
17
29
  setVariant: "SET",
18
30
  checkpointsTable: InternalTable.Checkpoints.table.tableName,
19
31
  checkpointChainIdColumn: "chain_id",
20
- checkpointBlockNumberColumn: "block_number"
32
+ checkpointBlockNumberColumn: "block_number",
33
+ chainsTable: InternalTable.Chains.table.tableName,
34
+ chainsCheckpointIdColumn: "checkpoint_id"
21
35
  };
22
36
  }
23
37
 
@@ -32,31 +46,6 @@ function make(url, username, password, database, chainIdMode, onWarning) {
32
46
  }, onWarning);
33
47
  }
34
48
 
35
- function kindOfOrdinal(ordinal) {
36
- switch (ordinal) {
37
- case 0 :
38
- return 0;
39
- case 1 :
40
- return 1;
41
- case 2 :
42
- return 2;
43
- case 3 :
44
- return 3;
45
- case 4 :
46
- return 4;
47
- default:
48
- return Stdlib_JsError.throwWithMessage(`Unknown ClickHouse column kind ` + ordinal.toString());
49
- }
50
- }
51
-
52
- function finiteOrThrow(number, column) {
53
- if (isFinite(number)) {
54
- return number;
55
- } else {
56
- return Stdlib_JsError.throwWithMessage(number.toString() + ` is not a finite number, so it cannot be stored in the \`` + column + `\` column. Store a finite number, or keep it out of the entity.`);
57
- }
58
- }
59
-
60
49
  function jsonSafe(column) {
61
50
  return (param, value) => {
62
51
  let match = typeof value;
@@ -65,7 +54,7 @@ function jsonSafe(column) {
65
54
  }
66
55
  if (match !== "object") {
67
56
  if (match === "number") {
68
- finiteOrThrow(value, column);
57
+ Staging.finiteOrThrow(value, column);
69
58
  return value;
70
59
  } else {
71
60
  return value;
@@ -88,159 +77,17 @@ function makeTable(name, param) {
88
77
  name: name,
89
78
  columns: param.names.map((name, index) => ({
90
79
  name: name,
91
- kind: kindOfOrdinal(kinds[index]),
80
+ kind: Staging.kindOfOrdinal(kinds[index]),
92
81
  isNullable: nullable[index],
93
82
  replacer: jsonSafe(name)
94
83
  }))
95
84
  };
96
85
  }
97
86
 
98
- function toText(value, replacer) {
99
- let match = typeof value;
100
- if (match === "bigint") {
101
- return String(value);
102
- } else if (match === "string") {
103
- return value;
104
- } else {
105
- return JSON.stringify(value, replacer);
106
- }
107
- }
108
-
109
- let noFloats = new Float64Array(0);
110
-
111
- let noUnsigned = new BigUint64Array(0);
112
-
113
- let noSigned = new BigInt64Array(0);
114
-
115
- let noBytes = new Uint8Array(0);
116
-
117
- function makeBuilder(param, rows) {
118
- let kind = param.kind;
119
- return {
120
- name: param.name,
121
- kind: kind,
122
- isNullable: param.isNullable,
123
- floats: kind === 0 ? new Float64Array(rows) : noFloats,
124
- unsigned: kind === 1 ? new BigUint64Array(rows) : noUnsigned,
125
- signed: kind === 2 ? new BigInt64Array(rows) : noSigned,
126
- texts: kind === 3 ? Stdlib_Array.make(rows, "") : [],
127
- bytes: kind === 4 ? Stdlib_Array.make(rows, noBytes) : [],
128
- replacer: param.replacer,
129
- nulls: undefined,
130
- rows: rows
131
- };
132
- }
133
-
134
- function markNull(builder, row) {
135
- let nulls = builder.nulls;
136
- let nulls$1;
137
- if (nulls !== undefined) {
138
- nulls$1 = nulls;
139
- } else {
140
- let nulls$2 = new Uint8Array(builder.rows);
141
- builder.nulls = nulls$2;
142
- nulls$1 = nulls$2;
143
- }
144
- nulls$1[row] = 1;
145
- }
146
-
147
- function checkedBigInt(value, builder, min, max) {
148
- let value$1 = BigInt(value);
149
- if (value$1 < min || value$1 > max) {
150
- Stdlib_JsError.throwWithMessage(value$1.toString() + ` is out of range for the \`` + builder.name + `\` column`);
151
- }
152
- return value$1;
153
- }
154
-
155
- function writePresent(builder, row, value) {
156
- let match = builder.kind;
157
- switch (match) {
158
- case 0 :
159
- builder.floats[row] = finiteOrThrow(Number(value), builder.name);
160
- return;
161
- case 1 :
162
- builder.unsigned[row] = checkedBigInt(value, builder, 0n, 18446744073709551615n);
163
- return;
164
- case 2 :
165
- builder.signed[row] = checkedBigInt(value, builder, -9223372036854775808n, 9223372036854775807n);
166
- return;
167
- case 3 :
168
- builder.texts[row] = toText(value, builder.replacer);
169
- return;
170
- case 4 :
171
- builder.bytes[row] = value;
172
- return;
173
- }
174
- }
175
-
176
- function isAbsent(value) {
177
- if (value === undefined) {
178
- return true;
179
- } else {
180
- return value === null;
181
- }
182
- }
183
-
184
- function writeValue(builder, row, value) {
185
- if (isAbsent(value)) {
186
- if (builder.isNullable) {
187
- return markNull(builder, row);
188
- } else {
189
- return Stdlib_JsError.throwWithMessage(`No value for the \`` + builder.name + `\` column, which is not nullable. Set the field before saving the entity, or make it optional in the schema.`);
190
- }
191
- } else {
192
- return writePresent(builder, row, value);
193
- }
194
- }
195
-
196
- function writeDeletedValue(builder, row, value) {
197
- if (isAbsent(value)) {
198
- return markNull(builder, row);
199
- } else {
200
- return writePresent(builder, row, value);
201
- }
202
- }
203
-
204
- function builderPayload(builder) {
205
- let base_nulls = builder.nulls;
206
- let base = {
207
- nulls: base_nulls
208
- };
209
- let match = builder.kind;
210
- switch (match) {
211
- case 0 :
212
- let newrecord = {...base};
213
- newrecord.numbers = builder.floats;
214
- return newrecord;
215
- case 1 :
216
- let newrecord$1 = {...base};
217
- newrecord$1.unsigned64 = builder.unsigned;
218
- return newrecord$1;
219
- case 2 :
220
- let newrecord$2 = {...base};
221
- newrecord$2.signed64 = builder.signed;
222
- return newrecord$2;
223
- case 3 :
224
- let newrecord$3 = {...base};
225
- newrecord$3.texts = builder.texts;
226
- return newrecord$3;
227
- case 4 :
228
- let newrecord$4 = {...base};
229
- newrecord$4.bytes = builder.bytes;
230
- return newrecord$4;
231
- }
232
- }
233
-
234
87
  export {
88
+ arena,
235
89
  historySchema,
236
90
  make,
237
- kindOfOrdinal,
238
91
  makeTable,
239
- toText,
240
- makeBuilder,
241
- markNull,
242
- writeValue,
243
- writeDeletedValue,
244
- builderPayload,
245
92
  }
246
- /* noFloats Not a pure module */
93
+ /* Core Not a pure module */
@@ -0,0 +1,109 @@
1
+ // How checkpoint ids are handed out. A schema with a cross-chain entity has
2
+ // rows any chain's reorg can reach, so its checkpoints have to be comparable
3
+ // across chains and come from one shared counter. Without one, no chain can
4
+ // touch another's rows, and each gets a counter of its own — which is what lets
5
+ // a rollback, a prune or a resume name one chain without saying anything about
6
+ // the others.
7
+ type t =
8
+ | SharedAcrossChains
9
+ | PerChain
10
+
11
+ let fromEntities = (entities: array<Internal.entityConfig>) =>
12
+ entities->Array.some(entityConfig => entityConfig.crossChain) ? SharedAcrossChains : PerChain
13
+
14
+ // Where a chain stands: under one shared counter that is wherever the counter
15
+ // got to, whichever chain moved it last.
16
+ let position = (sequence: t, frontier: Frontier.t, ~chainId) =>
17
+ switch sequence {
18
+ | SharedAcrossChains => frontier->Frontier.max
19
+ | PerChain => frontier->Frontier.get(chainId)
20
+ }
21
+
22
+ // The committed (or processed) id a scope's rows compare against. A chain scope
23
+ // reads its own. A cross-chain scope spans every chain: under one shared
24
+ // sequence the highest id is exactly how far the scope has got, while per-chain
25
+ // ids aren't comparable across chains, so only the lowest is an id every chain
26
+ // has passed.
27
+ let forScope = (sequence: t, frontier: Frontier.t, ~scope: Internal.chainScope) =>
28
+ switch scope {
29
+ | Chain(chainId) => frontier->Frontier.get(chainId)
30
+ | CrossChain =>
31
+ switch sequence {
32
+ | SharedAcrossChains => frontier->Frontier.max
33
+ | PerChain => frontier->Frontier.min
34
+ }
35
+ }
36
+
37
+ // The diff id a scope's rows carry after a rollback, taken from the frontier the
38
+ // rollback stamped them with. A per-chain rollback names only the chains it
39
+ // moved: a sibling it left alone has no diff row, and so no id to compare
40
+ // against.
41
+ let findForScope = (sequence: t, frontier: Frontier.t, ~scope: Internal.chainScope): option<
42
+ Internal.checkpointId,
43
+ > =>
44
+ switch scope {
45
+ | Chain(chainId) => frontier->Frontier.find(chainId)
46
+ | CrossChain => Some(sequence->forScope(frontier, ~scope))
47
+ }
48
+
49
+ // Hands out the ids of one batch, starting from where the frontier left each
50
+ // chain. Under `SharedAcrossChains` the ids come from a single run of the counter, so they
51
+ // interleave across chains in allocation order; under `PerChain` each chain
52
+ // continues its own.
53
+ type cursor = {sequence: t, frontier: Frontier.t}
54
+
55
+ let cursor = (sequence: t, ~frontier: Frontier.t) => {sequence, frontier: frontier->Frontier.copy}
56
+
57
+ let next = (cursor, ~chainId): Internal.checkpointId => {
58
+ let checkpointId = cursor.sequence->position(cursor.frontier, ~chainId)->BigInt.add(1n)
59
+ cursor.frontier->Frontier.set(chainId, checkpointId)
60
+ checkpointId
61
+ }
62
+
63
+ // The checkpoint id each chain's rows are compared against in a query,
64
+ // together with the sequence that decides how the comparison is rendered.
65
+ type checkpointBoundsByChain = {sequence: t, byChain: Frontier.t}
66
+
67
+ // The pieces a query splices in to read its bound as `checkpointId`.
68
+ type sql = {
69
+ join: string,
70
+ using: string,
71
+ usingMatch: string,
72
+ checkpointId: string,
73
+ }
74
+
75
+ // The chain ids are cast to the widest integer type rather than the column's
76
+ // own: nothing indexes the join, so the values only have to compare.
77
+ let relation = `unnest($1::${(Postgres.BigInt :> string)}[],$2::${(Postgres.BigInt :> string)}[]) AS envio_bounds(chain_id, checkpoint_id)`
78
+
79
+ let sql = (
80
+ bounds: checkpointBoundsByChain,
81
+ ~chainIdColumn: option<string>,
82
+ ~tableRef: string,
83
+ ): sql =>
84
+ switch (bounds.sequence, chainIdColumn) {
85
+ | (SharedAcrossChains, _) => {join: "", using: "", usingMatch: "", checkpointId: "$1"}
86
+ | (PerChain, Some(column)) =>
87
+ let chainMatch = `envio_bounds.chain_id = ${tableRef}."${column}"`
88
+ {
89
+ join: ` JOIN ${relation} ON ${chainMatch}`,
90
+ using: ` USING ${relation}`,
91
+ usingMatch: ` AND ${chainMatch}`,
92
+ checkpointId: "envio_bounds.checkpoint_id",
93
+ }
94
+ | (PerChain, None) =>
95
+ JsError.throwWithMessage(
96
+ "Internal error: per-chain checkpoint bounds can't bound a table with no chain-id column. Only a schema whose entities are all per-chain has them.",
97
+ )
98
+ }
99
+
100
+ // Bound in the order `sql` reads them: under one shared sequence the lowest of
101
+ // the ids as `$1` (every chain is held to it), otherwise the chains and their
102
+ // own ids as the two parallel arrays the relation unnests.
103
+ let params = (bounds: checkpointBoundsByChain): unknown =>
104
+ switch bounds.sequence {
105
+ | SharedAcrossChains =>
106
+ [bounds.byChain->Frontier.min->BigInt.toString]->(Utils.magic: array<string> => unknown)
107
+ | PerChain =>
108
+ bounds.byChain->Frontier.unnestParams->(Utils.magic: Frontier.unnestParams => unknown)
109
+ }