@optimystic/db-core 1.19.0 → 1.20.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 (171) hide show
  1. package/dist/src/cluster/structs.d.ts +41 -8
  2. package/dist/src/cluster/structs.d.ts.map +1 -1
  3. package/dist/src/cluster/structs.js +18 -7
  4. package/dist/src/cluster/structs.js.map +1 -1
  5. package/dist/src/collection/action.d.ts +29 -0
  6. package/dist/src/collection/action.d.ts.map +1 -1
  7. package/dist/src/collection/action.js +6 -0
  8. package/dist/src/collection/action.js.map +1 -1
  9. package/dist/src/collection/collection.d.ts +533 -120
  10. package/dist/src/collection/collection.d.ts.map +1 -1
  11. package/dist/src/collection/collection.js +974 -500
  12. package/dist/src/collection/collection.js.map +1 -1
  13. package/dist/src/collection/head-commit.d.ts +110 -0
  14. package/dist/src/collection/head-commit.d.ts.map +1 -0
  15. package/dist/src/collection/head-commit.js +65 -0
  16. package/dist/src/collection/head-commit.js.map +1 -0
  17. package/dist/src/collection/index.d.ts +2 -0
  18. package/dist/src/collection/index.d.ts.map +1 -1
  19. package/dist/src/collection/index.js +2 -0
  20. package/dist/src/collection/index.js.map +1 -1
  21. package/dist/src/collection/segments.d.ts +77 -0
  22. package/dist/src/collection/segments.d.ts.map +1 -0
  23. package/dist/src/collection/segments.js +110 -0
  24. package/dist/src/collection/segments.js.map +1 -0
  25. package/dist/src/collection/struct.d.ts +156 -1
  26. package/dist/src/collection/struct.d.ts.map +1 -1
  27. package/dist/src/collection/struct.js +70 -0
  28. package/dist/src/collection/struct.js.map +1 -1
  29. package/dist/src/collection/write-attempts.d.ts +245 -0
  30. package/dist/src/collection/write-attempts.d.ts.map +1 -0
  31. package/dist/src/collection/write-attempts.js +439 -0
  32. package/dist/src/collection/write-attempts.js.map +1 -0
  33. package/dist/src/collections/tree/struct.d.ts +100 -17
  34. package/dist/src/collections/tree/struct.d.ts.map +1 -1
  35. package/dist/src/collections/tree/struct.js +38 -7
  36. package/dist/src/collections/tree/struct.js.map +1 -1
  37. package/dist/src/collections/tree/tree.d.ts +17 -5
  38. package/dist/src/collections/tree/tree.d.ts.map +1 -1
  39. package/dist/src/collections/tree/tree.js +112 -26
  40. package/dist/src/collections/tree/tree.js.map +1 -1
  41. package/dist/src/index.d.ts +2 -0
  42. package/dist/src/index.d.ts.map +1 -1
  43. package/dist/src/index.js +2 -0
  44. package/dist/src/index.js.map +1 -1
  45. package/dist/src/network/i-repo.d.ts +3 -0
  46. package/dist/src/network/i-repo.d.ts.map +1 -1
  47. package/dist/src/network/index.d.ts +3 -0
  48. package/dist/src/network/index.d.ts.map +1 -1
  49. package/dist/src/network/index.js +3 -0
  50. package/dist/src/network/index.js.map +1 -1
  51. package/dist/src/network/promise-shortfall-error.d.ts +40 -0
  52. package/dist/src/network/promise-shortfall-error.d.ts.map +1 -0
  53. package/dist/src/network/promise-shortfall-error.js +67 -0
  54. package/dist/src/network/promise-shortfall-error.js.map +1 -0
  55. package/dist/src/network/refuse-solo.d.ts +8 -0
  56. package/dist/src/network/refuse-solo.d.ts.map +1 -0
  57. package/dist/src/network/refuse-solo.js +8 -0
  58. package/dist/src/network/refuse-solo.js.map +1 -0
  59. package/dist/src/network/stale-failure.d.ts +20 -3
  60. package/dist/src/network/stale-failure.d.ts.map +1 -1
  61. package/dist/src/network/stale-failure.js +24 -3
  62. package/dist/src/network/stale-failure.js.map +1 -1
  63. package/dist/src/network/struct.d.ts +148 -2
  64. package/dist/src/network/struct.d.ts.map +1 -1
  65. package/dist/src/network/struct.js.map +1 -1
  66. package/dist/src/network/validity-rejected-error.d.ts +28 -0
  67. package/dist/src/network/validity-rejected-error.d.ts.map +1 -0
  68. package/dist/src/network/validity-rejected-error.js +51 -0
  69. package/dist/src/network/validity-rejected-error.js.map +1 -0
  70. package/dist/src/offline/entry.d.ts +72 -0
  71. package/dist/src/offline/entry.d.ts.map +1 -0
  72. package/dist/src/offline/entry.js +73 -0
  73. package/dist/src/offline/entry.js.map +1 -0
  74. package/dist/src/offline/index.d.ts +5 -0
  75. package/dist/src/offline/index.d.ts.map +1 -0
  76. package/dist/src/offline/index.js +5 -0
  77. package/dist/src/offline/index.js.map +1 -0
  78. package/dist/src/offline/journal.d.ts +61 -0
  79. package/dist/src/offline/journal.d.ts.map +1 -0
  80. package/dist/src/offline/journal.js +2 -0
  81. package/dist/src/offline/journal.js.map +1 -0
  82. package/dist/src/offline/queue.d.ts +484 -0
  83. package/dist/src/offline/queue.d.ts.map +1 -0
  84. package/dist/src/offline/queue.js +1236 -0
  85. package/dist/src/offline/queue.js.map +1 -0
  86. package/dist/src/offline/reachability.d.ts +25 -0
  87. package/dist/src/offline/reachability.d.ts.map +1 -0
  88. package/dist/src/offline/reachability.js +13 -0
  89. package/dist/src/offline/reachability.js.map +1 -0
  90. package/dist/src/offline/status.d.ts +148 -0
  91. package/dist/src/offline/status.d.ts.map +1 -0
  92. package/dist/src/offline/status.js +63 -0
  93. package/dist/src/offline/status.js.map +1 -0
  94. package/dist/src/offline/wake.d.ts +30 -0
  95. package/dist/src/offline/wake.d.ts.map +1 -0
  96. package/dist/src/offline/wake.js +68 -0
  97. package/dist/src/offline/wake.js.map +1 -0
  98. package/dist/src/testing/test-transactor.d.ts +29 -0
  99. package/dist/src/testing/test-transactor.d.ts.map +1 -1
  100. package/dist/src/testing/test-transactor.js +50 -0
  101. package/dist/src/testing/test-transactor.js.map +1 -1
  102. package/dist/src/transaction/commit-failure-class.d.ts +103 -0
  103. package/dist/src/transaction/commit-failure-class.d.ts.map +1 -0
  104. package/dist/src/transaction/commit-failure-class.js +96 -0
  105. package/dist/src/transaction/commit-failure-class.js.map +1 -0
  106. package/dist/src/transaction/coordinator.d.ts +165 -41
  107. package/dist/src/transaction/coordinator.d.ts.map +1 -1
  108. package/dist/src/transaction/coordinator.js +605 -300
  109. package/dist/src/transaction/coordinator.js.map +1 -1
  110. package/dist/src/transaction/entry-commit.d.ts +139 -0
  111. package/dist/src/transaction/entry-commit.d.ts.map +1 -0
  112. package/dist/src/transaction/entry-commit.js +199 -0
  113. package/dist/src/transaction/entry-commit.js.map +1 -0
  114. package/dist/src/transaction/index.d.ts +4 -1
  115. package/dist/src/transaction/index.d.ts.map +1 -1
  116. package/dist/src/transaction/index.js +1 -0
  117. package/dist/src/transaction/index.js.map +1 -1
  118. package/dist/src/transaction/transaction.d.ts +20 -0
  119. package/dist/src/transaction/transaction.d.ts.map +1 -1
  120. package/dist/src/transaction/validator.d.ts +7 -0
  121. package/dist/src/transaction/validator.d.ts.map +1 -1
  122. package/dist/src/transaction/validator.js +21 -12
  123. package/dist/src/transaction/validator.js.map +1 -1
  124. package/dist/src/transactor/network-transactor.d.ts +1 -0
  125. package/dist/src/transactor/network-transactor.d.ts.map +1 -1
  126. package/dist/src/transactor/network-transactor.js +61 -28
  127. package/dist/src/transactor/network-transactor.js.map +1 -1
  128. package/dist/src/transactor/transactor-source.d.ts +22 -11
  129. package/dist/src/transactor/transactor-source.d.ts.map +1 -1
  130. package/dist/src/transactor/transactor-source.js +9 -13
  131. package/dist/src/transactor/transactor-source.js.map +1 -1
  132. package/dist/src/utility/visit-every.d.ts +14 -0
  133. package/dist/src/utility/visit-every.d.ts.map +1 -0
  134. package/dist/src/utility/visit-every.js +27 -0
  135. package/dist/src/utility/visit-every.js.map +1 -0
  136. package/package.json +1 -1
  137. package/src/cluster/structs.ts +42 -7
  138. package/src/collection/action.ts +34 -0
  139. package/src/collection/collection.ts +1048 -543
  140. package/src/collection/head-commit.ts +148 -0
  141. package/src/collection/index.ts +2 -0
  142. package/src/collection/segments.ts +149 -0
  143. package/src/collection/struct.ts +183 -1
  144. package/src/collection/write-attempts.ts +506 -0
  145. package/src/collections/tree/struct.ts +97 -17
  146. package/src/collections/tree/tree.ts +125 -29
  147. package/src/index.ts +2 -0
  148. package/src/network/i-repo.ts +3 -0
  149. package/src/network/index.ts +3 -0
  150. package/src/network/promise-shortfall-error.ts +69 -0
  151. package/src/network/refuse-solo.ts +7 -0
  152. package/src/network/stale-failure.ts +27 -4
  153. package/src/network/struct.ts +154 -2
  154. package/src/network/validity-rejected-error.ts +55 -0
  155. package/src/offline/entry.ts +127 -0
  156. package/src/offline/index.ts +4 -0
  157. package/src/offline/journal.ts +60 -0
  158. package/src/offline/queue.ts +1342 -0
  159. package/src/offline/reachability.ts +32 -0
  160. package/src/offline/status.ts +183 -0
  161. package/src/offline/wake.ts +67 -0
  162. package/src/testing/test-transactor.ts +53 -0
  163. package/src/transaction/commit-failure-class.ts +133 -0
  164. package/src/transaction/coordinator.ts +696 -349
  165. package/src/transaction/entry-commit.ts +285 -0
  166. package/src/transaction/index.ts +4 -0
  167. package/src/transaction/transaction.ts +21 -0
  168. package/src/transaction/validator.ts +22 -12
  169. package/src/transactor/network-transactor.ts +62 -28
  170. package/src/transactor/transactor-source.ts +29 -13
  171. package/src/utility/visit-every.ts +25 -0
