@frostpillar/frostpillar-btree 0.2.4 → 0.2.5

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-JA.md CHANGED
@@ -280,7 +280,7 @@ const last = tree.popLast();
280
280
  // { entryId: ..., key: 20, value: 'twenty' } または null(ツリーが空の場合)
281
281
  ```
282
282
 
283
- **`clear()`** -- 全エントリを削除し、ツリーを空の状態に O(1) でリセットします。内部シーケンスカウンタもリセットされるため、新しい `EntryId` はゼロから始まります。`clear()` 前に取得した `EntryId` は無効になります。
283
+ **`clear()`** -- 全エントリを削除し、ツリーを空の状態に O(1) でリセットします。内部シーケンスカウンタはリセット**されない**ため、`EntryId` はインスタンスの存続期間中単調増加し、再利用されません。
284
284
 
285
285
  ```ts
286
286
  tree.clear();
@@ -429,7 +429,7 @@ tree.assertInvariants(); // 不正な場合はスロー
429
429
 
430
430
  #### クローンとシリアライズ
431
431
 
432
- **`clone()`** -- 構造的に独立したディープコピーを作成します。
432
+ **`clone()`** -- 構造的に独立したコピーを作成します。ツリー構造(ノード、リンク、エントリID)は完全に独立しますが、格納されたキーと値の参照は元のツリーと共有されます。
433
433
 
434
434
  ```ts
435
435
  const copy = tree.clone();
@@ -501,7 +501,9 @@ const tree = new InMemoryBTree<number, string>({
501
501
  - **`getLogEntriesSince(version)`** -- 指定バージョン以降のすべてのミューテーションを返し、各インスタンスが最新状態へキャッチアップできるようにします。
502
502
  - **`append(expectedVersion, mutations)`** -- バージョンが一致する場合にミューテーションをアトミックに追加します(compare-and-swap)。`{ applied, version }` を返します。
503
503
 
504
- ストアの実装は自由です。インメモリ配列、データベーステーブル、Redis stream など何でも使えます。以下はインメモリの参考実装です。
504
+ `getLogEntriesSince` で過去のミューテーションを返すストアは**マルチインスタンス・キャッチアップ**を実現します。各インスタンスが未受信のミューテーションをリプレイし、同一状態に収束します。ミューテーションをリプレイしないストア(空の `mutations` 配列と更新された `version` を返す)もサポートされています。この場合、各インスタンスは自身のローカル書き込みとバージョン進行のみを認識します。一貫性要件に応じてリプレイ戦略を選択してください。
505
+
506
+ ストアの実装は自由です。インメモリ配列、データベーステーブル、Redis stream など何でも使えます。以下はリプレイ対応のインメモリ参考実装です。
505
507
 
506
508
  **Node.js / TypeScript:**
507
509
 
@@ -791,7 +793,7 @@ try {
791
793
  | `size` | `() => number` | エントリ数を返す。 |
792
794
  | `getStats` | `() => BTreeStats` | 構造統計を返す。 |
793
795
  | `assertInvariants` | `() => void` | B+ tree の構造的な整合性を検証する。不正な場合はスローする。 |
794
- | `clone` | `() => InMemoryBTree<TKey, TValue>` | 構造的に独立したディープコピーを返す。 |
796
+ | `clone` | `() => InMemoryBTree<TKey, TValue>` | 構造的に独立したコピーを返す(キー・値参照は共有)。 |
795
797
  | `toJSON` | `() => BTreeJSON<TKey, TValue>` | バージョン付き JSON 互換ペイロードにシリアライズする。 |
796
798
  | `fromJSON` (静的) | `(json, compareKeys) => InMemoryBTree<TKey, TValue>` | `toJSON` ペイロードからツリーを再構築する。 |
797
799
 
package/README.md CHANGED
@@ -280,7 +280,7 @@ const last = tree.popLast();
280
280
  // { entryId: ..., key: 20, value: 'twenty' } or null if empty
281
281
  ```
282
282
 
283
- **`clear()`** -- remove all entries and reset the tree to its empty state in O(1). The internal sequence counter is also reset, so new `EntryId` values start from zero. Any `EntryId` obtained before `clear()` becomes invalid.
283
+ **`clear()`** -- remove all entries and reset the tree to its empty state in O(1). The internal sequence counter is **not** reset, so `EntryId` values continue to increase monotonically and are never reused within the lifetime of the instance.
284
284
 
285
285
  ```ts
286
286
  tree.clear();
@@ -429,7 +429,7 @@ tree.assertInvariants(); // throws if invalid
429
429
 
430
430
  #### Clone and Serialization
431
431
 
432
- **`clone()`** -- create a structurally independent deep copy:
432
+ **`clone()`** -- create a structurally independent copy. The tree structure (nodes, links, entry IDs) is fully independent, but stored key and value references are shared with the source tree:
433
433
 
434
434
  ```ts
435
435
  const copy = tree.clone();
