@frostpillar/frostpillar-btree 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-JA.md +95 -73
- package/README.md +96 -74
- package/dist/InMemoryBTree.d.ts +3 -36
- package/dist/btree/autoScale.d.ts +1 -0
- package/dist/btree/entry-lookup.d.ts +8 -0
- package/dist/btree/mutations.d.ts +1 -3
- package/dist/btree/rangeQuery.d.ts +4 -1
- package/dist/btree/rebalance-branch.d.ts +4 -0
- package/dist/btree/rebalance.d.ts +5 -2
- package/dist/btree/serialization.d.ts +3 -1
- package/dist/btree/split.d.ts +3 -0
- package/dist/btree/traversal.d.ts +7 -0
- package/dist/btree/types.d.ts +14 -3
- package/dist/{chunk-OWHENPGJ.js → chunk-OFXDCKLC.js} +663 -395
- package/dist/concurrency/ConcurrentInMemoryBTree.d.ts +3 -14
- package/dist/concurrency/coordinator.d.ts +21 -0
- package/dist/concurrency/syncLogValidation.d.ts +2 -0
- package/dist/core.cjs +663 -395
- package/dist/core.d.ts +2 -2
- package/dist/core.js +1 -1
- package/dist/frostpillar-btree-core.min.js +1 -1
- package/dist/frostpillar-btree.min.js +1 -1
- package/dist/index.cjs +856 -545
- package/dist/index.d.ts +1 -1
- package/dist/index.js +190 -147
- package/package.json +2 -2
package/dist/core.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { InMemoryBTree } from './InMemoryBTree.js';
|
|
2
|
-
export type { BTreeEntry, BTreeJSON, BTreeStats, DuplicateKeyPolicy, EntryId, InMemoryBTreeConfig, RangeBounds, } from './InMemoryBTree.js';
|
|
3
|
-
export { BTreeInvariantError, BTreeValidationError
|
|
2
|
+
export type { BTreeEntry, BTreeJSON, BTreeStats, DeleteRebalancePolicy, DuplicateKeyPolicy, EntryId, InMemoryBTreeConfig, RangeBounds, } from './InMemoryBTree.js';
|
|
3
|
+
export { BTreeInvariantError, BTreeValidationError } from './errors.js';
|
|
4
4
|
export type { KeyComparator } from './btree/types.js';
|
package/dist/core.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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
|
+
"use strict";var FrostpillarBTreeCore=(()=>{var $=Object.defineProperty;var Tn=Object.getOwnPropertyDescriptor;var sn=Object.getOwnPropertyNames;var cn=Object.prototype.hasOwnProperty;var fn=(e,n)=>{for(var t in n)$(e,t,{get:n[t],enumerable:!0})},dn=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of sn(n))!cn.call(e,l)&&l!==t&&$(e,l,{get:()=>n[l],enumerable:!(r=Tn(n,l))||r.enumerable});return e};var pn=e=>dn($({},"__esModule",{value:!0}),e);var Un={};fn(Un,{InMemoryBTree:()=>he});var c=class extends Error{constructor(n){super(n),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(n){super(n),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var S=(e,n)=>({kind:0,entries:e,entryOffset:0,parent:n,indexInParent:0,prev:null,next:null}),v=(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 a={key:void 0,sequence:0};if(!x(o,a))throw new u("branch child has no min key");t.push(a)}return r},T=e=>e.entries.length-e.entryOffset,y=(e,n)=>e.entries[e.entryOffset+n],M=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)},P=(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;if(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;else{let l=e.entries.length;r>=l?e.entries.push(t):(e.entries.push(e.entries[l-1]),e.entries.copyWithin(r+1,r,l),e.entries[r]=t)}},j=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},me=1,k=e=>{if(e.childOffset>0){let n=e.childOffset<=me?0:me;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 a=e.childOffset;a<l;a+=1)e.children[a].indexInParent=a;t.indexInParent=l-1}else{e.children.splice(l,0,t),e.keys.splice(l,0,r);for(let a=l;a<e.children.length;a+=1)e.children[a].indexInParent=a}},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&&k(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,z=3,X=16384,A=0;var Ve=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new c("Invalid duplicateKeys option.");return e},Be=e=>{if(e===void 0)return"standard";if(e!=="standard"&&e!=="lazy")throw new c("Invalid deleteRebalancePolicy option.");return e},p=e=>Object.freeze(e),I=(e,n,t)=>Object.freeze({key:e,entryId:n,value:t}),K=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<z||e>X)throw new c(`${n}: integer ${z}\u2013${X} required.`);return e};var Kn=(e,n,t,r)=>{let l=n.childOffset;if(l>=n.children.length)throw new u("branch has no children");let o=l,a=l,i=n.keys.length-1;for(;a<=i;){let s=a+i>>>1,f=n.keys[s],d=e(f.key,t);(d!==0?d:f.sequence<r?-1:f.sequence>r?1:0)<=0?(o=s,a=s+1):i=s-1}return n.children[o]},h=(e,n,t)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==A;)l=Kn(r,l,n,t);return l},m=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,a=n.entries.length;for(;o<a;){let i=o+a>>>1,s=n.entries[i],f=l(s.key,t);(f!==0?f:s.entryId<r?-1:s.entryId>r?1:0)<0?o=i+1:a=i}return o-n.entryOffset},E=(e,n,t,r)=>{let l=e.compareKeys,o=n.entryOffset,a=n.entries.length;for(;o<a;){let i=o+a>>>1,s=n.entries[i],f=l(s.key,t);(f!==0?f:s.entryId<r?-1:s.entryId>r?1:0)<=0?o=i+1:a=i}return o-n.entryOffset},xe=(e,n,t,r)=>{let l=e.compareKeys,o=n,a=32-Math.clz32(e.entryCount+1);for(;a>0&&o.next!==null&&T(o.next)>0;){let i=y(o.next,0),s=l(i.key,t);if(s>0||s===0&&i.entryId>r)break;o=o.next,a-=1}if(a===0&&o.next!==null&&T(o.next)>0){let i=y(o.next,0),s=l(i.key,t);if(s<0||s===0&&i.entryId<=r)return h(e,t,r)}return o},R=(e,n)=>{if(e.entryCount===0)return null;let t=h(e,n,0),r=m(e,t,n,0);if(r>=T(t)&&(t.next===null||(t=t.next,r=m(e,t,n,0),r>=T(t)))||e.compareKeys(y(t,r).key,n)!==0)return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Ee=(e,n)=>{if(e.entryCount===0)return null;let t=h(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=T(t),r===0))||(r-=1,e.compareKeys(y(t,r).key,n)!==0))return null;let l=e._cursor;return l.leaf=t,l.index=r,l},Ne=(e,n)=>R(e,n)!==null,Le=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,Number.MAX_SAFE_INTEGER),l=E(e,r,n,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<T(r)){let o=y(r,l);if(t(o.key,n)>0)return o.key;l+=1}else r=r.next,l=0;return null},we=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,0),l=m(e,r,n,0),o=r,a=l-1;for(;o!==null;){for(;a>=0;){let i=y(o,a);if(t(i.key,n)<0)return i.key;a-=1}o=o.prev,o!==null&&(a=T(o)-1)}return null},be=(e,n)=>{if(e.entryCount===0)return null;let t=e.compareKeys,r=h(e,n,0),l=m(e,r,n,0),o=e._cursor;if(l<T(r)){if(t(y(r,l).key,n)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let a=m(e,r.next,n,0);if(a<T(r.next)&&t(y(r.next,a).key,n)===0)return o.leaf=r.next,o.index=a,o}if(l>0)return o.leaf=r,o.index=l-1,o;if(r.prev!==null){let a=T(r.prev);if(a>0)return o.leaf=r.prev,o.index=a-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",a=r?.upperBound==="exclusive";return o&&a&&l===0}var oe=(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",a=r?.upperBound==="exclusive",i=o?Number.MAX_SAFE_INTEGER:0,s=h(e,n,i),f=o?E(e,s,n,Number.MAX_SAFE_INTEGER):m(e,s,n,0);return{leaf:s,index:f,compare:l,upperExclusive:a}},ae=(e,n,t,r)=>{let l=oe(e,n,t,r);if(l===null)return 0;let o=l.leaf,a=l.index,{compare:i,upperExclusive:s}=l,f=0;for(;o!==null;){let d=T(o);if(a>=d){o=o.next,a=0;continue}let B=y(o,d-1).key;if(W(B,t,i,s)){f+=d-a,o=o.next,a=0;continue}return f+=ie(e,o,t,s,d)-a,f}return f},W=(e,n,t,r)=>{let l=t(e,n);return r?l<0:l<=0},ie=(e,n,t,r,l)=>{let o=r?0:Number.MAX_SAFE_INTEGER,a=r?m(e,n,t,o):E(e,n,t,o);return a<l?a:l},hn=200,mn=(e,n,t,r,l)=>{let o=T(n.leaf);if(o-n.index>=hn&&n.leaf.next!==null){let i=y(n.leaf,o-1).key;if(W(i,r,n.compare,n.upperExclusive))return new Array(ae(e,t,r,l))}return[]},Ce=(e,n,t,r,l,o)=>{if(l)for(let a=n;a<t;a+=1)r[o++]=p(y(e,a));else for(let a=n;a<t;a+=1)r.push(p(y(e,a)));return o},Vn=(e,n,t,r)=>{let{compare:l,upperExclusive:o}=n,a=n.leaf,i=n.index,s=0,f=r.length>0;for(;a!==null;){let d=T(a);if(i>=d){a=a.next,i=0;continue}let B=y(a,d-1).key;if(W(B,t,l,o)){s=Ce(a,i,d,r,f,s),a=a.next,i=0;continue}let C=ie(e,a,t,o,d);Ce(a,i,C,r,f,s);return}},Se=(e,n,t,r)=>{let l=oe(e,n,t,r);if(l===null)return[];let o=mn(e,l,n,t,r);return Vn(e,l,t,o),o},ve=(e,n,t,r,l)=>{let o=oe(e,n,t,l);if(o===null)return;let a=o.leaf,i=o.index,{compare:s,upperExclusive:f}=o;for(;a!==null;){let d=T(a);if(i>=d){a=a.next,i=0;continue}let B=y(a,d-1).key;if(W(B,t,s,f)){for(let N=i;N<d;N+=1)r(p(y(a,N)));a=a.next,i=0;continue}let C=ie(e,a,t,f,d);for(let N=i;N<C;N+=1)r(p(y(a,N)));return}};var V=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}},G=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},ge=e=>{if(K(e))throw new u("expected branch, got leaf");return e},D=(e,n)=>{if(n<e.childOffset||n>=e.children.length)throw new u("child index out of range");ne(e,n)},Bn=(e,n,t)=>{let r=n.children.pop();if(r===void 0)throw new u("left branch borrow failed");n.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!x(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 a=0;a<e.children.length;a+=1)e.children[a].indexInParent=a}let o=G(e);o.keys[t]={key:l.key,sequence:l.sequence},V(e)},xn=(e,n,t)=>{let r=n.childOffset;if(r>=n.children.length)throw new u("right branch borrow failed");let l=n.children[r];n.childOffset+=1,n.childOffset>=n.children.length>>>1&&k(n),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!x(l,o))throw new u("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let a=G(e);x(n,a.keys[t+1])},En=(e,n,t,r)=>{for(let o=n.childOffset;o<n.children.length;o+=1){let a=n.children[o];a.parent=t,a.indexInParent=t.children.length,t.children.push(a),t.keys.push(n.keys[o])}let l=G(n);D(l,r),F(e,l)},Nn=(e,n,t,r)=>{for(let o=t.childOffset;o<t.children.length;o+=1){let a=t.children[o];a.parent=n,a.indexInParent=n.children.length,n.children.push(a),n.keys.push(t.keys[o])}let l=G(n);D(l,r+1),F(e,l)},Ln=(e,n)=>{let t=n>e.childOffset?ge(e.children[n-1]):null,r=n+1<e.children.length?ge(e.children[n+1]):null;return{left:t,right:r}},F=(e,n)=>{let t=g(n);if(n===e.root){if(t===1){let i=n.children[n.childOffset];i.parent=null,e.root=i,K(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(t>=e.minBranchChildren)return;let r=n.parent;if(r===null)throw new u("branch has no parent");let l=n.indexInParent,{left:o,right:a}=Ln(r,l);if(a!==null&&g(a)>e.minBranchChildren){xn(n,a,l);return}if(o!==null&&g(o)>e.minBranchChildren){Bn(n,o,l);return}if(o!==null){En(e,n,o,l);return}if(a!==null){Nn(e,n,a,l);return}throw new u("no branch siblings to rebalance")};var Ie=e=>{if(!K(e))throw new u("expected leaf, got branch");return e},Oe=(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},ke=(e,n)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),e.entries.push(...n.entries.slice(n.entryOffset))},ue=e=>Math.max(1,Math.ceil(e/4)),ye=e=>e.deleteRebalancePolicy==="lazy"?ue(e.minLeafEntries):e.minLeafEntries,wn=(e,n)=>{let t=n>e.childOffset?Ie(e.children[n-1]):null,r=n+1<e.children.length?Ie(e.children[n+1]):null;return{left:t,right:r}},bn=(e,n,t,r,l,o)=>{if(o!==null&&T(o)>e.minLeafEntries){let a=M(o);if(a===void 0)throw new u("right leaf borrow failed");let i=T(n)===0;return n.entries.push(a),x(o,t.keys[r+1]),i&&V(n),!0}if(l!==null&&T(l)>e.minLeafEntries){let a=l.entries.pop();if(a===void 0)throw new u("left leaf borrow failed");return Q(n,a),t.keys[r]={key:a.key,sequence:a.entryId},V(n),!0}return!1},Cn=(e,n,t,r,l,o)=>{if(l!==null){ke(l,n),Oe(e,n),D(t,r),F(e,t);return}if(o!==null){let a=T(n)===0;ke(n,o),Oe(e,o),a&&V(n),D(t,r+1),F(e,t);return}throw new u("no leaf siblings to rebalance")},w=(e,n)=>{if(n===e.root){e.entryCount===0&&(e.leftmostLeaf=n,e.rightmostLeaf=n);return}if(T(n)>=ye(e))return;let t=n.parent;if(t===null)throw new u("Leaf node has no parent during rebalance.");let r=n.indexInParent,{left:l,right:o}=wn(t,r);bn(e,n,t,r,l,o)||Cn(e,n,t,r,l,o)};var Sn=(e,n,t)=>{let r=t?Number.MAX_SAFE_INTEGER:0,l=h(e,n,r),o=t?E(e,l,n,Number.MAX_SAFE_INTEGER):m(e,l,n,0);return o>=T(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},vn=(e,n,t,r)=>{let l=T(n),o=y(n,l-1),a=e.compareKeys(o.key,t);if(r?a<0:a<=0)return l;let i=r?0:Number.MAX_SAFE_INTEGER,s=r?m(e,n,t,i):E(e,n,t,i);return s<l?s:l},gn=(e,n,t,r)=>{if(e.entryKeys!==null)for(let f=t;f<t+r;f+=1)e.entryKeys.delete(y(n,f).entryId);let l=n.entryOffset+t;n.entries.copyWithin(l,l+r),n.entries.length-=r,e.entryCount-=r;let o=T(n)===0;t===0&&!o&&n.parent!==null&&V(n);let a=T(n),i=ye(e),s=e.minLeafEntries+4;for(;s>0&&n!==e.root&&T(n)<i&&(w(e,n),!(n.parent!==null&&n.parent.children[n.indexInParent]!==n));)s-=1;return a},In=(e,n)=>n.parent===null?n===e.root:n.parent.children[n.indexInParent]===n,Ae=(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",a=0,i=!0,s=null,f=0;for(;e.entryCount>0;){if(i){let U=Sn(e,n,l);if(U===null)break;s=U.leaf,f=U.idx,i=!1}if(f>=T(s))break;let d=T(s),B=vn(e,s,t,o),C=B-f;if(C===0)break;let N=gn(e,s,f,C);if(a+=C,B<d)break;if(!In(e,s)){i=!0;continue}if(T(s)>N){i=!0;continue}if(s.next===null)break;s=s.next,f=0}return a};var b=e=>Math.ceil(e/2),O=[{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=O[0];for(let t=1;t<O.length&&e>=O[t].threshold;t+=1)n=O[t];return n},Te=e=>{for(let n=1;n<O.length;n+=1)if(e<O[n].threshold)return O[n].threshold;return Number.MAX_SAFE_INTEGER},On=(e,n)=>{if(n&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new c("autoScale conflicts with explicit capacity.");if(n){let t=L(0);return{maxLeafEntries:t.maxLeaf,maxBranchChildren:t.maxBranch}}return{maxLeafEntries:_(e.maxLeafEntries,"maxLeafEntries",te),maxBranchChildren:_(e.maxBranchChildren,"maxBranchChildren",re)}},Re=e=>{if(typeof e.compareKeys!="function")throw new c("compareKeys must be a function.");let n=e.autoScale===!0,{maxLeafEntries:t,maxBranchChildren:r}=On(e,n),l=Ve(e.duplicateKeys),o=Be(e.deleteRebalancePolicy),a=S([],null);return{compareKeys:e.compareKeys,maxLeafEntries:t,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:b(t),minBranchChildren:b(r),root:a,leftmostLeaf:a,rightmostLeaf:a,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:n,deleteRebalancePolicy:o,_nextAutoScaleThreshold:n?Te(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:a,index:0}}},H=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:n,maxBranch:t}=L(e.entryCount);n>e.maxLeafEntries&&(e.maxLeafEntries=n,e.minLeafEntries=b(n)),t>e.maxBranchChildren&&(e.maxBranchChildren=t,e.minBranchChildren=b(t)),e._nextAutoScaleThreshold=Te(e.entryCount)},se=(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 c("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=l,e.maxBranchChildren=o,e.minLeafEntries=b(l),e.minBranchChildren=b(o)},Me=e=>{let n=L(0);e.maxLeafEntries=n.maxLeaf,e.maxBranchChildren=n.maxBranch,e.minLeafEntries=b(n.maxLeaf),e.minBranchChildren=b(n.maxBranch),e._nextAutoScaleThreshold=Te(0)};var Pe=(e,n,t)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>n&&o-n<t){let i=Math.ceil(o/2);r.push(l+i),r.push(e);break}let a=l+n<e?l+n:e;r.push(a),l=a}return r},kn=(e,n,t,r)=>{let l=Pe(n.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),a=0;for(let i=0;i<l.length;i+=1){let s=l[i],f=new Array(s-a);for(let d=a;d<s;d+=1){let B=r+d;f[d-a]=I(n[d].key,B,n[d].value),t[d]=B,e.entryKeys!==null&&e.entryKeys.set(B,n[d].key)}o[i]=S(f,null),a=s}return o},_e=(e,n)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let t=e.nextSequence;if(t+n.length>Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");let r=new Array(n.length),l=kn(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 a=Pe(o.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(a.length),s=0;for(let f=0;f<a.length;f+=1)i[f]=v(o.slice(s,a[f]),null),s=a[f];o=i}e.root=o[0]}return H(e),r};var De=(e,n,t,r)=>{let l={key:void 0,sequence:0};if(!x(r,l))throw new u("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&&An(e,n)},Fe=(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=v([n,r],null);return}De(e,n.parent,n,r)},An=(e,n)=>{k(n);let t=Math.ceil(n.children.length/2),r=v(n.children.splice(t),n.parent);if(n.keys.splice(t),n.parent===null){e.root=v([n,r],null);return}De(e,n.parent,n,r)};var ce=(e,n,t)=>{let r=h(e,n,t),l=m(e,r,n,t);return l>=T(r)||y(r,l).entryId!==t?null:{leaf:r,index:l}},fe=(e,n)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ce(e,t,n);return r===null?null:y(r.leaf,r.index)},de=(e,n,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(n);if(r===void 0)return null;let l=ce(e,r,n);if(l===null)return null;let o=y(l.leaf,l.index),a=I(o.key,o.entryId,t);return l.leaf.entries[l.leaf.entryOffset+l.index]=a,a},pe=(e,n)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let t=e.entryKeys.get(n);if(t===void 0)return null;let r=ce(e,t,n);if(r===null)return null;let l=y(r.leaf,r.index);return P(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(n),r.index===0&&T(r.leaf)>0&&r.leaf.parent!==null&&V(r.leaf),r.leaf!==e.root&&T(r.leaf)<e.minLeafEntries&&w(e,r.leaf),l};var Rn=(e,n,t,r)=>{if(e.duplicateKeys==="allow")return null;if(r>0){let l=y(n,r-1);if(e.compareKeys(l.key,t)===0)return{leaf:n,physIndex:n.entryOffset+r-1,entry:l}}else if(n.prev!==null&&T(n.prev)>0){let l=n.prev,o=T(l),a=y(l,o-1);if(e.compareKeys(a.key,t)===0)return{leaf:l,physIndex:l.entryOffset+o-1,entry:a}}return null},qe=(e,n,t,r)=>{let l=e.nextSequence,o=E(e,n,t,l),a=Rn(e,n,t,o);if(a!==null){if(e.duplicateKeys==="reject")throw new c("Duplicate key rejected.");return a.leaf.entries[a.physIndex]=I(a.entry.key,a.entry.entryId,r),a.entry.entryId}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");return e.nextSequence+=1,Z(n,o,I(t,l,r)),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,t),o===0&&n.parent!==null&&V(n),T(n)>e.maxLeafEntries&&Fe(e,n),H(e),l},Je=(e,n,t)=>{let r=h(e,n,e.nextSequence);return qe(e,r,n,t)},ze=e=>{if(e.entryCount===0)return null;let n=M(e.leftmostLeaf);if(n===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),T(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&V(e.leftmostLeaf),e.leftmostLeaf!==e.root&&T(e.leftmostLeaf)<e.minLeafEntries&&w(e,e.leftmostLeaf),n},Xe=e=>{if(e.entryCount===0)return null;let n=Y(e.rightmostLeaf);if(n===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(n.entryId),e.rightmostLeaf!==e.root&&T(e.rightmostLeaf)<e.minLeafEntries&&w(e,e.rightmostLeaf),n},We=(e,n)=>{let t=R(e,n);if(t===null)return null;let r=t.leaf,l=t.index,o=y(r,l);return P(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&T(r)>0&&r.parent!==null&&V(r),r!==e.root&&T(r)<e.minLeafEntries&&w(e,r),o},Ge=(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 c("putMany: entries not in ascending order.");if(t&&l===0)throw new c(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=h(e,n[0].key,e.nextSequence);for(let o=0;o<n.length;o+=1){let a=n[o],i=xe(e,l,a.key,e.nextSequence);r[o]=qe(e,i,a.key,a.value),l=i}return r}return _e(e,n)};var He=e=>{let n={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale,deleteRebalancePolicy:e.deleteRebalancePolicy};return e.autoScale||(n.maxLeafEntries=e.maxLeafEntries,n.maxBranchChildren=e.maxBranchChildren),n},Ue=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let o=T(t);for(let a=0;a<o;a+=1){let i=y(t,a);n[r++]=[i.key,i.value]}t=t.next}let l={maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.deleteRebalancePolicy!=="standard"&&(l.deleteRebalancePolicy=e.deleteRebalancePolicy),{version:1,config:l,entries:n}},Mn=1e6,Pn=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new c(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new c("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new c("BTreeJSON: entries must be array.");if(e.entries.length>Mn)throw new c("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 c(`BTreeJSON: bad entries[${n}].`)}},_n=e=>{let n=(t,r)=>{if(!Number.isInteger(r)||r<z||r>X)throw new c(`BTreeJSON: invalid ${t}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new c(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new c("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new c("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new c("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new c("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 c("BTreeJSON: autoScale capacity below tier-0.")}},$e=e=>{Pn(e),_n(e.config)},Ye=(e,n)=>{let t=e.config.duplicateKeys!=="allow";for(let r=1;r<e.entries.length;r+=1){let l=n(e.entries[r-1][0],e.entries[r][0]);if(l>0)throw new c("fromJSON: entries not sorted.");if(t&&l===0)throw new c('fromJSON: duplicate keys require duplicateKeys "allow".')}},Qe=(e,n)=>{let t=e.config,r={compareKeys:n,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale,deleteRebalancePolicy:t.deleteRebalancePolicy};return t.autoScale||(r.maxLeafEntries=t.maxLeafEntries,r.maxBranchChildren=t.maxBranchChildren),r};var Ze=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=T(t);for(let o=0;o<l;o+=1)n[r++]=p(y(t,o));t=t.next}return n},je=e=>{let n=new Array(e.entryCount),t=e.leftmostLeaf,r=0;for(;t!==null;){let l=T(t);for(let o=0;o<l;o+=1)n[r++]=y(t,o);t=t.next}return n},en=(e,n,t)=>{let r=e.leftmostLeaf;for(;r!==null;){let l=T(r);for(let o=0;o<l;o+=1)n.call(t,p(y(r,o)));r=r.next}};var Ke=e=>{if(K(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}},J=(e,n,t,r,l)=>{let o=e(n,r);return o!==0?o:t<l?-1:t>l?1:0},nn=e=>{if(K(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:nn(e.children[e.children.length-1])},q=e=>{if(!Number.isFinite(e))throw new c("compareKeys must return a finite number.");return e},Dn=(e,n)=>{if(q(e(n,n))!==0)throw new c("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Fn=(e,n,t,r)=>{let l=Math.sign(q(e(n,t))),o=Math.sign(q(e(t,r)));if(l<0&&o<0&&Math.sign(q(e(n,r)))>=0)throw new c("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(q(e(n,r)))<=0)throw new c("compareKeys must satisfy transitivity for observed key triples.")},tn=(e,n)=>{try{Dn(e.compareKeys,n)}catch(t){throw t instanceof c?new u(t.message):t}},rn=(e,n,t,r)=>{try{Fn(e.compareKeys,n,t,r)}catch(l){throw l instanceof c?new u(l.message):l}},qn=(e,n,t)=>{let r=nn(n),l=Ke(t);if(r===null||l===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(J(e.compareKeys,r.key,r.sequence,l.key,l.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let o=T(n),a=T(t);if(e.duplicateKeys!=="allow"&&o>0&&a>0&&e.compareKeys(y(n,o-1).key,y(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")},Jn=(e,n,t,r)=>{if(!K(n))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(n))throw new u("Cycle detected in leaf linkage.");if(n.prev!==t)throw new u("Leaf prev pointer mismatch.");t!==null&&K(t)&&qn(e,t,n)},ln=(e,n)=>{if(e.entryCount===0){if(!K(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let t=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)Jn(e,r,l,t),t.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(o!==n)throw new u("Leaf chain count mismatch with tree traversal count.")};var zn=(e,n)=>{let t=T(n);for(let r=0;r<t;r+=1)tn(e,y(n,r).key);for(let r=1;r<t;r+=1)if(J(e.compareKeys,y(n,r-1).key,y(n,r-1).entryId,y(n,r).key,y(n,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<t;r+=1)if(e.compareKeys(y(n,r-1).key,y(n,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<t;r+=1){let l=y(n,r-2),o=y(n,r-1),a=y(n,r);rn(e,l.key,o.key,a.key)}if(t>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Xn=(e,n,t)=>{zn(e,n);let r=T(n),l=e.autoScale?Math.ceil(L(0).maxLeaf/2):e.minLeafEntries;if(e.deleteRebalancePolicy==="lazy"&&(l=ue(l)),n!==e.root&&r<l)throw new u("Non-root leaf node violates minimum occupancy.");let o=r===0?null:y(n,0),a=r===0?null:y(n,r-1),i=o===null?null:{key:o.key,sequence:o.entryId},s=a===null?null:{key:a.key,sequence:a.entryId};return{minKey:i,maxKey:s,leafDepth:r===0?null:t,leafCount:1,branchCount:0,entryCount:r}},Wn=(e,n)=>{let t=n.children.length-n.childOffset;if(t===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(L(0).maxBranch/2):e.minBranchChildren;if(n!==e.root&&t<r)throw new u("Non-root branch node violates minimum occupancy.");if(t>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(n.keys.length!==n.children.length)throw new u("Branch keys array length does not match children array length.")},Gn=(e,n,t,r)=>{let l=n.children[t];if(l.parent!==n)throw new u("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==t)throw new u("Child indexInParent does not match actual position in parent.");let o=on(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 a=n.keys[t],i=Ke(l);if(i===null||J(e.compareKeys,a.key,a.sequence,i.key,i.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return o},Hn=(e,n,t)=>{if(n.leafDepth!==null&&t.leafDepth!==null&&t.leafDepth!==n.leafDepth)throw new u("Leaf depth mismatch detected in tree.");if(n.leafDepth===null&&t.leafDepth!==null&&(n.leafDepth=t.leafDepth),n.previousChildMax!==null&&J(e.compareKeys,n.previousChildMax.key,n.previousChildMax.sequence,t.minKey.key,t.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");n.minKey===null&&(n.minKey=t.minKey),n.maxKey=t.maxKey,n.previousChildMax=t.maxKey,n.leafCount+=t.leafCount,n.branchCount+=t.branchCount,n.entryCount+=t.entryCount},on=(e,n,t)=>{if(K(n))return Xn(e,n,t);Wn(e,n);let r={leafDepth:null,leafCount:0,branchCount:1,entryCount:0,minKey:null,maxKey:null,previousChildMax:null};for(let l=n.childOffset;l<n.children.length;l+=1){let o=Gn(e,n,l,t);Hn(e,r,o)}return{minKey:r.minKey,maxKey:r.maxKey,leafDepth:r.leafDepth,leafCount:r.leafCount,branchCount:r.branchCount,entryCount:r.entryCount}},an=e=>{let n=on(e,e.root,0);if(n.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ln(e,n.leafCount)};var un=e=>{if(K(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],a=un(o);a.height>n&&(n=a.height),t+=a.leafCount,r+=a.branchCount}return{height:n+1,leafCount:t,branchCount:r}},yn=e=>{let n=un(e.root);return{height:n.height,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:e.entryCount}};var he=class e{constructor(n){this.state=Re(n)}put(n,t){return Je(this.state,n,t)}putMany(n){return Ge(this.state,n)}remove(n){let t=We(this.state,n);return t===null?null:p(t)}removeById(n){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let t=pe(this.state,n);return t===null?null:p(t)}peekById(n){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let t=fe(this.state,n);return t===null?null:p(t)}updateById(n,t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=de(this.state,n,t);return r===null?null:p(r)}popFirst(){let n=ze(this.state);return n===null?null:p(n)}peekFirst(){return this.state.entryCount===0?null:p(y(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let n=this.state.rightmostLeaf;return p(y(n,T(n)-1))}popLast(){let n=Xe(this.state);return n===null?null:p(n)}clear(){let n=S([],null);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?.clear(),this.state.autoScale&&Me(this.state)}get(n){let t=R(this.state,n);return t===null?null:y(t.leaf,t.index).value}hasKey(n){return Ne(this.state,n)}findFirst(n){let t=R(this.state,n);return t===null?null:p(y(t.leaf,t.index))}findLast(n){let t=Ee(this.state,n);return t===null?null:p(y(t.leaf,t.index))}nextHigherKey(n){return Le(this.state,n)}nextLowerKey(n){return we(this.state,n)}getPairOrNextLower(n){let t=be(this.state,n);return t===null?null:p(y(t.leaf,t.index))}count(n,t,r){return ae(this.state,n,t,r)}deleteRange(n,t,r){return Ae(this.state,n,t,r)}range(n,t,r){return Se(this.state,n,t,r)}*entries(){let n=this.state.leftmostLeaf;for(;n!==null;){let t=T(n);for(let r=0;r<t;r+=1)yield p(y(n,r));n=n.next}}*entriesReversed(){let n=this.state.rightmostLeaf;for(;n!==null;){let t=T(n);for(let r=t-1;r>=0;r-=1)yield p(y(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()}forEachRange(n,t,r,l){ve(this.state,n,t,r,l)}forEach(n,t){en(this.state,n,t)}snapshot(){return Ze(this.state)}clone(){let n=new e(He(this.state));return se(n.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0&&n.putMany(je(this.state)),n}toJSON(){return Ue(this.state)}static fromJSON(n,t){$e(n),Ye(n,t);let r=new e(Qe(n,t));if(se(r.state,n.config.maxLeafEntries,n.config.maxBranchChildren),n.entries.length>0){let l=new Array(n.entries.length);for(let o=0;o<n.entries.length;o+=1)l[o]={key:n.entries[o][0],value:n.entries[o][1]};r.putMany(l)}return r}size(){return this.state.entryCount}assertInvariants(){an(this.state)}getStats(){return yn(this.state)}};return pn(Un);})();
|
|
@@ -1 +1 @@
|
|
|
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);})();
|
|
1
|
+
"use strict";var FrostpillarBTree=(()=>{var re=Object.defineProperty;var kt=Object.getOwnPropertyDescriptor;var Mt=Object.getOwnPropertyNames;var Ot=Object.prototype.hasOwnProperty;var Rt=(e,t)=>{for(var r in t)re(e,r,{get:t[r],enumerable:!0})},At=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Mt(t))!Ot.call(e,o)&&o!==r&&re(e,o,{get:()=>t[o],enumerable:!(n=kt(t,o))||n.enumerable});return e};var Pt=e=>At(re({},"__esModule",{value:!0}),e);var Er={};Rt(Er,{BTreeConcurrencyError:()=>f,BTreeInvariantError:()=>u,BTreeValidationError:()=>c,ConcurrentInMemoryBTree:()=>j,InMemoryBTree:()=>O});var c=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)}},f=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var g=(e,t)=>({kind:0,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),S=(e,t)=>{let r=[],n={kind:1,children:e,keys:r,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let a=e[o];a.parent=n,a.indexInParent=o;let l={key:void 0,sequence:0};if(!E(a,l))throw new u("branch child has no min key");r.push(l)}return n},T=e=>e.entries.length-e.entryOffset,y=(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()},oe=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},F=(e,t)=>{let r=e.entries.length-e.entryOffset,n=e.entryOffset+t;t<r-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,n),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(n,n+1),e.entries.length-=1)},ae=(e,t,r)=>{let n=e.entryOffset+t;if(e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1)e.entries.copyWithin(e.entryOffset-1,e.entryOffset,n),e.entryOffset-=1,e.entries[n-1]=r;else{let o=e.entries.length;n>=o?e.entries.push(r):(e.entries.push(e.entries[o-1]),e.entries.copyWithin(n+1,n,o),e.entries[n]=r)}},le=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},Ee=1,R=e=>{if(e.childOffset>0){let t=e.childOffset<=Ee?0:Ee;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 r=t;r<e.children.length;r+=1)e.children[r].indexInParent=r}},C=e=>e.children.length-e.childOffset,ie=(e,t,r,n)=>{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]=r,e.keys[o-1]=n;for(let l=e.childOffset;l<o;l+=1)e.children[l].indexInParent=l;r.indexInParent=o-1}else{e.children.splice(o,0,r),e.keys.splice(o,0,n);for(let l=o;l<e.children.length;l+=1)e.children[l].indexInParent=l}},ue=(e,t)=>{let r=t-e.childOffset,n=e.children.length-e.childOffset;if(r<n-1-r){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&&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 o=t;o<e.children.length;o+=1)e.children[o].indexInParent=o}};var D=64,q=64,W=3,G=16384,A=0;var we=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new c("Invalid duplicateKeys option.");return e},be=e=>{if(e===void 0)return"standard";if(e!=="standard"&&e!=="lazy")throw new c("Invalid deleteRebalancePolicy option.");return e},m=e=>Object.freeze(e),k=(e,t,r)=>Object.freeze({key:e,entryId:t,value:r}),h=e=>e.kind===A,E=(e,t)=>{if(e.kind===A){if(e.entryOffset>=e.entries.length)return!1;let r=e.entries[e.entryOffset];return t.key=r.key,t.sequence=r.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},J=(e,t,r)=>{if(e===void 0)return r;if(!Number.isInteger(e)||e<W||e>G)throw new c(`${t}: integer ${W}\u2013${G} required.`);return e};var _t=(e,t,r,n)=>{let o=t.childOffset;if(o>=t.children.length)throw new u("branch has no children");let a=o,l=o,i=t.keys.length-1;for(;l<=i;){let s=l+i>>>1,d=t.keys[s],p=e(d.key,r);(p!==0?p:d.sequence<n?-1:d.sequence>n?1:0)<=0?(a=s,l=s+1):i=s-1}return t.children[a]},K=(e,t,r)=>{let n=e.compareKeys,o=e.root;for(;o.kind!==A;)o=_t(n,o,t,r);return o},B=(e,t,r,n)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,s=t.entries[i],d=o(s.key,r);(d!==0?d:s.entryId<n?-1:s.entryId>n?1:0)<0?a=i+1:l=i}return a-t.entryOffset},b=(e,t,r,n)=>{let o=e.compareKeys,a=t.entryOffset,l=t.entries.length;for(;a<l;){let i=a+l>>>1,s=t.entries[i],d=o(s.key,r);(d!==0?d:s.entryId<n?-1:s.entryId>n?1:0)<=0?a=i+1:l=i}return a-t.entryOffset},Le=(e,t,r,n)=>{let o=e.compareKeys,a=t,l=32-Math.clz32(e.entryCount+1);for(;l>0&&a.next!==null&&T(a.next)>0;){let i=y(a.next,0),s=o(i.key,r);if(s>0||s===0&&i.entryId>n)break;a=a.next,l-=1}if(l===0&&a.next!==null&&T(a.next)>0){let i=y(a.next,0),s=o(i.key,r);if(s<0||s===0&&i.entryId<=n)return K(e,r,n)}return a},P=(e,t)=>{if(e.entryCount===0)return null;let r=K(e,t,0),n=B(e,r,t,0);if(n>=T(r)&&(r.next===null||(r=r.next,n=B(e,r,t,0),n>=T(r)))||e.compareKeys(y(r,n).key,t)!==0)return null;let o=e._cursor;return o.leaf=r,o.index=n,o},Ne=(e,t)=>{if(e.entryCount===0)return null;let r=K(e,t,Number.MAX_SAFE_INTEGER),n=b(e,r,t,Number.MAX_SAFE_INTEGER);if(n===0&&(r.prev===null||(r=r.prev,n=T(r),n===0))||(n-=1,e.compareKeys(y(r,n).key,t)!==0))return null;let o=e._cursor;return o.leaf=r,o.index=n,o},ve=(e,t)=>P(e,t)!==null,Ie=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,Number.MAX_SAFE_INTEGER),o=b(e,n,t,Number.MAX_SAFE_INTEGER);for(;n!==null;)if(o<T(n)){let a=y(n,o);if(r(a.key,t)>0)return a.key;o+=1}else n=n.next,o=0;return null},ge=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,0),o=B(e,n,t,0),a=n,l=o-1;for(;a!==null;){for(;l>=0;){let i=y(a,l);if(r(i.key,t)<0)return i.key;l-=1}a=a.prev,a!==null&&(l=T(a)-1)}return null},Se=(e,t)=>{if(e.entryCount===0)return null;let r=e.compareKeys,n=K(e,t,0),o=B(e,n,t,0),a=e._cursor;if(o<T(n)){if(r(y(n,o).key,t)===0)return a.leaf=n,a.index=o,a}else if(n.next!==null){let l=B(e,n.next,t,0);if(l<T(n.next)&&r(y(n.next,l).key,t)===0)return a.leaf=n.next,a.index=l,a}if(o>0)return a.leaf=n,a.index=o-1,a;if(n.prev!==null){let l=T(n.prev);if(l>0)return a.leaf=n.prev,a.index=l-1,a}return null};function ye(e,t,r,n){let o=e(t,r);if(o>0)return!0;let a=n?.lowerBound==="exclusive",l=n?.upperBound==="exclusive";return a&&l&&o===0}var se=(e,t,r,n)=>{if(e.entryCount===0)return null;let o=e.compareKeys;if(ye(o,t,r,n))return null;let a=n?.lowerBound==="exclusive",l=n?.upperBound==="exclusive",i=a?Number.MAX_SAFE_INTEGER:0,s=K(e,t,i),d=a?b(e,s,t,Number.MAX_SAFE_INTEGER):B(e,s,t,0);return{leaf:s,index:d,compare:o,upperExclusive:l}},Te=(e,t,r,n)=>{let o=se(e,t,r,n);if(o===null)return 0;let a=o.leaf,l=o.index,{compare:i,upperExclusive:s}=o,d=0;for(;a!==null;){let p=T(a);if(l>=p){a=a.next,l=0;continue}let x=y(a,p-1).key;if($(x,r,i,s)){d+=p-l,a=a.next,l=0;continue}return d+=ce(e,a,r,s,p)-l,d}return d},$=(e,t,r,n)=>{let o=r(e,t);return n?o<0:o<=0},ce=(e,t,r,n,o)=>{let a=n?0:Number.MAX_SAFE_INTEGER,l=n?B(e,t,r,a):b(e,t,r,a);return l<o?l:o},Ft=200,Dt=(e,t,r,n,o)=>{let a=T(t.leaf);if(a-t.index>=Ft&&t.leaf.next!==null){let i=y(t.leaf,a-1).key;if($(i,n,t.compare,t.upperExclusive))return new Array(Te(e,r,n,o))}return[]},Ce=(e,t,r,n,o,a)=>{if(o)for(let l=t;l<r;l+=1)n[a++]=m(y(e,l));else for(let l=t;l<r;l+=1)n.push(m(y(e,l)));return a},qt=(e,t,r,n)=>{let{compare:o,upperExclusive:a}=t,l=t.leaf,i=t.index,s=0,d=n.length>0;for(;l!==null;){let p=T(l);if(i>=p){l=l.next,i=0;continue}let x=y(l,p-1).key;if($(x,r,o,a)){s=Ce(l,i,p,n,d,s),l=l.next,i=0;continue}let I=ce(e,l,r,a,p);Ce(l,i,I,n,d,s);return}},ke=(e,t,r,n)=>{let o=se(e,t,r,n);if(o===null)return[];let a=Dt(e,o,t,r,n);return qt(e,o,r,a),a},Me=(e,t,r,n,o)=>{let a=se(e,t,r,o);if(a===null)return;let l=a.leaf,i=a.index,{compare:s,upperExclusive:d}=a;for(;l!==null;){let p=T(l);if(i>=p){l=l.next,i=0;continue}let x=y(l,p-1).key;if($(x,r,s,d)){for(let L=i;L<p;L+=1)n(m(y(l,L)));l=l.next,i=0;continue}let I=ce(e,l,r,d,p);for(let L=i;L<I;L+=1)n(m(y(l,L)));return}};var V=e=>{let t=e;for(;t.parent!==null;){let r=t.indexInParent;if(!E(t,t.parent.keys[r])||r!==t.parent.childOffset)return;t=t.parent}},Q=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},Oe=e=>{if(h(e))throw new u("expected branch, got leaf");return e},z=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");ue(e,t)},Jt=(e,t,r)=>{let n=t.children.pop();if(n===void 0)throw new u("left branch borrow failed");t.keys.pop(),n.parent=e;let o={key:void 0,sequence:0};if(!E(n,o))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=n,e.keys[e.childOffset]=o,n.indexInParent=e.childOffset;else{e.children.unshift(n),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[r]={key:o.key,sequence:o.sequence},V(e)},zt=(e,t,r)=>{let n=t.childOffset;if(n>=t.children.length)throw new u("right branch borrow failed");let o=t.children[n];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&R(t),e.children.push(o),o.parent=e;let a={key:void 0,sequence:0};if(!E(o,a))throw new u("borrowed child has no min key");e.keys.push(a),o.indexInParent=e.children.length-1;let l=Q(e);E(t,l.keys[r+1])},Ut=(e,t,r,n)=>{for(let a=t.childOffset;a<t.children.length;a+=1){let l=t.children[a];l.parent=r,l.indexInParent=r.children.length,r.children.push(l),r.keys.push(t.keys[a])}let o=Q(t);z(o,n),U(e,o)},Xt=(e,t,r,n)=>{for(let a=r.childOffset;a<r.children.length;a+=1){let l=r.children[a];l.parent=t,l.indexInParent=t.children.length,t.children.push(l),t.keys.push(r.keys[a])}let o=Q(t);z(o,n+1),U(e,o)},Ht=(e,t)=>{let r=t>e.childOffset?Oe(e.children[t-1]):null,n=t+1<e.children.length?Oe(e.children[t+1]):null;return{left:r,right:n}},U=(e,t)=>{let r=C(t);if(t===e.root){if(r===1){let i=t.children[t.childOffset];i.parent=null,e.root=i,h(i)&&(e.leftmostLeaf=i,e.rightmostLeaf=i)}return}if(r>=e.minBranchChildren)return;let n=t.parent;if(n===null)throw new u("branch has no parent");let o=t.indexInParent,{left:a,right:l}=Ht(n,o);if(l!==null&&C(l)>e.minBranchChildren){zt(t,l,o);return}if(a!==null&&C(a)>e.minBranchChildren){Jt(t,a,o);return}if(a!==null){Ut(e,t,a,o);return}if(l!==null){Xt(e,t,l,o);return}throw new u("no branch siblings to rebalance")};var Re=e=>{if(!h(e))throw new u("expected leaf, got branch");return e},Ae=(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},Pe=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),e.entries.push(...t.entries.slice(t.entryOffset))},de=e=>Math.max(1,Math.ceil(e/4)),fe=e=>e.deleteRebalancePolicy==="lazy"?de(e.minLeafEntries):e.minLeafEntries,jt=(e,t)=>{let r=t>e.childOffset?Re(e.children[t-1]):null,n=t+1<e.children.length?Re(e.children[t+1]):null;return{left:r,right:n}},Wt=(e,t,r,n,o,a)=>{if(a!==null&&T(a)>e.minLeafEntries){let l=_(a);if(l===void 0)throw new u("right leaf borrow failed");let i=T(t)===0;return t.entries.push(l),E(a,r.keys[n+1]),i&&V(t),!0}if(o!==null&&T(o)>e.minLeafEntries){let l=o.entries.pop();if(l===void 0)throw new u("left leaf borrow failed");return oe(t,l),r.keys[n]={key:l.key,sequence:l.entryId},V(t),!0}return!1},Gt=(e,t,r,n,o,a)=>{if(o!==null){Pe(o,t),Ae(e,t),z(r,n),U(e,r);return}if(a!==null){let l=T(t)===0;Pe(t,a),Ae(e,a),l&&V(t),z(r,n+1),U(e,r);return}throw new u("no leaf siblings to rebalance")},N=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(T(t)>=fe(e))return;let r=t.parent;if(r===null)throw new u("Leaf node has no parent during rebalance.");let n=t.indexInParent,{left:o,right:a}=jt(r,n);Wt(e,t,r,n,o,a)||Gt(e,t,r,n,o,a)};var $t=(e,t,r)=>{let n=r?Number.MAX_SAFE_INTEGER:0,o=K(e,t,n),a=r?b(e,o,t,Number.MAX_SAFE_INTEGER):B(e,o,t,0);return a>=T(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:a}},Qt=(e,t,r,n)=>{let o=T(t),a=y(t,o-1),l=e.compareKeys(a.key,r);if(n?l<0:l<=0)return o;let i=n?0:Number.MAX_SAFE_INTEGER,s=n?B(e,t,r,i):b(e,t,r,i);return s<o?s:o},Yt=(e,t,r,n)=>{if(e.entryKeys!==null)for(let d=r;d<r+n;d+=1)e.entryKeys.delete(y(t,d).entryId);let o=t.entryOffset+r;t.entries.copyWithin(o,o+n),t.entries.length-=n,e.entryCount-=n;let a=T(t)===0;r===0&&!a&&t.parent!==null&&V(t);let l=T(t),i=fe(e),s=e.minLeafEntries+4;for(;s>0&&t!==e.root&&T(t)<i&&(N(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)s-=1;return l},Zt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,_e=(e,t,r,n)=>{if(e.entryCount===0||ye(e.compareKeys,t,r,n))return 0;let o=n?.lowerBound==="exclusive",a=n?.upperBound==="exclusive",l=0,i=!0,s=null,d=0;for(;e.entryCount>0;){if(i){let te=$t(e,t,o);if(te===null)break;s=te.leaf,d=te.idx,i=!1}if(d>=T(s))break;let p=T(s),x=Qt(e,s,r,a),I=x-d;if(I===0)break;let L=Yt(e,s,d,I);if(l+=I,x<p)break;if(!Zt(e,s)){i=!0;continue}if(T(s)>L){i=!0;continue}if(s.next===null)break;s=s.next,d=0}return l};var v=e=>Math.ceil(e/2),M=[{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}],w=e=>{let t=M[0];for(let r=1;r<M.length&&e>=M[r].threshold;r+=1)t=M[r];return t},pe=e=>{for(let t=1;t<M.length;t+=1)if(e<M[t].threshold)return M[t].threshold;return Number.MAX_SAFE_INTEGER},er=(e,t)=>{if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new c("autoScale conflicts with explicit capacity.");if(t){let r=w(0);return{maxLeafEntries:r.maxLeaf,maxBranchChildren:r.maxBranch}}return{maxLeafEntries:J(e.maxLeafEntries,"maxLeafEntries",D),maxBranchChildren:J(e.maxBranchChildren,"maxBranchChildren",q)}},Fe=e=>{if(typeof e.compareKeys!="function")throw new c("compareKeys must be a function.");let t=e.autoScale===!0,{maxLeafEntries:r,maxBranchChildren:n}=er(e,t),o=we(e.duplicateKeys),a=be(e.deleteRebalancePolicy),l=g([],null);return{compareKeys:e.compareKeys,maxLeafEntries:r,maxBranchChildren:n,duplicateKeys:o,minLeafEntries:v(r),minBranchChildren:v(n),root:l,leftmostLeaf:l,rightmostLeaf:l,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,deleteRebalancePolicy:a,_nextAutoScaleThreshold:t?pe(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:l,index:0}}},Y=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:r}=w(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=v(t)),r>e.maxBranchChildren&&(e.maxBranchChildren=r,e.minBranchChildren=v(r)),e._nextAutoScaleThreshold=pe(e.entryCount)},me=(e,t,r)=>{if(!e.autoScale)return;let n=w(0),o=J(t,"maxLeafEntries",D),a=J(r,"maxBranchChildren",q);if(o<n.maxLeaf||a<n.maxBranch)throw new c("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=a,e.minLeafEntries=v(o),e.minBranchChildren=v(a)},De=e=>{let t=w(0);e.maxLeafEntries=t.maxLeaf,e.maxBranchChildren=t.maxBranch,e.minLeafEntries=v(t.maxLeaf),e.minBranchChildren=v(t.maxBranch),e._nextAutoScaleThreshold=pe(0)};var qe=(e,t,r)=>{let n=[],o=0;for(;o<e;){let a=e-o;if(a>t&&a-t<r){let i=Math.ceil(a/2);n.push(o+i),n.push(e);break}let l=o+t<e?o+t:e;n.push(l),o=l}return n},tr=(e,t,r,n)=>{let o=qe(t.length,e.maxLeafEntries,e.minLeafEntries),a=new Array(o.length),l=0;for(let i=0;i<o.length;i+=1){let s=o[i],d=new Array(s-l);for(let p=l;p<s;p+=1){let x=n+p;d[p-l]=k(t[p].key,x,t[p].value),r[p]=x,e.entryKeys!==null&&e.entryKeys.set(x,t[p].key)}a[i]=g(d,null),l=s}return a},Je=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let r=e.nextSequence;if(r+t.length>Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");let n=new Array(t.length),o=tr(e,t,n,r);e.nextSequence=r+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=qe(a.length,e.maxBranchChildren,e.minBranchChildren),i=new Array(l.length),s=0;for(let d=0;d<l.length;d+=1)i[d]=S(a.slice(s,l[d]),null),s=l[d];a=i}e.root=a[0]}return Y(e),n};var ze=(e,t,r,n)=>{let o={key:void 0,sequence:0};if(!E(n,o))throw new u("inserted child has no min key");n.parent=t;let a=r.indexInParent-t.childOffset+1;ie(t,a,n,o),C(t)>e.maxBranchChildren&&rr(e,t)},Ue=(e,t)=>{le(t);let r=Math.ceil(t.entries.length/2),n={kind:A,entries:t.entries.splice(r),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=n:e.rightmostLeaf=n,t.next=n,t.parent===null){e.root=S([t,n],null);return}ze(e,t.parent,t,n)},rr=(e,t)=>{R(t);let r=Math.ceil(t.children.length/2),n=S(t.children.splice(r),t.parent);if(t.keys.splice(r),t.parent===null){e.root=S([t,n],null);return}ze(e,t.parent,t,n)};var he=(e,t,r)=>{let n=K(e,t,r),o=B(e,n,t,r);return o>=T(n)||y(n,o).entryId!==r?null:{leaf:n,index:o}},Ke=(e,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let n=he(e,r,t);return n===null?null:y(n.leaf,n.index)},Be=(e,t,r)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let n=e.entryKeys.get(t);if(n===void 0)return null;let o=he(e,n,t);if(o===null)return null;let a=y(o.leaf,o.index),l=k(a.key,a.entryId,r);return o.leaf.entries[o.leaf.entryOffset+o.index]=l,l},Ve=(e,t)=>{if(e.entryKeys===null)throw new u("entryKeys lookup map is not enabled on this tree.");let r=e.entryKeys.get(t);if(r===void 0)return null;let n=he(e,r,t);if(n===null)return null;let o=y(n.leaf,n.index);return F(n.leaf,n.index),e.entryCount-=1,e.entryKeys.delete(t),n.index===0&&T(n.leaf)>0&&n.leaf.parent!==null&&V(n.leaf),n.leaf!==e.root&&T(n.leaf)<e.minLeafEntries&&N(e,n.leaf),o};var nr=(e,t,r,n)=>{if(e.duplicateKeys==="allow")return null;if(n>0){let o=y(t,n-1);if(e.compareKeys(o.key,r)===0)return{leaf:t,physIndex:t.entryOffset+n-1,entry:o}}else if(t.prev!==null&&T(t.prev)>0){let o=t.prev,a=T(o),l=y(o,a-1);if(e.compareKeys(l.key,r)===0)return{leaf:o,physIndex:o.entryOffset+a-1,entry:l}}return null},Xe=(e,t,r,n)=>{let o=e.nextSequence,a=b(e,t,r,o),l=nr(e,t,r,a);if(l!==null){if(e.duplicateKeys==="reject")throw new c("Duplicate key rejected.");return l.leaf.entries[l.physIndex]=k(l.entry.key,l.entry.entryId,n),l.entry.entryId}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new c("Sequence overflow.");return e.nextSequence+=1,ae(t,a,k(r,o,n)),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,r),a===0&&t.parent!==null&&V(t),T(t)>e.maxLeafEntries&&Ue(e,t),Y(e),o},He=(e,t,r)=>{let n=K(e,t,e.nextSequence);return Xe(e,n,t,r)},je=e=>{if(e.entryCount===0)return null;let t=_(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),T(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&V(e.leftmostLeaf),e.leftmostLeaf!==e.root&&T(e.leftmostLeaf)<e.minLeafEntries&&N(e,e.leftmostLeaf),t},We=e=>{if(e.entryCount===0)return null;let t=ne(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&&T(e.rightmostLeaf)<e.minLeafEntries&&N(e,e.rightmostLeaf),t},Ge=(e,t)=>{let r=P(e,t);if(r===null)return null;let n=r.leaf,o=r.index,a=y(n,o);return F(n,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(a.entryId),o===0&&T(n)>0&&n.parent!==null&&V(n),n!==e.root&&T(n)<e.minLeafEntries&&N(e,n),a},$e=(e,t)=>{if(t.length===0)return[];let r=e.duplicateKeys!=="allow";for(let n=1;n<t.length;n+=1){let o=e.compareKeys(t[n-1].key,t[n].key);if(o>0)throw new c("putMany: entries not in ascending order.");if(r&&o===0)throw new c(e.duplicateKeys==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(e.entryCount>0){let n=new Array(t.length),o=K(e,t[0].key,e.nextSequence);for(let a=0;a<t.length;a+=1){let l=t[a],i=Le(e,o,l.key,e.nextSequence);n[a]=Xe(e,i,l.key,l.value),o=i}return n}return Je(e,t)};var Qe=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale,deleteRebalancePolicy:e.deleteRebalancePolicy};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},Ye=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let a=T(r);for(let l=0;l<a;l+=1){let i=y(r,l);t[n++]=[i.key,i.value]}r=r.next}let o={maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.deleteRebalancePolicy!=="standard"&&(o.deleteRebalancePolicy=e.deleteRebalancePolicy),{version:1,config:o,entries:t}},or=1e6,ar=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new c(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new c("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new c("BTreeJSON: entries must be array.");if(e.entries.length>or)throw new c("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let r=e.entries[t];if(!Array.isArray(r)||r.length!==2)throw new c(`BTreeJSON: bad entries[${t}].`)}},lr=e=>{let t=(r,n)=>{if(!Number.isInteger(n)||n<W||n>G)throw new c(`BTreeJSON: invalid ${r}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new c(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new c("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new c("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new c("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new c("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let r=w(0);if(e.maxLeafEntries<r.maxLeaf||e.maxBranchChildren<r.maxBranch)throw new c("BTreeJSON: autoScale capacity below tier-0.")}},Ze=e=>{ar(e),lr(e.config)},et=(e,t)=>{let r=e.config.duplicateKeys!=="allow";for(let n=1;n<e.entries.length;n+=1){let o=t(e.entries[n-1][0],e.entries[n][0]);if(o>0)throw new c("fromJSON: entries not sorted.");if(r&&o===0)throw new c('fromJSON: duplicate keys require duplicateKeys "allow".')}},tt=(e,t)=>{let r=e.config,n={compareKeys:t,duplicateKeys:r.duplicateKeys,enableEntryIdLookup:r.enableEntryIdLookup,autoScale:r.autoScale,deleteRebalancePolicy:r.deleteRebalancePolicy};return r.autoScale||(n.maxLeafEntries=r.maxLeafEntries,n.maxBranchChildren=r.maxBranchChildren),n};var rt=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let o=T(r);for(let a=0;a<o;a+=1)t[n++]=m(y(r,a));r=r.next}return t},nt=e=>{let t=new Array(e.entryCount),r=e.leftmostLeaf,n=0;for(;r!==null;){let o=T(r);for(let a=0;a<o;a+=1)t[n++]=y(r,a);r=r.next}return t},ot=(e,t,r)=>{let n=e.leftmostLeaf;for(;n!==null;){let o=T(n);for(let a=0;a<o;a+=1)t.call(r,m(y(n,a)));n=n.next}};var xe=e=>{if(h(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}},H=(e,t,r,n,o)=>{let a=e(t,n);return a!==0?a:r<o?-1:r>o?1:0},at=e=>{if(h(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:at(e.children[e.children.length-1])},X=e=>{if(!Number.isFinite(e))throw new c("compareKeys must return a finite number.");return e},ir=(e,t)=>{if(X(e(t,t))!==0)throw new c("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},ur=(e,t,r,n)=>{let o=Math.sign(X(e(t,r))),a=Math.sign(X(e(r,n)));if(o<0&&a<0&&Math.sign(X(e(t,n)))>=0)throw new c("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&a>0&&Math.sign(X(e(t,n)))<=0)throw new c("compareKeys must satisfy transitivity for observed key triples.")},lt=(e,t)=>{try{ir(e.compareKeys,t)}catch(r){throw r instanceof c?new u(r.message):r}},it=(e,t,r,n)=>{try{ur(e.compareKeys,t,r,n)}catch(o){throw o instanceof c?new u(o.message):o}},yr=(e,t,r)=>{let n=at(t),o=xe(r);if(n===null||o===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(H(e.compareKeys,n.key,n.sequence,o.key,o.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let a=T(t),l=T(r);if(e.duplicateKeys!=="allow"&&a>0&&l>0&&e.compareKeys(y(t,a-1).key,y(r,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")},sr=(e,t,r,n)=>{if(!h(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(n.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==r)throw new u("Leaf prev pointer mismatch.");r!==null&&h(r)&&yr(e,r,t)},ut=(e,t)=>{if(e.entryCount===0){if(!h(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 r=new Set,n=e.leftmostLeaf,o=null,a=0;for(;n!==null;)sr(e,n,o,r),r.add(n),o=n,n=n.next,a+=1;if(o!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(a!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var Tr=(e,t)=>{let r=T(t);for(let n=0;n<r;n+=1)lt(e,y(t,n).key);for(let n=1;n<r;n+=1)if(H(e.compareKeys,y(t,n-1).key,y(t,n-1).entryId,y(t,n).key,y(t,n).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let n=1;n<r;n+=1)if(e.compareKeys(y(t,n-1).key,y(t,n).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let n=2;n<r;n+=1){let o=y(t,n-2),a=y(t,n-1),l=y(t,n);it(e,o.key,a.key,l.key)}if(r>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},cr=(e,t,r)=>{Tr(e,t);let n=T(t),o=e.autoScale?Math.ceil(w(0).maxLeaf/2):e.minLeafEntries;if(e.deleteRebalancePolicy==="lazy"&&(o=de(o)),t!==e.root&&n<o)throw new u("Non-root leaf node violates minimum occupancy.");let a=n===0?null:y(t,0),l=n===0?null:y(t,n-1),i=a===null?null:{key:a.key,sequence:a.entryId},s=l===null?null:{key:l.key,sequence:l.entryId};return{minKey:i,maxKey:s,leafDepth:n===0?null:r,leafCount:1,branchCount:0,entryCount:n}},dr=(e,t)=>{let r=t.children.length-t.childOffset;if(r===0)throw new u("Branch node has zero children.");let n=e.autoScale?Math.ceil(w(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&r<n)throw new u("Non-root branch node violates minimum occupancy.");if(r>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.")},fr=(e,t,r,n)=>{let o=t.children[r];if(o.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==r)throw new u("Child indexInParent does not match actual position in parent.");let a=yt(e,o,n+1);if(a.minKey===null||a.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let l=t.keys[r],i=xe(o);if(i===null||H(e.compareKeys,l.key,l.sequence,i.key,i.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return a},pr=(e,t,r)=>{if(t.leafDepth!==null&&r.leafDepth!==null&&r.leafDepth!==t.leafDepth)throw new u("Leaf depth mismatch detected in tree.");if(t.leafDepth===null&&r.leafDepth!==null&&(t.leafDepth=r.leafDepth),t.previousChildMax!==null&&H(e.compareKeys,t.previousChildMax.key,t.previousChildMax.sequence,r.minKey.key,r.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");t.minKey===null&&(t.minKey=r.minKey),t.maxKey=r.maxKey,t.previousChildMax=r.maxKey,t.leafCount+=r.leafCount,t.branchCount+=r.branchCount,t.entryCount+=r.entryCount},yt=(e,t,r)=>{if(h(t))return cr(e,t,r);dr(e,t);let n={leafDepth:null,leafCount:0,branchCount:1,entryCount:0,minKey:null,maxKey:null,previousChildMax:null};for(let o=t.childOffset;o<t.children.length;o+=1){let a=fr(e,t,o,r);pr(e,n,a)}return{minKey:n.minKey,maxKey:n.maxKey,leafDepth:n.leafDepth,leafCount:n.leafCount,branchCount:n.branchCount,entryCount:n.entryCount}},st=e=>{let t=yt(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ut(e,t.leafCount)};var Tt=e=>{if(h(e))return{height:1,leafCount:1,branchCount:0};let t=0,r=0,n=1;for(let o=e.childOffset;o<e.children.length;o+=1){let a=e.children[o],l=Tt(a);l.height>t&&(t=l.height),r+=l.leafCount,n+=l.branchCount}return{height:t+1,leafCount:r,branchCount:n}},ct=e=>{let t=Tt(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var O=class e{constructor(t){this.state=Fe(t)}put(t,r){return He(this.state,t,r)}putMany(t){return $e(this.state,t)}remove(t){let r=Ge(this.state,t);return r===null?null:m(r)}removeById(t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=Ve(this.state,t);return r===null?null:m(r)}peekById(t){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let r=Ke(this.state,t);return r===null?null:m(r)}updateById(t,r){if(this.state.entryKeys===null)throw new c("Requires enableEntryIdLookup: true.");let n=Be(this.state,t,r);return n===null?null:m(n)}popFirst(){let t=je(this.state);return t===null?null:m(t)}peekFirst(){return this.state.entryCount===0?null:m(y(this.state.leftmostLeaf,0))}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return m(y(t,T(t)-1))}popLast(){let t=We(this.state);return t===null?null:m(t)}clear(){let t=g([],null);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?.clear(),this.state.autoScale&&De(this.state)}get(t){let r=P(this.state,t);return r===null?null:y(r.leaf,r.index).value}hasKey(t){return ve(this.state,t)}findFirst(t){let r=P(this.state,t);return r===null?null:m(y(r.leaf,r.index))}findLast(t){let r=Ne(this.state,t);return r===null?null:m(y(r.leaf,r.index))}nextHigherKey(t){return Ie(this.state,t)}nextLowerKey(t){return ge(this.state,t)}getPairOrNextLower(t){let r=Se(this.state,t);return r===null?null:m(y(r.leaf,r.index))}count(t,r,n){return Te(this.state,t,r,n)}deleteRange(t,r,n){return _e(this.state,t,r,n)}range(t,r,n){return ke(this.state,t,r,n)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let r=T(t);for(let n=0;n<r;n+=1)yield m(y(t,n));t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let r=T(t);for(let n=r-1;n>=0;n-=1)yield m(y(t,n));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()}forEachRange(t,r,n,o){Me(this.state,t,r,n,o)}forEach(t,r){ot(this.state,t,r)}snapshot(){return rt(this.state)}clone(){let t=new e(Qe(this.state));return me(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0&&t.putMany(nt(this.state)),t}toJSON(){return Ye(this.state)}static fromJSON(t,r){Ze(t),et(t,r);let n=new e(tt(t,r));if(me(n.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let o=new Array(t.entries.length);for(let a=0;a<t.entries.length;a+=1)o[a]={key:t.entries[a][0],value:t.entries[a][1]};n.putMany(o)}return n}size(){return this.state.entryCount}assertInvariants(){st(this.state)}getStats(){return ct(this.state)}};var mr=16,dt=1024,hr=1e5,ft=1e6,pt=e=>{let t=e.autoScale===!0,r=t?w(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(r?r.maxLeaf:D),maxBranchChildren:e.maxBranchChildren??(r?r.maxBranch:q),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},mt=e=>{let t=e;throw new f(`Unsupported mutation type from shared store: ${String(t.type)}`)},Kr=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.")},Br=(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.")},Vr=e=>{switch(e.type){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"putMany":if(!("entries"in e)||!Array.isArray(e.entries))throw new f("Malformed putMany mutation: missing entries array.");Kr(e.entries);break;case"deleteRange":if(!("startKey"in e)||!("endKey"in e))throw new f("Malformed deleteRange mutation: missing startKey or endKey.");break;default:break}},xr=(e,t)=>{switch(e.type){case"init":Br(e,t);break;case"put":case"remove":case"removeById":case"updateById":case"putMany":case"deleteRange":Vr(e);break;case"popFirst":case"popLast":case"clear":break;default:throw new f(`Unsupported mutation type from shared store: ${String(e.type)}`)}},ht=(e,t)=>{for(let r of e){if(typeof r!="object"||r===null)throw new f("Malformed mutation: expected an object.");xr(r,t)}},Kt=e=>{if(e===void 0)return mr;if(!Number.isInteger(e)||e<1||e>dt)throw new f(`maxRetries: integer 1\u2013${dt} required.`);return e},Bt=e=>{if(e===void 0)return hr;if(!Number.isInteger(e)||e<1||e>ft)throw new f(`maxSyncMutationsPerBatch: integer 1\u2013${ft} required.`);return e},Vt=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new f("readMode: must be 'strong' or 'local'.");return e};function xt(e,t){if(typeof t!="object"||t===null)throw new f("Store contract: append() must return {applied, version}.");let r=t;if(typeof r.applied!="boolean")throw new f("Store contract: applied must be boolean.");if(typeof r.version!="bigint")throw new f("Store contract: version must be bigint.");if(r.applied&&r.version<=e)throw new f("Store contract: applied version must exceed expected.");if(!r.applied&&r.version<e)throw new f("Store contract: rejected version must be >= expected.")}var Z=(e,t,r)=>{switch(t.type){case"init":return r(),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 mt(t)}},Et=(e,t,r)=>n=>{if(e==="reject"&&n.hasKey(t))throw new c("Duplicate key rejected.");return{type:"put",key:t,value:r}},wt=e=>t=>t.hasKey(e)?{type:"remove",key:e}:null,bt=e=>t=>t.peekById(e)!==null?{type:"removeById",entryId:e}:null,Lt=(e,t)=>r=>r.peekById(e)!==null?{type:"updateById",entryId:e,value:t}:null,Nt=()=>e=>e.peekFirst()!==null?{type:"popFirst"}:null,vt=()=>e=>e.peekLast()!==null?{type:"popLast"}:null,It=(e,t,r)=>n=>{let o=t!=="allow";for(let a=1;a<e.length;a+=1){let l=r(e[a-1].key,e[a].key);if(l>0)throw new c("putMany: entries not in ascending order.");if(o&&l===0)throw new c(t==="reject"?"putMany: duplicate key rejected.":"putMany: equal keys not allowed in strict mode.")}if(t==="reject"){for(let a of e)if(n.hasKey(a.key))throw new c("Duplicate key rejected.")}return{type:"putMany",entries:e}},gt=(e,t,r)=>n=>n.count(e,t,r)===0?null:{type:"deleteRange",startKey:e,endKey:t,options:r},St=()=>()=>({type:"clear"});var Ct=(e,t)=>{if(typeof e.version!="bigint")throw new f("Store contract: version must be bigint.");if(!Array.isArray(e.mutations))throw new f("Store contract: mutations must be an array.");if(e.mutations.length>t)throw new f(`Sync batch exceeded limit (${String(t)}).`)};var ee=class{constructor(t,r,n,o,a,l){this.tree=t,this.store=r,this.maxRetries=n,this.maxSyncMutationsPerBatch=o,this.configFingerprint=a,this.readMode=l,this.currentVersion=0n,this.operationQueue=Promise.resolve(),this.initSeen=!1,this.corrupted=!1}async syncUnlocked(){let t=await this.store.getLogEntriesSince(this.currentVersion);if(Ct(t,this.maxSyncMutationsPerBatch),!(t.version<=this.currentVersion)){ht(t.mutations,this.configFingerprint);try{let r=()=>{this.initSeen=!0};for(let n of t.mutations)Z(this.tree,n,r);this.currentVersion=t.version}catch(r){this.corrupted=!0;let n=r instanceof Error?r.message:String(r);throw new f(`Replay failure: instance is permanently corrupted. Discard and create a new instance. Cause: ${n}`)}}}runExclusive(t){let r=async()=>{if(this.corrupted)throw new f("Instance is permanently corrupted. Discard and create a new instance.");return t()},n=this.operationQueue.then(r,r);return this.operationQueue=n.then(()=>{},()=>{}),n}readOp(t){return this.runExclusive(async()=>(this.readMode==="strong"&&await this.syncUnlocked(),t(this.tree)))}async appendAndApply(t){for(let r=0;r<this.maxRetries;r+=1){await this.syncUnlocked();let n=t(this.tree);if(n===null)return null;let o=this.currentVersion,a=this.initSeen?[n]:[{type:"init",configFingerprint:this.configFingerprint},n],l=await this.store.append(o,a);if(xt(o,l),l.applied)try{let i=()=>{this.initSeen=!0};for(let d of a){if(d===n)break;Z(this.tree,d,i)}let s=Z(this.tree,n,i);return this.currentVersion=l.version,s}catch(i){this.corrupted=!0;let s=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: ${s}`)}}throw new f(`Mutation failed after ${String(this.maxRetries)} retries.`)}writeOp(t){return this.runExclusive(async()=>this.appendAndApply(t))}};var j=class{constructor(t){this.compareKeys=t.compareKeys,this.duplicateKeys=t.duplicateKeys??"replace";let r=new O({compareKeys:t.compareKeys,maxLeafEntries:t.maxLeafEntries,maxBranchChildren:t.maxBranchChildren,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale,deleteRebalancePolicy:t.deleteRebalancePolicy});this.coord=new ee(r,t.store,Kt(t.maxRetries),Bt(t.maxSyncMutationsPerBatch),pt(t),Vt(t.readMode))}async sync(){await this.coord.runExclusive(async()=>{await this.coord.syncUnlocked()})}async syncThenRead(t){return this.coord.runExclusive(async()=>(await this.coord.syncUnlocked(),t(this.coord.tree)))}async put(t,r){return this.coord.writeOp(Et(this.duplicateKeys,t,r))}async remove(t){return this.coord.writeOp(wt(t))}async removeById(t){return this.coord.writeOp(bt(t))}async updateById(t,r){return this.coord.writeOp(Lt(t,r))}async popFirst(){return this.coord.writeOp(Nt())}async popLast(){return this.coord.writeOp(vt())}async putMany(t){return t.length===0?[]:this.coord.writeOp(It(t,this.duplicateKeys,this.compareKeys))}async deleteRange(t,r,n){return await this.coord.writeOp(gt(t,r,n))??0}async clear(){await this.coord.writeOp(St())}async get(t){return this.coord.readOp(r=>r.get(t))}async hasKey(t){return this.coord.readOp(r=>r.hasKey(t))}async findFirst(t){return this.coord.readOp(r=>r.findFirst(t))}async findLast(t){return this.coord.readOp(r=>r.findLast(t))}async range(t,r,n){return this.coord.readOp(o=>o.range(t,r,n))}async snapshot(){return this.coord.readOp(t=>t.snapshot())}async size(){return this.coord.readOp(t=>t.size())}async assertInvariants(){await this.coord.readOp(t=>t.assertInvariants())}async getStats(){return this.coord.readOp(t=>t.getStats())}async peekFirst(){return this.coord.readOp(t=>t.peekFirst())}async peekLast(){return this.coord.readOp(t=>t.peekLast())}async peekById(t){return this.coord.readOp(r=>r.peekById(t))}async count(t,r,n){return this.coord.readOp(o=>o.count(t,r,n))}async nextHigherKey(t){return this.coord.readOp(r=>r.nextHigherKey(t))}async nextLowerKey(t){return this.coord.readOp(r=>r.nextLowerKey(t))}async getPairOrNextLower(t){return this.coord.readOp(r=>r.getPairOrNextLower(t))}async entries(){return this.coord.readOp(t=>Array.from(t.entries()))}async entriesReversed(){return this.coord.readOp(t=>Array.from(t.entriesReversed()))}async keys(){return this.coord.readOp(t=>Array.from(t.keys()))}async values(){return this.coord.readOp(t=>Array.from(t.values()))}async forEach(t){await this.coord.readOp(r=>{r.forEach(t)})}async forEachRange(t,r,n,o){await this.coord.readOp(a=>{a.forEachRange(t,r,n,o)})}async*[Symbol.asyncIterator](){let t=await this.entries();for(let r of t)yield r}async clone(){return this.coord.readOp(t=>t.clone())}async toJSON(){return this.coord.readOp(t=>t.toJSON())}static fromJSON(t,r){return O.fromJSON(t,r)}};return Pt(Er);})();
|