@frostpillar/frostpillar-btree 0.2.4 → 0.2.6

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/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-OWHENPGJ.js";
6
6
  export {
7
7
  BTreeInvariantError,
8
8
  BTreeValidationError,
package/dist/errors.d.ts CHANGED
@@ -1,9 +1,12 @@
1
+ /** Thrown when a caller supplies invalid input, such as a duplicate key, sequence overflow, or malformed configuration. */
1
2
  export declare class BTreeValidationError extends Error {
2
3
  constructor(message: string);
3
4
  }
5
+ /** Thrown when an internal tree invariant is violated, indicating a bug in the library rather than invalid caller input. */
4
6
  export declare class BTreeInvariantError extends Error {
5
7
  constructor(message: string);
6
8
  }
9
+ /** Thrown when the concurrent store contract is violated or a mutation batch is malformed during optimistic concurrency operations. */
7
10
  export declare class BTreeConcurrencyError extends Error {
8
11
  constructor(message: string);
9
12
  }
@@ -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 $=Object.defineProperty;var je=Object.getOwnPropertyDescriptor;var en=Object.getOwnPropertyNames;var nn=Object.prototype.hasOwnProperty;var tn=(e,n)=>{for(var t in n)$(e,t,{get:n[t],enumerable:!0})},rn=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of en(n))!nn.call(e,l)&&l!==t&&$(e,l,{get:()=>n[l],enumerable:!(r=je(n,l))||r.enumerable});return e};var ln=e=>rn($({},"__esModule",{value:!0}),e);var In={};tn(In,{InMemoryBTree:()=>ye});var f=class extends Error{constructor(n){super(n),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},y=class extends Error{constructor(n){super(n),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var b=(e,n)=>({kind:0,entries:e,entryOffset:0,parent:n,indexInParent:0,prev:null,next:null}),S=(e,n)=>{let t=[],r={kind:1,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(!x(o,i))throw new y("branch child has no min key");t.push(i)}return r},s=e=>e.entries.length-e.entryOffset,T=(e,n)=>e.entries[e.entryOffset+n],R=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},Y=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},Q=(e,n)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=n):e.entries.unshift(n)},D=(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)},Z=(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)},j=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},Te=1,O=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}},g=e=>e.children.length-e.childOffset,ee=(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}},ne=(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&&O(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 te=64,re=64,J=3,X=16384,A=0;var ce=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new f("Invalid duplicateKeys option.");return e},p=e=>({entryId:e.entryId,key:e.key,value:e.value}),m=e=>e.kind===A,x=(e,n)=>{if(e.kind===A){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)},_=(e,n,t)=>{if(e===void 0)return t;if(!Number.isInteger(e)||e<J||e>X)throw new f(`${n}: integer ${J}\u2013${X} required.`);return e};var on=(e,n,t,r)=>{let l=n.childOffset;if(l>=n.children.length)throw new y("branch has no children");let o=l,i=l,u=n.keys.length-1;for(;i<=u;){let a=i+u>>>1,c=n.keys[a],d=e(c.key,t);(d!==0?d:c.sequence<r?-1:c.sequence>r?1:0)<=0?(o=a,i=a+1):u=a-1}return n.children[o]},V=(e,n,t)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==A;)l=on(r,l,n,t);return l},B=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,i=n.entries.length;for(;o<i;){let u=o+i>>>1,a=n.entries[u],c=l(a.key,t);(c!==0?c:a.entryId<r?-1:a.entryId>r?1:0)<0?o=u+1:i=u}return o-n.entryOffset},E=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,i=n.entries.length;for(;o<i;){let u=o+i>>>1,a=n.entries[u],c=l(a.key,t);(c!==0?c:a.entryId<r?-1:a.entryId>r?1:0)<=0?o=u+1:i=u}return o-n.entryOffset},fe=(e,n,t,r)=>{let l=e.compareKeys,o=n,i=32-Math.clz32(e.entryCount+1);for(;i>0&&o.next!==null&&s(o.next)>0;){let u=T(o.next,0),a=l(u.key,t);if(a>0||a===0&&u.entryId>r)break;o=o.next,i-=1}if(i===0&&o.next!==null&&s(o.next)>0){let u=T(o.next,0),a=l(u.key,t);if(a<0||a===0&&u.entryId<=r)return V(e,t,r)}return o},M=(e,n)=>{if(e.entryCount===0)return null;let t=V(e,n,0),r=B(e,t,n,0);if(r>=s(t)&&(t.next===null||(t=t.next,r=B(e,t,n,0),r>=s(t)))||e.compareKeys(T(t,r).key,n)!==0)return null;let l=e._cursor;return l.leaf=t,l.index=r,l},de=(e,n)=>{if(e.entryCount===0)return null;let t=V(e,n,Number.MAX_SAFE_INTEGER),r=E(e,t,n,Number.MAX_SAFE_INTEGER);if(r===0&&(t.prev===null||(t=t.prev,r=s(t),r===0))||(r-=1,e.compareKeys(T(t,r).key,n)!==0))return null;let l=e._cursor;return l.leaf=t,l.index=r,l},he=(e,n)=>M(e,n)!==null,pe=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=V(e,n,Number.MAX_SAFE_INTEGER),l=E(e,r,n,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<s(r)){let o=T(r,l);if(t(o.key,n)>0)return o.key;l+=1}else r=r.next,l=0;return null},Ke=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=V(e,n,0),l=B(e,r,n,0),o=r,i=l-1;for(;o!==null;){for(;i>=0;){let u=T(o,i);if(t(u.key,n)<0)return u.key;i-=1}o=o.prev,o!==null&&(i=s(o)-1)}return null},me=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=V(e,n,0),l=B(e,r,n,0),o=e._cursor;if(l<s(r)){if(t(T(r,l).key,n)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let i=B(e,r.next,n,0);if(i<s(r.next)&&t(T(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=s(r.prev);if(i>0)return o.leaf=r.prev,o.index=i-1,o}return null};function le(e,n,t,r){let l=e(n,t);if(l>0)return!0;let o=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive";return o&&i&&l===0}var Be=(e,n,t,r)=>{if(e.entryCount===0)return null;let l=e.compareKeys;if(le(l,n,t,r))return null;let o=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive",u=o?Number.MAX_SAFE_INTEGER:0,a=V(e,n,u),c=o?E(e,a,n,Number.MAX_SAFE_INTEGER):B(e,a,n,0);return{leaf:a,index:c,compare:l,upperExclusive:i}},oe=(e,n,t,r)=>{let l=Be(e,n,t,r);if(l===null)return 0;let o=l.leaf,i=l.index,{compare:u,upperExclusive:a}=l,c=0;for(;o!==null;){let d=s(o);if(i>=d){o=o.next,i=0;continue}let h=T(o,d-1),K=u(h.key,t);if(a?K<0:K<=0){c+=d-i,o=o.next,i=0;continue}let k=a?0:Number.MAX_SAFE_INTEGER,w=a?B(e,o,t,k):E(e,o,t,k),q=w<d?w:d;return c+=q-i,c}return c},un=200,an=(e,n,t,r,l,o,i,u)=>{let a=s(n);if(a-t>=un&&n.next!==null){let d=T(n,a-1),h=r(d.key,i);if(l?h<0:h<=0){let K=oe(e,o,i,u);return new Array(K)}}return[]},Ve=(e,n,t,r,l,o)=>{if(l)for(let i=n;i<t;i+=1)r[o++]=T(e,i);else for(let i=n;i<t;i+=1)r.push(T(e,i));return o},xe=(e,n,t,r)=>{let l=Be(e,n,t,r);if(l===null)return[];let o=l.leaf,i=l.index,{compare:u,upperExclusive:a}=l,c=an(e,o,i,u,a,n,t,r),d=0,h=c.length>0;for(;o!==null;){let K=s(o);if(i>=K){o=o.next,i=0;continue}let k=T(o,K-1),w=u(k.key,t);if(a?w<0:w<=0){d=Ve(o,i,K,c,h,d),o=o.next,i=0;continue}let q=a?0:Number.MAX_SAFE_INTEGER,se=a?B(e,o,t,q):E(e,o,t,q),Ze=se<K?se:K;return Ve(o,i,Ze,c,h,d),c}return c};var N=e=>{let n=e;for(;n.parent!==null;){let t=n.indexInParent;if(!x(n,n.parent.keys[t])||t!==n.parent.childOffset)return;n=n.parent}},H=e=>{if(e.parent===null)throw new y("no parent during rebalance");return e.parent},Ee=e=>{if(!m(e))throw new y("expected leaf, got branch");return e},Ne=e=>{if(m(e))throw new y("expected branch, got leaf");return e},G=(e,n)=>{if(n<e.childOffset||n>=e.children.length)throw new y("child index out of range");ne(e,n)},Le=(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},yn=(e,n,t)=>{let r=n.children.pop();if(r===void 0)throw new y("left branch borrow failed");n.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!x(r,l))throw new y("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},N(e)},sn=(e,n,t)=>{let r=n.childOffset;if(r>=n.children.length)throw new y("right branch borrow failed");let l=n.children[r];n.childOffset+=1,n.childOffset>=n.children.length>>>1&&O(n),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!x(l,o))throw new y("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let i=H(e);x(n,i.keys[t+1])},Tn=(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);G(l,r),W(e,l)},cn=(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);G(l,r+1),W(e,l)},fn=(e,n)=>{let t=n>e.childOffset?Ne(e.children[n-1]):null,r=n+1<e.children.length?Ne(e.children[n+1]):null;return{left:t,right:r}},W=(e,n)=>{let t=g(n);if(n===e.root){if(t===1){let u=n.children[n.childOffset];u.parent=null,e.root=u,m(u)&&(e.leftmostLeaf=u,e.rightmostLeaf=u)}return}if(t>=e.minBranchChildren)return;let r=n.parent;if(r===null)throw new y("branch has no parent");let l=n.indexInParent,{left:o,right:i}=fn(r,l);if(i!==null&&g(i)>e.minBranchChildren){sn(n,i,l);return}if(o!==null&&g(o)>e.minBranchChildren){yn(n,o,l);return}if(o!==null){Tn(e,n,o,l);return}if(i!==null){cn(e,n,i,l);return}throw new y("no branch siblings to rebalance")};var we=(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])},v=(e,n)=>{if(n===e.root){e.entryCount===0&&(e.leftmostLeaf=n,e.rightmostLeaf=n);return}if(s(n)>=e.minLeafEntries)return;let t=n.parent;if(t===null)throw new y("Leaf node has no parent during rebalance.");let r=n.indexInParent,l=r>t.childOffset?Ee(t.children[r-1]):null,o=r+1<t.children.length?Ee(t.children[r+1]):null;if(o!==null&&s(o)>e.minLeafEntries){let i=R(o);if(i===void 0)throw new y("right leaf borrow failed");n.entries.push(i),x(o,t.keys[r+1]);return}if(l!==null&&s(l)>e.minLeafEntries){let i=l.entries.pop();if(i===void 0)throw new y("left leaf borrow failed");Q(n,i),t.keys[r]={key:i.key,sequence:i.entryId},N(n);return}if(l!==null){we(l,n),Le(e,n),G(t,r),W(e,t);return}if(o!==null){we(n,o),Le(e,o),G(t,r+1),W(e,t);return}throw new y("no leaf siblings to rebalance")};var dn=(e,n,t)=>{let r=t?Number.MAX_SAFE_INTEGER:0,l=V(e,n,r),o=t?E(e,l,n,Number.MAX_SAFE_INTEGER):B(e,l,n,0);return o>=s(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},hn=(e,n,t,r,l)=>{let o=s(n),i=t;for(;i<o;){let u=T(n,i),a=e.compareKeys(u.key,r);if(l?a>=0:a>0)break;i+=1}return i},pn=(e,n,t,r)=>{if(e.entryKeys!==null)for(let a=t;a<t+r;a+=1)e.entryKeys.delete(T(n,a).entryId);let l=n.entryOffset+t;n.entries.copyWithin(l,l+r),n.entries.length-=r,e.entryCount-=r;let o=s(n)===0;t===0&&!o&&n.parent!==null&&N(n);let i=s(n),u=e.minLeafEntries+4;for(;u>0&&n!==e.root&&s(n)<e.minLeafEntries&&(v(e,n),!(n.parent!==null&&n.parent.children[n.indexInParent]!==n));)u-=1;return o&&s(n)>0&&n.parent!==null&&n.parent.children[n.indexInParent]===n&&N(n),i},Kn=(e,n)=>n.parent===null?n===e.root:n.parent.children[n.indexInParent]===n,Ce=(e,n,t,r)=>{if(e.entryCount===0||le(e.compareKeys,n,t,r))return 0;let l=r?.lowerBound==="exclusive",o=r?.upperBound==="exclusive",i=0,u=!0,a=null,c=0;for(;e.entryCount>0;){if(u){let w=dn(e,n,l);if(w===null)break;a=w.leaf,c=w.idx,u=!1}if(c>=s(a))break;let d=s(a),h=hn(e,a,c,t,o),K=h-c;if(K===0)break;let k=pn(e,a,c,K);if(i+=K,h<d)break;if(!Kn(e,a)){u=!0;continue}if(s(a)>k){u=!0;continue}if(a.next===null)break;a=a.next,c=0}return i};var C=e=>Math.ceil(e/2),I=[{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}],L=e=>{let n=I[0];for(let t=1;t<I.length&&e>=I[t].threshold;t+=1)n=I[t];return n},U=e=>{for(let n=1;n<I.length;n+=1)if(e<I[n].threshold)return I[n].threshold;return Number.MAX_SAFE_INTEGER},be=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=L(0);t=i.maxLeaf,r=i.maxBranch}else t=_(e.maxLeafEntries,"maxLeafEntries",te),r=_(e.maxBranchChildren,"maxBranchChildren",re);let l=ce(e.duplicateKeys),o=b([],null);return{compareKeys:e.compareKeys,maxLeafEntries:t,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:C(t),minBranchChildren:C(r),root:o,leftmostLeaf:o,rightmostLeaf:o,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:n,_nextAutoScaleThreshold:n?U(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:o,index:0}}},z=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:n,maxBranch:t}=L(e.entryCount);n>e.maxLeafEntries&&(e.maxLeafEntries=n,e.minLeafEntries=C(n)),t>e.maxBranchChildren&&(e.maxBranchChildren=t,e.minBranchChildren=C(t)),e._nextAutoScaleThreshold=U(e.entryCount)},ie=(e,n,t)=>{if(!e.autoScale)return;let r=L(0),l=_(n,"maxLeafEntries",te),o=_(t,"maxBranchChildren",re);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=C(l),e.minBranchChildren=C(o)};var Se=(e,n,t)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>n&&o-n<t){let u=Math.ceil(o/2);r.push(l+u),r.push(e);break}let i=l+n<e?l+n:e;r.push(i),l=i}return r},mn=(e,n,t,r)=>{let l=Se(n.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),i=0;for(let u=0;u<l.length;u+=1){let a=l[u],c=new Array(a-i);for(let d=i;d<a;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[u]=b(c,null),i=a}return o},ge=(e,n)=>{if(e.entryCount!==0)throw new y("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=mn(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=Se(o.length,e.maxBranchChildren,e.minBranchChildren),u=new Array(i.length),a=0;for(let c=0;c<i.length;c+=1)u[c]=S(o.slice(a,i[c]),null),a=i[c];o=u}e.root=o[0]}return z(e),r};var ve=(e,n,t,r)=>{let l={key:void 0,sequence:0};if(!x(r,l))throw new y("inserted child has no min key");r.parent=n;let o=t.indexInParent-n.childOffset+1;ee(n,o,r,l),g(n)>e.maxBranchChildren&&Bn(e,n)},Vn=(e,n)=>{j(n);let t=Math.ceil(n.entries.length/2),r={kind:A,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=S([n,r],null);return}ve(e,n.parent,n,r)},Bn=(e,n)=>{O(n);let t=Math.ceil(n.children.length/2),r=S(n.children.splice(t),n.parent);if(n.keys.splice(t),n.parent===null){e.root=S([n,r],null);return}ve(e,n.parent,n,r)},Ie=(e,n,t,r)=>{let l=e.nextSequence,o=E(e,n,t,l);if(e.duplicateKeys!=="allow"){let i=null;if(o>0){let u=T(n,o-1);e.compareKeys(u.key,t)===0&&(i=u)}else if(n.prev!==null&&s(n.prev)>0){let u=n.prev,a=T(u,s(u)-1);e.compareKeys(a.key,t)===0&&(i=a)}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,Z(n,o,{key:t,entryId:l,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,t),o===0&&n.parent!==null&&N(n),s(n)>e.maxLeafEntries&&Vn(e,n),z(e),l},ke=(e,n,t)=>{let r=V(e,n,e.nextSequence);return Ie(e,r,n,t)},Oe=e=>{if(e.entryCount===0)return null;let n=R(e.leftmostLeaf);if(n===void 0)throw new y("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),s(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&N(e.leftmostLeaf),e.leftmostLeaf!==e.root&&s(e.leftmostLeaf)<e.minLeafEntries&&v(e,e.leftmostLeaf),n},Ae=e=>{if(e.entryCount===0)return null;let n=Y(e.rightmostLeaf);if(n===void 0)throw new y("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),e.rightmostLeaf!==e.root&&s(e.rightmostLeaf)<e.minLeafEntries&&v(e,e.rightmostLeaf),n},Me=(e,n)=>{let t=M(e,n);if(t===null)return null;let r=t.leaf,l=t.index,o=T(r,l);return D(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&s(r)>0&&r.parent!==null&&N(r),r!==e.root&&s(r)<e.minLeafEntries&&v(e,r),o},ue=(e,n,t)=>{let r=V(e,n,t),l=B(e,r,n,t);return l>=s(r)||T(r,l).entryId!==t?null:{leaf:r,index:l}},Re=(e,n)=>{if(e.entryKeys===null)throw new y("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ue(e,t,n);if(r===null)return null;let l=T(r.leaf,r.index);return D(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(n),r.index===0&&s(r.leaf)>0&&r.leaf.parent!==null&&N(r.leaf),r.leaf!==e.root&&s(r.leaf)<e.minLeafEntries&&v(e,r.leaf),l},_e=(e,n)=>{if(e.entryKeys===null)throw new y("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ue(e,t,n);return r===null?null:T(r.leaf,r.index)},Pe=(e,n,t)=>{if(e.entryKeys===null)throw new y("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(n);if(r===void 0)return null;let l=ue(e,r,n);if(l===null)return null;let o=T(l.leaf,l.index);return o.value=t,o},Fe=(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(l>0)throw new f("putMany: entries not in ascending order.");if(t&&l===0)throw new f(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let r=new Array(n.length),l=V(e,n[0].key,e.nextSequence);for(let o=0;o<n.length;o+=1){let i=n[o],u=fe(e,l,i.key,e.nextSequence);r[o]=Ie(e,u,i.key,i.value),l=u}return r}return ge(e,n)};var qe=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},De=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=s(t);for(let o=0;o<l;o+=1){let i=T(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}},xn=1e6,En=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>xn)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}].`)}},Nn=e=>{let n=(t,r)=>{if(!Number.isInteger(r)||r<J||r>X)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=L(0);if(e.maxLeafEntries<t.maxLeaf||e.maxBranchChildren<t.maxBranch)throw new f("BTreeJSON: autoScale capacity below tier-0.")}},Je=e=>{En(e),Nn(e.config)},Xe=(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 ae=e=>{if(m(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}},F=(e,n,t,r,l)=>{let o=e(n,r);return o!==0?o:t<l?-1:t>l?1:0},Ge=e=>{if(m(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:Ge(e.children[e.children.length-1])},P=e=>{if(!Number.isFinite(e))throw new f("compareKeys must return a finite number.");return e},Ln=(e,n)=>{if(P(e(n,n))!==0)throw new f("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},wn=(e,n,t,r)=>{let l=Math.sign(P(e(n,t))),o=Math.sign(P(e(t,r)));if(l<0&&o<0&&Math.sign(P(e(n,r)))>=0)throw new f("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(P(e(n,r)))<=0)throw new f("compareKeys must satisfy transitivity for observed key triples.")},We=(e,n)=>{try{Ln(e.compareKeys,n)}catch(t){throw t instanceof f?new y(t.message):t}},He=(e,n,t,r)=>{try{wn(e.compareKeys,n,t,r)}catch(l){throw l instanceof f?new y(l.message):l}},Cn=(e,n,t,r)=>{if(!m(n))throw new y("Leaf linkage cursor reached non-leaf node.");if(r.has(n))throw new y("Cycle detected in leaf linkage.");if(n.prev!==t)throw new y("Leaf prev pointer mismatch.");if(t!==null&&m(t)){let l=Ge(t),o=ae(n);if(l===null||o===null)throw new y("Non-empty tree leaf chain contains empty leaf node.");if(F(e.compareKeys,l.key,l.sequence,o.key,o.sequence)>0)throw new y("Adjacent leaf key ranges are out of order.");let i=s(t),u=s(n);if(e.duplicateKeys!=="allow"&&i>0&&u>0&&e.compareKeys(T(t,i-1).key,T(n,0).key)===0)throw new y("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},Ue=(e,n)=>{if(e.entryCount===0){if(!m(e.root))throw new y("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new y("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new y("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new y("Rightmost leaf next pointer must be null.");let t=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)Cn(e,r,l,t),t.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new y("Rightmost leaf pointer mismatch.");if(o!==n)throw new y("Leaf chain count mismatch with tree traversal count.")};var bn=(e,n)=>{let t=s(n);for(let r=0;r<t;r+=1)We(e,T(n,r).key);for(let r=1;r<t;r+=1)if(F(e.compareKeys,T(n,r-1).key,T(n,r-1).entryId,T(n,r).key,T(n,r).entryId)>=0)throw new y("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<t;r+=1)if(e.compareKeys(T(n,r-1).key,T(n,r).key)===0)throw new y("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<t;r+=1){let l=T(n,r-2),o=T(n,r-1),i=T(n,r);He(e,l.key,o.key,i.key)}if(t>e.maxLeafEntries)throw new y("Leaf node exceeds maximum occupancy.")},Sn=(e,n,t)=>{bn(e,n);let r=s(n),l=e.autoScale?Math.ceil(L(0).maxLeaf/2):e.minLeafEntries;if(n!==e.root&&r<l)throw new y("Non-root leaf node violates minimum occupancy.");let o=r===0?null:T(n,0),i=r===0?null:T(n,r-1),u=o===null?null:{key:o.key,sequence:o.entryId},a=i===null?null:{key:i.key,sequence:i.entryId};return{minKey:u,maxKey:a,leafDepth:r===0?null:t,leafCount:1,branchCount:0,entryCount:r}},gn=(e,n)=>{let t=n.children.length-n.childOffset;if(t===0)throw new y("Branch node has zero children.");let r=e.autoScale?Math.ceil(L(0).maxBranch/2):e.minBranchChildren;if(n!==e.root&&t<r)throw new y("Non-root branch node violates minimum occupancy.");if(t>e.maxBranchChildren)throw new y("Branch node exceeds maximum occupancy.");if(n.keys.length!==n.children.length)throw new y("Branch keys array length does not match children array length.")},vn=(e,n,t,r)=>{let l=n.children[t];if(l.parent!==n)throw new y("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==t)throw new y("Child indexInParent does not match actual position in parent.");let o=ze(e,l,r+1);if(o.minKey===null||o.maxKey===null)throw new y("Branch child must not be empty in non-root branch tree.");let i=n.keys[t],u=ae(l);if(u===null||F(e.compareKeys,i.key,i.sequence,u.key,u.sequence)!==0)throw new y("Branch cached key does not match actual child minimum key.");return o},ze=(e,n,t)=>{if(m(n))return Sn(e,n,t);gn(e,n);let r=null,l=0,o=1,i=0,u=null,a=null,c=null;for(let d=n.childOffset;d<n.children.length;d+=1){let h=vn(e,n,d,t);if(r!==null&&h.leafDepth!==null&&h.leafDepth!==r)throw new y("Leaf depth mismatch detected in tree.");if(r===null&&h.leafDepth!==null&&(r=h.leafDepth),c!==null&&F(e.compareKeys,c.key,c.sequence,h.minKey.key,h.minKey.sequence)>=0)throw new y("Branch child key ranges are not strictly ordered.");u===null&&(u=h.minKey),a=h.maxKey,c=h.maxKey,l+=h.leafCount,o+=h.branchCount,i+=h.entryCount}return{minKey:u,maxKey:a,leafDepth:r,leafCount:l,branchCount:o,entryCount:i}},$e=e=>{let n=ze(e,e.root,0);if(n.entryCount!==e.entryCount)throw new y("Index entry count mismatch between tree traversal and tracked state.");Ue(e,n.leafCount)};var Ye=e=>{if(m(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=Ye(o);i.height>n&&(n=i.height),t+=i.leafCount,r+=i.branchCount}return{height:n+1,leafCount:t,branchCount:r}},Qe=e=>{let n=Ye(e.root);return{height:n.height,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:e.entryCount}};var ye=class e{constructor(n){this.state=be(n)}put(n,t){return ke(this.state,n,t)}putMany(n){return Fe(this.state,n)}remove(n){let t=Me(this.state,n);return t===null?null:p(t)}removeById(n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");let t=Re(this.state,n);return t===null?null:p(t)}peekById(n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");let t=_e(this.state,n);return t===null?null:p(t)}updateById(n,t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");let r=Pe(this.state,n,t);return r===null?null:p(r)}popFirst(){let n=Oe(this.state);return n===null?null:p(n)}peekFirst(){return this.state.entryCount===0?null:p(T(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let n=this.state.rightmostLeaf;return p(T(n,s(n)-1))}popLast(){let n=Ae(this.state);return n===null?null:p(n)}clear(){let n=b([],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=L(0);this.state.maxLeafEntries=t.maxLeaf,this.state.maxBranchChildren=t.maxBranch,this.state.minLeafEntries=C(t.maxLeaf),this.state.minBranchChildren=C(t.maxBranch),this.state._nextAutoScaleThreshold=U(0)}}get(n){let t=M(this.state,n);return t===null?null:T(t.leaf,t.index).value}hasKey(n){return he(this.state,n)}findFirst(n){let t=M(this.state,n);return t===null?null:p(T(t.leaf,t.index))}findLast(n){let t=de(this.state,n);return t===null?null:p(T(t.leaf,t.index))}nextHigherKey(n){return pe(this.state,n)}nextLowerKey(n){return Ke(this.state,n)}getPairOrNextLower(n){let t=me(this.state,n);return t===null?null:p(T(t.leaf,t.index))}count(n,t,r){return oe(this.state,n,t,r)}deleteRange(n,t,r){return Ce(this.state,n,t,r)}range(n,t,r){return xe(this.state,n,t,r).map(p)}*entries(){let n=this.state.leftmostLeaf;for(;n!==null;){let t=s(n);for(let r=0;r<t;r+=1)yield p(T(n,r));n=n.next}}*entriesReversed(){let n=this.state.rightmostLeaf;for(;n!==null;){let t=s(n);for(let r=t-1;r>=0;r-=1)yield p(T(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=s(r);for(let o=0;o<l;o+=1)n.call(t,p(T(r,o)));r=r.next}}snapshot(){let n=new Array(this.state.entryCount),t=this.state.leftmostLeaf,r=0;for(;t!==null;){let l=s(t);for(let o=0;o<l;o+=1)n[r++]=p(T(t,o));t=t.next}return n}clone(){let n=new e(qe(this.state));if(ie(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=s(r);for(let i=0;i<o;i+=1)t[l++]=T(r,i);r=r.next}n.putMany(t)}return n}toJSON(){return De(this.state)}static fromJSON(n,t){Je(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(Xe(n,t));if(ie(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(){$e(this.state)}getStats(){return Qe(this.state)}};return ln(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 te=Object.defineProperty;var Vt=Object.getOwnPropertyDescriptor;var xt=Object.getOwnPropertyNames;var Et=Object.prototype.hasOwnProperty;var wt=(e,t)=>{for(var n in t)te(e,n,{get:t[n],enumerable:!0})},Lt=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of xt(t))!Et.call(e,o)&&o!==n&&te(e,o,{get:()=>t[o],enumerable:!(r=Vt(t,o))||r.enumerable});return e};var Nt=e=>Lt(te({},"__esModule",{value:!0}),e);var nn={};wt(nn,{BTreeConcurrencyError:()=>f,BTreeInvariantError:()=>y,BTreeValidationError:()=>T,ConcurrentInMemoryBTree:()=>X,InMemoryBTree:()=>M});var T=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},y=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}},f=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var I=(e,t)=>({kind:0,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),g=(e,t)=>{let n=[],r={kind:1,children:e,keys:n,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let a=e[o];a.parent=r,a.indexInParent=o;let l={key:void 0,sequence:0};if(!w(a,l))throw new y("branch child has no min key");n.push(l)}return r},s=e=>e.entries.length-e.entryOffset,c=(e,t)=>e.entries[e.entryOffset+t],_=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},ne=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},re=(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)},oe=(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)},fe=1,A=e=>{if(e.childOffset>0){let t=e.childOffset<=fe?0:fe;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}},S=e=>e.children.length-e.childOffset,le=(e,t,n,r)=>{let o=e.childOffset+t,a=e.children.length-e.childOffset;if(e.childOffset>0&&t<a>>>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}},ie=(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&&A(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 F=64,D=64,G=3,W=16384,R=0;var pe=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new T("Invalid duplicateKeys option.");return e},h=e=>({entryId:e.entryId,key:e.key,value:e.value}),B=e=>e.kind===R,w=(e,t)=>{if(e.kind===R){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)},q=(e,t,n)=>{if(e===void 0)return n;if(!Number.isInteger(e)||e<G||e>W)throw new T(`${t}: integer ${G}\u2013${W} required.`);return e};var bt=(e,t,n,r)=>{let o=t.childOffset;if(o>=t.children.length)throw new y("branch has no children");let a=o,l=o,i=t.keys.length-1;for(;l<=i;){let u=l+i>>>1,d=t.keys[u],p=e(d.key,n);(p!==0?p:d.sequence<r?-1:d.sequence>r?1:0)<=0?(a=u,l=u+1):i=u-1}return t.children[a]},V=(e,t,n)=>{let r=e.compareKeys,o=e.root;for(;o.kind!==R;)o=bt(r,o,t,n);return o},x=(e,t,n,r)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,u=t.entries[i],d=o(u.key,n);(d!==0?d:u.entryId<r?-1:u.entryId>r?1:0)<0?a=i+1:l=i}return a-t.entryOffset},L=(e,t,n,r)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,u=t.entries[i],d=o(u.key,n);(d!==0?d:u.entryId<r?-1:u.entryId>r?1:0)<=0?a=i+1:l=i}return a-t.entryOffset},me=(e,t,n,r)=>{let o=e.compareKeys,a=t,l=32-Math.clz32(e.entryCount+1);for(;l>0&&a.next!==null&&s(a.next)>0;){let i=c(a.next,0),u=o(i.key,n);if(u>0||u===0&&i.entryId>r)break;a=a.next,l-=1}if(l===0&&a.next!==null&&s(a.next)>0){let i=c(a.next,0),u=o(i.key,n);if(u<0||u===0&&i.entryId<=r)return V(e,n,r)}return a},P=(e,t)=>{if(e.entryCount===0)return null;let n=V(e,t,0),r=x(e,n,t,0);if(r>=s(n)&&(n.next===null||(n=n.next,r=x(e,n,t,0),r>=s(n)))||e.compareKeys(c(n,r).key,t)!==0)return null;let o=e._cursor;return o.leaf=n,o.index=r,o},he=(e,t)=>{if(e.entryCount===0)return null;let n=V(e,t,Number.MAX_SAFE_INTEGER),r=L(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(c(n,r).key,t)!==0))return null;let o=e._cursor;return o.leaf=n,o.index=r,o},Ke=(e,t)=>P(e,t)!==null,Be=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=V(e,t,Number.MAX_SAFE_INTEGER),o=L(e,r,t,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(o<s(r)){let a=c(r,o);if(n(a.key,t)>0)return a.key;o+=1}else r=r.next,o=0;return null},Ve=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=V(e,t,0),o=x(e,r,t,0),a=r,l=o-1;for(;a!==null;){for(;l>=0;){let i=c(a,l);if(n(i.key,t)<0)return i.key;l-=1}a=a.prev,a!==null&&(l=s(a)-1)}return null},xe=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=V(e,t,0),o=x(e,r,t,0),a=e._cursor;if(o<s(r)){if(n(c(r,o).key,t)===0)return a.leaf=r,a.index=o,a}else if(r.next!==null){let l=x(e,r.next,t,0);if(l<s(r.next)&&n(c(r.next,l).key,t)===0)return a.leaf=r.next,a.index=l,a}if(o>0)return a.leaf=r,a.index=o-1,a;if(r.prev!==null){let l=s(r.prev);if(l>0)return a.leaf=r.prev,a.index=l-1,a}return null};function ue(e,t,n,r){let o=e(t,n);if(o>0)return!0;let a=r?.lowerBound==="exclusive",l=r?.upperBound==="exclusive";return a&&l&&o===0}var we=(e,t,n,r)=>{if(e.entryCount===0)return null;let o=e.compareKeys;if(ue(o,t,n,r))return null;let a=r?.lowerBound==="exclusive",l=r?.upperBound==="exclusive",i=a?Number.MAX_SAFE_INTEGER:0,u=V(e,t,i),d=a?L(e,u,t,Number.MAX_SAFE_INTEGER):x(e,u,t,0);return{leaf:u,index:d,compare:o,upperExclusive:l}},ye=(e,t,n,r)=>{let o=we(e,t,n,r);if(o===null)return 0;let a=o.leaf,l=o.index,{compare:i,upperExclusive:u}=o,d=0;for(;a!==null;){let p=s(a);if(l>=p){a=a.next,l=0;continue}let m=c(a,p-1),K=i(m.key,n);if(u?K<0:K<=0){d+=p-l,a=a.next,l=0;continue}let O=u?0:Number.MAX_SAFE_INTEGER,b=u?x(e,a,n,O):L(e,a,n,O),H=b<p?b:p;return d+=H-l,d}return d},vt=200,It=(e,t,n,r,o,a,l,i)=>{let u=s(t);if(u-n>=vt&&t.next!==null){let p=c(t,u-1),m=r(p.key,l);if(o?m<0:m<=0){let K=ye(e,a,l,i);return new Array(K)}}return[]},Ee=(e,t,n,r,o,a)=>{if(o)for(let l=t;l<n;l+=1)r[a++]=c(e,l);else for(let l=t;l<n;l+=1)r.push(c(e,l));return a},Le=(e,t,n,r)=>{let o=we(e,t,n,r);if(o===null)return[];let a=o.leaf,l=o.index,{compare:i,upperExclusive:u}=o,d=It(e,a,l,i,u,t,n,r),p=0,m=d.length>0;for(;a!==null;){let K=s(a);if(l>=K){a=a.next,l=0;continue}let O=c(a,K-1),b=i(O.key,n);if(u?b<0:b<=0){p=Ee(a,l,K,d,m,p),a=a.next,l=0;continue}let H=u?0:Number.MAX_SAFE_INTEGER,de=u?x(e,a,n,H):L(e,a,n,H),Bt=de<K?de:K;return Ee(a,l,Bt,d,m,p),d}return d};var N=e=>{let t=e;for(;t.parent!==null;){let n=t.indexInParent;if(!w(t,t.parent.keys[n])||n!==t.parent.childOffset)return;t=t.parent}},Q=e=>{if(e.parent===null)throw new y("no parent during rebalance");return e.parent},Ne=e=>{if(!B(e))throw new y("expected leaf, got branch");return e},be=e=>{if(B(e))throw new y("expected branch, got leaf");return e},j=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new y("child index out of range");ie(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},gt=(e,t,n)=>{let r=t.children.pop();if(r===void 0)throw new y("left branch borrow failed");t.keys.pop(),r.parent=e;let o={key:void 0,sequence:0};if(!w(r,o))throw new y("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 a=Q(e);a.keys[n]={key:o.key,sequence:o.sequence},N(e)},St=(e,t,n)=>{let r=t.childOffset;if(r>=t.children.length)throw new y("right branch borrow failed");let o=t.children[r];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&A(t),e.children.push(o),o.parent=e;let a={key:void 0,sequence:0};if(!w(o,a))throw new y("borrowed child has no min key");e.keys.push(a),o.indexInParent=e.children.length-1;let l=Q(e);w(t,l.keys[n+1])},Ct=(e,t,n,r)=>{for(let a=t.childOffset;a<t.children.length;a+=1){let l=t.children[a];l.parent=n,l.indexInParent=n.children.length,n.children.push(l),n.keys.push(t.keys[a])}let o=Q(t);j(o,r),$(e,o)},kt=(e,t,n,r)=>{for(let a=n.childOffset;a<n.children.length;a+=1){let l=n.children[a];l.parent=t,l.indexInParent=t.children.length,t.children.push(l),t.keys.push(n.keys[a])}let o=Q(t);j(o,r+1),$(e,o)},Mt=(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}},$=(e,t)=>{let n=S(t);if(t===e.root){if(n===1){let i=t.children[t.childOffset];i.parent=null,e.root=i,B(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(n>=e.minBranchChildren)return;let r=t.parent;if(r===null)throw new y("branch has no parent");let o=t.indexInParent,{left:a,right:l}=Mt(r,o);if(l!==null&&S(l)>e.minBranchChildren){St(t,l,o);return}if(a!==null&&S(a)>e.minBranchChildren){gt(t,a,o);return}if(a!==null){Ct(e,t,a,o);return}if(l!==null){kt(e,t,l,o);return}throw new y("no branch siblings to rebalance")};var Ie=(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])},C=(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 y("Leaf node has no parent during rebalance.");let r=t.indexInParent,o=r>n.childOffset?Ne(n.children[r-1]):null,a=r+1<n.children.length?Ne(n.children[r+1]):null;if(a!==null&&s(a)>e.minLeafEntries){let l=_(a);if(l===void 0)throw new y("right leaf borrow failed");t.entries.push(l),w(a,n.keys[r+1]);return}if(o!==null&&s(o)>e.minLeafEntries){let l=o.entries.pop();if(l===void 0)throw new y("left leaf borrow failed");re(t,l),n.keys[r]={key:l.key,sequence:l.entryId},N(t);return}if(o!==null){Ie(o,t),ve(e,t),j(n,r),$(e,n);return}if(a!==null){Ie(t,a),ve(e,a),j(n,r+1),$(e,n);return}throw new y("no leaf siblings to rebalance")};var Ot=(e,t,n)=>{let r=n?Number.MAX_SAFE_INTEGER:0,o=V(e,t,r),a=n?L(e,o,t,Number.MAX_SAFE_INTEGER):x(e,o,t,0);return a>=s(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:a}},At=(e,t,n,r,o)=>{let a=s(t),l=n;for(;l<a;){let i=c(t,l),u=e.compareKeys(i.key,r);if(o?u>=0:u>0)break;l+=1}return l},Rt=(e,t,n,r)=>{if(e.entryKeys!==null)for(let u=n;u<n+r;u+=1)e.entryKeys.delete(c(t,u).entryId);let o=t.entryOffset+n;t.entries.copyWithin(o,o+r),t.entries.length-=r,e.entryCount-=r;let a=s(t)===0;n===0&&!a&&t.parent!==null&&N(t);let l=s(t),i=e.minLeafEntries+4;for(;i>0&&t!==e.root&&s(t)<e.minLeafEntries&&(C(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)i-=1;return a&&s(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&N(t),l},Pt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,ge=(e,t,n,r)=>{if(e.entryCount===0||ue(e.compareKeys,t,n,r))return 0;let o=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive",l=0,i=!0,u=null,d=0;for(;e.entryCount>0;){if(i){let b=Ot(e,t,o);if(b===null)break;u=b.leaf,d=b.idx,i=!1}if(d>=s(u))break;let p=s(u),m=At(e,u,d,n,a),K=m-d;if(K===0)break;let O=Rt(e,u,d,K);if(l+=K,m<p)break;if(!Pt(e,u)){i=!0;continue}if(s(u)>O){i=!0;continue}if(u.next===null)break;u=u.next,d=0}return l};var v=e=>Math.ceil(e/2),k=[{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=k[0];for(let n=1;n<k.length&&e>=k[n].threshold;n+=1)t=k[n];return t},Y=e=>{for(let t=1;t<k.length;t+=1)if(e<k[t].threshold)return k[t].threshold;return Number.MAX_SAFE_INTEGER},Se=e=>{if(typeof e.compareKeys!="function")throw new T("compareKeys must be a function.");let t=e.autoScale===!0;if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new T("autoScale conflicts with explicit capacity.");let n,r;if(t){let l=E(0);n=l.maxLeaf,r=l.maxBranch}else n=q(e.maxLeafEntries,"maxLeafEntries",F),r=q(e.maxBranchChildren,"maxBranchChildren",D);let o=pe(e.duplicateKeys),a=I([],null);return{compareKeys:e.compareKeys,maxLeafEntries:n,maxBranchChildren:r,duplicateKeys:o,minLeafEntries:v(n),minBranchChildren:v(r),root:a,leftmostLeaf:a,rightmostLeaf:a,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,_nextAutoScaleThreshold:t?Y(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:a,index:0}}},Z=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:n}=E(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=v(t)),n>e.maxBranchChildren&&(e.maxBranchChildren=n,e.minBranchChildren=v(n)),e._nextAutoScaleThreshold=Y(e.entryCount)},se=(e,t,n)=>{if(!e.autoScale)return;let r=E(0),o=q(t,"maxLeafEntries",F),a=q(n,"maxBranchChildren",D);if(o<r.maxLeaf||a<r.maxBranch)throw new T("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=a,e.minLeafEntries=v(o),e.minBranchChildren=v(a)};var Ce=(e,t,n)=>{let r=[],o=0;for(;o<e;){let a=e-o;if(a>t&&a-t<n){let i=Math.ceil(a/2);r.push(o+i),r.push(e);break}let l=o+t<e?o+t:e;r.push(l),o=l}return r},_t=(e,t,n,r)=>{let o=Ce(t.length,e.maxLeafEntries,e.minLeafEntries),a=new Array(o.length),l=0;for(let i=0;i<o.length;i+=1){let u=o[i],d=new Array(u-l);for(let p=l;p<u;p+=1){let m=r+p;d[p-l]={key:t[p].key,entryId:m,value:t[p].value},n[p]=m,e.entryKeys!==null&&e.entryKeys.set(m,t[p].key)}a[i]=I(d,null),l=u}return a},ke=(e,t)=>{if(e.entryCount!==0)throw new y("bulk load requires empty tree");let n=e.nextSequence;if(n+t.length>Number.MAX_SAFE_INTEGER)throw new T("Sequence overflow.");let r=new Array(t.length),o=_t(e,t,r,n);e.nextSequence=n+t.length,e.entryCount=t.length;for(let a=0;a<o.length;a+=1)a>0&&(o[a].prev=o[a-1]),a<o.length-1&&(o[a].next=o[a+1]);if(e.leftmostLeaf=o[0],e.rightmostLeaf=o[o.length-1],o.length===1)e.root=o[0];else{let a=o;for(;a.length>1;){let l=Ce(a.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(l.length),u=0;for(let d=0;d<l.length;d+=1)i[d]=g(a.slice(u,l[d]),null),u=l[d];a=i}e.root=a[0]}return Z(e),r};var Me=(e,t,n,r)=>{let o={key:void 0,sequence:0};if(!w(r,o))throw new y("inserted child has no min key");r.parent=t;let a=n.indexInParent-t.childOffset+1;le(t,a,r,o),S(t)>e.maxBranchChildren&&Dt(e,t)},Ft=(e,t)=>{ae(t);let n=Math.ceil(t.entries.length/2),r={kind:R,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=g([t,r],null);return}Me(e,t.parent,t,r)},Dt=(e,t)=>{A(t);let n=Math.ceil(t.children.length/2),r=g(t.children.splice(n),t.parent);if(t.keys.splice(n),t.parent===null){e.root=g([t,r],null);return}Me(e,t.parent,t,r)},Oe=(e,t,n,r)=>{let o=e.nextSequence,a=L(e,t,n,o);if(e.duplicateKeys!=="allow"){let l=null;if(a>0){let i=c(t,a-1);e.compareKeys(i.key,n)===0&&(l=i)}else if(t.prev!==null&&s(t.prev)>0){let i=t.prev,u=c(i,s(i)-1);e.compareKeys(u.key,n)===0&&(l=u)}if(l!==null){if(e.duplicateKeys==="reject")throw new T("Duplicate key rejected.");return l.value=r,l.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new T("Sequence overflow.");return e.nextSequence+=1,oe(t,a,{key:n,entryId:o,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,n),a===0&&t.parent!==null&&N(t),s(t)>e.maxLeafEntries&&Ft(e,t),Z(e),o},Ae=(e,t,n)=>{let r=V(e,t,e.nextSequence);return Oe(e,r,t,n)},Re=e=>{if(e.entryCount===0)return null;let t=_(e.leftmostLeaf);if(t===void 0)throw new y("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&&N(e.leftmostLeaf),e.leftmostLeaf!==e.root&&s(e.leftmostLeaf)<e.minLeafEntries&&C(e,e.leftmostLeaf),t},Pe=e=>{if(e.entryCount===0)return null;let t=ne(e.rightmostLeaf);if(t===void 0)throw new y("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&&C(e,e.rightmostLeaf),t},_e=(e,t)=>{let n=P(e,t);if(n===null)return null;let r=n.leaf,o=n.index,a=c(r,o);return z(r,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(a.entryId),o===0&&s(r)>0&&r.parent!==null&&N(r),r!==e.root&&s(r)<e.minLeafEntries&&C(e,r),a},ce=(e,t,n)=>{let r=V(e,t,n),o=x(e,r,t,n);return o>=s(r)||c(r,o).entryId!==n?null:{leaf:r,index:o}},Fe=(e,t)=>{if(e.entryKeys===null)throw new y("entryKeys lookup map is not enabled on this tree.");let n=e.entryKeys.get(t);if(n===void 0)return null;let r=ce(e,n,t);if(r===null)return null;let o=c(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&&N(r.leaf),r.leaf!==e.root&&s(r.leaf)<e.minLeafEntries&&C(e,r.leaf),o},De=(e,t)=>{if(e.entryKeys===null)throw new y("entryKeys lookup map is not enabled on this tree.");let n=e.entryKeys.get(t);if(n===void 0)return null;let r=ce(e,n,t);return r===null?null:c(r.leaf,r.index)},qe=(e,t,n)=>{if(e.entryKeys===null)throw new y("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let o=ce(e,r,t);if(o===null)return null;let a=c(o.leaf,o.index);return a.value=n,a},Je=(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(o>0)throw new T("putMany: entries not in ascending order.");if(n&&o===0)throw new T(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let r=new Array(t.length),o=V(e,t[0].key,e.nextSequence);for(let a=0;a<t.length;a+=1){let l=t[a],i=me(e,o,l.key,e.nextSequence);r[a]=Oe(e,i,l.key,l.value),o=i}return r}return ke(e,t)};var Ue=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},Xe=e=>{let t=new Array(e.entryCount),n=e.leftmostLeaf,r=0;for(;n!==null;){let o=s(n);for(let a=0;a<o;a+=1){let l=c(n,a);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}},qt=1e6,Jt=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new T(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new T("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new T("BTreeJSON: entries must be array.");if(e.entries.length>qt)throw new T("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 T(`BTreeJSON: bad entries[${t}].`)}},Ut=e=>{let t=(n,r)=>{if(!Number.isInteger(r)||r<G||r>W)throw new T(`BTreeJSON: invalid ${n}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new T(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new T("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new T("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new T("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new T("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 T("BTreeJSON: autoScale capacity below tier-0.")}},He=e=>{Jt(e),Ut(e.config)},ze=(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 Te=e=>{if(B(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}},U=(e,t,n,r,o)=>{let a=e(t,r);return a!==0?a:n<o?-1:n>o?1:0},Ge=e=>{if(B(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:Ge(e.children[e.children.length-1])},J=e=>{if(!Number.isFinite(e))throw new T("compareKeys must return a finite number.");return e},Xt=(e,t)=>{if(J(e(t,t))!==0)throw new T("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Ht=(e,t,n,r)=>{let o=Math.sign(J(e(t,n))),a=Math.sign(J(e(n,r)));if(o<0&&a<0&&Math.sign(J(e(t,r)))>=0)throw new T("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&a>0&&Math.sign(J(e(t,r)))<=0)throw new T("compareKeys must satisfy transitivity for observed key triples.")},We=(e,t)=>{try{Xt(e.compareKeys,t)}catch(n){throw n instanceof T?new y(n.message):n}},je=(e,t,n,r)=>{try{Ht(e.compareKeys,t,n,r)}catch(o){throw o instanceof T?new y(o.message):o}},zt=(e,t,n,r)=>{if(!B(t))throw new y("Leaf linkage cursor reached non-leaf node.");if(r.has(t))throw new y("Cycle detected in leaf linkage.");if(t.prev!==n)throw new y("Leaf prev pointer mismatch.");if(n!==null&&B(n)){let o=Ge(n),a=Te(t);if(o===null||a===null)throw new y("Non-empty tree leaf chain contains empty leaf node.");if(U(e.compareKeys,o.key,o.sequence,a.key,a.sequence)>0)throw new y("Adjacent leaf key ranges are out of order.");let l=s(n),i=s(t);if(e.duplicateKeys!=="allow"&&l>0&&i>0&&e.compareKeys(c(n,l-1).key,c(t,0).key)===0)throw new y("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},$e=(e,t)=>{if(e.entryCount===0){if(!B(e.root))throw new y("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new y("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new y("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new y("Rightmost leaf next pointer must be null.");let n=new Set,r=e.leftmostLeaf,o=null,a=0;for(;r!==null;)zt(e,r,o,n),n.add(r),o=r,r=r.next,a+=1;if(o!==e.rightmostLeaf)throw new y("Rightmost leaf pointer mismatch.");if(a!==t)throw new y("Leaf chain count mismatch with tree traversal count.")};var Gt=(e,t)=>{let n=s(t);for(let r=0;r<n;r+=1)We(e,c(t,r).key);for(let r=1;r<n;r+=1)if(U(e.compareKeys,c(t,r-1).key,c(t,r-1).entryId,c(t,r).key,c(t,r).entryId)>=0)throw new y("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<n;r+=1)if(e.compareKeys(c(t,r-1).key,c(t,r).key)===0)throw new y("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<n;r+=1){let o=c(t,r-2),a=c(t,r-1),l=c(t,r);je(e,o.key,a.key,l.key)}if(n>e.maxLeafEntries)throw new y("Leaf node exceeds maximum occupancy.")},Wt=(e,t,n)=>{Gt(e,t);let r=s(t),o=e.autoScale?Math.ceil(E(0).maxLeaf/2):e.minLeafEntries;if(t!==e.root&&r<o)throw new y("Non-root leaf node violates minimum occupancy.");let a=r===0?null:c(t,0),l=r===0?null:c(t,r-1),i=a===null?null:{key:a.key,sequence:a.entryId},u=l===null?null:{key:l.key,sequence:l.entryId};return{minKey:i,maxKey:u,leafDepth:r===0?null:n,leafCount:1,branchCount:0,entryCount:r}},jt=(e,t)=>{let n=t.children.length-t.childOffset;if(n===0)throw new y("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 y("Non-root branch node violates minimum occupancy.");if(n>e.maxBranchChildren)throw new y("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new y("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 y("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==n)throw new y("Child indexInParent does not match actual position in parent.");let a=Qe(e,o,r+1);if(a.minKey===null||a.maxKey===null)throw new y("Branch child must not be empty in non-root branch tree.");let l=t.keys[n],i=Te(o);if(i===null||U(e.compareKeys,l.key,l.sequence,i.key,i.sequence)!==0)throw new y("Branch cached key does not match actual child minimum key.");return a},Qe=(e,t,n)=>{if(B(t))return Wt(e,t,n);jt(e,t);let r=null,o=0,a=1,l=0,i=null,u=null,d=null;for(let p=t.childOffset;p<t.children.length;p+=1){let m=$t(e,t,p,n);if(r!==null&&m.leafDepth!==null&&m.leafDepth!==r)throw new y("Leaf depth mismatch detected in tree.");if(r===null&&m.leafDepth!==null&&(r=m.leafDepth),d!==null&&U(e.compareKeys,d.key,d.sequence,m.minKey.key,m.minKey.sequence)>=0)throw new y("Branch child key ranges are not strictly ordered.");i===null&&(i=m.minKey),u=m.maxKey,d=m.maxKey,o+=m.leafCount,a+=m.branchCount,l+=m.entryCount}return{minKey:i,maxKey:u,leafDepth:r,leafCount:o,branchCount:a,entryCount:l}},Ye=e=>{let t=Qe(e,e.root,0);if(t.entryCount!==e.entryCount)throw new y("Index entry count mismatch between tree traversal and tracked state.");$e(e,t.leafCount)};var Ze=e=>{if(B(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 a=e.children[o],l=Ze(a);l.height>t&&(t=l.height),n+=l.leafCount,r+=l.branchCount}return{height:t+1,leafCount:n,branchCount:r}},et=e=>{let t=Ze(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var M=class e{constructor(t){this.state=Se(t)}put(t,n){return Ae(this.state,t,n)}putMany(t){return Je(this.state,t)}remove(t){let n=_e(this.state,t);return n===null?null:h(n)}removeById(t){if(this.state.entryKeys===null)throw new T("Requires enableEntryIdLookup: true.");let n=Fe(this.state,t);return n===null?null:h(n)}peekById(t){if(this.state.entryKeys===null)throw new T("Requires enableEntryIdLookup: true.");let n=De(this.state,t);return n===null?null:h(n)}updateById(t,n){if(this.state.entryKeys===null)throw new T("Requires enableEntryIdLookup: true.");let r=qe(this.state,t,n);return r===null?null:h(r)}popFirst(){let t=Re(this.state);return t===null?null:h(t)}peekFirst(){return this.state.entryCount===0?null:h(c(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return h(c(t,s(t)-1))}popLast(){let t=Pe(this.state);return t===null?null:h(t)}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=v(n.maxLeaf),this.state.minBranchChildren=v(n.maxBranch),this.state._nextAutoScaleThreshold=Y(0)}}get(t){let n=P(this.state,t);return n===null?null:c(n.leaf,n.index).value}hasKey(t){return Ke(this.state,t)}findFirst(t){let n=P(this.state,t);return n===null?null:h(c(n.leaf,n.index))}findLast(t){let n=he(this.state,t);return n===null?null:h(c(n.leaf,n.index))}nextHigherKey(t){return Be(this.state,t)}nextLowerKey(t){return Ve(this.state,t)}getPairOrNextLower(t){let n=xe(this.state,t);return n===null?null:h(c(n.leaf,n.index))}count(t,n,r){return ye(this.state,t,n,r)}deleteRange(t,n,r){return ge(this.state,t,n,r)}range(t,n,r){return Le(this.state,t,n,r).map(h)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=s(t);for(let r=0;r<n;r+=1)yield h(c(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 h(c(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 a=0;a<o;a+=1)t.call(n,h(c(r,a)));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 a=0;a<o;a+=1)t[r++]=h(c(n,a));n=n.next}return t}clone(){let t=new e(Ue(this.state));if(se(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 a=s(r);for(let l=0;l<a;l+=1)n[o++]=c(r,l);r=r.next}t.putMany(n)}return t}toJSON(){return Xe(this.state)}static fromJSON(t,n){He(t);let r=t.config.duplicateKeys!=="allow";for(let a=1;a<t.entries.length;a+=1){let l=n(t.entries[a-1][0],t.entries[a][0]);if(l>0)throw new T("fromJSON: entries not sorted.");if(r&&l===0)throw new T('fromJSON: duplicate keys require duplicateKeys "allow".')}let o=new e(ze(t,n));if(se(o.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let a=new Array(t.entries.length);for(let l=0;l<t.entries.length;l+=1)a[l]={key:t.entries[l][0],value:t.entries[l][1]};o.putMany(a)}return o}size(){return this.state.entryCount}assertInvariants(){Ye(this.state)}getStats(){return et(this.state)}};var Qt=16,tt=1024,Yt=1e5,nt=1e6,rt=e=>{let t=e.autoScale===!0,n=t?E(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(n?n.maxLeaf:F),maxBranchChildren:e.maxBranchChildren??(n?n.maxBranch:D),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},ot=e=>{let t=e;throw new f(`Unsupported mutation type from shared store: ${String(t.type)}`)},Zt=e=>{for(let t of e)if(typeof t!="object"||t===null||!("key"in t)||!("value"in t))throw new f("Malformed putMany mutation: each entry must have key and value.")},en=(e,t)=>{if(typeof e.configFingerprint!="string")throw new f("Malformed init mutation: missing configFingerprint.");if(t!==void 0&&e.configFingerprint!==t)throw new f("Config mismatch: store peers must share identical tree config.")},tn=(e,t)=>{switch(e.type){case"init":en(e,t);break;case"put":if(!("key"in e)||!("value"in e))throw new f("Malformed put mutation: missing key or value.");break;case"remove":if(!("key"in e))throw new f("Malformed remove mutation: missing key.");break;case"removeById":if(!("entryId"in e))throw new f("Malformed removeById mutation: missing entryId.");break;case"updateById":if(!("entryId"in e)||!("value"in e))throw new f("Malformed updateById mutation: missing entryId or value.");break;case"popFirst":case"popLast":break;case"putMany":if(!("entries"in e)||!Array.isArray(e.entries))throw new f("Malformed putMany mutation: missing entries array.");Zt(e.entries);break;case"deleteRange":if(!("startKey"in e)||!("endKey"in e))throw new f("Malformed deleteRange mutation: missing startKey or endKey.");break;case"clear":break;default:throw new f(`Unsupported mutation type from shared store: ${String(e.type)}`)}},at=(e,t)=>{for(let n of e){if(typeof n!="object"||n===null)throw new f("Malformed mutation: expected an object.");tn(n,t)}},lt=e=>{if(e===void 0)return Qt;if(!Number.isInteger(e)||e<1||e>tt)throw new f(`maxRetries: integer 1\u2013${tt} required.`);return e},it=e=>{if(e===void 0)return Yt;if(!Number.isInteger(e)||e<1||e>nt)throw new f(`maxSyncMutationsPerBatch: integer 1\u2013${nt} required.`);return e},ut=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new f("readMode: must be 'strong' or 'local'.");return e};function yt(e,t){if(typeof t!="object"||t===null)throw new f("Store contract: append() must return {applied, version}.");let n=t;if(typeof n.applied!="boolean")throw new f("Store contract: applied must be boolean.");if(typeof n.version!="bigint")throw new f("Store contract: version must be bigint.");if(n.applied&&n.version<=e)throw new f("Store contract: applied version must exceed expected.");if(!n.applied&&n.version<e)throw new f("Store contract: rejected version must be >= expected.")}var ee=(e,t,n)=>{switch(t.type){case"init":return n(),null;case"put":return e.put(t.key,t.value);case"putMany":return e.putMany(t.entries);case"remove":return e.remove(t.key);case"removeById":return e.removeById(t.entryId);case"updateById":return e.updateById(t.entryId,t.value);case"popFirst":return e.popFirst();case"popLast":return e.popLast();case"deleteRange":return e.deleteRange(t.startKey,t.endKey,t.options);case"clear":return e.clear(),null;default:return ot(t)}},st=(e,t,n)=>r=>{if(e==="reject"&&r.hasKey(t))throw new T("Duplicate key rejected.");return{type:"put",key:t,value:n}},ct=e=>t=>t.hasKey(e)?{type:"remove",key:e}:null,Tt=e=>t=>t.peekById(e)!==null?{type:"removeById",entryId:e}:null,dt=(e,t)=>n=>n.peekById(e)!==null?{type:"updateById",entryId:e,value:t}:null,ft=()=>e=>e.peekFirst()!==null?{type:"popFirst"}:null,pt=()=>e=>e.peekLast()!==null?{type:"popLast"}:null,mt=(e,t,n)=>r=>{let o=t!=="allow";for(let a=1;a<e.length;a+=1){let l=n(e[a-1].key,e[a].key);if(l>0)throw new T("putMany: entries not in ascending order.");if(o&&l===0)throw new T(t==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(t==="reject"){for(let a of e)if(r.hasKey(a.key))throw new T("Duplicate key rejected.")}return{type:"putMany",entries:e}},ht=(e,t,n)=>r=>r.count(e,t,n)===0?null:{type:"deleteRange",startKey:e,endKey:t,options:n},Kt=()=>()=>({type:"clear"});var X=class{constructor(t){this.store=t.store,this.compareKeys=t.compareKeys,this.maxRetries=lt(t.maxRetries),this.maxSyncMutationsPerBatch=it(t.maxSyncMutationsPerBatch),this.duplicateKeys=t.duplicateKeys??"replace",this.readMode=ut(t.readMode),this.configFingerprint=rt(t),this.tree=new M({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,this.corrupted=!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 f("Store contract: version must be bigint.");if(!Array.isArray(t.mutations))throw new f("Store contract: mutations must be an array.");if(t.mutations.length>this.maxSyncMutationsPerBatch)throw new f(`Sync batch exceeded limit (${String(this.maxSyncMutationsPerBatch)}).`);if(!(t.version<=this.currentVersion)){at(t.mutations,this.configFingerprint);try{for(let n of t.mutations)ee(this.tree,n,()=>{this.initSeen=!0});this.currentVersion=t.version}catch(n){this.corrupted=!0;let r=n instanceof Error?n.message:String(n);throw new f(`Replay failure: instance is permanently corrupted. Discard and create a new instance. Cause: ${r}`)}}}runExclusive(t){let n=async()=>{if(this.corrupted)throw new f("Instance is permanently corrupted. Discard and create a new instance.");return 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,a=this.initSeen?[r]:[{type:"init",configFingerprint:this.configFingerprint},r],l=await this.store.append(o,a);if(yt(o,l),l.applied)try{for(let u of a){if(u===r)break;ee(this.tree,u,()=>{this.initSeen=!0})}let i=ee(this.tree,r,()=>{this.initSeen=!0});return this.currentVersion=l.version,i}catch(i){this.corrupted=!0;let u=i instanceof Error?i.message:String(i);throw new f(`Local apply failure after successful append: instance is permanently corrupted. Discard and create a new instance. Cause: ${u}`)}}throw new f(`Mutation failed after ${String(this.maxRetries)} retries.`)}async put(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(st(this.duplicateKeys,t,n)))}async remove(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(ct(t)))}async removeById(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(Tt(t)))}async updateById(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(dt(t,n)))}async popFirst(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(ft()))}async popLast(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(pt()))}async putMany(t){return t.length===0?[]:this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(mt(t,this.duplicateKeys,this.compareKeys)))}async deleteRange(t,n,r){return this.runExclusive(async()=>await this.appendMutationAndApplyUnlocked(ht(t,n,r))??0)}async clear(){await this.runExclusive(async()=>{await this.appendMutationAndApplyUnlocked(Kt())})}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 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))}async entries(){return this.readOp(t=>Array.from(t.entries()))}async entriesReversed(){return this.readOp(t=>Array.from(t.entriesReversed()))}async keys(){return this.readOp(t=>Array.from(t.keys()))}async values(){return this.readOp(t=>Array.from(t.values()))}async forEach(t){await this.readOp(n=>{n.forEach(t)})}async*[Symbol.asyncIterator](){let t=await this.entries();for(let n of t)yield n}async clone(){return this.readOp(t=>t.clone())}async toJSON(){return this.readOp(t=>t.toJSON())}static fromJSON(t,n){return M.fromJSON(t,n)}};return Nt(nn);})();