@@ -6,11 +6,32 @@ import { withBlockRevs } from "../network/durability.js";
6
6
  import type { ReadDependency } from "../transaction/transaction.js";
7
7
  import { ReadDependencyCollector } from "../transaction/read-dependency-collector.js";
8
8
  import { blockDigestsField, baseRevsField } from "../transform/digest.js";
9
+ import { refuseSoloField } from "../network/refuse-solo.js";
9
10
  import type { BlockFloorCheck } from "./block-floors.js";
10
11
  import { createLogger } from "../logger.js";
11
12
 
12
13
  const log = createLogger('transactor-source');
13
14
 
15
+ /** {@link TransactorSource.transact}'s optional parts of one attempt. Each is omitted from the
16
+ * request it rides on when unset, so a caller that sets nothing sends exactly the requests it always
17
+ * did — every one of them is hashed verbatim into the cohort signature preimages. */
18
+ export type TransactOptions = {
19
+ /** Aged, advisory retry priority (default 0). Rides on the pend so a repeatedly-losing
20
+ * single-collection sync out-ranks fresh rivals in a concurrent race (`resolveRace`);
21
+ * fairness-only, never affects validity. Omitted from the pend when 0. */
22
+ readonly priority?: number;
23
+ /** Per-block content declarations for the commit (see {@link BlockContentDigests}), computed by
24
+ * the caller from the same tracker that produced the transforms. */
25
+ readonly blockDigests?: BlockContentDigests;
26
+ /** Per-block base revisions for the pend (see `PendRequest.baseRevs`): the committed revision each
27
+ * update-only block's operations were computed against, from the same tracker
28
+ * (`Tracker.stagedBaseRevs`). */
29
+ readonly baseRevs?: BlockBaseRevs;
30
+ /** Refuse to be committed by a lone coordinator that knows it is not alone
31
+ * (`SyncOptions.refuseSoloCommit`): set on both the pend and the commit as `refuseSolo`. */
32
+ readonly refuseSolo?: boolean;
33
+ };
34
+
14
35
  /** The block `entry` answers for `id`, for a read at `context` (`undefined` for an unpinned "give me
15
36
  * latest" read) — `undefined` when the block is absent — or a throw when the entry is not an answer
16
37
  * that read may use. Every read that consumes a raw {@link GetBlockResult} goes through here, so a
@@ -218,16 +239,8 @@ export class TransactorSource<TBlock extends IBlock> implements BlockSource<TBlo
218
239
  * that creates it.
219
240
  * @param tailId - The Id of the collection's log tail block. This block's transform is committed FIRST
220
241
  * (prior to the rest of the block operations), to resolve the "winner" of a race to commit to the collection.
221
- * @param priority - Aged, advisory retry priority (default 0). Rides on the pend so a repeatedly-losing
222
- * single-collection sync out-ranks fresh rivals in a concurrent race (`resolveRace`); fairness-only, never
223
- * affects validity. Omitted from the pend when 0 so the common first-attempt pend serializes exactly as before.
224
- * @param blockDigests - Optional per-block content declarations for this commit (see {@link BlockContentDigests}),
225
- * computed by the caller from the same tracker that produced `transform`. Omitted from the commit request when
226
- * undefined, so a caller that declares nothing produces exactly the request shape as before — the field rides
227
- * inside every cohort signature's hash preimage, so keeping the shape clean keeps those preimages clean.
228
- * @param baseRevs - Optional per-block base revisions for the pend (see {@link PendRequest.baseRevs}): the
229
- * committed revision each update-only block's operations were computed against, from the same tracker
230
- * (`Tracker.stagedBaseRevs`). Omitted from the pend request when empty, for the same preimage reason.
242
+ * @param options - The attempt's optional parts ({@link TransactOptions}); each is left off the request it
243
+ * rides on when unset.
231
244
  * @returns The transactor's own verdict, unflattened: a {@link CommitSuccess} carrying the
232
245
  * {@link WriteDurability} of the committed revision — naming every block it committed at `rev`
233
246
  * (`blockRevs`) — or a {@link StaleFailure} if the pend or the commit was refused. Success is
@@ -236,13 +249,15 @@ export class TransactorSource<TBlock extends IBlock> implements BlockSource<TBlo
236
249
  * `result.success`. Test "is this completely saved" through `isFullyDurable`, never
237
250
  * by comparing `quorum` (see {@link WriteDurability}).
238
251
  */
