@optimystic/db-core 1.18.0 → 1.19.0
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 +2 -1
- package/dist/src/blocks/helpers.d.ts +7 -0
- package/dist/src/blocks/helpers.d.ts.map +1 -1
- package/dist/src/blocks/helpers.js +7 -1
- package/dist/src/blocks/helpers.js.map +1 -1
- package/dist/src/btree/btree.d.ts +100 -4
- package/dist/src/btree/btree.d.ts.map +1 -1
- package/dist/src/btree/btree.js +252 -90
- package/dist/src/btree/btree.js.map +1 -1
- package/dist/src/btree/node-division.d.ts +37 -0
- package/dist/src/btree/node-division.d.ts.map +1 -0
- package/dist/src/btree/node-division.js +133 -0
- package/dist/src/btree/node-division.js.map +1 -0
- package/dist/src/chain/chain.d.ts +23 -0
- package/dist/src/chain/chain.d.ts.map +1 -1
- package/dist/src/chain/chain.js +43 -6
- package/dist/src/chain/chain.js.map +1 -1
- package/dist/src/collection/collection.d.ts +38 -5
- package/dist/src/collection/collection.d.ts.map +1 -1
- package/dist/src/collection/collection.js +126 -11
- package/dist/src/collection/collection.js.map +1 -1
- package/dist/src/collection/struct.d.ts +20 -0
- package/dist/src/collection/struct.d.ts.map +1 -1
- package/dist/src/collection/struct.js.map +1 -1
- package/dist/src/collections/tree/tree.d.ts.map +1 -1
- package/dist/src/collections/tree/tree.js +5 -3
- package/dist/src/collections/tree/tree.js.map +1 -1
- package/dist/src/log/log.d.ts +5 -1
- package/dist/src/log/log.d.ts.map +1 -1
- package/dist/src/log/log.js +7 -1
- package/dist/src/log/log.js.map +1 -1
- package/dist/src/network/block-size.d.ts +67 -0
- package/dist/src/network/block-size.d.ts.map +1 -0
- package/dist/src/network/block-size.js +82 -0
- package/dist/src/network/block-size.js.map +1 -0
- package/dist/src/network/index.d.ts +2 -0
- package/dist/src/network/index.d.ts.map +1 -1
- package/dist/src/network/index.js +2 -0
- package/dist/src/network/index.js.map +1 -1
- package/dist/src/network/struct.d.ts +13 -11
- package/dist/src/network/struct.d.ts.map +1 -1
- package/dist/src/network/struct.js.map +1 -1
- package/dist/src/network/transaction-size.d.ts +59 -0
- package/dist/src/network/transaction-size.d.ts.map +1 -0
- package/dist/src/network/transaction-size.js +74 -0
- package/dist/src/network/transaction-size.js.map +1 -0
- package/dist/src/reactivity/config.d.ts +8 -4
- package/dist/src/reactivity/config.d.ts.map +1 -1
- package/dist/src/reactivity/config.js +7 -3
- package/dist/src/reactivity/config.js.map +1 -1
- package/dist/src/reactivity/rotation.d.ts +9 -3
- package/dist/src/reactivity/rotation.d.ts.map +1 -1
- package/dist/src/reactivity/rotation.js +8 -2
- package/dist/src/reactivity/rotation.js.map +1 -1
- package/dist/src/testing/test-transactor.d.ts +2 -0
- package/dist/src/testing/test-transactor.d.ts.map +1 -1
- package/dist/src/testing/test-transactor.js +10 -0
- package/dist/src/testing/test-transactor.js.map +1 -1
- package/dist/src/transaction/coordinator.d.ts +72 -3
- package/dist/src/transaction/coordinator.d.ts.map +1 -1
- package/dist/src/transaction/coordinator.js +320 -146
- package/dist/src/transaction/coordinator.js.map +1 -1
- package/dist/src/transactor/change-notifier.d.ts +13 -4
- package/dist/src/transactor/change-notifier.d.ts.map +1 -1
- package/dist/src/transactor/change-notifier.js.map +1 -1
- package/dist/src/transactor/network-transactor.d.ts +57 -8
- package/dist/src/transactor/network-transactor.d.ts.map +1 -1
- package/dist/src/transactor/network-transactor.js +185 -24
- package/dist/src/transactor/network-transactor.js.map +1 -1
- package/dist/src/transactor/transactor.d.ts +14 -0
- package/dist/src/transactor/transactor.d.ts.map +1 -1
- package/dist/src/transform/digest.d.ts +20 -0
- package/dist/src/transform/digest.d.ts.map +1 -1
- package/dist/src/transform/digest.js +23 -3
- package/dist/src/transform/digest.js.map +1 -1
- package/package.json +1 -1
- package/src/blocks/helpers.ts +8 -1
- package/src/btree/btree.ts +266 -88
- package/src/btree/node-division.ts +155 -0
- package/src/chain/chain.ts +55 -6
- package/src/collection/collection.ts +135 -13
- package/src/collection/struct.ts +18 -0
- package/src/collections/tree/tree.ts +4 -2
- package/src/log/log.ts +7 -1
- package/src/network/block-size.ts +81 -0
- package/src/network/index.ts +2 -0
- package/src/network/struct.ts +15 -13
- package/src/network/transaction-size.ts +73 -0
- package/src/reactivity/config.ts +8 -4
- package/src/reactivity/rotation.ts +9 -3
- package/src/testing/test-transactor.ts +12 -0
- package/src/transaction/coordinator.ts +383 -154
- package/src/transactor/change-notifier.ts +13 -4
- package/src/transactor/network-transactor.ts +192 -29
- package/src/transactor/transactor.ts +16 -0
- package/src/transform/digest.ts +36 -3
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { BlockId } from "../blocks/index.js";
|
|
2
|
+
import type { BranchNode, LeafNode } from "./nodes.js";
|
|
3
|
+
import { jsonEncodedBytes } from "../network/transaction-size.js";
|
|
4
|
+
|
|
5
|
+
// The arithmetic behind a node's size limit: how a node's items divide into nodes once they no
|
|
6
|
+
// longer fit one, by count or by bytes. Every function here is pure, so a replayed or retried action
|
|
7
|
+
// divides a node identically; the tree (`btree.ts`) applies the division it is handed. A node's JSON
|
|
8
|
+
// is its header and empty arrays (a fixed part per node) plus each item's JSON and the commas between
|
|
9
|
+
// them, so one serialization per item answers both whether a node fits and where to divide it.
|
|
10
|
+
|
|
11
|
+
/** The byte rule a node's items are divided under: the target, and the bytes a node holding items
|
|
12
|
+
* `[from, to)` encodes to. */
|
|
13
|
+
export type ItemBytes = { target: number; between(from: number, to: number): number };
|
|
14
|
+
|
|
15
|
+
/** The byte rule for a leaf that would hold `entries`: its fixed part plus the entries and the
|
|
16
|
+
* commas between them. */
|
|
17
|
+
export function leafItemBytes(leaf: LeafNode<unknown>, entries: readonly unknown[], target: number): ItemBytes {
|
|
18
|
+
const before = prefixSums(itemBytes(entries));
|
|
19
|
+
const base = emptyLeafBytes(leaf);
|
|
20
|
+
return {
|
|
21
|
+
target,
|
|
22
|
+
between: (from, to) => base + before[to]! - before[from]! + Math.max(0, to - from - 1),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The byte rule for a branch that would hold children `nodes` under the keys `partitions`. A run of
|
|
27
|
+
* children `[from, to)` carries the keys between them; the key before `from` is promoted to the
|
|
28
|
+
* parent and counts on neither side. */
|
|
29
|
+
export function branchItemBytes(branch: BranchNode<unknown>, partitions: readonly unknown[], nodes: readonly BlockId[], target: number): ItemBytes {
|
|
30
|
+
const ids = prefixSums(itemBytes(nodes));
|
|
31
|
+
const keys = prefixSums(itemBytes(partitions));
|
|
32
|
+
const base = emptyBranchBytes(branch);
|
|
33
|
+
return {
|
|
34
|
+
target,
|
|
35
|
+
between: (from, to) => base + ids[to]! - ids[from]! + keys[to - 1]! - keys[from]!
|
|
36
|
+
+ Math.max(0, to - from - 1) + Math.max(0, to - from - 2),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What a leaf encodes to with no entries: the part of its size the entries do not account for. */
|
|
41
|
+
export function emptyLeafBytes(leaf: LeafNode<unknown>): number {
|
|
42
|
+
return jsonEncodedBytes({ header: leaf.header, entries: [] });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What a branch encodes to with no children: the part of its size the keys and ids do not account for. */
|
|
46
|
+
export function emptyBranchBytes(branch: BranchNode<unknown>): number {
|
|
47
|
+
return jsonEncodedBytes({ header: branch.header, partitions: [], nodes: [] });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The JSON size of each item, by the measure block limits are judged with. */
|
|
51
|
+
function itemBytes(items: readonly unknown[]): number[] {
|
|
52
|
+
return items.map(item => jsonEncodedBytes(item));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Running totals: `sums[i]` is the bytes of the first `i` items, so any run of items is one subtraction. */
|
|
56
|
+
function prefixSums(sizes: readonly number[]): number[] {
|
|
57
|
+
const sums = [0];
|
|
58
|
+
for (const size of sizes) {
|
|
59
|
+
sums.push(sums[sums.length - 1]! + size);
|
|
60
|
+
}
|
|
61
|
+
return sums;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* How `count` items of one node divide into nodes when they no longer fit one, as run boundaries —
|
|
66
|
+
* run `i` is the items `[runs[i], runs[i + 1])`, the first boundary 0 and the last `count` — or
|
|
67
|
+
* `undefined` when they fit. A run fits when it holds at most `capacity` items and either no more
|
|
68
|
+
* than `always` items (one entry, or two children: the least a node of its kind holds, so a node over
|
|
69
|
+
* the byte target holds no more than that) or, under the byte rule, no more than the target's bytes.
|
|
70
|
+
*
|
|
71
|
+
* - Over the count alone: two runs at the midpoint ({@link countSplit}), the division the tree has
|
|
72
|
+
* always made, kept as it was so a tree of small entries writes the transforms it always did.
|
|
73
|
+
* - Over the bytes: the fewest fitting runs ({@link fewestRuns}), so an entry larger than the target
|
|
74
|
+
* is left alone in a node of its own and a node that outgrew the target before this rule existed is
|
|
75
|
+
* brought within it in one write; where two runs suffice, the two whose larger is smallest
|
|
76
|
+
* ({@link balancedSplit}).
|
|
77
|
+
*/
|
|
78
|
+
export function divideItems(count: number, capacity: number, at: number, always: number, bytes?: ItemBytes): number[] | undefined {
|
|
79
|
+
const fits = (from: number, to: number) => to - from <= capacity
|
|
80
|
+
&& (bytes === undefined || to - from <= always || bytes.between(from, to) <= bytes.target);
|
|
81
|
+
if (fits(0, count)) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
if (bytes === undefined || count <= always || bytes.between(0, count) <= bytes.target) {
|
|
85
|
+
return [0, countSplit(count, at), count]; // the count is the only reason
|
|
86
|
+
}
|
|
87
|
+
const runs = fewestRuns(count, always, fits);
|
|
88
|
+
if (runs.length === 3) {
|
|
89
|
+
runs[1] = balancedSplit(count, always, fits, bytes.between, runs[1]!);
|
|
90
|
+
}
|
|
91
|
+
return runs;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Which run of `runs` (see {@link divideItems}) item `index` falls in. */
|
|
95
|
+
export function runOf(runs: readonly number[], index: number): number {
|
|
96
|
+
let run = 0;
|
|
97
|
+
while (index >= runs[run + 1]!) {
|
|
98
|
+
++run;
|
|
99
|
+
}
|
|
100
|
+
return run;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Where the count rule divides `count` items: at the midpoint, the odd item going to the half the
|
|
104
|
+
* edit at `at` lands in. */
|
|
105
|
+
function countSplit(count: number, at: number): number {
|
|
106
|
+
const mid = count >>> 1;
|
|
107
|
+
return mid + (at < mid ? 1 : 0);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The fewest runs that each fit, packing from the left: as many items into a run as fit, then the
|
|
111
|
+
* next run. A final run of a single item takes one more from the run before it when that run can
|
|
112
|
+
* spare one (it keeps more than `always`) and the pair fits — so a branch is not left routing to one
|
|
113
|
+
* child when its neighbour could have given it another.
|
|
114
|
+
*
|
|
115
|
+
* NOTE: three children whose two keys each pass half the target still leave a one-child branch (the
|
|
116
|
+
* run before it holds only `always`); the tree routes through it, and a delete merges it away. Only
|
|
117
|
+
* a key over half the target gets there, which no key the SQL layer frames does; if one ever does,
|
|
118
|
+
* let a run take an item past the target before leaving one alone. */
|
|
119
|
+
function fewestRuns(count: number, always: number, fits: (from: number, to: number) => boolean): number[] {
|
|
120
|
+
const runs = [0];
|
|
121
|
+
for (let from = 0; from < count;) {
|
|
122
|
+
let to = from + 1;
|
|
123
|
+
while (to < count && fits(from, to + 1)) {
|
|
124
|
+
++to;
|
|
125
|
+
}
|
|
126
|
+
runs.push(to);
|
|
127
|
+
from = to;
|
|
128
|
+
}
|
|
129
|
+
const last = runs.length - 1;
|
|
130
|
+
if (last >= 2 && runs[last]! - runs[last - 1]! === 1 && runs[last - 1]! - runs[last - 2]! > always && fits(runs[last - 1]! - 1, count)) {
|
|
131
|
+
runs[last - 1] = runs[last - 1]! - 1;
|
|
132
|
+
}
|
|
133
|
+
return runs;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Where to divide `count` items into two runs that both fit so that the larger run's bytes are as
|
|
137
|
+
* small as possible. Each run keeps at least `always` items when the count allows, so a branch is
|
|
138
|
+
* not divided down to one child for the sake of balance. `fallback` is a division already known to
|
|
139
|
+
* fit, returned when no candidate does. */
|
|
140
|
+
function balancedSplit(count: number, always: number, fits: (from: number, to: number) => boolean, between: (from: number, to: number) => number, fallback: number): number {
|
|
141
|
+
const least = count >= 2 * always ? always : 1;
|
|
142
|
+
let best = fallback;
|
|
143
|
+
let bestLarger = Infinity;
|
|
144
|
+
for (let m = least; m <= count - least; ++m) {
|
|
145
|
+
if (!fits(0, m) || !fits(m, count)) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const larger = Math.max(between(0, m), between(m, count));
|
|
149
|
+
if (larger < bestLarger) {
|
|
150
|
+
bestLarger = larger;
|
|
151
|
+
best = m;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return best;
|
|
155
|
+
}
|
package/src/chain/chain.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { BlockStore, BlockId, IBlock } from "../index.js";
|
|
|
3
3
|
import { ChainDataBlockType, ChainHeaderBlockType, entries$, headId$, nextId$, priorId$, tailId$ } from "./chain-nodes.js";
|
|
4
4
|
import type { ChainDataNode, ChainHeaderNode } from "./chain-nodes.js";
|
|
5
5
|
import { apply } from "../blocks/index.js";
|
|
6
|
+
import { jsonEncodedBytes } from "../network/transaction-size.js";
|
|
6
7
|
|
|
7
8
|
export const EntriesPerBlock = 32;
|
|
8
9
|
|
|
@@ -20,6 +21,17 @@ export type ChainInitOptions<TEntry> = {
|
|
|
20
21
|
createDataBlock?: () => ChainNodeInit<ChainDataNode<TEntry>>;
|
|
21
22
|
createHeaderBlock?: (id?: BlockId) => ChainNodeInit<ChainHeaderNode>;
|
|
22
23
|
newBlock?: (newTail: ChainDataNode<TEntry>, oldTail: ChainDataNode<TEntry> | undefined) => Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* When set, a data block that already holds an entry also starts a new block once the next entry
|
|
26
|
+
* would take its encoded size (`jsonEncodedBytes`) past this many bytes — besides the
|
|
27
|
+
* {@link EntriesPerBlock} count, which always applies. A block holding no entry always takes the
|
|
28
|
+
* next one, so an entry larger than this sits alone in its own block. Absent, blocks roll over by
|
|
29
|
+
* count alone. The log sets it to `BLOCK_TARGET_BYTES`.
|
|
30
|
+
*
|
|
31
|
+
* The decision is a pure function of the tail block's content and the entries being added, so a
|
|
32
|
+
* retried append at the same revision splits identically.
|
|
33
|
+
*/
|
|
34
|
+
blockTargetBytes?: number;
|
|
23
35
|
}
|
|
24
36
|
|
|
25
37
|
export type ChainCreateOptions<TEntry> = ChainInitOptions<TEntry> & {
|
|
@@ -109,19 +121,21 @@ export class Chain<TEntry> {
|
|
|
109
121
|
const trx = new Atomic(this.store);
|
|
110
122
|
|
|
111
123
|
// Attempt to fit in current block
|
|
112
|
-
|
|
113
|
-
if (
|
|
114
|
-
apply(trx, tail, [entries$, tail.entries.length, 0,
|
|
115
|
-
entries = entries.slice(copied.length);
|
|
124
|
+
let next = this.takenBy(tail, entries, 0);
|
|
125
|
+
if (next > 0) {
|
|
126
|
+
apply(trx, tail, [entries$, tail.entries.length, 0, entries.slice(0, next)]);
|
|
116
127
|
}
|
|
117
128
|
|
|
118
|
-
while (entries.length
|
|
129
|
+
while (next < entries.length) {
|
|
119
130
|
const newTail = {
|
|
120
131
|
...(this.options?.createDataBlock?.() ?? { header: this.store.createBlockHeader(ChainDataBlockType) }),
|
|
121
|
-
entries:
|
|
132
|
+
entries: [] as TEntry[],
|
|
122
133
|
priorId: tail.header.id,
|
|
123
134
|
nextId: undefined,
|
|
124
135
|
} as ChainDataNode<TEntry>;
|
|
136
|
+
const taken = this.takenBy(newTail, entries, next);
|
|
137
|
+
newTail.entries = entries.slice(next, next + taken);
|
|
138
|
+
next += taken;
|
|
125
139
|
// Predecessor is the running `tail` (immediately preceding block), NOT the original
|
|
126
140
|
// `oldTail`: across a multi-block append each new block links to the one before it.
|
|
127
141
|
await this.options?.newBlock?.(newTail, tail);
|
|
@@ -139,6 +153,41 @@ export class Chain<TEntry> {
|
|
|
139
153
|
return { headerBlock, block: tail, index: tail.entries.length - 1 };
|
|
140
154
|
}
|
|
141
155
|
|
|
156
|
+
/**
|
|
157
|
+
* How many of `entries`, from `start`, `block` takes before it is full: up to
|
|
158
|
+
* {@link EntriesPerBlock} entries, and — when the chain has a byte target — no further entry once
|
|
159
|
+
* it holds one and the next would take it past the target. A block holding no entry always takes
|
|
160
|
+
* the first, so this is at least one for an empty block with entries left to place.
|
|
161
|
+
*
|
|
162
|
+
* The measure is the block's JSON as it stands, not counting what the `newBlock` hook adds to a
|
|
163
|
+
* new block afterwards (the log's `priorHash`) nor what a caller writes into an entry after the add
|
|
164
|
+
* (the log's `blockIds`) — both small against the gap between the target and the most a block may
|
|
165
|
+
* hold.
|
|
166
|
+
*/
|
|
167
|
+
private takenBy(block: ChainDataNode<TEntry>, entries: readonly TEntry[], start: number): number {
|
|
168
|
+
const room = Math.max(0, Math.min(EntriesPerBlock - block.entries.length, entries.length - start));
|
|
169
|
+
const target = this.options?.blockTargetBytes;
|
|
170
|
+
if (target === undefined || room === 0) {
|
|
171
|
+
return room;
|
|
172
|
+
}
|
|
173
|
+
// NOTE: serializes the whole tail block once per append, the same order of cost as the commit
|
|
174
|
+
// path's digest of every touched block. If appends to large logs ever show this in a profile,
|
|
175
|
+
// keep a running byte count for the tail instead of re-measuring it.
|
|
176
|
+
let bytes = jsonEncodedBytes(block);
|
|
177
|
+
let held = block.entries.length;
|
|
178
|
+
let taken = 0;
|
|
179
|
+
for (; taken < room; ++taken) {
|
|
180
|
+
// Each entry after the first adds its own JSON plus the separating comma.
|
|
181
|
+
const grown = bytes + jsonEncodedBytes(entries[start + taken]) + (held > 0 ? 1 : 0);
|
|
182
|
+
if (held > 0 && grown > target) {
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
bytes = grown;
|
|
186
|
+
++held;
|
|
187
|
+
}
|
|
188
|
+
return taken;
|
|
189
|
+
}
|
|
190
|
+
|
|
142
191
|
/** Updates the entry at the given path. */
|
|
143
192
|
updateAt(path: ChainPath<TEntry>, entry: TEntry) {
|
|
144
193
|
if (!pathValid(path)) {
|
|
@@ -6,7 +6,8 @@ import { Atomic } from "../transform/atomic.js";
|
|
|
6
6
|
import { Tracker } from "../transform/tracker.js";
|
|
7
7
|
import type { BasePins } from "../transform/base-pins.js";
|
|
8
8
|
import { CacheSource } from "../transform/cache-source.js";
|
|
9
|
-
import {
|
|
9
|
+
import { describeTouchedBlocks, baseRevsField } from "../transform/digest.js";
|
|
10
|
+
import { oversizedBlock } from "../network/block-size.js";
|
|
10
11
|
import type { BlockBaseRevs, BlockGets } from "../network/struct.js";
|
|
11
12
|
import { copyTransforms, isTransformsEmpty } from "../transform/helpers.js";
|
|
12
13
|
import { TransactorSource, answeredBlock, servedRevision } from "../transactor/transactor-source.js";
|
|
@@ -15,7 +16,7 @@ import type { WriteDurability } from "../network/struct.js";
|
|
|
15
16
|
import { mergeDurability, withBlockRevs } from "../network/durability.js";
|
|
16
17
|
import { highestStaleAt, isExpiryFailure } from "../network/stale-failure.js";
|
|
17
18
|
import { TransactionExpiredError } from "../network/transaction-expired-error.js";
|
|
18
|
-
import type { CollectionHeaderBlock, CollectionId, ICollection, SyncOptions, TornActionReason } from "./index.js";
|
|
19
|
+
import type { CollectionHeaderBlock, CollectionId, CommittedTail, CommittedTailAnswer, CommittedTailQuery, ICollection, SyncOptions, TornActionReason } from "./index.js";
|
|
19
20
|
import { CollectionHeaderVanishedError, SyncRetryExhaustedError, SyncRevisionStalledError, TornActionError } from "./struct.js";
|
|
20
21
|
import type { ActionContext } from "./action.js";
|
|
21
22
|
import { actionIdAt } from "./action.js";
|
|
@@ -102,6 +103,11 @@ type LogEnds = {
|
|
|
102
103
|
served: Array<[BlockId, IBlock, number]>;
|
|
103
104
|
};
|
|
104
105
|
|
|
106
|
+
/** One block's answer in a {@link Collection.readCommittedTails} round: the repo's entry, or why
|
|
107
|
+
* reading the block failed. */
|
|
108
|
+
type AttributedRead = { entry: GetBlockResult | undefined } | { error: unknown };
|
|
109
|
+
type AttributedReads = Map<BlockId, AttributedRead>;
|
|
110
|
+
|
|
105
111
|
/** Default base backoff (and historical fixed delay) between sync retries, in ms. */
|
|
106
112
|
const PendingRetryDelayMs = 100;
|
|
107
113
|
/** Default max consecutive no-progress stale-failure retries before {@link Collection.sync} gives up. */
|
|
@@ -376,11 +382,55 @@ export class Collection<TAction> implements ICollection<TAction> {
|
|
|
376
382
|
* Throws what a refresh's first read throws: `BlockUnavailableError` for a header or tail the
|
|
377
383
|
* storage layer could not retrieve, `BlockPossiblyStaleError` for one it could not confirm
|
|
378
384
|
* current. Neither is an absence. */
|
|
379
|
-
static async readCommittedTail(transactor: ITransactor, id: CollectionId, knownTailId?: BlockId): Promise<
|
|
385
|
+
static async readCommittedTail(transactor: ITransactor, id: CollectionId, knownTailId?: BlockId): Promise<CommittedTail | undefined> {
|
|
380
386
|
const ends = await Collection.readLogEnds(transactor, id, knownTailId);
|
|
381
387
|
const tailId = ends?.header.tailId;
|
|
382
|
-
|
|
383
|
-
|
|
388
|
+
return tailId === undefined ? undefined : Collection.committedTailOf(tailId, ends?.tail);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** {@link readCommittedTail} for several collections at once, answered per collection: what
|
|
392
|
+
* the single read would have resolved, as `tail`, or what it would have thrown, as `error`. A
|
|
393
|
+
* collection named twice is read once.
|
|
394
|
+
*
|
|
395
|
+
* One request names every header and every known tail id. A collection whose header names a
|
|
396
|
+
* different tail than its known one costs a second request, shared by every such collection. So
|
|
397
|
+
* a poll of many collections whose logs stayed on their blocks is one request, however many it
|
|
398
|
+
* names; the per-block judgement is {@link readLogEnds}'s, through the same helpers.
|
|
399
|
+
*
|
|
400
|
+
* A request that throws is not every collection's failure: `NetworkTransactor.get` fails as a
|
|
401
|
+
* whole when any block in it gets no answer, so {@link readAttributed} then reads each block
|
|
402
|
+
* alone and only the collections whose blocks failed report an error. That fallback costs what
|
|
403
|
+
* reading each collection separately costs, and only when a request fails. */
|
|
404
|
+
static async readCommittedTails(transactor: ITransactor, queries: ReadonlyArray<CommittedTailQuery>): Promise<Map<CollectionId, CommittedTailAnswer>> {
|
|
405
|
+
const knownTails = new Map(queries.map(query => [query.id, query.knownTailId]));
|
|
406
|
+
const ends = await Collection.readAttributed(transactor,
|
|
407
|
+
[...knownTails].flatMap(([id, knownTailId]) => knownTailId === undefined ? [id] : [id, knownTailId]));
|
|
408
|
+
const answers = new Map<CollectionId, CommittedTailAnswer>();
|
|
409
|
+
/** Collections whose header names a tail other than the known one, with the tail it names. */
|
|
410
|
+
const moved = new Map<CollectionId, BlockId>();
|
|
411
|
+
for (const [id, knownTailId] of knownTails) {
|
|
412
|
+
try {
|
|
413
|
+
const tailId = Collection.answeredHeader(id, Collection.entryIn(ends, id))?.header.tailId;
|
|
414
|
+
if (tailId === undefined || tailId === knownTailId) {
|
|
415
|
+
answers.set(id, { tail: Collection.judgedTail(tailId, ends) });
|
|
416
|
+
} else {
|
|
417
|
+
moved.set(id, tailId);
|
|
418
|
+
}
|
|
419
|
+
} catch (error) {
|
|
420
|
+
answers.set(id, { error });
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (moved.size > 0) {
|
|
424
|
+
const tails = await Collection.readAttributed(transactor, [...new Set(moved.values())]);
|
|
425
|
+
for (const [id, tailId] of moved) {
|
|
426
|
+
try {
|
|
427
|
+
answers.set(id, { tail: Collection.judgedTail(tailId, tails) });
|
|
428
|
+
} catch (error) {
|
|
429
|
+
answers.set(id, { error });
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return answers;
|
|
384
434
|
}
|
|
385
435
|
|
|
386
436
|
/** The per-instance read wiring every open path needs, plus the collection's log ends — the
|
|
@@ -1991,7 +2041,15 @@ export class Collection<TAction> implements ICollection<TAction> {
|
|
|
1991
2041
|
// iteration), so a sync that loses N races pays N full hashing passes over its touched
|
|
1992
2042
|
// blocks. Unmeasured and cheap relative to the round trips it is retrying; if a
|
|
1993
2043
|
// high-contention sync ever shows digest hashing in a profile, memoize per (id, staged ops).
|
|
1994
|
-
const
|
|
2044
|
+
const touched = await describeTouchedBlocks(tracker, tracker.transformedBlockIds());
|
|
2045
|
+
// No block may be left larger than one message can carry. Refused here, before anything is
|
|
2046
|
+
// sent, as the transaction limit is at the pend, and judged apart from the declaration, which
|
|
2047
|
+
// never throws: only the blocks it could materialize are measured (see describeTouchedBlocks).
|
|
2048
|
+
// It escapes `sync` unretried — a later attempt would leave the same block — and nothing on
|
|
2049
|
+
// this instance has been touched, since the attempt ran on the snapshot tracker.
|
|
2050
|
+
const tooLarge = oversizedBlock(touched.bytes);
|
|
2051
|
+
if (tooLarge) throw tooLarge;
|
|
2052
|
+
const blockDigests = touched.digests;
|
|
1995
2053
|
// The base each update-only block's operations were computed against, for the pend (see
|
|
1996
2054
|
// `PendRequest.baseRevs`): the tracker's pinned revisions, which `restageIfBasesMoved` at the
|
|
1997
2055
|
// top of this iteration has just re-judged, so a moved base was re-staged before it is named.
|
|
@@ -2306,15 +2364,12 @@ export class Collection<TAction> implements ICollection<TAction> {
|
|
|
2306
2364
|
blockIds: knownTailId === undefined ? [id] : [id, knownTailId],
|
|
2307
2365
|
...Collection.tailFloorField(knownTailId, tailFloor),
|
|
2308
2366
|
});
|
|
2309
|
-
const
|
|
2310
|
-
if (
|
|
2367
|
+
const answered = Collection.answeredHeader(id, results?.[id]);
|
|
2368
|
+
if (answered === undefined) {
|
|
2311
2369
|
return undefined;
|
|
2312
2370
|
}
|
|
2313
|
-
const header =
|
|
2314
|
-
|
|
2315
|
-
return undefined;
|
|
2316
|
-
}
|
|
2317
|
-
const served: LogEnds['served'] = [[id, header, servedRevision(headerEntry)]];
|
|
2371
|
+
const { header } = answered;
|
|
2372
|
+
const served: LogEnds['served'] = [[id, header, answered.servedRev]];
|
|
2318
2373
|
const tailId = header.tailId;
|
|
2319
2374
|
if (tailId === undefined) {
|
|
2320
2375
|
return { header, served };
|
|
@@ -2328,6 +2383,73 @@ export class Collection<TAction> implements ICollection<TAction> {
|
|
|
2328
2383
|
return { header, tail, served };
|
|
2329
2384
|
}
|
|
2330
2385
|
|
|
2386
|
+
/** The header a repo answered for collection `id`, judged as an unpinned read, with the
|
|
2387
|
+
* revision it was served at. `undefined` when it is authoritatively absent — no entry, or one
|
|
2388
|
+
* with no block; a throw when the repo could not retrieve it or could not confirm it current
|
|
2389
|
+
* ({@link answeredBlock}). */
|
|
2390
|
+
private static answeredHeader(id: CollectionId, entry: GetBlockResult | undefined): { header: CollectionHeaderBlock; servedRev: number } | undefined {
|
|
2391
|
+
if (entry === undefined) {
|
|
2392
|
+
return undefined;
|
|
2393
|
+
}
|
|
2394
|
+
const header = answeredBlock(id, entry, undefined) as CollectionHeaderBlock | undefined;
|
|
2395
|
+
return header ? { header, servedRev: servedRevision(entry) } : undefined;
|
|
2396
|
+
}
|
|
2397
|
+
|
|
2398
|
+
/** The committed tail a header naming `tailId` describes, given the repo's answer for that
|
|
2399
|
+
* block: `undefined` when the block has no committed revision (nothing committed yet). */
|
|
2400
|
+
private static committedTailOf(tailId: BlockId, tail: GetBlockResult | undefined): CommittedTail | undefined {
|
|
2401
|
+
const rev = tail?.state.latest?.rev;
|
|
2402
|
+
return rev === undefined ? undefined : { tailId, rev };
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
/** The committed tail `tailId` names, read out of `reads` and checked as {@link checkedLogTail}
|
|
2406
|
+
* describes; `undefined` for no tail id. Throws the block's failed read, or a doubted answer. */
|
|
2407
|
+
private static judgedTail(tailId: BlockId | undefined, reads: AttributedReads): CommittedTail | undefined {
|
|
2408
|
+
return tailId === undefined
|
|
2409
|
+
? undefined
|
|
2410
|
+
: Collection.committedTailOf(tailId, Collection.checkedLogTail(tailId, Collection.entryIn(reads, tailId)));
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
/** The repo's entry for `id` in `reads`, or the error reading it failed with, thrown. */
|
|
2414
|
+
private static entryIn(reads: AttributedReads, id: BlockId): GetBlockResult | undefined {
|
|
2415
|
+
const read = reads.get(id);
|
|
2416
|
+
if (read !== undefined && 'error' in read) {
|
|
2417
|
+
throw read.error;
|
|
2418
|
+
}
|
|
2419
|
+
return read?.entry;
|
|
2420
|
+
}
|
|
2421
|
+
|
|
2422
|
+
/** Unpinned reads of `blockIds` in ONE request, each block's answer kept apart. When that
|
|
2423
|
+
* request throws, each block is read in a request of its own — concurrently — so the failure
|
|
2424
|
+
* lands on the blocks it belongs to and every other block still answers.
|
|
2425
|
+
*
|
|
2426
|
+
* NOTE: the fallback is unbounded — two gets per collection at once, only after a failed
|
|
2427
|
+
* request. Fine for the tens of tables a host watches; if callers ever name hundreds, read the
|
|
2428
|
+
* fallback in bounded batches. */
|
|
2429
|
+
private static async readAttributed(transactor: ITransactor, blockIds: BlockId[]): Promise<AttributedReads> {
|
|
2430
|
+
if (blockIds.length === 0) {
|
|
2431
|
+
return new Map();
|
|
2432
|
+
}
|
|
2433
|
+
try {
|
|
2434
|
+
const results = await transactor.get({ blockIds });
|
|
2435
|
+
return new Map(blockIds.map(id => [id, { entry: results?.[id] }]));
|
|
2436
|
+
} catch (error) {
|
|
2437
|
+
if (blockIds.length === 1) {
|
|
2438
|
+
return new Map([[blockIds[0]!, { error }]]);
|
|
2439
|
+
}
|
|
2440
|
+
log('collection:tails-read-split blocks=%d error=%o', blockIds.length, error);
|
|
2441
|
+
return new Map(await Promise.all(blockIds.map(async id => [id, await Collection.readAlone(transactor, id)] as const)));
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2445
|
+
private static async readAlone(transactor: ITransactor, id: BlockId): Promise<AttributedRead> {
|
|
2446
|
+
try {
|
|
2447
|
+
return { entry: (await transactor.get({ blockIds: [id] }))?.[id] };
|
|
2448
|
+
} catch (error) {
|
|
2449
|
+
return { error };
|
|
2450
|
+
}
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2331
2453
|
/** An unpinned read of the log tail block, checked as {@link checkedLogTail} describes, carrying
|
|
2332
2454
|
* `tailFloor` when the caller has one (see {@link readLogEnds}). */
|
|
2333
2455
|
private static async readLogTail(transactor: ITransactor, tailId: BlockId, tailFloor?: number): Promise<GetBlockResult | undefined> {
|
package/src/collection/struct.ts
CHANGED
|
@@ -6,6 +6,24 @@ export type CollectionId = BlockId;
|
|
|
6
6
|
|
|
7
7
|
export type CollectionHeaderBlock = IBlock & Partial<IChainHeader>;
|
|
8
8
|
|
|
9
|
+
/** A collection's committed tail: the log tail block its header names, and that block's latest
|
|
10
|
+
* committed revision, which is the collection's (every commit touches the log tail). */
|
|
11
|
+
export type CommittedTail = { tailId: BlockId; rev: number };
|
|
12
|
+
|
|
13
|
+
/** One collection a batched committed-tail read names (`Collection.readCommittedTails`). */
|
|
14
|
+
export type CommittedTailQuery = {
|
|
15
|
+
readonly id: CollectionId;
|
|
16
|
+
/** The tail block the caller's previous read found. While the header still names it, the read
|
|
17
|
+
* needs no second request for this collection. */
|
|
18
|
+
readonly knownTailId?: BlockId;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** One collection's answer to a batched committed-tail read: its tail (`undefined` when it has
|
|
22
|
+
* never committed), or why reading it failed — what the single-collection read would have thrown. */
|
|
23
|
+
export type CommittedTailAnswer =
|
|
24
|
+
| { readonly tail: CommittedTail | undefined }
|
|
25
|
+
| { readonly error: unknown };
|
|
26
|
+
|
|
9
27
|
/** Bounds the retry loop inside {@link ICollection.sync} / {@link ICollection.updateAndSync}
|
|
10
28
|
* so a transactor that keeps rejecting the sync can no longer spin the collection latch forever.
|
|
11
29
|
* All fields are optional; unset fields fall back to conservative defaults. */
|
|
@@ -3,6 +3,7 @@ import type { ITransactor, BlockId, BlockStore, IBlock, ActionId, WriteDurabilit
|
|
|
3
3
|
import { BTree, type Path, type KeyRange } from "../../btree/index.js";
|
|
4
4
|
import { CollectionTrunk } from "./collection-trunk.js";
|
|
5
5
|
import { structuralEquals } from "../../utility/structural-equals.js";
|
|
6
|
+
import { BLOCK_TARGET_BYTES } from "../../network/block-size.js";
|
|
6
7
|
import {
|
|
7
8
|
TreeHeaderBlockType, TreeDeleteGuardKindError, TreeEntryChangedError, TreeKeyTakenError,
|
|
8
9
|
TreeRangeTakenError, type TreeEntryGuard, type TreeReplaceAction,
|
|
@@ -184,6 +185,7 @@ export class Tree<TKey, TEntry> implements TreeReadView<TKey, TEntry> {
|
|
|
184
185
|
keyFromEntry,
|
|
185
186
|
compare,
|
|
186
187
|
nodeCapacity, // keep the write btree's fan-out in lock-step with the read btree
|
|
188
|
+
BLOCK_TARGET_BYTES, // and its node size rule: every node is a block that must stay copyable
|
|
187
189
|
);
|
|
188
190
|
for (const [key, entry, guard] of actions) {
|
|
189
191
|
// NOTE: an element is a DELETE when its entry slot is falsy, not when it is
|
|
@@ -237,7 +239,7 @@ export class Tree<TKey, TEntry> implements TreeReadView<TKey, TEntry> {
|
|
|
237
239
|
held.btree = BTree.create<TKey, TEntry>(store, (_s, r) => {
|
|
238
240
|
rootId = r;
|
|
239
241
|
return new CollectionTrunk(store, hid);
|
|
240
|
-
}, keyFromEntry, compare, nodeCapacity);
|
|
242
|
+
}, keyFromEntry, compare, nodeCapacity, undefined, BLOCK_TARGET_BYTES);
|
|
241
243
|
return {
|
|
242
244
|
header: store.createBlockHeader(TreeHeaderBlockType, hid),
|
|
243
245
|
rootId: rootId!,
|
|
@@ -258,7 +260,7 @@ export class Tree<TKey, TEntry> implements TreeReadView<TKey, TEntry> {
|
|
|
258
260
|
nodeCapacity: number | undefined,
|
|
259
261
|
): Tree<TKey, TEntry> {
|
|
260
262
|
held.btree = held.btree
|
|
261
|
-
?? new BTree<TKey, TEntry>(collection.tracker, new CollectionTrunk(collection.tracker, collection.id), keyFromEntry, compare, nodeCapacity);
|
|
263
|
+
?? new BTree<TKey, TEntry>(collection.tracker, new CollectionTrunk(collection.tracker, collection.id), keyFromEntry, compare, nodeCapacity, BLOCK_TARGET_BYTES);
|
|
262
264
|
return new Tree<TKey, TEntry>(collection, held.btree, keyFromEntry, compare);
|
|
263
265
|
}
|
|
264
266
|
|
package/src/log/log.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { LogEntry, ActionEntry, DisputeResolutionProof, InvalidationEntry,
|
|
|
7
7
|
import { LogDataBlockType, LogHeaderBlockType } from "./struct.js";
|
|
8
8
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
9
9
|
import type { GetFromResult } from './struct.js';
|
|
10
|
+
import { BLOCK_TARGET_BYTES } from '../network/block-size.js';
|
|
10
11
|
|
|
11
12
|
export type LogBlock<TAction> = ChainDataNode<LogEntry<TAction>>
|
|
12
13
|
& {
|
|
@@ -284,10 +285,15 @@ export class Log<TAction> {
|
|
|
284
285
|
return pendings;
|
|
285
286
|
}
|
|
286
287
|
|
|
287
|
-
/** Chain options used by every log: block type factories
|
|
288
|
+
/** Chain options used by every log: block type factories, the priorHash `newBlock` hook, and the
|
|
289
|
+
* byte target a log block rolls over at. Every entry carries its action's data, so a count alone
|
|
290
|
+
* would let a log of large entries build a block too large to copy between machines; with
|
|
291
|
+
* {@link BLOCK_TARGET_BYTES} a log block either holds one entry (and is then no larger than the
|
|
292
|
+
* transaction that wrote it) or stays near the target.
|
|
288
293
|
* Exposed (not private) so tests can drive a raw {@link Chain} through the exact same hook. */
|
|
289
294
|
static getChainOptions<TAction>(store: BlockStore<IBlock>, newDataBlockId?: () => BlockId) {
|
|
290
295
|
return {
|
|
296
|
+
blockTargetBytes: BLOCK_TARGET_BYTES,
|
|
291
297
|
createDataBlock: () => ({ header: store.createBlockHeader(LogDataBlockType, newDataBlockId?.()) }),
|
|
292
298
|
createHeaderBlock: (id?: BlockId) => ({ header: store.createBlockHeader(LogHeaderBlockType, id) }),
|
|
293
299
|
newBlock: async (newTail: LogBlock<TAction>, oldTail: LogBlock<TAction> | undefined) => {
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { BlockId } from "../blocks/structs.js";
|
|
2
|
+
import { MAX_TRANSACTION_BYTES } from "./transaction-size.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The most bytes one block may encode to, measured by `jsonEncodedBytes` (the UTF-8 length of its
|
|
6
|
+
* `JSON.stringify`): 7.5 MiB, the same as {@link MAX_TRANSACTION_BYTES}.
|
|
7
|
+
*
|
|
8
|
+
* Every path that copies a whole block to another machine sends it in one message under db-p2p's
|
|
9
|
+
* `MAX_BLOCK_MESSAGE_BYTES` frame cap — a replication push, a sync archive answer, a remote `get` reply —
|
|
10
|
+
* so a block larger than one frame can never be replicated, repaired or read remotely. Two things pin the
|
|
11
|
+
* value:
|
|
12
|
+
*
|
|
13
|
+
* - **It fits the frame with its wrapping.** A block at this size, plus what it travels in (a `get`
|
|
14
|
+
* reply entry's state, an archive revision entry, a push's metadata and commit proof), fits the
|
|
15
|
+
* 8 MiB frame inside the same envelope reserve a transaction gets (`TRANSACTION_ENVELOPE_RESERVE_BYTES`).
|
|
16
|
+
* - **No single transaction can exceed it on its own.** A block holding one entry or child is never
|
|
17
|
+
* larger than the transaction that wrote it, since that entry is inside the transaction's pend (as the
|
|
18
|
+
* new block's insert, or as the update operation that appends it), and the pend is held to
|
|
19
|
+
* {@link MAX_TRANSACTION_BYTES}.
|
|
20
|
+
*
|
|
21
|
+
* Structures that hold many entries per block (the log, tree nodes) keep each block that holds more than
|
|
22
|
+
* one entry near {@link BLOCK_TARGET_BYTES}, far under this limit; this is the bound itself.
|
|
23
|
+
*/
|
|
24
|
+
export const MAX_BLOCK_BYTES = MAX_TRANSACTION_BYTES;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The size past which a block holding more than one entry starts a new block: 1 MiB, by the same
|
|
28
|
+
* `jsonEncodedBytes` measure as {@link MAX_BLOCK_BYTES}. The log rolls over to a new tail block at it
|
|
29
|
+
* (`ChainInitOptions.blockTargetBytes`); an entry larger than it sits alone in its own block.
|
|
30
|
+
*
|
|
31
|
+
* Why 1 MiB:
|
|
32
|
+
*
|
|
33
|
+
* - One block's copy stays far under the 8 MiB frame, so a remote `get` reply carrying a few blocks
|
|
34
|
+
* still fits one message in the ordinary case, and a single replication push stays short on a slow
|
|
35
|
+
* relayed link.
|
|
36
|
+
* - It is 32 entries of 32 KiB, so a log of ordinary small entries keeps rolling over by count
|
|
37
|
+
* (`EntriesPerBlock`) exactly as it did before the size rule.
|
|
38
|
+
*
|
|
39
|
+
* The cost: a structure of large entries spreads over more blocks, so walking it reads more blocks.
|
|
40
|
+
*/
|
|
41
|
+
export const BLOCK_TARGET_BYTES = 1024 * 1024;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A write that would leave a block larger than {@link MAX_BLOCK_BYTES}: once committed, the block could
|
|
45
|
+
* never be copied to another machine in one message. Raised by the writer before anything is sent
|
|
46
|
+
* ({@link oversizedBlock}), so nothing was reserved and nothing needs cancelling. Not a `StaleFailure`
|
|
47
|
+
* and never retried — the same write leaves the same block on every attempt. The structures that hold
|
|
48
|
+
* many entries per block split by size (the log rolls over, a tree node divides), so this names a block
|
|
49
|
+
* that outgrew the limit some other way; the change has to be written in a form that does not.
|
|
50
|
+
*/
|
|
51
|
+
export class BlockTooLargeError extends Error {
|
|
52
|
+
constructor(
|
|
53
|
+
readonly blockId: BlockId,
|
|
54
|
+
/** What the block's post-write content encodes to, by `jsonEncodedBytes`. */
|
|
55
|
+
readonly bytes: number,
|
|
56
|
+
/** The limit it exceeded: {@link MAX_BLOCK_BYTES}. */
|
|
57
|
+
readonly limit: number = MAX_BLOCK_BYTES,
|
|
58
|
+
) {
|
|
59
|
+
super(`block ${blockId} would encode to ${bytes} bytes once written, over the per-block limit MAX_BLOCK_BYTES (${limit} bytes); a block that large could never be copied to another machine`);
|
|
60
|
+
this.name = 'BlockTooLargeError';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The refusal for the first block in `bytes` — per block, what a write would leave its content encoding
|
|
66
|
+
* to — that is over {@link MAX_BLOCK_BYTES}, or `undefined` when every one fits. The one rule both write
|
|
67
|
+
* paths apply before anything is sent. It judges only the blocks it is given: a writer measures what it
|
|
68
|
+
* can materialize locally (`describeTouchedBlocks`), which leaves out a deleted block and an updated
|
|
69
|
+
* block whose base it cannot materialize.
|
|
70
|
+
*/
|
|
71
|
+
// NOTE: accepted tradeoff (no backwards compatibility yet, AGENTS.md) — a block a build without the split
|
|
72
|
+
// rules already grew past the limit refuses every write that leaves it over, including ones that do not
|
|
73
|
+
// grow it: a log tail's roll-over (which rewrites its `nextId`) wedges every later write to that
|
|
74
|
+
// collection, and a delete from an oversized leaf is refused. If a deployment must keep writing over such
|
|
75
|
+
// data, refuse only a block that grows past its base's size here, rather than every block over the limit.
|
|
76
|
+
export function oversizedBlock(bytes: Readonly<Record<BlockId, number>>): BlockTooLargeError | undefined {
|
|
77
|
+
for (const [blockId, size] of Object.entries(bytes)) {
|
|
78
|
+
if (size > MAX_BLOCK_BYTES) return new BlockTooLargeError(blockId, size);
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
package/src/network/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./i-key-network.js";
|
|
2
2
|
export * from "./i-peer-network.js";
|
|
3
|
+
export * from "./block-size.js";
|
|
3
4
|
export * from "./durability.js";
|
|
4
5
|
export * from "./i-repo.js";
|
|
5
6
|
export * from "./lineage.js";
|
|
@@ -8,4 +9,5 @@ export * from "./routing-key.js";
|
|
|
8
9
|
export * from "./stale-failure.js";
|
|
9
10
|
export * from "./struct.js";
|
|
10
11
|
export * from "./transaction-expired-error.js";
|
|
12
|
+
export * from "./transaction-size.js";
|
|
11
13
|
export * from "./types.js";
|