@@ -501,7 +501,9 @@ const tree = new InMemoryBTree<number, string>({
501
501
  - **`getLogEntriesSince(version)`** -- returns all mutations since a given version, so each instance can catch up.
502
502
  - **`append(expectedVersion, mutations)`** -- atomically appends mutations if the version matches (compare-and-swap). Returns `{ applied, version }`.
503
503
 
504
- The store can be backed by anything: an in-memory array, a database table, a Redis stream, etc. Below is a complete in-memory reference implementation.
504
+ A store that returns historical mutations from `getLogEntriesSince` enables **multi-instance catch-up**: any instance can replay missed mutations to converge on the same state. Stores that do not replay mutations (returning an empty `mutations` array with an updated `version`) are also supported — in this mode, each instance only sees its own local writes and version advancement. Choose the replay strategy that matches your consistency requirements.
505
+
506
+ The store can be backed by anything: an in-memory array, a database table, a Redis stream, etc. Below is a complete in-memory reference implementation with replay support.
505
507
 
506
508
  **Node.js / TypeScript:**
507
509
 
@@ -791,7 +793,7 @@ try {
791
793
  | `size` | `() => number` | Return the total number of entries. |
792
794
  | `getStats` | `() => BTreeStats` | Return structural statistics. |
793
795
  | `assertInvariants` | `() => void` | Assert B+ tree structural integrity. Throws if invalid. |
794
- | `clone` | `() => InMemoryBTree<TKey, TValue>` | Return a structurally independent deep copy. |
796
+ | `clone` | `() => InMemoryBTree<TKey, TValue>` | Return a structurally independent copy (shared key/value refs). |
795
797
  | `toJSON` | `() => BTreeJSON<TKey, TValue>` | Serialize to a versioned JSON-safe payload. |
796
798
  | `fromJSON` (static) | `(json, compareKeys) => InMemoryBTree<TKey, TValue>` | Reconstruct a tree from a `toJSON` payload. |
797
799
 
@@ -10,10 +10,6 @@ export interface BTreeJSON<TKey, TValue> {
10
10
  };
11
11
  entries: [TKey, TValue][];
12
12
  }
13
- export declare const collectEntryPairs: <TKey, TValue>(state: BTreeState<TKey, TValue>) => {
14
- key: TKey;
15
- value: TValue;
16
- }[];
17
13
  export declare const buildConfigFromState: <TKey, TValue>(state: BTreeState<TKey, TValue>) => InMemoryBTreeConfig<TKey>;
18
14
  export declare const serializeToJSON: <TKey, TValue>(state: BTreeState<TKey, TValue>) => BTreeJSON<TKey, TValue>;
19
15
  export declare const validateBTreeJSON: <TKey, TValue>(json: BTreeJSON<TKey, TValue>) => void;
@@ -1781,23 +1781,13 @@ var InMemoryBTree = class _InMemoryBTree {
1781
1781
  }
1782
1782
  }
1783
1783
  *keys() {
1784
- let leaf = this.state.leftmostLeaf;
1785
- while (leaf !== null) {
1786
- const count = leafEntryCount(leaf);
1787
- for (let i = 0; i < count; i += 1) {
1788
- yield leafEntryAt(leaf, i).key;
1789
- }
1790
- leaf = leaf.next;
1784
+ for (const entry of this.entries()) {
1785
+ yield entry.key;
1791
1786
  }
1792
1787
  }
1793
1788
  *values() {
1794
- let leaf = this.state.leftmostLeaf;
1795
- while (leaf !== null) {
1796
- const count = leafEntryCount(leaf);
1797
- for (let i = 0; i < count; i += 1) {
1798
- yield leafEntryAt(leaf, i).value;
1799
- }
1800
- leaf = leaf.next;
1789
+ for (const entry of this.entries()) {
1790
+ yield entry.value;
1801
1791
  }
1802
1792
  }
1803
1793
  [Symbol.iterator]() {
@@ -18,6 +18,7 @@ export type MutationResult<TKey, TValue, TMutation extends BTreeMutation<TKey, T
18
18
  } ? BTreeEntry<TKey, TValue> | null : never;
19
19
  export type AnyMutationResult<TKey, TValue> = EntryId | BTreeEntry<TKey, TValue> | null;
20
20
  export declare const assertNeverMutation: (mutation: never) => never;
21
+ export declare const validateMutationBatch: <TKey, TValue>(mutations: BTreeMutation<TKey, TValue>[], expectedConfigFingerprint?: string) => void;
21
22
  export declare const normalizeMaxRetries: (value: number | undefined) => number;
22
23
  export declare const normalizeMaxSyncMutationsPerBatch: (value: number | undefined) => number;
23
24
  export declare const normalizeReadMode: (value: ReadMode | undefined) => ReadMode;
package/dist/core.cjs CHANGED
@@ -1802,23 +1802,13 @@ var InMemoryBTree = class _InMemoryBTree {
1802
1802
  }
1803
1803
  }
1804
1804
  *keys() {
1805
- let leaf = this.state.leftmostLeaf;
1806
- while (leaf !== null) {
1807
- const count = leafEntryCount(leaf);
1808
- for (let i = 0; i < count; i += 1) {
1809
- yield leafEntryAt(leaf, i).key;
1810
- }
1811
- leaf = leaf.next;
1805
+ for (const entry of this.entries()) {
1806
+ yield entry.key;
1812
1807
  }
1813
1808
  }
1814
1809
  *values() {
1815
- let leaf = this.state.leftmostLeaf;
1816
- while (leaf !== null) {
1817
- const count = leafEntryCount(leaf);
1818
- for (let i = 0; i < count; i += 1) {
1819
- yield leafEntryAt(leaf, i).value;
1820
- }
1821
- leaf = leaf.next;
1810
+ for (const entry of this.entries()) {
1811
+ yield entry.value;
1822
1812
  }
1823
1813
  }
1824
1814
  [Symbol.iterator]() {
package/dist/core.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  BTreeInvariantError,
3
3
  BTreeValidationError,
4
4
  InMemoryBTree
5
- } from "./chunk-ZA3EQNDI.js";
5
+ } from "./chunk-CZFRT2NN.js";
6
6
  export {
7
7
  BTreeInvariantError,
8
8
  BTreeValidationError,
@@ -1 +1 @@
1
- "use strict";var FrostpillarBTreeCore=(()=>{var U=Object.defineProperty;var Ye=Object.getOwnPropertyDescriptor;var Qe=Object.getOwnPropertyNames;var Ze=Object.prototype.hasOwnProperty;var je=(e,t)=>{for(var n in t)U(e,n,{get:t[n],enumerable:!0})},et=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of Qe(t))!Ze.call(e,l)&&l!==n&&U(e,l,{get:()=>t[l],enumerable:!(r=Ye(t,l))||r.enumerable});return e};var tt=e=>et(U({},"__esModule",{value:!0}),e);var gt={};je(gt,{InMemoryBTree:()=>te});var f=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var z=64,$=64,q=3,F=16384,C=0,nt=1,re=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new f("Invalid duplicateKeys option.");return e},p=e=>e.kind===C,N=(e,t)=>{if(e.kind===C){if(e.entryOffset>=e.entries.length)return!1;let n=e.entries[e.entryOffset];return t.key=n.key,t.sequence=n.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},M=(e,t,n)=>{if(e===void 0)return n;if(!Number.isInteger(e)||e<q||e>F)throw new f(`${t}: integer ${q}\u2013${F} required.`);return e},I=(e,t)=>({kind:C,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),k=(e,t)=>{let n=[],r={kind:nt,children:e,keys:n,childOffset:0,parent:t,indexInParent:0};for(let l=0;l<e.length;l+=1){let o=e[l];o.parent=r,o.indexInParent=l;let i={key:void 0,sequence:0};if(!N(o,i))throw new u("branch child has no min key");n.push(i)}return r},y=e=>e.entries.length-e.entryOffset,T=(e,t)=>e.entries[e.entryOffset+t],D=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},le=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},oe=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},Y=(e,t)=>{let n=e.entries.length-e.entryOffset,r=e.entryOffset+t;t<n-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},ie=(e,t,n)=>{let r=e.entryOffset+t;e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1?(e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=n):e.entries.splice(r,0,n)},ae=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},ne=1,R=e=>{if(e.childOffset>0){let t=e.childOffset<=ne?0:ne;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let n=t;n<e.children.length;n+=1)e.children[n].indexInParent=n}},O=e=>e.children.length-e.childOffset,ue=(e,t,n,r)=>{let l=e.childOffset+t,o=e.children.length-e.childOffset;if(e.childOffset>0&&t<o>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,l),e.keys.copyWithin(e.childOffset-1,e.childOffset,l),e.childOffset-=1,e.children[l-1]=n,e.keys[l-1]=r;for(let i=e.childOffset;i<l;i+=1)e.children[i].indexInParent=i;n.indexInParent=l-1}else{e.children.splice(l,0,n),e.keys.splice(l,0,r);for(let i=l;i<e.children.length;i+=1)e.children[i].indexInParent=i}},ye=(e,t)=>{let n=t-e.childOffset,r=e.children.length-e.childOffset;if(n<r-1-n){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let l=e.childOffset;l<=t;l+=1)e.children[l].indexInParent=l;e.childOffset>=e.children.length>>>1&&R(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let l=t;l<e.children.length;l+=1)e.children[l].indexInParent=l}};var rt=(e,t,n,r)=>{let l=t.childOffset;if(l>=t.children.length)throw new u("branch has no children");let o=l,i=l,a=t.keys.length-1;for(;i<=a;){let s=i+a>>>1,c=t.keys[s],d=e(c.key,n);(d!==0?d:c.sequence-r)<=0?(o=s,i=s+1):a=s-1}return t.children[o]},K=(e,t,n)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==C;)l=rt(r,l,t,n);return l},m=(e,t,n,r)=>{let l=e.compareKeys,o=t.entryOffset,i=t.entries.length;for(;o<i;){let a=o+i>>>1,s=t.entries[a],c=l(s.key,n);(c!==0?c:s.entryId-r)<0?o=a+1:i=a}return o-t.entryOffset},B=(e,t,n,r)=>{let l=e.compareKeys,o=t.entryOffset,i=t.entries.length;for(;o<i;){let a=o+i>>>1,s=t.entries[a],c=l(s.key,n);(c!==0?c:s.entryId-r)<=0?o=a+1:i=a}return o-t.entryOffset},Te=(e,t,n,r)=>{let l=e.compareKeys,o=t,i=32-Math.clz32(e.entryCount+1);for(;i>0&&o.next!==null&&y(o.next)>0;){let a=T(o.next,0),s=l(a.key,n);if(s>0||s===0&&a.entryId>r)break;o=o.next,i-=1}if(i===0&&o.next!==null&&y(o.next)>0){let a=T(o.next,0),s=l(a.key,n);if(s<0||s===0&&a.entryId<=r)return K(e,n,r)}return o},A=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,0),r=m(e,n,t,0);if(r>=y(n)&&(n.next===null||(n=n.next,r=m(e,n,t,0),r>=y(n)))||e.compareKeys(T(n,r).key,t)!==0)return null;let l=e._cursor;return l.leaf=n,l.index=r,l},se=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,Number.MAX_SAFE_INTEGER),r=B(e,n,t,Number.MAX_SAFE_INTEGER);if(r===0&&(n.prev===null||(n=n.prev,r=y(n),r===0))||(r-=1,e.compareKeys(T(n,r).key,t)!==0))return null;let l=e._cursor;return l.leaf=n,l.index=r,l},ce=(e,t)=>A(e,t)!==null,fe=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,Number.MAX_SAFE_INTEGER),l=B(e,r,t,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<y(r)){let o=T(r,l);if(n(o.key,t)>0)return o.key;l+=1}else r=r.next,l=0;return null},de=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),l=m(e,r,t,0),o=r,i=l-1;for(;o!==null;){for(;i>=0;){let a=T(o,i);if(n(a.key,t)<0)return a.key;i-=1}o=o.prev,o!==null&&(i=y(o)-1)}return null},he=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),l=m(e,r,t,0),o=e._cursor;if(l<y(r)){if(n(T(r,l).key,t)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let i=m(e,r.next,t,0);if(i<y(r.next)&&n(T(r.next,i).key,t)===0)return o.leaf=r.next,o.index=i,o}if(l>0)return o.leaf=r,o.index=l-1,o;if(r.prev!==null){let i=y(r.prev);if(i>0)return o.leaf=r.prev,o.index=i-1,o}return null};var x=e=>{let t=e;for(;t.parent!==null;){let n=t.indexInParent;if(!N(t,t.parent.keys[n])||n!==t.parent.childOffset)return;t=t.parent}},H=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},pe=e=>{if(!p(e))throw new u("expected leaf, got branch");return e},Ke=e=>{if(p(e))throw new u("expected branch, got leaf");return e},J=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");ye(e,t)},me=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},lt=(e,t,n)=>{let r=t.children.pop();if(r===void 0)throw new u("left branch borrow failed");t.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!N(r,l))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=l,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(l);for(let i=0;i<e.children.length;i+=1)e.children[i].indexInParent=i}let o=H(e);o.keys[n]={key:l.key,sequence:l.sequence},x(e)},ot=(e,t,n)=>{let r=t.childOffset;if(r>=t.children.length)throw new u("right branch borrow failed");let l=t.children[r];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&R(t),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!N(l,o))throw new u("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let i=H(e);N(t,i.keys[n+1])},it=(e,t,n,r)=>{for(let o=t.childOffset;o<t.children.length;o+=1){let i=t.children[o];i.parent=n,i.indexInParent=n.children.length,n.children.push(i),n.keys.push(t.keys[o])}let l=H(t);J(l,r),X(e,l)},at=(e,t,n,r)=>{for(let o=n.childOffset;o<n.children.length;o+=1){let i=n.children[o];i.parent=t,i.indexInParent=t.children.length,t.children.push(i),t.keys.push(n.keys[o])}let l=H(t);J(l,r+1),X(e,l)},ut=(e,t)=>{let n=t>e.childOffset?Ke(e.children[t-1]):null,r=t+1<e.children.length?Ke(e.children[t+1]):null;return{left:n,right:r}},X=(e,t)=>{let n=O(t);if(t===e.root){if(n===1){let a=t.children[t.childOffset];a.parent=null,e.root=a,p(a)&&(e.leftmostLeaf=a,e.rightmostLeaf=a)}return}if(n>=e.minBranchChildren)return;let r=t.parent;if(r===null)throw new u("branch has no parent");let l=t.indexInParent,{left:o,right:i}=ut(r,l);if(i!==null&&O(i)>e.minBranchChildren){ot(t,i,l);return}if(o!==null&&O(o)>e.minBranchChildren){lt(t,o,l);return}if(o!==null){it(e,t,o,l);return}if(i!==null){at(e,t,i,l);return}throw new u("no branch siblings to rebalance")};var Ve=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0);let n=t.entries;for(let r=t.entryOffset;r<n.length;r+=1)e.entries.push(n[r])},S=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(y(t)>=e.minLeafEntries)return;let n=t.parent;if(n===null)throw new u("Leaf node has no parent during rebalance.");let r=t.indexInParent,l=r>n.childOffset?pe(n.children[r-1]):null,o=r+1<n.children.length?pe(n.children[r+1]):null;if(o!==null&&y(o)>e.minLeafEntries){let i=D(o);if(i===void 0)throw new u("right leaf borrow failed");t.entries.push(i),N(o,n.keys[r+1]);return}if(l!==null&&y(l)>e.minLeafEntries){let i=l.entries.pop();if(i===void 0)throw new u("left leaf borrow failed");oe(t,i),n.keys[r]={key:i.key,sequence:i.entryId},x(t);return}if(l!==null){Ve(l,t),me(e,t),J(n,r),X(e,n);return}if(o!==null){Ve(t,o),me(e,o),J(n,r+1),X(e,n);return}throw new u("no leaf siblings to rebalance")};var yt=(e,t,n)=>{let r=n?Number.MAX_SAFE_INTEGER:0,l=K(e,t,r),o=n?B(e,l,t,Number.MAX_SAFE_INTEGER):m(e,l,t,0);return o>=y(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},Tt=(e,t,n,r,l)=>{let o=y(t),i=n;for(;i<o;){let a=T(t,i),s=e.compareKeys(a.key,r);if(l?s>=0:s>0)break;i+=1}return i},st=e=>{let t=0,n=e.root;for(;!p(n);)n=n.children[n.childOffset],t+=1;return t},ct=(e,t,n,r,l)=>{if(e.entryKeys!==null)for(let c=n;c<n+r;c+=1)e.entryKeys.delete(T(t,c).entryId);let o=t.entryOffset+n;t.entries.copyWithin(o,o+r),t.entries.length-=r,e.entryCount-=r;let i=y(t)===0;n===0&&!i&&t.parent!==null&&x(t);let a=y(t),s=l;for(;s>0&&t!==e.root&&y(t)<e.minLeafEntries&&(S(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)s-=1;return i&&y(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&x(t),a},ft=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,Be=(e,t,n,r)=>{if(e.entryCount===0)return 0;let l=e.compareKeys(t,n);if(l>0)return 0;let o=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive";if(o&&i&&l===0)return 0;let a=st(e),s=0,c=!0,d=null,h=0;for(;e.entryCount>0;){if(c){let v=yt(e,t,o);if(v===null)break;d=v.leaf,h=v.idx,c=!1}if(h>=y(d))break;let V=y(d),w=Tt(e,d,h,n,i),L=w-h;if(L===0)break;let g=ct(e,d,h,L,a);if(s+=L,w<V)break;if(!ft(e,d)){c=!0;continue}if(y(d)>g){c=!0;continue}if(d.next===null)break;d=d.next,h=0}return s};var b=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],E=e=>{let t=b[0];for(let n=1;n<b.length&&e>=b[n].threshold;n+=1)t=b[n];return t},G=e=>{for(let t=1;t<b.length;t+=1)if(e<b[t].threshold)return b[t].threshold;return Number.MAX_SAFE_INTEGER},xe=e=>{if(typeof e.compareKeys!="function")throw new f("compareKeys must be a function.");let t=e.autoScale===!0;if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new f("autoScale conflicts with explicit capacity.");let n,r;if(t){let i=E(0);n=i.maxLeaf,r=i.maxBranch}else n=M(e.maxLeafEntries,"maxLeafEntries",z),r=M(e.maxBranchChildren,"maxBranchChildren",$);let l=re(e.duplicateKeys),o=I([],null);return{compareKeys:e.compareKeys,maxLeafEntries:n,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:Math.ceil(n/2),minBranchChildren:Math.ceil(r/2),root:o,leftmostLeaf:o,rightmostLeaf:o,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,_nextAutoScaleThreshold:t?G(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:o,index:0}}},W=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:n}=E(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=Math.ceil(t/2)),n>e.maxBranchChildren&&(e.maxBranchChildren=n,e.minBranchChildren=Math.ceil(n/2)),e._nextAutoScaleThreshold=G(e.entryCount)},Q=(e,t,n)=>{if(!e.autoScale)return;let r=E(0),l=M(t,"maxLeafEntries",z),o=M(n,"maxBranchChildren",$);if(l<r.maxLeaf||o<r.maxBranch)throw new f("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=l,e.maxBranchChildren=o,e.minLeafEntries=Math.ceil(l/2),e.minBranchChildren=Math.ceil(o/2)};var Ee=(e,t,n)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>t&&o-t<n){let a=Math.ceil(o/2);r.push(l+a),r.push(e);break}let i=l+t<e?l+t:e;r.push(i),l=i}return r},dt=(e,t,n,r)=>{let l=Ee(t.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),i=0;for(let a=0;a<l.length;a+=1){let s=l[a],c=new Array(s-i);for(let d=i;d<s;d+=1){let h=r+d;c[d-i]={key:t[d].key,entryId:h,value:t[d].value},n[d]=h,e.entryKeys!==null&&e.entryKeys.set(h,t[d].key)}o[a]=I(c,null),i=s}return o},Le=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let n=e.nextSequence;if(n+t.length>Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");let r=new Array(t.length),l=dt(e,t,r,n);e.nextSequence=n+t.length,e.entryCount=t.length;for(let o=0;o<l.length;o+=1)o>0&&(l[o].prev=l[o-1]),o<l.length-1&&(l[o].next=l[o+1]);if(e.leftmostLeaf=l[0],e.rightmostLeaf=l[l.length-1],l.length===1)e.root=l[0];else{let o=l;for(;o.length>1;){let i=Ee(o.length,e.maxBranchChildren,e.minBranchChildren),a=new Array(i.length),s=0;for(let c=0;c<i.length;c+=1)a[c]=k(o.slice(s,i[c]),null),s=i[c];o=a}e.root=o[0]}return W(e),r};var Ne=(e,t,n,r)=>{let l={key:void 0,sequence:0};if(!N(r,l))throw new u("inserted child has no min key");r.parent=t;let o=n.indexInParent-t.childOffset+1;ue(t,o,r,l),O(t)>e.maxBranchChildren&&pt(e,t)},ht=(e,t)=>{ae(t);let n=Math.ceil(t.entries.length/2),r={kind:C,entries:t.entries.splice(n),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=r:e.rightmostLeaf=r,t.next=r,t.parent===null){e.root=k([t,r],null);return}Ne(e,t.parent,t,r)},pt=(e,t)=>{R(t);let n=Math.ceil(t.children.length/2),r=k(t.children.splice(n),t.parent);if(t.keys.splice(n),t.parent===null){e.root=k([t,r],null);return}Ne(e,t.parent,t,r)},we=(e,t,n,r)=>{let l=e.nextSequence,o=B(e,t,n,l);if(e.duplicateKeys!=="allow"){let i=null;if(o>0){let a=T(t,o-1);e.compareKeys(a.key,n)===0&&(i=a)}else if(t.prev!==null&&y(t.prev)>0){let a=t.prev,s=T(a,y(a)-1);e.compareKeys(s.key,n)===0&&(i=s)}if(i!==null){if(e.duplicateKeys==="reject")throw new f("Duplicate key rejected.");return i.value=r,i.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");return e.nextSequence+=1,ie(t,o,{key:n,entryId:l,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,n),o===0&&t.parent!==null&&x(t),y(t)>e.maxLeafEntries&&ht(e,t),W(e),l},Ce=(e,t,n)=>{let r=K(e,t,e.nextSequence);return we(e,r,t,n)},Se=e=>{if(e.entryCount===0)return null;let t=D(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),y(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&x(e.leftmostLeaf),e.leftmostLeaf!==e.root&&y(e.leftmostLeaf)<e.minLeafEntries&&S(e,e.leftmostLeaf),t},be=e=>{if(e.entryCount===0)return null;let t=le(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&y(e.rightmostLeaf)<e.minLeafEntries&&S(e,e.rightmostLeaf),t},ge=(e,t)=>{let n=A(e,t);if(n===null)return null;let r=n.leaf,l=n.index,o=T(r,l);return Y(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&y(r)>0&&r.parent!==null&&x(r),r!==e.root&&y(r)<e.minLeafEntries&&S(e,r),o},Z=(e,t,n)=>{let r=K(e,t,n),l=m(e,r,t,n);return l>=y(r)||T(r,l).entryId!==n?null:{leaf:r,index:l}},ve=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=Z(e,n,t);if(r===null)return null;let l=T(r.leaf,r.index);return Y(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(t),r.index===0&&y(r.leaf)>0&&r.leaf.parent!==null&&x(r.leaf),r.leaf!==e.root&&y(r.leaf)<e.minLeafEntries&&S(e,r.leaf),l},Ie=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=Z(e,n,t);return r===null?null:T(r.leaf,r.index)},ke=(e,t,n)=>{let r=e.entryKeys.get(t);if(r===void 0)return null;let l=Z(e,r,t);if(l===null)return null;let o=T(l.leaf,l.index);return o.value=n,o},Oe=(e,t)=>{if(t.length===0)return[];let n=e.duplicateKeys!=="allow";for(let r=1;r<t.length;r+=1){let l=e.compareKeys(t[r-1].key,t[r].key);if(n?l>=0:l>0)throw new f(n?"putMany: not sorted in strict ascending order.":"putMany: not sorted in non-descending order.")}if(e.entryCount>0){let r=new Array(t.length),l=K(e,t[0].key,e.nextSequence);for(let o=0;o<t.length;o+=1){let i=t[o],a=Te(e,l,i.key,e.nextSequence);r[o]=we(e,a,i.key,i.value),l=a}return r}return Le(e,t)};var Me=(e,t,n,r)=>{if(e.entryCount===0)return null;let l=e.compareKeys,o=l(t,n);if(o>0)return null;let i=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";if(i&&a&&o===0)return null;let s=i?Number.MAX_SAFE_INTEGER:0,c=K(e,t,s),d=i?B(e,c,t,Number.MAX_SAFE_INTEGER):m(e,c,t,0);return{leaf:c,index:d,compare:l,upperExclusive:a}},j=(e,t,n,r)=>{let l=Me(e,t,n,r);if(l===null)return 0;let o=l.leaf,i=l.index,{compare:a,upperExclusive:s}=l,c=0;for(;o!==null;){let d=y(o);if(i>=d){o=o.next,i=0;continue}let h=T(o,d-1),V=a(h.key,n);if(s?V<0:V<=0){c+=d-i,o=o.next,i=0;continue}let w=s?0:Number.MAX_SAFE_INTEGER,L=s?m(e,o,n,w):B(e,o,n,w),g=L<d?L:d;return c+=g-i,c}return c},Kt=200,mt=(e,t,n,r,l,o,i,a)=>{let s=y(t);if(s-n>=Kt&&t.next!==null){let d=T(t,s-1),h=r(d.key,i);if(l?h<0:h<=0){let V=j(e,o,i,a);return new Array(V)}}return[]},Ae=(e,t,n,r,l,o)=>{if(l)for(let i=t;i<n;i+=1)r[o++]=T(e,i);else for(let i=t;i<n;i+=1)r.push(T(e,i));return o},Re=(e,t,n,r)=>{let l=Me(e,t,n,r);if(l===null)return[];let o=l.leaf,i=l.index,{compare:a,upperExclusive:s}=l,c=mt(e,o,i,a,s,t,n,r),d=0,h=c.length>0;for(;o!==null;){let V=y(o);if(i>=V){o=o.next,i=0;continue}let w=T(o,V-1),L=a(w.key,n);if(s?L<0:L<=0){d=Ae(o,i,V,c,h,d),o=o.next,i=0;continue}let g=s?0:Number.MAX_SAFE_INTEGER,v=s?m(e,o,n,g):B(e,o,n,g),$e=v<V?v:V;return Ae(o,i,$e,c,h,d),c}return c};var _e=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},Pe=e=>{let t=new Array(e.entryCount),n=e.leftmostLeaf,r=0;for(;n!==null;){let l=y(n);for(let o=0;o<l;o+=1){let i=T(n,o);t[r++]=[i.key,i.value]}n=n.next}return{version:1,config:{maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale},entries:t}},Vt=1e6,Bt=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new f(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new f("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new f("BTreeJSON: entries must be array.");if(e.entries.length>Vt)throw new f("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let n=e.entries[t];if(!Array.isArray(n)||n.length!==2)throw new f(`BTreeJSON: bad entries[${t}].`)}},xt=e=>{let t=(n,r)=>{if(!Number.isInteger(r)||r<q||r>F)throw new f(`BTreeJSON: invalid ${n}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new f(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new f("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new f("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new f("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new f("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let n=E(0);if(e.maxLeafEntries<n.maxLeaf||e.maxBranchChildren<n.maxBranch)throw new f("BTreeJSON: autoScale capacity below tier-0.")}},qe=e=>{Bt(e),xt(e.config)},Fe=(e,t)=>{let n=e.config,r={compareKeys:t,duplicateKeys:n.duplicateKeys,enableEntryIdLookup:n.enableEntryIdLookup,autoScale:n.autoScale};return n.autoScale||(r.maxLeafEntries=n.maxLeafEntries,r.maxBranchChildren=n.maxBranchChildren),r};var ee=e=>{if(p(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},P=(e,t,n,r,l)=>{let o=e(t,r);return o!==0?o:n-l},De=e=>{if(p(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:De(e.children[e.children.length-1])},_=e=>{if(!Number.isFinite(e))throw new f("compareKeys must return a finite number.");return e},Et=(e,t)=>{if(_(e(t,t))!==0)throw new f("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Lt=(e,t,n,r)=>{let l=Math.sign(_(e(t,n))),o=Math.sign(_(e(n,r)));if(l<0&&o<0&&Math.sign(_(e(t,r)))>=0)throw new f("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(_(e(t,r)))<=0)throw new f("compareKeys must satisfy transitivity for observed key triples.")},Je=(e,t)=>{try{Et(e.compareKeys,t)}catch(n){throw n instanceof f?new u(n.message):n}},Xe=(e,t,n,r)=>{try{Lt(e.compareKeys,t,n,r)}catch(l){throw l instanceof f?new u(l.message):l}},Nt=(e,t,n,r)=>{if(!p(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==n)throw new u("Leaf prev pointer mismatch.");if(n!==null&&p(n)){let l=De(n),o=ee(t);if(l===null||o===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(P(e.compareKeys,l.key,l.sequence,o.key,o.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let i=y(n),a=y(t);if(e.duplicateKeys!=="allow"&&i>0&&a>0&&e.compareKeys(T(n,i-1).key,T(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},He=(e,t)=>{if(e.entryCount===0){if(!p(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let n=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)Nt(e,r,l,n),n.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(o!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var wt=(e,t)=>{let n=y(t);for(let r=0;r<n;r+=1)Je(e,T(t,r).key);for(let r=1;r<n;r+=1)if(P(e.compareKeys,T(t,r-1).key,T(t,r-1).entryId,T(t,r).key,T(t,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<n;r+=1)if(e.compareKeys(T(t,r-1).key,T(t,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<n;r+=1){let l=T(t,r-2),o=T(t,r-1),i=T(t,r);Xe(e,l.key,o.key,i.key)}if(n>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Ct=(e,t,n)=>{wt(e,t);let r=y(t),l=e.autoScale?Math.ceil(E(0).maxLeaf/2):e.minLeafEntries;if(t!==e.root&&r<l)throw new u("Non-root leaf node violates minimum occupancy.");let o=r===0?null:T(t,0),i=r===0?null:T(t,r-1),a=o===null?null:{key:o.key,sequence:o.entryId},s=i===null?null:{key:i.key,sequence:i.entryId};return{minKey:a,maxKey:s,leafDepth:r===0?null:n,leafCount:1,branchCount:0,entryCount:r}},St=(e,t)=>{let n=t.children.length-t.childOffset;if(n===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(E(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&n<r)throw new u("Non-root branch node violates minimum occupancy.");if(n>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},bt=(e,t,n,r)=>{let l=t.children[n];if(l.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==n)throw new u("Child indexInParent does not match actual position in parent.");let o=Ge(e,l,r+1);if(o.minKey===null||o.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let i=t.keys[n],a=ee(l);if(a===null||P(e.compareKeys,i.key,i.sequence,a.key,a.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return o},Ge=(e,t,n)=>{if(p(t))return Ct(e,t,n);St(e,t);let r=null,l=0,o=1,i=0,a=null,s=null,c=null;for(let d=t.childOffset;d<t.children.length;d+=1){let h=bt(e,t,d,n);if(r!==null&&h.leafDepth!==null&&h.leafDepth!==r)throw new u("Leaf depth mismatch detected in tree.");if(r===null&&h.leafDepth!==null&&(r=h.leafDepth),c!==null&&P(e.compareKeys,c.key,c.sequence,h.minKey.key,h.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");a===null&&(a=h.minKey),s=h.maxKey,c=h.maxKey,l+=h.leafCount,o+=h.branchCount,i+=h.entryCount}return{minKey:a,maxKey:s,leafDepth:r,leafCount:l,branchCount:o,entryCount:i}},We=e=>{let t=Ge(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");He(e,t.leafCount)};var Ue=e=>{if(p(e))return{height:1,leafCount:1,branchCount:0};let t=0,n=0,r=1;for(let l=e.childOffset;l<e.children.length;l+=1){let o=e.children[l],i=Ue(o);i.height>t&&(t=i.height),n+=i.leafCount,r+=i.branchCount}return{height:t+1,leafCount:n,branchCount:r}},ze=e=>{let t=Ue(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var te=class e{constructor(t){this.state=xe(t)}put(t,n){return Ce(this.state,t,n)}putMany(t){return Oe(this.state,t)}remove(t){return ge(this.state,t)}removeById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ve(this.state,t)}peekById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Ie(this.state,t)}updateById(t,n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ke(this.state,t,n)}popFirst(){return Se(this.state)}peekFirst(){return this.state.entryCount===0?null:T(this.state.leftmostLeaf,0)}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return T(t,y(t)-1)}popLast(){return be(this.state)}clear(){let t=I([],null);if(this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys!==null&&this.state.entryKeys.clear(),this.state.autoScale){let n=E(0);this.state.maxLeafEntries=n.maxLeaf,this.state.maxBranchChildren=n.maxBranch,this.state.minLeafEntries=Math.ceil(n.maxLeaf/2),this.state.minBranchChildren=Math.ceil(n.maxBranch/2),this.state._nextAutoScaleThreshold=G(0)}}get(t){let n=A(this.state,t);return n===null?null:T(n.leaf,n.index).value}hasKey(t){return ce(this.state,t)}findFirst(t){let n=A(this.state,t);return n===null?null:T(n.leaf,n.index)}findLast(t){let n=se(this.state,t);return n===null?null:T(n.leaf,n.index)}nextHigherKey(t){return fe(this.state,t)}nextLowerKey(t){return de(this.state,t)}getPairOrNextLower(t){let n=he(this.state,t);return n===null?null:T(n.leaf,n.index)}count(t,n,r){return j(this.state,t,n,r)}deleteRange(t,n,r){return Be(this.state,t,n,r)}range(t,n,r){return Re(this.state,t,n,r)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield T(t,r);t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let n=y(t);for(let r=n-1;r>=0;r-=1)yield T(t,r);t=t.prev}}*keys(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield T(t,r).key;t=t.next}}*values(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield T(t,r).value;t=t.next}}[Symbol.iterator](){return this.entries()}forEach(t,n){let r=this.state.leftmostLeaf;for(;r!==null;){let l=y(r);for(let o=0;o<l;o+=1)t.call(n,T(r,o));r=r.next}}snapshot(){let t=new Array(this.state.entryCount),n=this.state.leftmostLeaf,r=0;for(;n!==null;){let l=y(n);for(let o=0;o<l;o+=1)t[r++]=T(n,o);n=n.next}return t}clone(){let t=new e(_e(this.state));if(Q(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0){let n=new Array(this.state.entryCount),r=this.state.leftmostLeaf,l=0;for(;r!==null;){let o=y(r);for(let i=0;i<o;i+=1)n[l++]=T(r,i);r=r.next}t.putMany(n)}return t}toJSON(){return Pe(this.state)}static fromJSON(t,n){qe(t);let r=t.config.duplicateKeys!=="allow";for(let o=1;o<t.entries.length;o+=1){let i=n(t.entries[o-1][0],t.entries[o][0]);if(i>0)throw new f("fromJSON: entries not sorted.");if(r&&i===0)throw new f('fromJSON: duplicate keys require duplicateKeys "allow".')}let l=new e(Fe(t,n));if(Q(l.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let o=new Array(t.entries.length);for(let i=0;i<t.entries.length;i+=1)o[i]={key:t.entries[i][0],value:t.entries[i][1]};l.putMany(o)}return l}size(){return this.state.entryCount}assertInvariants(){We(this.state)}getStats(){return ze(this.state)}};return tt(gt);})();
1
+ "use strict";var FrostpillarBTreeCore=(()=>{var U=Object.defineProperty;var Ye=Object.getOwnPropertyDescriptor;var Qe=Object.getOwnPropertyNames;var Ze=Object.prototype.hasOwnProperty;var je=(e,n)=>{for(var t in n)U(e,t,{get:n[t],enumerable:!0})},en=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of Qe(n))!Ze.call(e,l)&&l!==t&&U(e,l,{get:()=>n[l],enumerable:!(r=Ye(n,l))||r.enumerable});return e};var nn=e=>en(U({},"__esModule",{value:!0}),e);var In={};je(In,{InMemoryBTree:()=>ne});var f=class extends Error{constructor(n){super(n),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(n){super(n),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var z=64,$=64,q=3,F=16384,C=0,tn=1,re=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new f("Invalid duplicateKeys option.");return e},p=e=>e.kind===C,L=(e,n)=>{if(e.kind===C){if(e.entryOffset>=e.entries.length)return!1;let t=e.entries[e.entryOffset];return n.key=t.key,n.sequence=t.entryId,!0}return e.childOffset>=e.keys.length?!1:(n.key=e.keys[e.childOffset].key,n.sequence=e.keys[e.childOffset].sequence,!0)},M=(e,n,t)=>{if(e===void 0)return t;if(!Number.isInteger(e)||e<q||e>F)throw new f(`${n}: integer ${q}\u2013${F} required.`);return e},v=(e,n)=>({kind:C,entries:e,entryOffset:0,parent:n,indexInParent:0,prev:null,next:null}),O=(e,n)=>{let t=[],r={kind:tn,children:e,keys:t,childOffset:0,parent:n,indexInParent:0};for(let l=0;l<e.length;l+=1){let o=e[l];o.parent=r,o.indexInParent=l;let i={key:void 0,sequence:0};if(!L(o,i))throw new u("branch child has no min key");t.push(i)}return r},y=e=>e.entries.length-e.entryOffset,s=(e,n)=>e.entries[e.entryOffset+n],D=e=>{if(e.entryOffset>=e.entries.length)return;let n=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),n},le=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},oe=(e,n)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=n):e.entries.unshift(n)},Y=(e,n)=>{let t=e.entries.length-e.entryOffset,r=e.entryOffset+n;n<t-1-n?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},ie=(e,n,t)=>{let r=e.entryOffset+n;e.entryOffset>0&&n<e.entries.length-e.entryOffset>>>1?(e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=t):e.entries.splice(r,0,t)},ae=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},te=1,_=e=>{if(e.childOffset>0){let n=e.childOffset<=te?0:te;e.children.copyWithin(n,e.childOffset),e.children.length-=e.childOffset-n,e.keys.copyWithin(n,e.childOffset),e.keys.length-=e.childOffset-n,e.childOffset=n;for(let t=n;t<e.children.length;t+=1)e.children[t].indexInParent=t}},k=e=>e.children.length-e.childOffset,ue=(e,n,t,r)=>{let l=e.childOffset+n,o=e.children.length-e.childOffset;if(e.childOffset>0&&n<o>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,l),e.keys.copyWithin(e.childOffset-1,e.childOffset,l),e.childOffset-=1,e.children[l-1]=t,e.keys[l-1]=r;for(let i=e.childOffset;i<l;i+=1)e.children[i].indexInParent=i;t.indexInParent=l-1}else{e.children.splice(l,0,t),e.keys.splice(l,0,r);for(let i=l;i<e.children.length;i+=1)e.children[i].indexInParent=i}},ye=(e,n)=>{let t=n-e.childOffset,r=e.children.length-e.childOffset;if(t<r-1-t){e.children.copyWithin(e.childOffset+1,e.childOffset,n),e.keys.copyWithin(e.childOffset+1,e.childOffset,n),e.childOffset+=1;for(let l=e.childOffset;l<=n;l+=1)e.children[l].indexInParent=l;e.childOffset>=e.children.length>>>1&&_(e)}else{e.children.copyWithin(n,n+1),e.keys.copyWithin(n,n+1),e.children.length-=1,e.keys.length-=1;for(let l=n;l<e.children.length;l+=1)e.children[l].indexInParent=l}};var rn=(e,n,t,r)=>{let l=n.childOffset;if(l>=n.children.length)throw new u("branch has no children");let o=l,i=l,a=n.keys.length-1;for(;i<=a;){let T=i+a>>>1,c=n.keys[T],d=e(c.key,t);(d!==0?d:c.sequence-r)<=0?(o=T,i=T+1):a=T-1}return n.children[o]},K=(e,n,t)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==C;)l=rn(r,l,n,t);return l},m=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,i=n.entries.length;for(;o<i;){let a=o+i>>>1,T=n.entries[a],c=l(T.key,t);(c!==0?c:T.entryId-r)<0?o=a+1:i=a}return o-n.entryOffset},B=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,i=n.entries.length;for(;o<i;){let a=o+i>>>1,T=n.entries[a],c=l(T.key,t);(c!==0?c:T.entryId-r)<=0?o=a+1:i=a}return o-n.entryOffset},se=(e,n,t,r)=>{let l=e.compareKeys,o=n,i=32-Math.clz32(e.entryCount+1);for(;i>0&&o.next!==null&&y(o.next)>0;){let a=s(o.next,0),T=l(a.key,t);if(T>0||T===0&&a.entryId>r)break;o=o.next,i-=1}if(i===0&&o.next!==null&&y(o.next)>0){let a=s(o.next,0),T=l(a.key,t);if(T<0||T===0&&a.entryId<=r)return K(e,t,r)}return o},A=(e,n)=>{if(e.entryCount===0)return null;let t=K(e,n,0),r=m(e,t,n,0);if(r>=y(t)&&(t.next===null||(t=t.next,r=m(e,t,n,0),r>=y(t)))||e.compareKeys(s(t,r).key,n)!==0)return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Te=(e,n)=>{if(e.entryCount===0)return null;let t=K(e,n,Number.MAX_SAFE_INTEGER),r=B(e,t,n,Number.MAX_SAFE_INTEGER);if(r===0&&(t.prev===null||(t=t.prev,r=y(t),r===0))||(r-=1,e.compareKeys(s(t,r).key,n)!==0))return null;let l=e._cursor;return l.leaf=t,l.index=r,l},ce=(e,n)=>A(e,n)!==null,fe=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=K(e,n,Number.MAX_SAFE_INTEGER),l=B(e,r,n,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<y(r)){let o=s(r,l);if(t(o.key,n)>0)return o.key;l+=1}else r=r.next,l=0;return null},de=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=K(e,n,0),l=m(e,r,n,0),o=r,i=l-1;for(;o!==null;){for(;i>=0;){let a=s(o,i);if(t(a.key,n)<0)return a.key;i-=1}o=o.prev,o!==null&&(i=y(o)-1)}return null},he=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=K(e,n,0),l=m(e,r,n,0),o=e._cursor;if(l<y(r)){if(t(s(r,l).key,n)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let i=m(e,r.next,n,0);if(i<y(r.next)&&t(s(r.next,i).key,n)===0)return o.leaf=r.next,o.index=i,o}if(l>0)return o.leaf=r,o.index=l-1,o;if(r.prev!==null){let i=y(r.prev);if(i>0)return o.leaf=r.prev,o.index=i-1,o}return null};var x=e=>{let n=e;for(;n.parent!==null;){let t=n.indexInParent;if(!L(n,n.parent.keys[t])||t!==n.parent.childOffset)return;n=n.parent}},H=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},pe=e=>{if(!p(e))throw new u("expected leaf, got branch");return e},Ke=e=>{if(p(e))throw new u("expected branch, got leaf");return e},J=(e,n)=>{if(n<e.childOffset||n>=e.children.length)throw new u("child index out of range");ye(e,n)},me=(e,n)=>{n.prev!==null?n.prev.next=n.next:n.next!==null&&(e.leftmostLeaf=n.next),n.next!==null?n.next.prev=n.prev:n.prev!==null&&(e.rightmostLeaf=n.prev),n.prev=null,n.next=null},ln=(e,n,t)=>{let r=n.children.pop();if(r===void 0)throw new u("left branch borrow failed");n.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!L(r,l))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=l,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(l);for(let i=0;i<e.children.length;i+=1)e.children[i].indexInParent=i}let o=H(e);o.keys[t]={key:l.key,sequence:l.sequence},x(e)},on=(e,n,t)=>{let r=n.childOffset;if(r>=n.children.length)throw new u("right branch borrow failed");let l=n.children[r];n.childOffset+=1,n.childOffset>=n.children.length>>>1&&_(n),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!L(l,o))throw new u("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let i=H(e);L(n,i.keys[t+1])},an=(e,n,t,r)=>{for(let o=n.childOffset;o<n.children.length;o+=1){let i=n.children[o];i.parent=t,i.indexInParent=t.children.length,t.children.push(i),t.keys.push(n.keys[o])}let l=H(n);J(l,r),X(e,l)},un=(e,n,t,r)=>{for(let o=t.childOffset;o<t.children.length;o+=1){let i=t.children[o];i.parent=n,i.indexInParent=n.children.length,n.children.push(i),n.keys.push(t.keys[o])}let l=H(n);J(l,r+1),X(e,l)},yn=(e,n)=>{let t=n>e.childOffset?Ke(e.children[n-1]):null,r=n+1<e.children.length?Ke(e.children[n+1]):null;return{left:t,right:r}},X=(e,n)=>{let t=k(n);if(n===e.root){if(t===1){let a=n.children[n.childOffset];a.parent=null,e.root=a,p(a)&&(e.leftmostLeaf=a,e.rightmostLeaf=a)}return}if(t>=e.minBranchChildren)return;let r=n.parent;if(r===null)throw new u("branch has no parent");let l=n.indexInParent,{left:o,right:i}=yn(r,l);if(i!==null&&k(i)>e.minBranchChildren){on(n,i,l);return}if(o!==null&&k(o)>e.minBranchChildren){ln(n,o,l);return}if(o!==null){an(e,n,o,l);return}if(i!==null){un(e,n,i,l);return}throw new u("no branch siblings to rebalance")};var Ve=(e,n)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0);let t=n.entries;for(let r=n.entryOffset;r<t.length;r+=1)e.entries.push(t[r])},S=(e,n)=>{if(n===e.root){e.entryCount===0&&(e.leftmostLeaf=n,e.rightmostLeaf=n);return}if(y(n)>=e.minLeafEntries)return;let t=n.parent;if(t===null)throw new u("Leaf node has no parent during rebalance.");let r=n.indexInParent,l=r>t.childOffset?pe(t.children[r-1]):null,o=r+1<t.children.length?pe(t.children[r+1]):null;if(o!==null&&y(o)>e.minLeafEntries){let i=D(o);if(i===void 0)throw new u("right leaf borrow failed");n.entries.push(i),L(o,t.keys[r+1]);return}if(l!==null&&y(l)>e.minLeafEntries){let i=l.entries.pop();if(i===void 0)throw new u("left leaf borrow failed");oe(n,i),t.keys[r]={key:i.key,sequence:i.entryId},x(n);return}if(l!==null){Ve(l,n),me(e,n),J(t,r),X(e,t);return}if(o!==null){Ve(n,o),me(e,o),J(t,r+1),X(e,t);return}throw new u("no leaf siblings to rebalance")};var sn=(e,n,t)=>{let r=t?Number.MAX_SAFE_INTEGER:0,l=K(e,n,r),o=t?B(e,l,n,Number.MAX_SAFE_INTEGER):m(e,l,n,0);return o>=y(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},Tn=(e,n,t,r,l)=>{let o=y(n),i=t;for(;i<o;){let a=s(n,i),T=e.compareKeys(a.key,r);if(l?T>=0:T>0)break;i+=1}return i},cn=e=>{let n=0,t=e.root;for(;!p(t);)t=t.children[t.childOffset],n+=1;return n},fn=(e,n,t,r,l)=>{if(e.entryKeys!==null)for(let c=t;c<t+r;c+=1)e.entryKeys.delete(s(n,c).entryId);let o=n.entryOffset+t;n.entries.copyWithin(o,o+r),n.entries.length-=r,e.entryCount-=r;let i=y(n)===0;t===0&&!i&&n.parent!==null&&x(n);let a=y(n),T=l;for(;T>0&&n!==e.root&&y(n)<e.minLeafEntries&&(S(e,n),!(n.parent!==null&&n.parent.children[n.indexInParent]!==n));)T-=1;return i&&y(n)>0&&n.parent!==null&&n.parent.children[n.indexInParent]===n&&x(n),a},dn=(e,n)=>n.parent===null?n===e.root:n.parent.children[n.indexInParent]===n,Be=(e,n,t,r)=>{if(e.entryCount===0)return 0;let l=e.compareKeys(n,t);if(l>0)return 0;let o=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive";if(o&&i&&l===0)return 0;let a=cn(e),T=0,c=!0,d=null,h=0;for(;e.entryCount>0;){if(c){let I=sn(e,n,o);if(I===null)break;d=I.leaf,h=I.idx,c=!1}if(h>=y(d))break;let V=y(d),w=Tn(e,d,h,t,i),N=w-h;if(N===0)break;let g=fn(e,d,h,N,a);if(T+=N,w<V)break;if(!dn(e,d)){c=!0;continue}if(y(d)>g){c=!0;continue}if(d.next===null)break;d=d.next,h=0}return T};var b=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],E=e=>{let n=b[0];for(let t=1;t<b.length&&e>=b[t].threshold;t+=1)n=b[t];return n},G=e=>{for(let n=1;n<b.length;n+=1)if(e<b[n].threshold)return b[n].threshold;return Number.MAX_SAFE_INTEGER},xe=e=>{if(typeof e.compareKeys!="function")throw new f("compareKeys must be a function.");let n=e.autoScale===!0;if(n&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new f("autoScale conflicts with explicit capacity.");let t,r;if(n){let i=E(0);t=i.maxLeaf,r=i.maxBranch}else t=M(e.maxLeafEntries,"maxLeafEntries",z),r=M(e.maxBranchChildren,"maxBranchChildren",$);let l=re(e.duplicateKeys),o=v([],null);return{compareKeys:e.compareKeys,maxLeafEntries:t,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:Math.ceil(t/2),minBranchChildren:Math.ceil(r/2),root:o,leftmostLeaf:o,rightmostLeaf:o,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:n,_nextAutoScaleThreshold:n?G(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:o,index:0}}},W=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:n,maxBranch:t}=E(e.entryCount);n>e.maxLeafEntries&&(e.maxLeafEntries=n,e.minLeafEntries=Math.ceil(n/2)),t>e.maxBranchChildren&&(e.maxBranchChildren=t,e.minBranchChildren=Math.ceil(t/2)),e._nextAutoScaleThreshold=G(e.entryCount)},Q=(e,n,t)=>{if(!e.autoScale)return;let r=E(0),l=M(n,"maxLeafEntries",z),o=M(t,"maxBranchChildren",$);if(l<r.maxLeaf||o<r.maxBranch)throw new f("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=l,e.maxBranchChildren=o,e.minLeafEntries=Math.ceil(l/2),e.minBranchChildren=Math.ceil(o/2)};var Ee=(e,n,t)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>n&&o-n<t){let a=Math.ceil(o/2);r.push(l+a),r.push(e);break}let i=l+n<e?l+n:e;r.push(i),l=i}return r},hn=(e,n,t,r)=>{let l=Ee(n.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),i=0;for(let a=0;a<l.length;a+=1){let T=l[a],c=new Array(T-i);for(let d=i;d<T;d+=1){let h=r+d;c[d-i]={key:n[d].key,entryId:h,value:n[d].value},t[d]=h,e.entryKeys!==null&&e.entryKeys.set(h,n[d].key)}o[a]=v(c,null),i=T}return o},Ne=(e,n)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let t=e.nextSequence;if(t+n.length>Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");let r=new Array(n.length),l=hn(e,n,r,t);e.nextSequence=t+n.length,e.entryCount=n.length;for(let o=0;o<l.length;o+=1)o>0&&(l[o].prev=l[o-1]),o<l.length-1&&(l[o].next=l[o+1]);if(e.leftmostLeaf=l[0],e.rightmostLeaf=l[l.length-1],l.length===1)e.root=l[0];else{let o=l;for(;o.length>1;){let i=Ee(o.length,e.maxBranchChildren,e.minBranchChildren),a=new Array(i.length),T=0;for(let c=0;c<i.length;c+=1)a[c]=O(o.slice(T,i[c]),null),T=i[c];o=a}e.root=o[0]}return W(e),r};var Le=(e,n,t,r)=>{let l={key:void 0,sequence:0};if(!L(r,l))throw new u("inserted child has no min key");r.parent=n;let o=t.indexInParent-n.childOffset+1;ue(n,o,r,l),k(n)>e.maxBranchChildren&&Kn(e,n)},pn=(e,n)=>{ae(n);let t=Math.ceil(n.entries.length/2),r={kind:C,entries:n.entries.splice(t),entryOffset:0,parent:n.parent,indexInParent:0,prev:n,next:n.next};if(n.next!==null?n.next.prev=r:e.rightmostLeaf=r,n.next=r,n.parent===null){e.root=O([n,r],null);return}Le(e,n.parent,n,r)},Kn=(e,n)=>{_(n);let t=Math.ceil(n.children.length/2),r=O(n.children.splice(t),n.parent);if(n.keys.splice(t),n.parent===null){e.root=O([n,r],null);return}Le(e,n.parent,n,r)},we=(e,n,t,r)=>{let l=e.nextSequence,o=B(e,n,t,l);if(e.duplicateKeys!=="allow"){let i=null;if(o>0){let a=s(n,o-1);e.compareKeys(a.key,t)===0&&(i=a)}else if(n.prev!==null&&y(n.prev)>0){let a=n.prev,T=s(a,y(a)-1);e.compareKeys(T.key,t)===0&&(i=T)}if(i!==null){if(e.duplicateKeys==="reject")throw new f("Duplicate key rejected.");return i.value=r,i.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");return e.nextSequence+=1,ie(n,o,{key:t,entryId:l,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,t),o===0&&n.parent!==null&&x(n),y(n)>e.maxLeafEntries&&pn(e,n),W(e),l},Ce=(e,n,t)=>{let r=K(e,n,e.nextSequence);return we(e,r,n,t)},Se=e=>{if(e.entryCount===0)return null;let n=D(e.leftmostLeaf);if(n===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),y(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&x(e.leftmostLeaf),e.leftmostLeaf!==e.root&&y(e.leftmostLeaf)<e.minLeafEntries&&S(e,e.leftmostLeaf),n},be=e=>{if(e.entryCount===0)return null;let n=le(e.rightmostLeaf);if(n===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),e.rightmostLeaf!==e.root&&y(e.rightmostLeaf)<e.minLeafEntries&&S(e,e.rightmostLeaf),n},ge=(e,n)=>{let t=A(e,n);if(t===null)return null;let r=t.leaf,l=t.index,o=s(r,l);return Y(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&y(r)>0&&r.parent!==null&&x(r),r!==e.root&&y(r)<e.minLeafEntries&&S(e,r),o},Z=(e,n,t)=>{let r=K(e,n,t),l=m(e,r,n,t);return l>=y(r)||s(r,l).entryId!==t?null:{leaf:r,index:l}},Ie=(e,n)=>{let t=e.entryKeys.get(n);if(t===void 0)return null;let r=Z(e,t,n);if(r===null)return null;let l=s(r.leaf,r.index);return Y(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(n),r.index===0&&y(r.leaf)>0&&r.leaf.parent!==null&&x(r.leaf),r.leaf!==e.root&&y(r.leaf)<e.minLeafEntries&&S(e,r.leaf),l},ve=(e,n)=>{let t=e.entryKeys.get(n);if(t===void 0)return null;let r=Z(e,t,n);return r===null?null:s(r.leaf,r.index)},Oe=(e,n,t)=>{let r=e.entryKeys.get(n);if(r===void 0)return null;let l=Z(e,r,n);if(l===null)return null;let o=s(l.leaf,l.index);return o.value=t,o},ke=(e,n)=>{if(n.length===0)return[];let t=e.duplicateKeys!=="allow";for(let r=1;r<n.length;r+=1){let l=e.compareKeys(n[r-1].key,n[r].key);if(t?l>=0:l>0)throw new f(t?"putMany: not sorted in strict ascending order.":"putMany: not sorted in non-descending order.")}if(e.entryCount>0){let r=new Array(n.length),l=K(e,n[0].key,e.nextSequence);for(let o=0;o<n.length;o+=1){let i=n[o],a=se(e,l,i.key,e.nextSequence);r[o]=we(e,a,i.key,i.value),l=a}return r}return Ne(e,n)};var Me=(e,n,t,r)=>{if(e.entryCount===0)return null;let l=e.compareKeys,o=l(n,t);if(o>0)return null;let i=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";if(i&&a&&o===0)return null;let T=i?Number.MAX_SAFE_INTEGER:0,c=K(e,n,T),d=i?B(e,c,n,Number.MAX_SAFE_INTEGER):m(e,c,n,0);return{leaf:c,index:d,compare:l,upperExclusive:a}},j=(e,n,t,r)=>{let l=Me(e,n,t,r);if(l===null)return 0;let o=l.leaf,i=l.index,{compare:a,upperExclusive:T}=l,c=0;for(;o!==null;){let d=y(o);if(i>=d){o=o.next,i=0;continue}let h=s(o,d-1),V=a(h.key,t);if(T?V<0:V<=0){c+=d-i,o=o.next,i=0;continue}let w=T?0:Number.MAX_SAFE_INTEGER,N=T?m(e,o,t,w):B(e,o,t,w),g=N<d?N:d;return c+=g-i,c}return c},mn=200,Vn=(e,n,t,r,l,o,i,a)=>{let T=y(n);if(T-t>=mn&&n.next!==null){let d=s(n,T-1),h=r(d.key,i);if(l?h<0:h<=0){let V=j(e,o,i,a);return new Array(V)}}return[]},Ae=(e,n,t,r,l,o)=>{if(l)for(let i=n;i<t;i+=1)r[o++]=s(e,i);else for(let i=n;i<t;i+=1)r.push(s(e,i));return o},_e=(e,n,t,r)=>{let l=Me(e,n,t,r);if(l===null)return[];let o=l.leaf,i=l.index,{compare:a,upperExclusive:T}=l,c=Vn(e,o,i,a,T,n,t,r),d=0,h=c.length>0;for(;o!==null;){let V=y(o);if(i>=V){o=o.next,i=0;continue}let w=s(o,V-1),N=a(w.key,t);if(T?N<0:N<=0){d=Ae(o,i,V,c,h,d),o=o.next,i=0;continue}let g=T?0:Number.MAX_SAFE_INTEGER,I=T?m(e,o,t,g):B(e,o,t,g),$e=I<V?I:V;return Ae(o,i,$e,c,h,d),c}return c};var Re=e=>{let n={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.autoScale||(n.maxLeafEntries=e.maxLeafEntries,n.maxBranchChildren=e.maxBranchChildren),n},Pe=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=y(t);for(let o=0;o<l;o+=1){let i=s(t,o);n[r++]=[i.key,i.value]}t=t.next}return{version:1,config:{maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale},entries:n}},Bn=1e6,xn=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new f(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new f("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new f("BTreeJSON: entries must be array.");if(e.entries.length>Bn)throw new f("BTreeJSON: entry count exceeds maximum.");for(let n=0;n<e.entries.length;n+=1){let t=e.entries[n];if(!Array.isArray(t)||t.length!==2)throw new f(`BTreeJSON: bad entries[${n}].`)}},En=e=>{let n=(t,r)=>{if(!Number.isInteger(r)||r<q||r>F)throw new f(`BTreeJSON: invalid ${t}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new f(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new f("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new f("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new f("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new f("BTreeJSON: invalid maxBranchChildren.");if(n("maxLeafEntries",e.maxLeafEntries),n("maxBranchChildren",e.maxBranchChildren),e.autoScale){let t=E(0);if(e.maxLeafEntries<t.maxLeaf||e.maxBranchChildren<t.maxBranch)throw new f("BTreeJSON: autoScale capacity below tier-0.")}},qe=e=>{xn(e),En(e.config)},Fe=(e,n)=>{let t=e.config,r={compareKeys:n,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale};return t.autoScale||(r.maxLeafEntries=t.maxLeafEntries,r.maxBranchChildren=t.maxBranchChildren),r};var ee=e=>{if(p(e)){if(e.entryOffset>=e.entries.length)return null;let n=e.entries[e.entryOffset];return{key:n.key,sequence:n.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},P=(e,n,t,r,l)=>{let o=e(n,r);return o!==0?o:t-l},De=e=>{if(p(e)){if(e.entryOffset>=e.entries.length)return null;let n=e.entries[e.entries.length-1];return{key:n.key,sequence:n.entryId}}return e.childOffset>=e.children.length?null:De(e.children[e.children.length-1])},R=e=>{if(!Number.isFinite(e))throw new f("compareKeys must return a finite number.");return e},Nn=(e,n)=>{if(R(e(n,n))!==0)throw new f("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Ln=(e,n,t,r)=>{let l=Math.sign(R(e(n,t))),o=Math.sign(R(e(t,r)));if(l<0&&o<0&&Math.sign(R(e(n,r)))>=0)throw new f("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(R(e(n,r)))<=0)throw new f("compareKeys must satisfy transitivity for observed key triples.")},Je=(e,n)=>{try{Nn(e.compareKeys,n)}catch(t){throw t instanceof f?new u(t.message):t}},Xe=(e,n,t,r)=>{try{Ln(e.compareKeys,n,t,r)}catch(l){throw l instanceof f?new u(l.message):l}},wn=(e,n,t,r)=>{if(!p(n))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(n))throw new u("Cycle detected in leaf linkage.");if(n.prev!==t)throw new u("Leaf prev pointer mismatch.");if(t!==null&&p(t)){let l=De(t),o=ee(n);if(l===null||o===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(P(e.compareKeys,l.key,l.sequence,o.key,o.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let i=y(t),a=y(n);if(e.duplicateKeys!=="allow"&&i>0&&a>0&&e.compareKeys(s(t,i-1).key,s(n,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},He=(e,n)=>{if(e.entryCount===0){if(!p(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let t=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)wn(e,r,l,t),t.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(o!==n)throw new u("Leaf chain count mismatch with tree traversal count.")};var Cn=(e,n)=>{let t=y(n);for(let r=0;r<t;r+=1)Je(e,s(n,r).key);for(let r=1;r<t;r+=1)if(P(e.compareKeys,s(n,r-1).key,s(n,r-1).entryId,s(n,r).key,s(n,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<t;r+=1)if(e.compareKeys(s(n,r-1).key,s(n,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<t;r+=1){let l=s(n,r-2),o=s(n,r-1),i=s(n,r);Xe(e,l.key,o.key,i.key)}if(t>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Sn=(e,n,t)=>{Cn(e,n);let r=y(n),l=e.autoScale?Math.ceil(E(0).maxLeaf/2):e.minLeafEntries;if(n!==e.root&&r<l)throw new u("Non-root leaf node violates minimum occupancy.");let o=r===0?null:s(n,0),i=r===0?null:s(n,r-1),a=o===null?null:{key:o.key,sequence:o.entryId},T=i===null?null:{key:i.key,sequence:i.entryId};return{minKey:a,maxKey:T,leafDepth:r===0?null:t,leafCount:1,branchCount:0,entryCount:r}},bn=(e,n)=>{let t=n.children.length-n.childOffset;if(t===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(E(0).maxBranch/2):e.minBranchChildren;if(n!==e.root&&t<r)throw new u("Non-root branch node violates minimum occupancy.");if(t>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(n.keys.length!==n.children.length)throw new u("Branch keys array length does not match children array length.")},gn=(e,n,t,r)=>{let l=n.children[t];if(l.parent!==n)throw new u("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==t)throw new u("Child indexInParent does not match actual position in parent.");let o=Ge(e,l,r+1);if(o.minKey===null||o.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let i=n.keys[t],a=ee(l);if(a===null||P(e.compareKeys,i.key,i.sequence,a.key,a.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return o},Ge=(e,n,t)=>{if(p(n))return Sn(e,n,t);bn(e,n);let r=null,l=0,o=1,i=0,a=null,T=null,c=null;for(let d=n.childOffset;d<n.children.length;d+=1){let h=gn(e,n,d,t);if(r!==null&&h.leafDepth!==null&&h.leafDepth!==r)throw new u("Leaf depth mismatch detected in tree.");if(r===null&&h.leafDepth!==null&&(r=h.leafDepth),c!==null&&P(e.compareKeys,c.key,c.sequence,h.minKey.key,h.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");a===null&&(a=h.minKey),T=h.maxKey,c=h.maxKey,l+=h.leafCount,o+=h.branchCount,i+=h.entryCount}return{minKey:a,maxKey:T,leafDepth:r,leafCount:l,branchCount:o,entryCount:i}},We=e=>{let n=Ge(e,e.root,0);if(n.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");He(e,n.leafCount)};var Ue=e=>{if(p(e))return{height:1,leafCount:1,branchCount:0};let n=0,t=0,r=1;for(let l=e.childOffset;l<e.children.length;l+=1){let o=e.children[l],i=Ue(o);i.height>n&&(n=i.height),t+=i.leafCount,r+=i.branchCount}return{height:n+1,leafCount:t,branchCount:r}},ze=e=>{let n=Ue(e.root);return{height:n.height,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:e.entryCount}};var ne=class e{constructor(n){this.state=xe(n)}put(n,t){return Ce(this.state,n,t)}putMany(n){return ke(this.state,n)}remove(n){return ge(this.state,n)}removeById(n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Ie(this.state,n)}peekById(n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ve(this.state,n)}updateById(n,t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Oe(this.state,n,t)}popFirst(){return Se(this.state)}peekFirst(){return this.state.entryCount===0?null:s(this.state.leftmostLeaf,0)}peekLast(){if(this.state.entryCount===0)return null;let n=this.state.rightmostLeaf;return s(n,y(n)-1)}popLast(){return be(this.state)}clear(){let n=v([],null);if(this.state.root=n,this.state.leftmostLeaf=n,this.state.rightmostLeaf=n,this.state.entryCount=0,this.state._cursor.leaf=n,this.state._cursor.index=0,this.state.entryKeys!==null&&this.state.entryKeys.clear(),this.state.autoScale){let t=E(0);this.state.maxLeafEntries=t.maxLeaf,this.state.maxBranchChildren=t.maxBranch,this.state.minLeafEntries=Math.ceil(t.maxLeaf/2),this.state.minBranchChildren=Math.ceil(t.maxBranch/2),this.state._nextAutoScaleThreshold=G(0)}}get(n){let t=A(this.state,n);return t===null?null:s(t.leaf,t.index).value}hasKey(n){return ce(this.state,n)}findFirst(n){let t=A(this.state,n);return t===null?null:s(t.leaf,t.index)}findLast(n){let t=Te(this.state,n);return t===null?null:s(t.leaf,t.index)}nextHigherKey(n){return fe(this.state,n)}nextLowerKey(n){return de(this.state,n)}getPairOrNextLower(n){let t=he(this.state,n);return t===null?null:s(t.leaf,t.index)}count(n,t,r){return j(this.state,n,t,r)}deleteRange(n,t,r){return Be(this.state,n,t,r)}range(n,t,r){return _e(this.state,n,t,r)}*entries(){let n=this.state.leftmostLeaf;for(;n!==null;){let t=y(n);for(let r=0;r<t;r+=1)yield s(n,r);n=n.next}}*entriesReversed(){let n=this.state.rightmostLeaf;for(;n!==null;){let t=y(n);for(let r=t-1;r>=0;r-=1)yield s(n,r);n=n.prev}}*keys(){for(let n of this.entries())yield n.key}*values(){for(let n of this.entries())yield n.value}[Symbol.iterator](){return this.entries()}forEach(n,t){let r=this.state.leftmostLeaf;for(;r!==null;){let l=y(r);for(let o=0;o<l;o+=1)n.call(t,s(r,o));r=r.next}}snapshot(){let n=new Array(this.state.entryCount),t=this.state.leftmostLeaf,r=0;for(;t!==null;){let l=y(t);for(let o=0;o<l;o+=1)n[r++]=s(t,o);t=t.next}return n}clone(){let n=new e(Re(this.state));if(Q(n.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0){let t=new Array(this.state.entryCount),r=this.state.leftmostLeaf,l=0;for(;r!==null;){let o=y(r);for(let i=0;i<o;i+=1)t[l++]=s(r,i);r=r.next}n.putMany(t)}return n}toJSON(){return Pe(this.state)}static fromJSON(n,t){qe(n);let r=n.config.duplicateKeys!=="allow";for(let o=1;o<n.entries.length;o+=1){let i=t(n.entries[o-1][0],n.entries[o][0]);if(i>0)throw new f("fromJSON: entries not sorted.");if(r&&i===0)throw new f('fromJSON: duplicate keys require duplicateKeys "allow".')}let l=new e(Fe(n,t));if(Q(l.state,n.config.maxLeafEntries,n.config.maxBranchChildren),n.entries.length>0){let o=new Array(n.entries.length);for(let i=0;i<n.entries.length;i+=1)o[i]={key:n.entries[i][0],value:n.entries[i][1]};l.putMany(o)}return l}size(){return this.state.entryCount}assertInvariants(){We(this.state)}getStats(){return ze(this.state)}};return nn(In);})();
@@ -1 +1 @@
1
- "use strict";var FrostpillarBTree=(()=>{var j=Object.defineProperty;var it=Object.getOwnPropertyDescriptor;var at=Object.getOwnPropertyNames;var ut=Object.prototype.hasOwnProperty;var yt=(e,t)=>{for(var n in t)j(e,n,{get:t[n],enumerable:!0})},st=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of at(t))!ut.call(e,o)&&o!==n&&j(e,o,{get:()=>t[o],enumerable:!(r=it(t,o))||r.enumerable});return e};var Tt=e=>st(j({},"__esModule",{value:!0}),e);var Dt={};yt(Dt,{BTreeConcurrencyError:()=>h,BTreeInvariantError:()=>u,BTreeValidationError:()=>f,ConcurrentInMemoryBTree:()=>U,InMemoryBTree:()=>R});var f=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}},h=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var P=64,_=64,X=3,H=16384,S=0,ct=1,le=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new f("Invalid duplicateKeys option.");return e},m=e=>e.kind===S,N=(e,t)=>{if(e.kind===S){if(e.entryOffset>=e.entries.length)return!1;let n=e.entries[e.entryOffset];return t.key=n.key,t.sequence=n.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},F=(e,t,n)=>{if(e===void 0)return n;if(!Number.isInteger(e)||e<X||e>H)throw new f(`${t}: integer ${X}\u2013${H} required.`);return e},M=(e,t)=>({kind:S,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),k=(e,t)=>{let n=[],r={kind:ct,children:e,keys:n,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let l=e[o];l.parent=r,l.indexInParent=o;let i={key:void 0,sequence:0};if(!N(l,i))throw new u("branch child has no min key");n.push(i)}return r},y=e=>e.entries.length-e.entryOffset,s=(e,t)=>e.entries[e.entryOffset+t],z=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},ie=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},ae=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},Z=(e,t)=>{let n=e.entries.length-e.entryOffset,r=e.entryOffset+t;t<n-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},ue=(e,t,n)=>{let r=e.entryOffset+t;e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1?(e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=n):e.entries.splice(r,0,n)},ye=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},oe=1,q=e=>{if(e.childOffset>0){let t=e.childOffset<=oe?0:oe;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let n=t;n<e.children.length;n+=1)e.children[n].indexInParent=n}},O=e=>e.children.length-e.childOffset,se=(e,t,n,r)=>{let o=e.childOffset+t,l=e.children.length-e.childOffset;if(e.childOffset>0&&t<l>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,o),e.keys.copyWithin(e.childOffset-1,e.childOffset,o),e.childOffset-=1,e.children[o-1]=n,e.keys[o-1]=r;for(let i=e.childOffset;i<o;i+=1)e.children[i].indexInParent=i;n.indexInParent=o-1}else{e.children.splice(o,0,n),e.keys.splice(o,0,r);for(let i=o;i<e.children.length;i+=1)e.children[i].indexInParent=i}},Te=(e,t)=>{let n=t-e.childOffset,r=e.children.length-e.childOffset;if(n<r-1-n){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let o=e.childOffset;o<=t;o+=1)e.children[o].indexInParent=o;e.childOffset>=e.children.length>>>1&&q(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let o=t;o<e.children.length;o+=1)e.children[o].indexInParent=o}};var ft=(e,t,n,r)=>{let o=t.childOffset;if(o>=t.children.length)throw new u("branch has no children");let l=o,i=o,a=t.keys.length-1;for(;i<=a;){let T=i+a>>>1,c=t.keys[T],d=e(c.key,n);(d!==0?d:c.sequence-r)<=0?(l=T,i=T+1):a=T-1}return t.children[l]},K=(e,t,n)=>{let r=e.compareKeys,o=e.root;for(;o.kind!==S;)o=ft(r,o,t,n);return o},B=(e,t,n,r)=>{let o=e.compareKeys,l=t.entryOffset,i=t.entries.length;for(;l<i;){let a=l+i>>>1,T=t.entries[a],c=o(T.key,n);(c!==0?c:T.entryId-r)<0?l=a+1:i=a}return l-t.entryOffset},E=(e,t,n,r)=>{let o=e.compareKeys,l=t.entryOffset,i=t.entries.length;for(;l<i;){let a=l+i>>>1,T=t.entries[a],c=o(T.key,n);(c!==0?c:T.entryId-r)<=0?l=a+1:i=a}return l-t.entryOffset},ce=(e,t,n,r)=>{let o=e.compareKeys,l=t,i=32-Math.clz32(e.entryCount+1);for(;i>0&&l.next!==null&&y(l.next)>0;){let a=s(l.next,0),T=o(a.key,n);if(T>0||T===0&&a.entryId>r)break;l=l.next,i-=1}if(i===0&&l.next!==null&&y(l.next)>0){let a=s(l.next,0),T=o(a.key,n);if(T<0||T===0&&a.entryId<=r)return K(e,n,r)}return l},A=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,0),r=B(e,n,t,0);if(r>=y(n)&&(n.next===null||(n=n.next,r=B(e,n,t,0),r>=y(n)))||e.compareKeys(s(n,r).key,t)!==0)return null;let o=e._cursor;return o.leaf=n,o.index=r,o},fe=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,Number.MAX_SAFE_INTEGER),r=E(e,n,t,Number.MAX_SAFE_INTEGER);if(r===0&&(n.prev===null||(n=n.prev,r=y(n),r===0))||(r-=1,e.compareKeys(s(n,r).key,t)!==0))return null;let o=e._cursor;return o.leaf=n,o.index=r,o},de=(e,t)=>A(e,t)!==null,pe=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,Number.MAX_SAFE_INTEGER),o=E(e,r,t,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(o<y(r)){let l=s(r,o);if(n(l.key,t)>0)return l.key;o+=1}else r=r.next,o=0;return null},he=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),o=B(e,r,t,0),l=r,i=o-1;for(;l!==null;){for(;i>=0;){let a=s(l,i);if(n(a.key,t)<0)return a.key;i-=1}l=l.prev,l!==null&&(i=y(l)-1)}return null},me=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),o=B(e,r,t,0),l=e._cursor;if(o<y(r)){if(n(s(r,o).key,t)===0)return l.leaf=r,l.index=o,l}else if(r.next!==null){let i=B(e,r.next,t,0);if(i<y(r.next)&&n(s(r.next,i).key,t)===0)return l.leaf=r.next,l.index=i,l}if(o>0)return l.leaf=r,l.index=o-1,l;if(r.prev!==null){let i=y(r.prev);if(i>0)return l.leaf=r.prev,l.index=i-1,l}return null};var L=e=>{let t=e;for(;t.parent!==null;){let n=t.indexInParent;if(!N(t,t.parent.keys[n])||n!==t.parent.childOffset)return;t=t.parent}},$=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},Ke=e=>{if(!m(e))throw new u("expected leaf, got branch");return e},Be=e=>{if(m(e))throw new u("expected branch, got leaf");return e},G=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");Te(e,t)},Ve=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},dt=(e,t,n)=>{let r=t.children.pop();if(r===void 0)throw new u("left branch borrow failed");t.keys.pop(),r.parent=e;let o={key:void 0,sequence:0};if(!N(r,o))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=o,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(o);for(let i=0;i<e.children.length;i+=1)e.children[i].indexInParent=i}let l=$(e);l.keys[n]={key:o.key,sequence:o.sequence},L(e)},pt=(e,t,n)=>{let r=t.childOffset;if(r>=t.children.length)throw new u("right branch borrow failed");let o=t.children[r];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&q(t),e.children.push(o),o.parent=e;let l={key:void 0,sequence:0};if(!N(o,l))throw new u("borrowed child has no min key");e.keys.push(l),o.indexInParent=e.children.length-1;let i=$(e);N(t,i.keys[n+1])},ht=(e,t,n,r)=>{for(let l=t.childOffset;l<t.children.length;l+=1){let i=t.children[l];i.parent=n,i.indexInParent=n.children.length,n.children.push(i),n.keys.push(t.keys[l])}let o=$(t);G(o,r),W(e,o)},mt=(e,t,n,r)=>{for(let l=n.childOffset;l<n.children.length;l+=1){let i=n.children[l];i.parent=t,i.indexInParent=t.children.length,t.children.push(i),t.keys.push(n.keys[l])}let o=$(t);G(o,r+1),W(e,o)},Kt=(e,t)=>{let n=t>e.childOffset?Be(e.children[t-1]):null,r=t+1<e.children.length?Be(e.children[t+1]):null;return{left:n,right:r}},W=(e,t)=>{let n=O(t);if(t===e.root){if(n===1){let a=t.children[t.childOffset];a.parent=null,e.root=a,m(a)&&(e.leftmostLeaf=a,e.rightmostLeaf=a)}return}if(n>=e.minBranchChildren)return;let r=t.parent;if(r===null)throw new u("branch has no parent");let o=t.indexInParent,{left:l,right:i}=Kt(r,o);if(i!==null&&O(i)>e.minBranchChildren){pt(t,i,o);return}if(l!==null&&O(l)>e.minBranchChildren){dt(t,l,o);return}if(l!==null){ht(e,t,l,o);return}if(i!==null){mt(e,t,i,o);return}throw new u("no branch siblings to rebalance")};var xe=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0);let n=t.entries;for(let r=t.entryOffset;r<n.length;r+=1)e.entries.push(n[r])},g=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(y(t)>=e.minLeafEntries)return;let n=t.parent;if(n===null)throw new u("Leaf node has no parent during rebalance.");let r=t.indexInParent,o=r>n.childOffset?Ke(n.children[r-1]):null,l=r+1<n.children.length?Ke(n.children[r+1]):null;if(l!==null&&y(l)>e.minLeafEntries){let i=z(l);if(i===void 0)throw new u("right leaf borrow failed");t.entries.push(i),N(l,n.keys[r+1]);return}if(o!==null&&y(o)>e.minLeafEntries){let i=o.entries.pop();if(i===void 0)throw new u("left leaf borrow failed");ae(t,i),n.keys[r]={key:i.key,sequence:i.entryId},L(t);return}if(o!==null){xe(o,t),Ve(e,t),G(n,r),W(e,n);return}if(l!==null){xe(t,l),Ve(e,l),G(n,r+1),W(e,n);return}throw new u("no leaf siblings to rebalance")};var Bt=(e,t,n)=>{let r=n?Number.MAX_SAFE_INTEGER:0,o=K(e,t,r),l=n?E(e,o,t,Number.MAX_SAFE_INTEGER):B(e,o,t,0);return l>=y(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:l}},Vt=(e,t,n,r,o)=>{let l=y(t),i=n;for(;i<l;){let a=s(t,i),T=e.compareKeys(a.key,r);if(o?T>=0:T>0)break;i+=1}return i},xt=e=>{let t=0,n=e.root;for(;!m(n);)n=n.children[n.childOffset],t+=1;return t},Et=(e,t,n,r,o)=>{if(e.entryKeys!==null)for(let c=n;c<n+r;c+=1)e.entryKeys.delete(s(t,c).entryId);let l=t.entryOffset+n;t.entries.copyWithin(l,l+r),t.entries.length-=r,e.entryCount-=r;let i=y(t)===0;n===0&&!i&&t.parent!==null&&L(t);let a=y(t),T=o;for(;T>0&&t!==e.root&&y(t)<e.minLeafEntries&&(g(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)T-=1;return i&&y(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&L(t),a},Lt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,Ee=(e,t,n,r)=>{if(e.entryCount===0)return 0;let o=e.compareKeys(t,n);if(o>0)return 0;let l=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive";if(l&&i&&o===0)return 0;let a=xt(e),T=0,c=!0,d=null,p=0;for(;e.entryCount>0;){if(c){let I=Bt(e,t,l);if(I===null)break;d=I.leaf,p=I.idx,c=!1}if(p>=y(d))break;let V=y(d),b=Vt(e,d,p,n,i),w=b-p;if(w===0)break;let C=Et(e,d,p,w,a);if(T+=w,b<V)break;if(!Lt(e,d)){c=!0;continue}if(y(d)>C){c=!0;continue}if(d.next===null)break;d=d.next,p=0}return T};var v=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],x=e=>{let t=v[0];for(let n=1;n<v.length&&e>=v[n].threshold;n+=1)t=v[n];return t},Q=e=>{for(let t=1;t<v.length;t+=1)if(e<v[t].threshold)return v[t].threshold;return Number.MAX_SAFE_INTEGER},Le=e=>{if(typeof e.compareKeys!="function")throw new f("compareKeys must be a function.");let t=e.autoScale===!0;if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new f("autoScale conflicts with explicit capacity.");let n,r;if(t){let i=x(0);n=i.maxLeaf,r=i.maxBranch}else n=F(e.maxLeafEntries,"maxLeafEntries",P),r=F(e.maxBranchChildren,"maxBranchChildren",_);let o=le(e.duplicateKeys),l=M([],null);return{compareKeys:e.compareKeys,maxLeafEntries:n,maxBranchChildren:r,duplicateKeys:o,minLeafEntries:Math.ceil(n/2),minBranchChildren:Math.ceil(r/2),root:l,leftmostLeaf:l,rightmostLeaf:l,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,_nextAutoScaleThreshold:t?Q(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:l,index:0}}},Y=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:n}=x(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=Math.ceil(t/2)),n>e.maxBranchChildren&&(e.maxBranchChildren=n,e.minBranchChildren=Math.ceil(n/2)),e._nextAutoScaleThreshold=Q(e.entryCount)},ee=(e,t,n)=>{if(!e.autoScale)return;let r=x(0),o=F(t,"maxLeafEntries",P),l=F(n,"maxBranchChildren",_);if(o<r.maxLeaf||l<r.maxBranch)throw new f("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=l,e.minLeafEntries=Math.ceil(o/2),e.minBranchChildren=Math.ceil(l/2)};var we=(e,t,n)=>{let r=[],o=0;for(;o<e;){let l=e-o;if(l>t&&l-t<n){let a=Math.ceil(l/2);r.push(o+a),r.push(e);break}let i=o+t<e?o+t:e;r.push(i),o=i}return r},wt=(e,t,n,r)=>{let o=we(t.length,e.maxLeafEntries,e.minLeafEntries),l=new Array(o.length),i=0;for(let a=0;a<o.length;a+=1){let T=o[a],c=new Array(T-i);for(let d=i;d<T;d+=1){let p=r+d;c[d-i]={key:t[d].key,entryId:p,value:t[d].value},n[d]=p,e.entryKeys!==null&&e.entryKeys.set(p,t[d].key)}l[a]=M(c,null),i=T}return l},Ne=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let n=e.nextSequence;if(n+t.length>Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");let r=new Array(t.length),o=wt(e,t,r,n);e.nextSequence=n+t.length,e.entryCount=t.length;for(let l=0;l<o.length;l+=1)l>0&&(o[l].prev=o[l-1]),l<o.length-1&&(o[l].next=o[l+1]);if(e.leftmostLeaf=o[0],e.rightmostLeaf=o[o.length-1],o.length===1)e.root=o[0];else{let l=o;for(;l.length>1;){let i=we(l.length,e.maxBranchChildren,e.minBranchChildren),a=new Array(i.length),T=0;for(let c=0;c<i.length;c+=1)a[c]=k(l.slice(T,i[c]),null),T=i[c];l=a}e.root=l[0]}return Y(e),r};var be=(e,t,n,r)=>{let o={key:void 0,sequence:0};if(!N(r,o))throw new u("inserted child has no min key");r.parent=t;let l=n.indexInParent-t.childOffset+1;se(t,l,r,o),O(t)>e.maxBranchChildren&&bt(e,t)},Nt=(e,t)=>{ye(t);let n=Math.ceil(t.entries.length/2),r={kind:S,entries:t.entries.splice(n),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=r:e.rightmostLeaf=r,t.next=r,t.parent===null){e.root=k([t,r],null);return}be(e,t.parent,t,r)},bt=(e,t)=>{q(t);let n=Math.ceil(t.children.length/2),r=k(t.children.splice(n),t.parent);if(t.keys.splice(n),t.parent===null){e.root=k([t,r],null);return}be(e,t.parent,t,r)},Se=(e,t,n,r)=>{let o=e.nextSequence,l=E(e,t,n,o);if(e.duplicateKeys!=="allow"){let i=null;if(l>0){let a=s(t,l-1);e.compareKeys(a.key,n)===0&&(i=a)}else if(t.prev!==null&&y(t.prev)>0){let a=t.prev,T=s(a,y(a)-1);e.compareKeys(T.key,n)===0&&(i=T)}if(i!==null){if(e.duplicateKeys==="reject")throw new f("Duplicate key rejected.");return i.value=r,i.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");return e.nextSequence+=1,ue(t,l,{key:n,entryId:o,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,n),l===0&&t.parent!==null&&L(t),y(t)>e.maxLeafEntries&&Nt(e,t),Y(e),o},ge=(e,t,n)=>{let r=K(e,t,e.nextSequence);return Se(e,r,t,n)},ve=e=>{if(e.entryCount===0)return null;let t=z(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),y(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&L(e.leftmostLeaf),e.leftmostLeaf!==e.root&&y(e.leftmostLeaf)<e.minLeafEntries&&g(e,e.leftmostLeaf),t},Ce=e=>{if(e.entryCount===0)return null;let t=ie(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&y(e.rightmostLeaf)<e.minLeafEntries&&g(e,e.rightmostLeaf),t},Ie=(e,t)=>{let n=A(e,t);if(n===null)return null;let r=n.leaf,o=n.index,l=s(r,o);return Z(r,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(l.entryId),o===0&&y(r)>0&&r.parent!==null&&L(r),r!==e.root&&y(r)<e.minLeafEntries&&g(e,r),l},te=(e,t,n)=>{let r=K(e,t,n),o=B(e,r,t,n);return o>=y(r)||s(r,o).entryId!==n?null:{leaf:r,index:o}},Me=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=te(e,n,t);if(r===null)return null;let o=s(r.leaf,r.index);return Z(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(t),r.index===0&&y(r.leaf)>0&&r.leaf.parent!==null&&L(r.leaf),r.leaf!==e.root&&y(r.leaf)<e.minLeafEntries&&g(e,r.leaf),o},ke=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=te(e,n,t);return r===null?null:s(r.leaf,r.index)},Oe=(e,t,n)=>{let r=e.entryKeys.get(t);if(r===void 0)return null;let o=te(e,r,t);if(o===null)return null;let l=s(o.leaf,o.index);return l.value=n,l},Ae=(e,t)=>{if(t.length===0)return[];let n=e.duplicateKeys!=="allow";for(let r=1;r<t.length;r+=1){let o=e.compareKeys(t[r-1].key,t[r].key);if(n?o>=0:o>0)throw new f(n?"putMany: not sorted in strict ascending order.":"putMany: not sorted in non-descending order.")}if(e.entryCount>0){let r=new Array(t.length),o=K(e,t[0].key,e.nextSequence);for(let l=0;l<t.length;l+=1){let i=t[l],a=ce(e,o,i.key,e.nextSequence);r[l]=Se(e,a,i.key,i.value),o=a}return r}return Ne(e,t)};var Pe=(e,t,n,r)=>{if(e.entryCount===0)return null;let o=e.compareKeys,l=o(t,n);if(l>0)return null;let i=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";if(i&&a&&l===0)return null;let T=i?Number.MAX_SAFE_INTEGER:0,c=K(e,t,T),d=i?E(e,c,t,Number.MAX_SAFE_INTEGER):B(e,c,t,0);return{leaf:c,index:d,compare:o,upperExclusive:a}},ne=(e,t,n,r)=>{let o=Pe(e,t,n,r);if(o===null)return 0;let l=o.leaf,i=o.index,{compare:a,upperExclusive:T}=o,c=0;for(;l!==null;){let d=y(l);if(i>=d){l=l.next,i=0;continue}let p=s(l,d-1),V=a(p.key,n);if(T?V<0:V<=0){c+=d-i,l=l.next,i=0;continue}let b=T?0:Number.MAX_SAFE_INTEGER,w=T?B(e,l,n,b):E(e,l,n,b),C=w<d?w:d;return c+=C-i,c}return c},St=200,gt=(e,t,n,r,o,l,i,a)=>{let T=y(t);if(T-n>=St&&t.next!==null){let d=s(t,T-1),p=r(d.key,i);if(o?p<0:p<=0){let V=ne(e,l,i,a);return new Array(V)}}return[]},Re=(e,t,n,r,o,l)=>{if(o)for(let i=t;i<n;i+=1)r[l++]=s(e,i);else for(let i=t;i<n;i+=1)r.push(s(e,i));return l},_e=(e,t,n,r)=>{let o=Pe(e,t,n,r);if(o===null)return[];let l=o.leaf,i=o.index,{compare:a,upperExclusive:T}=o,c=gt(e,l,i,a,T,t,n,r),d=0,p=c.length>0;for(;l!==null;){let V=y(l);if(i>=V){l=l.next,i=0;continue}let b=s(l,V-1),w=a(b.key,n);if(T?w<0:w<=0){d=Re(l,i,V,c,p,d),l=l.next,i=0;continue}let C=T?0:Number.MAX_SAFE_INTEGER,I=T?B(e,l,n,C):E(e,l,n,C),lt=I<V?I:V;return Re(l,i,lt,c,p,d),c}return c};var Fe=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},qe=e=>{let t=new Array(e.entryCount),n=e.leftmostLeaf,r=0;for(;n!==null;){let o=y(n);for(let l=0;l<o;l+=1){let i=s(n,l);t[r++]=[i.key,i.value]}n=n.next}return{version:1,config:{maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale},entries:t}},vt=1e6,Ct=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new f(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new f("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new f("BTreeJSON: entries must be array.");if(e.entries.length>vt)throw new f("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let n=e.entries[t];if(!Array.isArray(n)||n.length!==2)throw new f(`BTreeJSON: bad entries[${t}].`)}},It=e=>{let t=(n,r)=>{if(!Number.isInteger(r)||r<X||r>H)throw new f(`BTreeJSON: invalid ${n}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new f(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new f("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new f("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new f("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new f("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let n=x(0);if(e.maxLeafEntries<n.maxLeaf||e.maxBranchChildren<n.maxBranch)throw new f("BTreeJSON: autoScale capacity below tier-0.")}},De=e=>{Ct(e),It(e.config)},Je=(e,t)=>{let n=e.config,r={compareKeys:t,duplicateKeys:n.duplicateKeys,enableEntryIdLookup:n.enableEntryIdLookup,autoScale:n.autoScale};return n.autoScale||(r.maxLeafEntries=n.maxLeafEntries,r.maxBranchChildren=n.maxBranchChildren),r};var re=e=>{if(m(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},J=(e,t,n,r,o)=>{let l=e(t,r);return l!==0?l:n-o},Ue=e=>{if(m(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:Ue(e.children[e.children.length-1])},D=e=>{if(!Number.isFinite(e))throw new f("compareKeys must return a finite number.");return e},Mt=(e,t)=>{if(D(e(t,t))!==0)throw new f("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},kt=(e,t,n,r)=>{let o=Math.sign(D(e(t,n))),l=Math.sign(D(e(n,r)));if(o<0&&l<0&&Math.sign(D(e(t,r)))>=0)throw new f("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&l>0&&Math.sign(D(e(t,r)))<=0)throw new f("compareKeys must satisfy transitivity for observed key triples.")},Xe=(e,t)=>{try{Mt(e.compareKeys,t)}catch(n){throw n instanceof f?new u(n.message):n}},He=(e,t,n,r)=>{try{kt(e.compareKeys,t,n,r)}catch(o){throw o instanceof f?new u(o.message):o}},Ot=(e,t,n,r)=>{if(!m(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==n)throw new u("Leaf prev pointer mismatch.");if(n!==null&&m(n)){let o=Ue(n),l=re(t);if(o===null||l===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(J(e.compareKeys,o.key,o.sequence,l.key,l.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let i=y(n),a=y(t);if(e.duplicateKeys!=="allow"&&i>0&&a>0&&e.compareKeys(s(n,i-1).key,s(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},ze=(e,t)=>{if(e.entryCount===0){if(!m(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let n=new Set,r=e.leftmostLeaf,o=null,l=0;for(;r!==null;)Ot(e,r,o,n),n.add(r),o=r,r=r.next,l+=1;if(o!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(l!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var At=(e,t)=>{let n=y(t);for(let r=0;r<n;r+=1)Xe(e,s(t,r).key);for(let r=1;r<n;r+=1)if(J(e.compareKeys,s(t,r-1).key,s(t,r-1).entryId,s(t,r).key,s(t,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<n;r+=1)if(e.compareKeys(s(t,r-1).key,s(t,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<n;r+=1){let o=s(t,r-2),l=s(t,r-1),i=s(t,r);He(e,o.key,l.key,i.key)}if(n>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Rt=(e,t,n)=>{At(e,t);let r=y(t),o=e.autoScale?Math.ceil(x(0).maxLeaf/2):e.minLeafEntries;if(t!==e.root&&r<o)throw new u("Non-root leaf node violates minimum occupancy.");let l=r===0?null:s(t,0),i=r===0?null:s(t,r-1),a=l===null?null:{key:l.key,sequence:l.entryId},T=i===null?null:{key:i.key,sequence:i.entryId};return{minKey:a,maxKey:T,leafDepth:r===0?null:n,leafCount:1,branchCount:0,entryCount:r}},Pt=(e,t)=>{let n=t.children.length-t.childOffset;if(n===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(x(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&n<r)throw new u("Non-root branch node violates minimum occupancy.");if(n>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},_t=(e,t,n,r)=>{let o=t.children[n];if(o.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==n)throw new u("Child indexInParent does not match actual position in parent.");let l=Ge(e,o,r+1);if(l.minKey===null||l.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let i=t.keys[n],a=re(o);if(a===null||J(e.compareKeys,i.key,i.sequence,a.key,a.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return l},Ge=(e,t,n)=>{if(m(t))return Rt(e,t,n);Pt(e,t);let r=null,o=0,l=1,i=0,a=null,T=null,c=null;for(let d=t.childOffset;d<t.children.length;d+=1){let p=_t(e,t,d,n);if(r!==null&&p.leafDepth!==null&&p.leafDepth!==r)throw new u("Leaf depth mismatch detected in tree.");if(r===null&&p.leafDepth!==null&&(r=p.leafDepth),c!==null&&J(e.compareKeys,c.key,c.sequence,p.minKey.key,p.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");a===null&&(a=p.minKey),T=p.maxKey,c=p.maxKey,o+=p.leafCount,l+=p.branchCount,i+=p.entryCount}return{minKey:a,maxKey:T,leafDepth:r,leafCount:o,branchCount:l,entryCount:i}},We=e=>{let t=Ge(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ze(e,t.leafCount)};var $e=e=>{if(m(e))return{height:1,leafCount:1,branchCount:0};let t=0,n=0,r=1;for(let o=e.childOffset;o<e.children.length;o+=1){let l=e.children[o],i=$e(l);i.height>t&&(t=i.height),n+=i.leafCount,r+=i.branchCount}return{height:t+1,leafCount:n,branchCount:r}},Qe=e=>{let t=$e(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var R=class e{constructor(t){this.state=Le(t)}put(t,n){return ge(this.state,t,n)}putMany(t){return Ae(this.state,t)}remove(t){return Ie(this.state,t)}removeById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Me(this.state,t)}peekById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ke(this.state,t)}updateById(t,n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Oe(this.state,t,n)}popFirst(){return ve(this.state)}peekFirst(){return this.state.entryCount===0?null:s(this.state.leftmostLeaf,0)}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return s(t,y(t)-1)}popLast(){return Ce(this.state)}clear(){let t=M([],null);if(this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys!==null&&this.state.entryKeys.clear(),this.state.autoScale){let n=x(0);this.state.maxLeafEntries=n.maxLeaf,this.state.maxBranchChildren=n.maxBranch,this.state.minLeafEntries=Math.ceil(n.maxLeaf/2),this.state.minBranchChildren=Math.ceil(n.maxBranch/2),this.state._nextAutoScaleThreshold=Q(0)}}get(t){let n=A(this.state,t);return n===null?null:s(n.leaf,n.index).value}hasKey(t){return de(this.state,t)}findFirst(t){let n=A(this.state,t);return n===null?null:s(n.leaf,n.index)}findLast(t){let n=fe(this.state,t);return n===null?null:s(n.leaf,n.index)}nextHigherKey(t){return pe(this.state,t)}nextLowerKey(t){return he(this.state,t)}getPairOrNextLower(t){let n=me(this.state,t);return n===null?null:s(n.leaf,n.index)}count(t,n,r){return ne(this.state,t,n,r)}deleteRange(t,n,r){return Ee(this.state,t,n,r)}range(t,n,r){return _e(this.state,t,n,r)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield s(t,r);t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let n=y(t);for(let r=n-1;r>=0;r-=1)yield s(t,r);t=t.prev}}*keys(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield s(t,r).key;t=t.next}}*values(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield s(t,r).value;t=t.next}}[Symbol.iterator](){return this.entries()}forEach(t,n){let r=this.state.leftmostLeaf;for(;r!==null;){let o=y(r);for(let l=0;l<o;l+=1)t.call(n,s(r,l));r=r.next}}snapshot(){let t=new Array(this.state.entryCount),n=this.state.leftmostLeaf,r=0;for(;n!==null;){let o=y(n);for(let l=0;l<o;l+=1)t[r++]=s(n,l);n=n.next}return t}clone(){let t=new e(Fe(this.state));if(ee(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0){let n=new Array(this.state.entryCount),r=this.state.leftmostLeaf,o=0;for(;r!==null;){let l=y(r);for(let i=0;i<l;i+=1)n[o++]=s(r,i);r=r.next}t.putMany(n)}return t}toJSON(){return qe(this.state)}static fromJSON(t,n){De(t);let r=t.config.duplicateKeys!=="allow";for(let l=1;l<t.entries.length;l+=1){let i=n(t.entries[l-1][0],t.entries[l][0]);if(i>0)throw new f("fromJSON: entries not sorted.");if(r&&i===0)throw new f('fromJSON: duplicate keys require duplicateKeys "allow".')}let o=new e(Je(t,n));if(ee(o.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let l=new Array(t.entries.length);for(let i=0;i<t.entries.length;i+=1)l[i]={key:t.entries[i][0],value:t.entries[i][1]};o.putMany(l)}return o}size(){return this.state.entryCount}assertInvariants(){We(this.state)}getStats(){return Qe(this.state)}};var Ft=16,Ye=1024,qt=1e5,je=1e6,Ze=e=>{let t=e.autoScale===!0,n=t?x(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(n?n.maxLeaf:P),maxBranchChildren:e.maxBranchChildren??(n?n.maxBranch:_),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},et=e=>{let t=e;throw new h(`Unsupported mutation type from shared store: ${String(t.type)}`)},tt=e=>{if(e===void 0)return Ft;if(!Number.isInteger(e)||e<1||e>Ye)throw new h(`maxRetries: integer 1\u2013${Ye} required.`);return e},nt=e=>{if(e===void 0)return qt;if(!Number.isInteger(e)||e<1||e>je)throw new h(`maxSyncMutationsPerBatch: integer 1\u2013${je} required.`);return e},rt=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new h("readMode: must be 'strong' or 'local'.");return e};function ot(e,t){if(typeof t!="object"||t===null)throw new h("Store contract: append() must return {applied, version}.");let n=t;if(typeof n.applied!="boolean")throw new h("Store contract: applied must be boolean.");if(typeof n.version!="bigint")throw new h("Store contract: version must be bigint.");if(n.applied&&n.version<=e)throw new h("Store contract: applied version must exceed expected.");if(!n.applied&&n.version<e)throw new h("Store contract: rejected version must be >= expected.")}var U=class{constructor(t){this.store=t.store,this.maxRetries=tt(t.maxRetries),this.maxSyncMutationsPerBatch=nt(t.maxSyncMutationsPerBatch),this.duplicateKeys=t.duplicateKeys??"replace",this.readMode=rt(t.readMode),this.configFingerprint=Ze(t),this.tree=new R({compareKeys:t.compareKeys,maxLeafEntries:t.maxLeafEntries,maxBranchChildren:t.maxBranchChildren,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale}),this.currentVersion=0n,this.operationQueue=Promise.resolve(),this.initSeen=!1}async sync(){await this.runExclusive(async()=>{await this.syncUnlocked()})}async syncUnlocked(){let t=await this.store.getLogEntriesSince(this.currentVersion);if(typeof t.version!="bigint")throw new h("Store contract: version must be bigint.");if(!Array.isArray(t.mutations))throw new h("Store contract: mutations must be an array.");if(t.mutations.length>this.maxSyncMutationsPerBatch)throw new h(`Sync batch exceeded limit (${String(this.maxSyncMutationsPerBatch)}).`);if(!(t.version<=this.currentVersion)){for(let n of t.mutations)this.applyMutationLocal(n);this.currentVersion=t.version}}applyMutationLocal(t){switch(t.type){case"init":if(t.configFingerprint!==this.configFingerprint)throw new h("Config mismatch: store peers must share identical tree config.");return this.initSeen=!0,null;case"put":return this.tree.put(t.key,t.value);case"remove":return this.tree.remove(t.key);case"removeById":return this.tree.removeById(t.entryId);case"updateById":return this.tree.updateById(t.entryId,t.value);case"popFirst":return this.tree.popFirst();case"popLast":return this.tree.popLast();default:return et(t)}}runExclusive(t){let n=async()=>t(),r=this.operationQueue.then(n,n);return this.operationQueue=r.then(()=>{},()=>{}),r}readOp(t){return this.runExclusive(async()=>(this.readMode==="strong"&&await this.syncUnlocked(),t(this.tree)))}async appendMutationAndApplyUnlocked(t){for(let n=0;n<this.maxRetries;n+=1){await this.syncUnlocked();let r=t(this.tree);if(r===null)return null;let o=this.currentVersion,l=this.initSeen?[r]:[{type:"init",configFingerprint:this.configFingerprint},r],i=await this.store.append(o,l);if(ot(o,i),i.applied){for(let T of l){if(T===r)break;this.applyMutationLocal(T)}let a=this.applyMutationLocal(r);return this.currentVersion=i.version,a}}throw new h(`Mutation failed after ${String(this.maxRetries)} retries.`)}async put(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(r=>{if(this.duplicateKeys==="reject"&&r.hasKey(t))throw new f("Duplicate key rejected.");return{type:"put",key:t,value:n}}))}async remove(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(n=>n.hasKey(t)?{type:"remove",key:t}:null))}async removeById(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(n=>n.peekById(t)!==null?{type:"removeById",entryId:t}:null))}async updateById(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(r=>r.peekById(t)!==null?{type:"updateById",entryId:t,value:n}:null))}async popFirst(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(t=>t.peekFirst()!==null?{type:"popFirst"}:null))}async get(t){return this.readOp(n=>n.get(t))}async hasKey(t){return this.readOp(n=>n.hasKey(t))}async findFirst(t){return this.readOp(n=>n.findFirst(t))}async findLast(t){return this.readOp(n=>n.findLast(t))}async range(t,n,r){return this.readOp(o=>o.range(t,n,r))}async snapshot(){return this.readOp(t=>t.snapshot())}async size(){return this.readOp(t=>t.size())}async assertInvariants(){await this.readOp(t=>t.assertInvariants())}async getStats(){return this.readOp(t=>t.getStats())}async peekFirst(){return this.readOp(t=>t.peekFirst())}async peekLast(){return this.readOp(t=>t.peekLast())}async popLast(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(t=>t.peekLast()!==null?{type:"popLast"}:null))}async peekById(t){return this.readOp(n=>n.peekById(t))}async count(t,n,r){return this.readOp(o=>o.count(t,n,r))}async nextHigherKey(t){return this.readOp(n=>n.nextHigherKey(t))}async nextLowerKey(t){return this.readOp(n=>n.nextLowerKey(t))}async getPairOrNextLower(t){return this.readOp(n=>n.getPairOrNextLower(t))}};return Tt(Dt);})();
1
+ "use strict";var FrostpillarBTree=(()=>{var j=Object.defineProperty;var at=Object.getOwnPropertyDescriptor;var ut=Object.getOwnPropertyNames;var yt=Object.prototype.hasOwnProperty;var st=(e,t)=>{for(var n in t)j(e,n,{get:t[n],enumerable:!0})},Tt=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of ut(t))!yt.call(e,o)&&o!==n&&j(e,o,{get:()=>t[o],enumerable:!(r=at(t,o))||r.enumerable});return e};var ct=e=>Tt(j({},"__esModule",{value:!0}),e);var Jt={};st(Jt,{BTreeConcurrencyError:()=>h,BTreeInvariantError:()=>u,BTreeValidationError:()=>d,ConcurrentInMemoryBTree:()=>U,InMemoryBTree:()=>R});var d=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}},h=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var P=64,_=64,X=3,H=16384,g=0,dt=1,ie=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new d("Invalid duplicateKeys option.");return e},m=e=>e.kind===g,N=(e,t)=>{if(e.kind===g){if(e.entryOffset>=e.entries.length)return!1;let n=e.entries[e.entryOffset];return t.key=n.key,t.sequence=n.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},F=(e,t,n)=>{if(e===void 0)return n;if(!Number.isInteger(e)||e<X||e>H)throw new d(`${t}: integer ${X}\u2013${H} required.`);return e},M=(e,t)=>({kind:g,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),k=(e,t)=>{let n=[],r={kind:dt,children:e,keys:n,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let i=e[o];i.parent=r,i.indexInParent=o;let l={key:void 0,sequence:0};if(!N(i,l))throw new u("branch child has no min key");n.push(l)}return r},s=e=>e.entries.length-e.entryOffset,T=(e,t)=>e.entries[e.entryOffset+t],z=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},le=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},ae=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},Z=(e,t)=>{let n=e.entries.length-e.entryOffset,r=e.entryOffset+t;t<n-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},ue=(e,t,n)=>{let r=e.entryOffset+t;e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1?(e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=n):e.entries.splice(r,0,n)},ye=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},oe=1,q=e=>{if(e.childOffset>0){let t=e.childOffset<=oe?0:oe;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let n=t;n<e.children.length;n+=1)e.children[n].indexInParent=n}},O=e=>e.children.length-e.childOffset,se=(e,t,n,r)=>{let o=e.childOffset+t,i=e.children.length-e.childOffset;if(e.childOffset>0&&t<i>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,o),e.keys.copyWithin(e.childOffset-1,e.childOffset,o),e.childOffset-=1,e.children[o-1]=n,e.keys[o-1]=r;for(let l=e.childOffset;l<o;l+=1)e.children[l].indexInParent=l;n.indexInParent=o-1}else{e.children.splice(o,0,n),e.keys.splice(o,0,r);for(let l=o;l<e.children.length;l+=1)e.children[l].indexInParent=l}},Te=(e,t)=>{let n=t-e.childOffset,r=e.children.length-e.childOffset;if(n<r-1-n){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let o=e.childOffset;o<=t;o+=1)e.children[o].indexInParent=o;e.childOffset>=e.children.length>>>1&&q(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let o=t;o<e.children.length;o+=1)e.children[o].indexInParent=o}};var ft=(e,t,n,r)=>{let o=t.childOffset;if(o>=t.children.length)throw new u("branch has no children");let i=o,l=o,a=t.keys.length-1;for(;l<=a;){let y=l+a>>>1,c=t.keys[y],f=e(c.key,n);(f!==0?f:c.sequence-r)<=0?(i=y,l=y+1):a=y-1}return t.children[i]},K=(e,t,n)=>{let r=e.compareKeys,o=e.root;for(;o.kind!==g;)o=ft(r,o,t,n);return o},B=(e,t,n,r)=>{let o=e.compareKeys,i=t.entryOffset,l=t.entries.length;for(;i<l;){let a=i+l>>>1,y=t.entries[a],c=o(y.key,n);(c!==0?c:y.entryId-r)<0?i=a+1:l=a}return i-t.entryOffset},E=(e,t,n,r)=>{let o=e.compareKeys,i=t.entryOffset,l=t.entries.length;for(;i<l;){let a=i+l>>>1,y=t.entries[a],c=o(y.key,n);(c!==0?c:y.entryId-r)<=0?i=a+1:l=a}return i-t.entryOffset},ce=(e,t,n,r)=>{let o=e.compareKeys,i=t,l=32-Math.clz32(e.entryCount+1);for(;l>0&&i.next!==null&&s(i.next)>0;){let a=T(i.next,0),y=o(a.key,n);if(y>0||y===0&&a.entryId>r)break;i=i.next,l-=1}if(l===0&&i.next!==null&&s(i.next)>0){let a=T(i.next,0),y=o(a.key,n);if(y<0||y===0&&a.entryId<=r)return K(e,n,r)}return i},A=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,0),r=B(e,n,t,0);if(r>=s(n)&&(n.next===null||(n=n.next,r=B(e,n,t,0),r>=s(n)))||e.compareKeys(T(n,r).key,t)!==0)return null;let o=e._cursor;return o.leaf=n,o.index=r,o},de=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,Number.MAX_SAFE_INTEGER),r=E(e,n,t,Number.MAX_SAFE_INTEGER);if(r===0&&(n.prev===null||(n=n.prev,r=s(n),r===0))||(r-=1,e.compareKeys(T(n,r).key,t)!==0))return null;let o=e._cursor;return o.leaf=n,o.index=r,o},fe=(e,t)=>A(e,t)!==null,pe=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,Number.MAX_SAFE_INTEGER),o=E(e,r,t,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(o<s(r)){let i=T(r,o);if(n(i.key,t)>0)return i.key;o+=1}else r=r.next,o=0;return null},he=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),o=B(e,r,t,0),i=r,l=o-1;for(;i!==null;){for(;l>=0;){let a=T(i,l);if(n(a.key,t)<0)return a.key;l-=1}i=i.prev,i!==null&&(l=s(i)-1)}return null},me=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),o=B(e,r,t,0),i=e._cursor;if(o<s(r)){if(n(T(r,o).key,t)===0)return i.leaf=r,i.index=o,i}else if(r.next!==null){let l=B(e,r.next,t,0);if(l<s(r.next)&&n(T(r.next,l).key,t)===0)return i.leaf=r.next,i.index=l,i}if(o>0)return i.leaf=r,i.index=o-1,i;if(r.prev!==null){let l=s(r.prev);if(l>0)return i.leaf=r.prev,i.index=l-1,i}return null};var L=e=>{let t=e;for(;t.parent!==null;){let n=t.indexInParent;if(!N(t,t.parent.keys[n])||n!==t.parent.childOffset)return;t=t.parent}},$=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},Ke=e=>{if(!m(e))throw new u("expected leaf, got branch");return e},Be=e=>{if(m(e))throw new u("expected branch, got leaf");return e},G=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");Te(e,t)},Ve=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},pt=(e,t,n)=>{let r=t.children.pop();if(r===void 0)throw new u("left branch borrow failed");t.keys.pop(),r.parent=e;let o={key:void 0,sequence:0};if(!N(r,o))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=o,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(o);for(let l=0;l<e.children.length;l+=1)e.children[l].indexInParent=l}let i=$(e);i.keys[n]={key:o.key,sequence:o.sequence},L(e)},ht=(e,t,n)=>{let r=t.childOffset;if(r>=t.children.length)throw new u("right branch borrow failed");let o=t.children[r];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&q(t),e.children.push(o),o.parent=e;let i={key:void 0,sequence:0};if(!N(o,i))throw new u("borrowed child has no min key");e.keys.push(i),o.indexInParent=e.children.length-1;let l=$(e);N(t,l.keys[n+1])},mt=(e,t,n,r)=>{for(let i=t.childOffset;i<t.children.length;i+=1){let l=t.children[i];l.parent=n,l.indexInParent=n.children.length,n.children.push(l),n.keys.push(t.keys[i])}let o=$(t);G(o,r),W(e,o)},Kt=(e,t,n,r)=>{for(let i=n.childOffset;i<n.children.length;i+=1){let l=n.children[i];l.parent=t,l.indexInParent=t.children.length,t.children.push(l),t.keys.push(n.keys[i])}let o=$(t);G(o,r+1),W(e,o)},Bt=(e,t)=>{let n=t>e.childOffset?Be(e.children[t-1]):null,r=t+1<e.children.length?Be(e.children[t+1]):null;return{left:n,right:r}},W=(e,t)=>{let n=O(t);if(t===e.root){if(n===1){let a=t.children[t.childOffset];a.parent=null,e.root=a,m(a)&&(e.leftmostLeaf=a,e.rightmostLeaf=a)}return}if(n>=e.minBranchChildren)return;let r=t.parent;if(r===null)throw new u("branch has no parent");let o=t.indexInParent,{left:i,right:l}=Bt(r,o);if(l!==null&&O(l)>e.minBranchChildren){ht(t,l,o);return}if(i!==null&&O(i)>e.minBranchChildren){pt(t,i,o);return}if(i!==null){mt(e,t,i,o);return}if(l!==null){Kt(e,t,l,o);return}throw new u("no branch siblings to rebalance")};var xe=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0);let n=t.entries;for(let r=t.entryOffset;r<n.length;r+=1)e.entries.push(n[r])},v=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(s(t)>=e.minLeafEntries)return;let n=t.parent;if(n===null)throw new u("Leaf node has no parent during rebalance.");let r=t.indexInParent,o=r>n.childOffset?Ke(n.children[r-1]):null,i=r+1<n.children.length?Ke(n.children[r+1]):null;if(i!==null&&s(i)>e.minLeafEntries){let l=z(i);if(l===void 0)throw new u("right leaf borrow failed");t.entries.push(l),N(i,n.keys[r+1]);return}if(o!==null&&s(o)>e.minLeafEntries){let l=o.entries.pop();if(l===void 0)throw new u("left leaf borrow failed");ae(t,l),n.keys[r]={key:l.key,sequence:l.entryId},L(t);return}if(o!==null){xe(o,t),Ve(e,t),G(n,r),W(e,n);return}if(i!==null){xe(t,i),Ve(e,i),G(n,r+1),W(e,n);return}throw new u("no leaf siblings to rebalance")};var Vt=(e,t,n)=>{let r=n?Number.MAX_SAFE_INTEGER:0,o=K(e,t,r),i=n?E(e,o,t,Number.MAX_SAFE_INTEGER):B(e,o,t,0);return i>=s(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:i}},xt=(e,t,n,r,o)=>{let i=s(t),l=n;for(;l<i;){let a=T(t,l),y=e.compareKeys(a.key,r);if(o?y>=0:y>0)break;l+=1}return l},Et=e=>{let t=0,n=e.root;for(;!m(n);)n=n.children[n.childOffset],t+=1;return t},Lt=(e,t,n,r,o)=>{if(e.entryKeys!==null)for(let c=n;c<n+r;c+=1)e.entryKeys.delete(T(t,c).entryId);let i=t.entryOffset+n;t.entries.copyWithin(i,i+r),t.entries.length-=r,e.entryCount-=r;let l=s(t)===0;n===0&&!l&&t.parent!==null&&L(t);let a=s(t),y=o;for(;y>0&&t!==e.root&&s(t)<e.minLeafEntries&&(v(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)y-=1;return l&&s(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&L(t),a},wt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,Ee=(e,t,n,r)=>{if(e.entryCount===0)return 0;let o=e.compareKeys(t,n);if(o>0)return 0;let i=r?.lowerBound==="exclusive",l=r?.upperBound==="exclusive";if(i&&l&&o===0)return 0;let a=Et(e),y=0,c=!0,f=null,p=0;for(;e.entryCount>0;){if(c){let C=Vt(e,t,i);if(C===null)break;f=C.leaf,p=C.idx,c=!1}if(p>=s(f))break;let V=s(f),b=xt(e,f,p,n,l),w=b-p;if(w===0)break;let I=Lt(e,f,p,w,a);if(y+=w,b<V)break;if(!wt(e,f)){c=!0;continue}if(s(f)>I){c=!0;continue}if(f.next===null)break;f=f.next,p=0}return y};var S=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],x=e=>{let t=S[0];for(let n=1;n<S.length&&e>=S[n].threshold;n+=1)t=S[n];return t},Q=e=>{for(let t=1;t<S.length;t+=1)if(e<S[t].threshold)return S[t].threshold;return Number.MAX_SAFE_INTEGER},Le=e=>{if(typeof e.compareKeys!="function")throw new d("compareKeys must be a function.");let t=e.autoScale===!0;if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new d("autoScale conflicts with explicit capacity.");let n,r;if(t){let l=x(0);n=l.maxLeaf,r=l.maxBranch}else n=F(e.maxLeafEntries,"maxLeafEntries",P),r=F(e.maxBranchChildren,"maxBranchChildren",_);let o=ie(e.duplicateKeys),i=M([],null);return{compareKeys:e.compareKeys,maxLeafEntries:n,maxBranchChildren:r,duplicateKeys:o,minLeafEntries:Math.ceil(n/2),minBranchChildren:Math.ceil(r/2),root:i,leftmostLeaf:i,rightmostLeaf:i,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,_nextAutoScaleThreshold:t?Q(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:i,index:0}}},Y=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:n}=x(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=Math.ceil(t/2)),n>e.maxBranchChildren&&(e.maxBranchChildren=n,e.minBranchChildren=Math.ceil(n/2)),e._nextAutoScaleThreshold=Q(e.entryCount)},ee=(e,t,n)=>{if(!e.autoScale)return;let r=x(0),o=F(t,"maxLeafEntries",P),i=F(n,"maxBranchChildren",_);if(o<r.maxLeaf||i<r.maxBranch)throw new d("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=i,e.minLeafEntries=Math.ceil(o/2),e.minBranchChildren=Math.ceil(i/2)};var we=(e,t,n)=>{let r=[],o=0;for(;o<e;){let i=e-o;if(i>t&&i-t<n){let a=Math.ceil(i/2);r.push(o+a),r.push(e);break}let l=o+t<e?o+t:e;r.push(l),o=l}return r},Nt=(e,t,n,r)=>{let o=we(t.length,e.maxLeafEntries,e.minLeafEntries),i=new Array(o.length),l=0;for(let a=0;a<o.length;a+=1){let y=o[a],c=new Array(y-l);for(let f=l;f<y;f+=1){let p=r+f;c[f-l]={key:t[f].key,entryId:p,value:t[f].value},n[f]=p,e.entryKeys!==null&&e.entryKeys.set(p,t[f].key)}i[a]=M(c,null),l=y}return i},Ne=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let n=e.nextSequence;if(n+t.length>Number.MAX_SAFE_INTEGER)throw new d("Sequence overflow.");let r=new Array(t.length),o=Nt(e,t,r,n);e.nextSequence=n+t.length,e.entryCount=t.length;for(let i=0;i<o.length;i+=1)i>0&&(o[i].prev=o[i-1]),i<o.length-1&&(o[i].next=o[i+1]);if(e.leftmostLeaf=o[0],e.rightmostLeaf=o[o.length-1],o.length===1)e.root=o[0];else{let i=o;for(;i.length>1;){let l=we(i.length,e.maxBranchChildren,e.minBranchChildren),a=new Array(l.length),y=0;for(let c=0;c<l.length;c+=1)a[c]=k(i.slice(y,l[c]),null),y=l[c];i=a}e.root=i[0]}return Y(e),r};var be=(e,t,n,r)=>{let o={key:void 0,sequence:0};if(!N(r,o))throw new u("inserted child has no min key");r.parent=t;let i=n.indexInParent-t.childOffset+1;se(t,i,r,o),O(t)>e.maxBranchChildren&&gt(e,t)},bt=(e,t)=>{ye(t);let n=Math.ceil(t.entries.length/2),r={kind:g,entries:t.entries.splice(n),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=r:e.rightmostLeaf=r,t.next=r,t.parent===null){e.root=k([t,r],null);return}be(e,t.parent,t,r)},gt=(e,t)=>{q(t);let n=Math.ceil(t.children.length/2),r=k(t.children.splice(n),t.parent);if(t.keys.splice(n),t.parent===null){e.root=k([t,r],null);return}be(e,t.parent,t,r)},ge=(e,t,n,r)=>{let o=e.nextSequence,i=E(e,t,n,o);if(e.duplicateKeys!=="allow"){let l=null;if(i>0){let a=T(t,i-1);e.compareKeys(a.key,n)===0&&(l=a)}else if(t.prev!==null&&s(t.prev)>0){let a=t.prev,y=T(a,s(a)-1);e.compareKeys(y.key,n)===0&&(l=y)}if(l!==null){if(e.duplicateKeys==="reject")throw new d("Duplicate key rejected.");return l.value=r,l.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new d("Sequence overflow.");return e.nextSequence+=1,ue(t,i,{key:n,entryId:o,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,n),i===0&&t.parent!==null&&L(t),s(t)>e.maxLeafEntries&&bt(e,t),Y(e),o},ve=(e,t,n)=>{let r=K(e,t,e.nextSequence);return ge(e,r,t,n)},Se=e=>{if(e.entryCount===0)return null;let t=z(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),s(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&L(e.leftmostLeaf),e.leftmostLeaf!==e.root&&s(e.leftmostLeaf)<e.minLeafEntries&&v(e,e.leftmostLeaf),t},Ie=e=>{if(e.entryCount===0)return null;let t=le(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&s(e.rightmostLeaf)<e.minLeafEntries&&v(e,e.rightmostLeaf),t},Ce=(e,t)=>{let n=A(e,t);if(n===null)return null;let r=n.leaf,o=n.index,i=T(r,o);return Z(r,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(i.entryId),o===0&&s(r)>0&&r.parent!==null&&L(r),r!==e.root&&s(r)<e.minLeafEntries&&v(e,r),i},te=(e,t,n)=>{let r=K(e,t,n),o=B(e,r,t,n);return o>=s(r)||T(r,o).entryId!==n?null:{leaf:r,index:o}},Me=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=te(e,n,t);if(r===null)return null;let o=T(r.leaf,r.index);return Z(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(t),r.index===0&&s(r.leaf)>0&&r.leaf.parent!==null&&L(r.leaf),r.leaf!==e.root&&s(r.leaf)<e.minLeafEntries&&v(e,r.leaf),o},ke=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=te(e,n,t);return r===null?null:T(r.leaf,r.index)},Oe=(e,t,n)=>{let r=e.entryKeys.get(t);if(r===void 0)return null;let o=te(e,r,t);if(o===null)return null;let i=T(o.leaf,o.index);return i.value=n,i},Ae=(e,t)=>{if(t.length===0)return[];let n=e.duplicateKeys!=="allow";for(let r=1;r<t.length;r+=1){let o=e.compareKeys(t[r-1].key,t[r].key);if(n?o>=0:o>0)throw new d(n?"putMany: not sorted in strict ascending order.":"putMany: not sorted in non-descending order.")}if(e.entryCount>0){let r=new Array(t.length),o=K(e,t[0].key,e.nextSequence);for(let i=0;i<t.length;i+=1){let l=t[i],a=ce(e,o,l.key,e.nextSequence);r[i]=ge(e,a,l.key,l.value),o=a}return r}return Ne(e,t)};var Pe=(e,t,n,r)=>{if(e.entryCount===0)return null;let o=e.compareKeys,i=o(t,n);if(i>0)return null;let l=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";if(l&&a&&i===0)return null;let y=l?Number.MAX_SAFE_INTEGER:0,c=K(e,t,y),f=l?E(e,c,t,Number.MAX_SAFE_INTEGER):B(e,c,t,0);return{leaf:c,index:f,compare:o,upperExclusive:a}},ne=(e,t,n,r)=>{let o=Pe(e,t,n,r);if(o===null)return 0;let i=o.leaf,l=o.index,{compare:a,upperExclusive:y}=o,c=0;for(;i!==null;){let f=s(i);if(l>=f){i=i.next,l=0;continue}let p=T(i,f-1),V=a(p.key,n);if(y?V<0:V<=0){c+=f-l,i=i.next,l=0;continue}let b=y?0:Number.MAX_SAFE_INTEGER,w=y?B(e,i,n,b):E(e,i,n,b),I=w<f?w:f;return c+=I-l,c}return c},vt=200,St=(e,t,n,r,o,i,l,a)=>{let y=s(t);if(y-n>=vt&&t.next!==null){let f=T(t,y-1),p=r(f.key,l);if(o?p<0:p<=0){let V=ne(e,i,l,a);return new Array(V)}}return[]},Re=(e,t,n,r,o,i)=>{if(o)for(let l=t;l<n;l+=1)r[i++]=T(e,l);else for(let l=t;l<n;l+=1)r.push(T(e,l));return i},_e=(e,t,n,r)=>{let o=Pe(e,t,n,r);if(o===null)return[];let i=o.leaf,l=o.index,{compare:a,upperExclusive:y}=o,c=St(e,i,l,a,y,t,n,r),f=0,p=c.length>0;for(;i!==null;){let V=s(i);if(l>=V){i=i.next,l=0;continue}let b=T(i,V-1),w=a(b.key,n);if(y?w<0:w<=0){f=Re(i,l,V,c,p,f),i=i.next,l=0;continue}let I=y?0:Number.MAX_SAFE_INTEGER,C=y?B(e,i,n,I):E(e,i,n,I),lt=C<V?C:V;return Re(i,l,lt,c,p,f),c}return c};var Fe=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},qe=e=>{let t=new Array(e.entryCount),n=e.leftmostLeaf,r=0;for(;n!==null;){let o=s(n);for(let i=0;i<o;i+=1){let l=T(n,i);t[r++]=[l.key,l.value]}n=n.next}return{version:1,config:{maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale},entries:t}},It=1e6,Ct=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new d(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new d("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new d("BTreeJSON: entries must be array.");if(e.entries.length>It)throw new d("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let n=e.entries[t];if(!Array.isArray(n)||n.length!==2)throw new d(`BTreeJSON: bad entries[${t}].`)}},Mt=e=>{let t=(n,r)=>{if(!Number.isInteger(r)||r<X||r>H)throw new d(`BTreeJSON: invalid ${n}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new d(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new d("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new d("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new d("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new d("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let n=x(0);if(e.maxLeafEntries<n.maxLeaf||e.maxBranchChildren<n.maxBranch)throw new d("BTreeJSON: autoScale capacity below tier-0.")}},De=e=>{Ct(e),Mt(e.config)},Je=(e,t)=>{let n=e.config,r={compareKeys:t,duplicateKeys:n.duplicateKeys,enableEntryIdLookup:n.enableEntryIdLookup,autoScale:n.autoScale};return n.autoScale||(r.maxLeafEntries=n.maxLeafEntries,r.maxBranchChildren=n.maxBranchChildren),r};var re=e=>{if(m(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},J=(e,t,n,r,o)=>{let i=e(t,r);return i!==0?i:n-o},Ue=e=>{if(m(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:Ue(e.children[e.children.length-1])},D=e=>{if(!Number.isFinite(e))throw new d("compareKeys must return a finite number.");return e},kt=(e,t)=>{if(D(e(t,t))!==0)throw new d("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Ot=(e,t,n,r)=>{let o=Math.sign(D(e(t,n))),i=Math.sign(D(e(n,r)));if(o<0&&i<0&&Math.sign(D(e(t,r)))>=0)throw new d("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&i>0&&Math.sign(D(e(t,r)))<=0)throw new d("compareKeys must satisfy transitivity for observed key triples.")},Xe=(e,t)=>{try{kt(e.compareKeys,t)}catch(n){throw n instanceof d?new u(n.message):n}},He=(e,t,n,r)=>{try{Ot(e.compareKeys,t,n,r)}catch(o){throw o instanceof d?new u(o.message):o}},At=(e,t,n,r)=>{if(!m(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==n)throw new u("Leaf prev pointer mismatch.");if(n!==null&&m(n)){let o=Ue(n),i=re(t);if(o===null||i===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(J(e.compareKeys,o.key,o.sequence,i.key,i.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let l=s(n),a=s(t);if(e.duplicateKeys!=="allow"&&l>0&&a>0&&e.compareKeys(T(n,l-1).key,T(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},ze=(e,t)=>{if(e.entryCount===0){if(!m(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let n=new Set,r=e.leftmostLeaf,o=null,i=0;for(;r!==null;)At(e,r,o,n),n.add(r),o=r,r=r.next,i+=1;if(o!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(i!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var Rt=(e,t)=>{let n=s(t);for(let r=0;r<n;r+=1)Xe(e,T(t,r).key);for(let r=1;r<n;r+=1)if(J(e.compareKeys,T(t,r-1).key,T(t,r-1).entryId,T(t,r).key,T(t,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<n;r+=1)if(e.compareKeys(T(t,r-1).key,T(t,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<n;r+=1){let o=T(t,r-2),i=T(t,r-1),l=T(t,r);He(e,o.key,i.key,l.key)}if(n>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Pt=(e,t,n)=>{Rt(e,t);let r=s(t),o=e.autoScale?Math.ceil(x(0).maxLeaf/2):e.minLeafEntries;if(t!==e.root&&r<o)throw new u("Non-root leaf node violates minimum occupancy.");let i=r===0?null:T(t,0),l=r===0?null:T(t,r-1),a=i===null?null:{key:i.key,sequence:i.entryId},y=l===null?null:{key:l.key,sequence:l.entryId};return{minKey:a,maxKey:y,leafDepth:r===0?null:n,leafCount:1,branchCount:0,entryCount:r}},_t=(e,t)=>{let n=t.children.length-t.childOffset;if(n===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(x(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&n<r)throw new u("Non-root branch node violates minimum occupancy.");if(n>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},Ft=(e,t,n,r)=>{let o=t.children[n];if(o.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==n)throw new u("Child indexInParent does not match actual position in parent.");let i=Ge(e,o,r+1);if(i.minKey===null||i.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let l=t.keys[n],a=re(o);if(a===null||J(e.compareKeys,l.key,l.sequence,a.key,a.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return i},Ge=(e,t,n)=>{if(m(t))return Pt(e,t,n);_t(e,t);let r=null,o=0,i=1,l=0,a=null,y=null,c=null;for(let f=t.childOffset;f<t.children.length;f+=1){let p=Ft(e,t,f,n);if(r!==null&&p.leafDepth!==null&&p.leafDepth!==r)throw new u("Leaf depth mismatch detected in tree.");if(r===null&&p.leafDepth!==null&&(r=p.leafDepth),c!==null&&J(e.compareKeys,c.key,c.sequence,p.minKey.key,p.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");a===null&&(a=p.minKey),y=p.maxKey,c=p.maxKey,o+=p.leafCount,i+=p.branchCount,l+=p.entryCount}return{minKey:a,maxKey:y,leafDepth:r,leafCount:o,branchCount:i,entryCount:l}},We=e=>{let t=Ge(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ze(e,t.leafCount)};var $e=e=>{if(m(e))return{height:1,leafCount:1,branchCount:0};let t=0,n=0,r=1;for(let o=e.childOffset;o<e.children.length;o+=1){let i=e.children[o],l=$e(i);l.height>t&&(t=l.height),n+=l.leafCount,r+=l.branchCount}return{height:t+1,leafCount:n,branchCount:r}},Qe=e=>{let t=$e(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var R=class e{constructor(t){this.state=Le(t)}put(t,n){return ve(this.state,t,n)}putMany(t){return Ae(this.state,t)}remove(t){return Ce(this.state,t)}removeById(t){if(this.state.entryKeys===null)throw new d("Requires enableEntryIdLookup: true.");return Me(this.state,t)}peekById(t){if(this.state.entryKeys===null)throw new d("Requires enableEntryIdLookup: true.");return ke(this.state,t)}updateById(t,n){if(this.state.entryKeys===null)throw new d("Requires enableEntryIdLookup: true.");return Oe(this.state,t,n)}popFirst(){return Se(this.state)}peekFirst(){return this.state.entryCount===0?null:T(this.state.leftmostLeaf,0)}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return T(t,s(t)-1)}popLast(){return Ie(this.state)}clear(){let t=M([],null);if(this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys!==null&&this.state.entryKeys.clear(),this.state.autoScale){let n=x(0);this.state.maxLeafEntries=n.maxLeaf,this.state.maxBranchChildren=n.maxBranch,this.state.minLeafEntries=Math.ceil(n.maxLeaf/2),this.state.minBranchChildren=Math.ceil(n.maxBranch/2),this.state._nextAutoScaleThreshold=Q(0)}}get(t){let n=A(this.state,t);return n===null?null:T(n.leaf,n.index).value}hasKey(t){return fe(this.state,t)}findFirst(t){let n=A(this.state,t);return n===null?null:T(n.leaf,n.index)}findLast(t){let n=de(this.state,t);return n===null?null:T(n.leaf,n.index)}nextHigherKey(t){return pe(this.state,t)}nextLowerKey(t){return he(this.state,t)}getPairOrNextLower(t){let n=me(this.state,t);return n===null?null:T(n.leaf,n.index)}count(t,n,r){return ne(this.state,t,n,r)}deleteRange(t,n,r){return Ee(this.state,t,n,r)}range(t,n,r){return _e(this.state,t,n,r)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=s(t);for(let r=0;r<n;r+=1)yield T(t,r);t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let n=s(t);for(let r=n-1;r>=0;r-=1)yield T(t,r);t=t.prev}}*keys(){for(let t of this.entries())yield t.key}*values(){for(let t of this.entries())yield t.value}[Symbol.iterator](){return this.entries()}forEach(t,n){let r=this.state.leftmostLeaf;for(;r!==null;){let o=s(r);for(let i=0;i<o;i+=1)t.call(n,T(r,i));r=r.next}}snapshot(){let t=new Array(this.state.entryCount),n=this.state.leftmostLeaf,r=0;for(;n!==null;){let o=s(n);for(let i=0;i<o;i+=1)t[r++]=T(n,i);n=n.next}return t}clone(){let t=new e(Fe(this.state));if(ee(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0){let n=new Array(this.state.entryCount),r=this.state.leftmostLeaf,o=0;for(;r!==null;){let i=s(r);for(let l=0;l<i;l+=1)n[o++]=T(r,l);r=r.next}t.putMany(n)}return t}toJSON(){return qe(this.state)}static fromJSON(t,n){De(t);let r=t.config.duplicateKeys!=="allow";for(let i=1;i<t.entries.length;i+=1){let l=n(t.entries[i-1][0],t.entries[i][0]);if(l>0)throw new d("fromJSON: entries not sorted.");if(r&&l===0)throw new d('fromJSON: duplicate keys require duplicateKeys "allow".')}let o=new e(Je(t,n));if(ee(o.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let i=new Array(t.entries.length);for(let l=0;l<t.entries.length;l+=1)i[l]={key:t.entries[l][0],value:t.entries[l][1]};o.putMany(i)}return o}size(){return this.state.entryCount}assertInvariants(){We(this.state)}getStats(){return Qe(this.state)}};var qt=16,Ye=1024,Dt=1e5,je=1e6,Ze=e=>{let t=e.autoScale===!0,n=t?x(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(n?n.maxLeaf:P),maxBranchChildren:e.maxBranchChildren??(n?n.maxBranch:_),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},et=e=>{let t=e;throw new h(`Unsupported mutation type from shared store: ${String(t.type)}`)},tt=(e,t)=>{for(let n of e){if(typeof n!="object"||n===null)throw new h("Malformed mutation: expected an object.");let r=n;switch(r.type){case"init":if(typeof r.configFingerprint!="string")throw new h("Malformed init mutation: missing configFingerprint.");if(t!==void 0&&r.configFingerprint!==t)throw new h("Config mismatch: store peers must share identical tree config.");break;case"put":if(!("key"in r)||!("value"in r))throw new h("Malformed put mutation: missing key or value.");break;case"remove":if(!("key"in r))throw new h("Malformed remove mutation: missing key.");break;case"removeById":if(!("entryId"in r))throw new h("Malformed removeById mutation: missing entryId.");break;case"updateById":if(!("entryId"in r)||!("value"in r))throw new h("Malformed updateById mutation: missing entryId or value.");break;case"popFirst":case"popLast":break;default:throw new h(`Unsupported mutation type from shared store: ${String(r.type)}`)}}},nt=e=>{if(e===void 0)return qt;if(!Number.isInteger(e)||e<1||e>Ye)throw new h(`maxRetries: integer 1\u2013${Ye} required.`);return e},rt=e=>{if(e===void 0)return Dt;if(!Number.isInteger(e)||e<1||e>je)throw new h(`maxSyncMutationsPerBatch: integer 1\u2013${je} required.`);return e},ot=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new h("readMode: must be 'strong' or 'local'.");return e};function it(e,t){if(typeof t!="object"||t===null)throw new h("Store contract: append() must return {applied, version}.");let n=t;if(typeof n.applied!="boolean")throw new h("Store contract: applied must be boolean.");if(typeof n.version!="bigint")throw new h("Store contract: version must be bigint.");if(n.applied&&n.version<=e)throw new h("Store contract: applied version must exceed expected.");if(!n.applied&&n.version<e)throw new h("Store contract: rejected version must be >= expected.")}var U=class{constructor(t){this.store=t.store,this.maxRetries=nt(t.maxRetries),this.maxSyncMutationsPerBatch=rt(t.maxSyncMutationsPerBatch),this.duplicateKeys=t.duplicateKeys??"replace",this.readMode=ot(t.readMode),this.configFingerprint=Ze(t),this.tree=new R({compareKeys:t.compareKeys,maxLeafEntries:t.maxLeafEntries,maxBranchChildren:t.maxBranchChildren,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale}),this.currentVersion=0n,this.operationQueue=Promise.resolve(),this.initSeen=!1}async sync(){await this.runExclusive(async()=>{await this.syncUnlocked()})}async syncUnlocked(){let t=await this.store.getLogEntriesSince(this.currentVersion);if(typeof t.version!="bigint")throw new h("Store contract: version must be bigint.");if(!Array.isArray(t.mutations))throw new h("Store contract: mutations must be an array.");if(t.mutations.length>this.maxSyncMutationsPerBatch)throw new h(`Sync batch exceeded limit (${String(this.maxSyncMutationsPerBatch)}).`);if(!(t.version<=this.currentVersion)){tt(t.mutations,this.configFingerprint);for(let n of t.mutations)this.applyMutationLocal(n);this.currentVersion=t.version}}applyMutationLocal(t){switch(t.type){case"init":return this.initSeen=!0,null;case"put":return this.tree.put(t.key,t.value);case"remove":return this.tree.remove(t.key);case"removeById":return this.tree.removeById(t.entryId);case"updateById":return this.tree.updateById(t.entryId,t.value);case"popFirst":return this.tree.popFirst();case"popLast":return this.tree.popLast();default:return et(t)}}runExclusive(t){let n=async()=>t(),r=this.operationQueue.then(n,n);return this.operationQueue=r.then(()=>{},()=>{}),r}readOp(t){return this.runExclusive(async()=>(this.readMode==="strong"&&await this.syncUnlocked(),t(this.tree)))}async appendMutationAndApplyUnlocked(t){for(let n=0;n<this.maxRetries;n+=1){await this.syncUnlocked();let r=t(this.tree);if(r===null)return null;let o=this.currentVersion,i=this.initSeen?[r]:[{type:"init",configFingerprint:this.configFingerprint},r],l=await this.store.append(o,i);if(it(o,l),l.applied){for(let y of i){if(y===r)break;this.applyMutationLocal(y)}let a=this.applyMutationLocal(r);return this.currentVersion=l.version,a}}throw new h(`Mutation failed after ${String(this.maxRetries)} retries.`)}async put(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(r=>{if(this.duplicateKeys==="reject"&&r.hasKey(t))throw new d("Duplicate key rejected.");return{type:"put",key:t,value:n}}))}async remove(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(n=>n.hasKey(t)?{type:"remove",key:t}:null))}async removeById(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(n=>n.peekById(t)!==null?{type:"removeById",entryId:t}:null))}async updateById(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(r=>r.peekById(t)!==null?{type:"updateById",entryId:t,value:n}:null))}async popFirst(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(t=>t.peekFirst()!==null?{type:"popFirst"}:null))}async get(t){return this.readOp(n=>n.get(t))}async hasKey(t){return this.readOp(n=>n.hasKey(t))}async findFirst(t){return this.readOp(n=>n.findFirst(t))}async findLast(t){return this.readOp(n=>n.findLast(t))}async range(t,n,r){return this.readOp(o=>o.range(t,n,r))}async snapshot(){return this.readOp(t=>t.snapshot())}async size(){return this.readOp(t=>t.size())}async assertInvariants(){await this.readOp(t=>t.assertInvariants())}async getStats(){return this.readOp(t=>t.getStats())}async peekFirst(){return this.readOp(t=>t.peekFirst())}async peekLast(){return this.readOp(t=>t.peekLast())}async popLast(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(t=>t.peekLast()!==null?{type:"popLast"}:null))}async peekById(t){return this.readOp(n=>n.peekById(t))}async count(t,n,r){return this.readOp(o=>o.count(t,n,r))}async nextHigherKey(t){return this.readOp(n=>n.nextHigherKey(t))}async nextLowerKey(t){return this.readOp(n=>n.nextLowerKey(t))}async getPairOrNextLower(t){return this.readOp(n=>n.getPairOrNextLower(t))}};return ct(Jt);})();
package/dist/index.cjs CHANGED
@@ -1811,23 +1811,13 @@ var InMemoryBTree = class _InMemoryBTree {
1811
1811
  }
1812
1812
  }
1813
1813
  *keys() {
1814
- let leaf = this.state.leftmostLeaf;
1815
- while (leaf !== null) {
1816
- const count = leafEntryCount(leaf);
1817
- for (let i = 0; i < count; i += 1) {
1818
- yield leafEntryAt(leaf, i).key;
1819
- }
1820
- leaf = leaf.next;
1814
+ for (const entry of this.entries()) {
1815
+ yield entry.key;
1821
1816
  }
1822
1817
  }
1823
1818
  *values() {
1824
- let leaf = this.state.leftmostLeaf;
1825
- while (leaf !== null) {
1826
- const count = leafEntryCount(leaf);
1827
- for (let i = 0; i < count; i += 1) {
1828
- yield leafEntryAt(leaf, i).value;
1829
- }
1830
- leaf = leaf.next;
1819
+ for (const entry of this.entries()) {
1820
+ yield entry.value;
1831
1821
  }
1832
1822
  }
1833
1823
  [Symbol.iterator]() {
@@ -1943,6 +1933,53 @@ var assertNeverMutation = (mutation) => {
1943
1933
  `Unsupported mutation type from shared store: ${String(unknownMutation.type)}`
1944
1934
  );
1945
1935
  };
1936
+ var validateMutationBatch = (mutations, expectedConfigFingerprint) => {
1937
+ for (const mutation of mutations) {
1938
+ if (typeof mutation !== "object" || mutation === null) {
1939
+ throw new BTreeConcurrencyError("Malformed mutation: expected an object.");
1940
+ }
1941
+ const m = mutation;
1942
+ switch (m.type) {
1943
+ case "init":
1944
+ if (typeof m.configFingerprint !== "string") {
1945
+ throw new BTreeConcurrencyError("Malformed init mutation: missing configFingerprint.");
1946
+ }
1947
+ if (expectedConfigFingerprint !== void 0 && m.configFingerprint !== expectedConfigFingerprint) {
1948
+ throw new BTreeConcurrencyError(
1949
+ "Config mismatch: store peers must share identical tree config."
1950
+ );
1951
+ }
1952
+ break;
1953
+ case "put":
1954
+ if (!("key" in m) || !("value" in m)) {
1955
+ throw new BTreeConcurrencyError("Malformed put mutation: missing key or value.");
1956
+ }
1957
+ break;
1958
+ case "remove":
1959
+ if (!("key" in m)) {
1960
+ throw new BTreeConcurrencyError("Malformed remove mutation: missing key.");
1961
+ }
1962
+ break;
1963
+ case "removeById":
1964
+ if (!("entryId" in m)) {
1965
+ throw new BTreeConcurrencyError("Malformed removeById mutation: missing entryId.");
1966
+ }
1967
+ break;
1968
+ case "updateById":
1969
+ if (!("entryId" in m) || !("value" in m)) {
1970
+ throw new BTreeConcurrencyError("Malformed updateById mutation: missing entryId or value.");
1971
+ }
1972
+ break;
1973
+ case "popFirst":
1974
+ case "popLast":
1975
+ break;
1976
+ default:
1977
+ throw new BTreeConcurrencyError(
1978
+ `Unsupported mutation type from shared store: ${String(m.type)}`
1979
+ );
1980
+ }
1981
+ }
1982
+ };
1946
1983
  var normalizeMaxRetries = (value) => {
1947
1984
  if (value === void 0) {
1948
1985
  return DEFAULT_MAX_RETRIES;
@@ -2045,6 +2082,7 @@ var ConcurrentInMemoryBTree = class {
2045
2082
  if (log.version <= this.currentVersion) {
2046
2083
  return;
2047
2084
  }
2085
+ validateMutationBatch(log.mutations, this.configFingerprint);
2048
2086
  for (const mutation of log.mutations) {
2049
2087
  this.applyMutationLocal(mutation);
2050
2088
  }
@@ -2053,11 +2091,6 @@ var ConcurrentInMemoryBTree = class {
2053
2091
  applyMutationLocal(mutation) {
2054
2092
  switch (mutation.type) {
2055
2093
  case "init":
2056
- if (mutation.configFingerprint !== this.configFingerprint) {
2057
- throw new BTreeConcurrencyError(
2058
- "Config mismatch: store peers must share identical tree config."
2059
- );
2060
- }
2061
2094
  this.initSeen = true;
2062
2095
  return null;
2063
2096
  case "put":
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  DEFAULT_MAX_LEAF_ENTRIES,
7
7
  InMemoryBTree,
8
8
  computeAutoScaleTier
9
- } from "./chunk-ZA3EQNDI.js";
9
+ } from "./chunk-CZFRT2NN.js";
10
10
 
11
11
  // src/concurrency/helpers.ts
12
12
  var DEFAULT_MAX_RETRIES = 16;
@@ -30,6 +30,53 @@ var assertNeverMutation = (mutation) => {
30
30
  `Unsupported mutation type from shared store: ${String(unknownMutation.type)}`
31
31
  );
32
32
  };
33
+ var validateMutationBatch = (mutations, expectedConfigFingerprint) => {
34
+ for (const mutation of mutations) {
35
+ if (typeof mutation !== "object" || mutation === null) {
36
+ throw new BTreeConcurrencyError("Malformed mutation: expected an object.");
37
+ }
38
+ const m = mutation;
39
+ switch (m.type) {
40
+ case "init":
41
+ if (typeof m.configFingerprint !== "string") {
42
+ throw new BTreeConcurrencyError("Malformed init mutation: missing configFingerprint.");
43
+ }
44
+ if (expectedConfigFingerprint !== void 0 && m.configFingerprint !== expectedConfigFingerprint) {
45
+ throw new BTreeConcurrencyError(
46
+ "Config mismatch: store peers must share identical tree config."
47
+ );
48
+ }
49
+ break;
50
+ case "put":
51
+ if (!("key" in m) || !("value" in m)) {
52
+ throw new BTreeConcurrencyError("Malformed put mutation: missing key or value.");
53
+ }
54
+ break;
55
+ case "remove":
56
+ if (!("key" in m)) {
57
+ throw new BTreeConcurrencyError("Malformed remove mutation: missing key.");
58
+ }
59
+ break;
60
+ case "removeById":
61
+ if (!("entryId" in m)) {
62
+ throw new BTreeConcurrencyError("Malformed removeById mutation: missing entryId.");
63
+ }
64
+ break;
65
+ case "updateById":
66
+ if (!("entryId" in m) || !("value" in m)) {
67
+ throw new BTreeConcurrencyError("Malformed updateById mutation: missing entryId or value.");
68
+ }
69
+ break;
70
+ case "popFirst":
71
+ case "popLast":
72
+ break;
73
+ default:
74
+ throw new BTreeConcurrencyError(
75
+ `Unsupported mutation type from shared store: ${String(m.type)}`
76
+ );
77
+ }
78
+ }
79
+ };
33
80
  var normalizeMaxRetries = (value) => {
34
81
  if (value === void 0) {
35
82
  return DEFAULT_MAX_RETRIES;
@@ -132,6 +179,7 @@ var ConcurrentInMemoryBTree = class {
132
179
  if (log.version <= this.currentVersion) {
133
180
  return;
134
181
  }
182
+ validateMutationBatch(log.mutations, this.configFingerprint);
135
183
  for (const mutation of log.mutations) {
136
184
  this.applyMutationLocal(mutation);
137
185
  }
@@ -140,11 +188,6 @@ var ConcurrentInMemoryBTree = class {
140
188
  applyMutationLocal(mutation) {
141
189
  switch (mutation.type) {
142
190
  case "init":
143
- if (mutation.configFingerprint !== this.configFingerprint) {
144
- throw new BTreeConcurrencyError(
145
- "Config mismatch: store peers must share identical tree config."
146
- );
147
- }
148
191
  this.initSeen = true;
149
192
  return null;
150
193
  case "put":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frostpillar/frostpillar-btree",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "A tiny, zero-dependency in-memory B+ tree for TypeScript, Node.js, and browser JavaScript.",
5
5
  "type": "module",
6
6
  "author": "Hajime Sano",