239
- async transact(transform: Transforms, actionId: ActionId, rev: number, headerId: BlockId, tailId: BlockId, priority = 0, blockDigests?: BlockContentDigests, baseRevs?: BlockBaseRevs): Promise<CommitResult> {
252
+ async transact(transform: Transforms, actionId: ActionId, rev: number, headerId: BlockId, tailId: BlockId, options: TransactOptions = {}): Promise<CommitResult> {
253
+ const { priority = 0, blockDigests, baseRevs, refuseSolo } = options;
240
254
  const pendResult = await this.transactor.pend({
241
255
  transforms: transform, actionId, rev, policy: 'r',
242
256
  ...(priority > 0 ? { priority } : {}),
243
257
  ...baseRevsField(baseRevs),
244
258
  // One collection, pended whole and then committed: the shape `PendRequest.soleReservation` names.
245
- soleReservation: true
259
+ soleReservation: true,
260
+ ...refuseSoloField(refuseSolo)
246
261
  });
247
262
  if (!pendResult.success) {
248
263
  return pendResult;
@@ -255,7 +270,8 @@ export class TransactorSource<TBlock extends IBlock> implements BlockSource<TBlo
255
270
  blockIds: pendResult.blockIds,
256
271
  actionId,
257
272
  rev,
258
- ...blockDigestsField(blockDigests)
273
+ ...blockDigestsField(blockDigests),
274
+ ...refuseSoloField(refuseSolo)
259
275
  });
260
276
  if (!commitResult.success) {
261
277
  // A confirmed conflict has to be RETURNED as the StaleFailure, because `Collection.sync`
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Visit every item in turn, letting no throw stop the rest, and throw the FIRST error once every
3
+ * item has been visited. Sequential on purpose: each visit must finish before the next starts.
4
+ *
5
+ * For a round of independent per-collection steps — a rollback's restores, a commit's unwinding of
6
+ * its failed participants — where stopping at the first throw would leave every later collection
7
+ * untouched, and silently: the caller sees one error and has no way to tell which collections it
8
+ * reached. Each step being independent (its own instance, its own state) is what makes the first
9
+ * error the right one to report; a step whose failure should change how the next is handled does
10
+ * not belong in this loop. `TransactionCoordinator.refreshBetweenAttempts` is the same idiom with
11
+ * a per-error classification this helper does not carry.
12
+ */
13
+ export async function visitEveryThenThrow<T>(items: Iterable<T>, visit: (item: T) => Promise<void>): Promise<void> {
14
+ let first: { error: unknown } | undefined;
15
+ for (const item of items) {
16
+ try {
17
+ await visit(item);
18
+ } catch (error) {
19
+ first ??= { error };
20
+ }
21
+ }
22
+ if (first !== undefined) {
23
+ throw first.error;
24
+ }
25
+ }