@frostpillar/frostpillar-btree 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README-JA.md +912 -0
- package/README.md +912 -0
- package/dist/InMemoryBTree.d.ts +45 -0
- package/dist/btree/autoScale.d.ts +9 -0
- package/dist/btree/bulkLoad.d.ts +5 -0
- package/dist/btree/deleteRange.d.ts +3 -0
- package/dist/btree/integrity-helpers.d.ts +6 -0
- package/dist/btree/integrity.d.ts +2 -0
- package/dist/btree/mutations.d.ts +12 -0
- package/dist/btree/navigation.d.ts +23 -0
- package/dist/btree/rangeQuery.d.ts +3 -0
- package/dist/btree/rebalance.d.ts +4 -0
- package/dist/btree/serialization.d.ts +20 -0
- package/dist/btree/stats.d.ts +2 -0
- package/dist/btree/types.d.ts +113 -0
- package/dist/chunk-ZA3EQNDI.js +1902 -0
- package/dist/concurrency/ConcurrentInMemoryBTree.d.ts +56 -0
- package/dist/concurrency/helpers.d.ts +27 -0
- package/dist/concurrency/index.d.ts +2 -0
- package/dist/concurrency/types.d.ts +41 -0
- package/dist/core.cjs +1919 -0
- package/dist/core.d.ts +4 -0
- package/dist/core.js +10 -0
- package/dist/errors.d.ts +9 -0
- package/dist/frostpillar-btree-core.min.js +1 -0
- package/dist/frostpillar-btree.min.js +1 -0
- package/dist/index.cjs +2230 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +316 -0
- package/package.json +80 -0
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { InMemoryBTree } from './InMemoryBTree.js';
|
|
2
|
+
export type { BTreeEntry, BTreeJSON, BTreeStats, DuplicateKeyPolicy, EntryId, InMemoryBTreeConfig, RangeBounds, } from './InMemoryBTree.js';
|
|
3
|
+
export { BTreeInvariantError, BTreeValidationError, } from './errors.js';
|
|
4
|
+
export type { KeyComparator } from './btree/types.js';
|
package/dist/core.js
ADDED
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class BTreeValidationError extends Error {
|
|
2
|
+
constructor(message: string);
|
|
3
|
+
}
|
|
4
|
+
export declare class BTreeInvariantError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class BTreeConcurrencyError extends Error {
|
|
8
|
+
constructor(message: string);
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var FrostpillarBTreeCore=(()=>{var U=Object.defineProperty;var Ye=Object.getOwnPropertyDescriptor;var Qe=Object.getOwnPropertyNames;var Ze=Object.prototype.hasOwnProperty;var je=(e,t)=>{for(var n in t)U(e,n,{get:t[n],enumerable:!0})},et=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let l of Qe(t))!Ze.call(e,l)&&l!==n&&U(e,l,{get:()=>t[l],enumerable:!(r=Ye(t,l))||r.enumerable});return e};var tt=e=>et(U({},"__esModule",{value:!0}),e);var gt={};je(gt,{InMemoryBTree:()=>te});var f=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}};var z=64,$=64,q=3,F=16384,C=0,nt=1,re=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new f("Invalid duplicateKeys option.");return e},p=e=>e.kind===C,N=(e,t)=>{if(e.kind===C){if(e.entryOffset>=e.entries.length)return!1;let n=e.entries[e.entryOffset];return t.key=n.key,t.sequence=n.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},M=(e,t,n)=>{if(e===void 0)return n;if(!Number.isInteger(e)||e<q||e>F)throw new f(`${t}: integer ${q}\u2013${F} required.`);return e},I=(e,t)=>({kind:C,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),k=(e,t)=>{let n=[],r={kind:nt,children:e,keys:n,childOffset:0,parent:t,indexInParent:0};for(let l=0;l<e.length;l+=1){let o=e[l];o.parent=r,o.indexInParent=l;let i={key:void 0,sequence:0};if(!N(o,i))throw new u("branch child has no min key");n.push(i)}return r},y=e=>e.entries.length-e.entryOffset,T=(e,t)=>e.entries[e.entryOffset+t],D=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},le=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},oe=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},Y=(e,t)=>{let n=e.entries.length-e.entryOffset,r=e.entryOffset+t;t<n-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},ie=(e,t,n)=>{let r=e.entryOffset+t;e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1?(e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=n):e.entries.splice(r,0,n)},ae=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},ne=1,R=e=>{if(e.childOffset>0){let t=e.childOffset<=ne?0:ne;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let n=t;n<e.children.length;n+=1)e.children[n].indexInParent=n}},O=e=>e.children.length-e.childOffset,ue=(e,t,n,r)=>{let l=e.childOffset+t,o=e.children.length-e.childOffset;if(e.childOffset>0&&t<o>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,l),e.keys.copyWithin(e.childOffset-1,e.childOffset,l),e.childOffset-=1,e.children[l-1]=n,e.keys[l-1]=r;for(let i=e.childOffset;i<l;i+=1)e.children[i].indexInParent=i;n.indexInParent=l-1}else{e.children.splice(l,0,n),e.keys.splice(l,0,r);for(let i=l;i<e.children.length;i+=1)e.children[i].indexInParent=i}},ye=(e,t)=>{let n=t-e.childOffset,r=e.children.length-e.childOffset;if(n<r-1-n){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let l=e.childOffset;l<=t;l+=1)e.children[l].indexInParent=l;e.childOffset>=e.children.length>>>1&&R(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let l=t;l<e.children.length;l+=1)e.children[l].indexInParent=l}};var rt=(e,t,n,r)=>{let l=t.childOffset;if(l>=t.children.length)throw new u("branch has no children");let o=l,i=l,a=t.keys.length-1;for(;i<=a;){let s=i+a>>>1,c=t.keys[s],d=e(c.key,n);(d!==0?d:c.sequence-r)<=0?(o=s,i=s+1):a=s-1}return t.children[o]},K=(e,t,n)=>{let r=e.compareKeys,l=e.root;for(;l.kind!==C;)l=rt(r,l,t,n);return l},m=(e,t,n,r)=>{let l=e.compareKeys,o=t.entryOffset,i=t.entries.length;for(;o<i;){let a=o+i>>>1,s=t.entries[a],c=l(s.key,n);(c!==0?c:s.entryId-r)<0?o=a+1:i=a}return o-t.entryOffset},B=(e,t,n,r)=>{let l=e.compareKeys,o=t.entryOffset,i=t.entries.length;for(;o<i;){let a=o+i>>>1,s=t.entries[a],c=l(s.key,n);(c!==0?c:s.entryId-r)<=0?o=a+1:i=a}return o-t.entryOffset},Te=(e,t,n,r)=>{let l=e.compareKeys,o=t,i=32-Math.clz32(e.entryCount+1);for(;i>0&&o.next!==null&&y(o.next)>0;){let a=T(o.next,0),s=l(a.key,n);if(s>0||s===0&&a.entryId>r)break;o=o.next,i-=1}if(i===0&&o.next!==null&&y(o.next)>0){let a=T(o.next,0),s=l(a.key,n);if(s<0||s===0&&a.entryId<=r)return K(e,n,r)}return o},A=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,0),r=m(e,n,t,0);if(r>=y(n)&&(n.next===null||(n=n.next,r=m(e,n,t,0),r>=y(n)))||e.compareKeys(T(n,r).key,t)!==0)return null;let l=e._cursor;return l.leaf=n,l.index=r,l},se=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,Number.MAX_SAFE_INTEGER),r=B(e,n,t,Number.MAX_SAFE_INTEGER);if(r===0&&(n.prev===null||(n=n.prev,r=y(n),r===0))||(r-=1,e.compareKeys(T(n,r).key,t)!==0))return null;let l=e._cursor;return l.leaf=n,l.index=r,l},ce=(e,t)=>A(e,t)!==null,fe=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,Number.MAX_SAFE_INTEGER),l=B(e,r,t,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(l<y(r)){let o=T(r,l);if(n(o.key,t)>0)return o.key;l+=1}else r=r.next,l=0;return null},de=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),l=m(e,r,t,0),o=r,i=l-1;for(;o!==null;){for(;i>=0;){let a=T(o,i);if(n(a.key,t)<0)return a.key;i-=1}o=o.prev,o!==null&&(i=y(o)-1)}return null},he=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),l=m(e,r,t,0),o=e._cursor;if(l<y(r)){if(n(T(r,l).key,t)===0)return o.leaf=r,o.index=l,o}else if(r.next!==null){let i=m(e,r.next,t,0);if(i<y(r.next)&&n(T(r.next,i).key,t)===0)return o.leaf=r.next,o.index=i,o}if(l>0)return o.leaf=r,o.index=l-1,o;if(r.prev!==null){let i=y(r.prev);if(i>0)return o.leaf=r.prev,o.index=i-1,o}return null};var x=e=>{let t=e;for(;t.parent!==null;){let n=t.indexInParent;if(!N(t,t.parent.keys[n])||n!==t.parent.childOffset)return;t=t.parent}},H=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},pe=e=>{if(!p(e))throw new u("expected leaf, got branch");return e},Ke=e=>{if(p(e))throw new u("expected branch, got leaf");return e},J=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");ye(e,t)},me=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},lt=(e,t,n)=>{let r=t.children.pop();if(r===void 0)throw new u("left branch borrow failed");t.keys.pop(),r.parent=e;let l={key:void 0,sequence:0};if(!N(r,l))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=l,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(l);for(let i=0;i<e.children.length;i+=1)e.children[i].indexInParent=i}let o=H(e);o.keys[n]={key:l.key,sequence:l.sequence},x(e)},ot=(e,t,n)=>{let r=t.childOffset;if(r>=t.children.length)throw new u("right branch borrow failed");let l=t.children[r];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&R(t),e.children.push(l),l.parent=e;let o={key:void 0,sequence:0};if(!N(l,o))throw new u("borrowed child has no min key");e.keys.push(o),l.indexInParent=e.children.length-1;let i=H(e);N(t,i.keys[n+1])},it=(e,t,n,r)=>{for(let o=t.childOffset;o<t.children.length;o+=1){let i=t.children[o];i.parent=n,i.indexInParent=n.children.length,n.children.push(i),n.keys.push(t.keys[o])}let l=H(t);J(l,r),X(e,l)},at=(e,t,n,r)=>{for(let o=n.childOffset;o<n.children.length;o+=1){let i=n.children[o];i.parent=t,i.indexInParent=t.children.length,t.children.push(i),t.keys.push(n.keys[o])}let l=H(t);J(l,r+1),X(e,l)},ut=(e,t)=>{let n=t>e.childOffset?Ke(e.children[t-1]):null,r=t+1<e.children.length?Ke(e.children[t+1]):null;return{left:n,right:r}},X=(e,t)=>{let n=O(t);if(t===e.root){if(n===1){let a=t.children[t.childOffset];a.parent=null,e.root=a,p(a)&&(e.leftmostLeaf=a,e.rightmostLeaf=a)}return}if(n>=e.minBranchChildren)return;let r=t.parent;if(r===null)throw new u("branch has no parent");let l=t.indexInParent,{left:o,right:i}=ut(r,l);if(i!==null&&O(i)>e.minBranchChildren){ot(t,i,l);return}if(o!==null&&O(o)>e.minBranchChildren){lt(t,o,l);return}if(o!==null){it(e,t,o,l);return}if(i!==null){at(e,t,i,l);return}throw new u("no branch siblings to rebalance")};var Ve=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0);let n=t.entries;for(let r=t.entryOffset;r<n.length;r+=1)e.entries.push(n[r])},S=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(y(t)>=e.minLeafEntries)return;let n=t.parent;if(n===null)throw new u("Leaf node has no parent during rebalance.");let r=t.indexInParent,l=r>n.childOffset?pe(n.children[r-1]):null,o=r+1<n.children.length?pe(n.children[r+1]):null;if(o!==null&&y(o)>e.minLeafEntries){let i=D(o);if(i===void 0)throw new u("right leaf borrow failed");t.entries.push(i),N(o,n.keys[r+1]);return}if(l!==null&&y(l)>e.minLeafEntries){let i=l.entries.pop();if(i===void 0)throw new u("left leaf borrow failed");oe(t,i),n.keys[r]={key:i.key,sequence:i.entryId},x(t);return}if(l!==null){Ve(l,t),me(e,t),J(n,r),X(e,n);return}if(o!==null){Ve(t,o),me(e,o),J(n,r+1),X(e,n);return}throw new u("no leaf siblings to rebalance")};var yt=(e,t,n)=>{let r=n?Number.MAX_SAFE_INTEGER:0,l=K(e,t,r),o=n?B(e,l,t,Number.MAX_SAFE_INTEGER):m(e,l,t,0);return o>=y(l)?l.next===null?null:{leaf:l.next,idx:0}:{leaf:l,idx:o}},Tt=(e,t,n,r,l)=>{let o=y(t),i=n;for(;i<o;){let a=T(t,i),s=e.compareKeys(a.key,r);if(l?s>=0:s>0)break;i+=1}return i},st=e=>{let t=0,n=e.root;for(;!p(n);)n=n.children[n.childOffset],t+=1;return t},ct=(e,t,n,r,l)=>{if(e.entryKeys!==null)for(let c=n;c<n+r;c+=1)e.entryKeys.delete(T(t,c).entryId);let o=t.entryOffset+n;t.entries.copyWithin(o,o+r),t.entries.length-=r,e.entryCount-=r;let i=y(t)===0;n===0&&!i&&t.parent!==null&&x(t);let a=y(t),s=l;for(;s>0&&t!==e.root&&y(t)<e.minLeafEntries&&(S(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)s-=1;return i&&y(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&x(t),a},ft=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,Be=(e,t,n,r)=>{if(e.entryCount===0)return 0;let l=e.compareKeys(t,n);if(l>0)return 0;let o=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive";if(o&&i&&l===0)return 0;let a=st(e),s=0,c=!0,d=null,h=0;for(;e.entryCount>0;){if(c){let v=yt(e,t,o);if(v===null)break;d=v.leaf,h=v.idx,c=!1}if(h>=y(d))break;let V=y(d),w=Tt(e,d,h,n,i),L=w-h;if(L===0)break;let g=ct(e,d,h,L,a);if(s+=L,w<V)break;if(!ft(e,d)){c=!0;continue}if(y(d)>g){c=!0;continue}if(d.next===null)break;d=d.next,h=0}return s};var b=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],E=e=>{let t=b[0];for(let n=1;n<b.length&&e>=b[n].threshold;n+=1)t=b[n];return t},G=e=>{for(let t=1;t<b.length;t+=1)if(e<b[t].threshold)return b[t].threshold;return Number.MAX_SAFE_INTEGER},xe=e=>{if(typeof e.compareKeys!="function")throw new f("compareKeys must be a function.");let t=e.autoScale===!0;if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new f("autoScale conflicts with explicit capacity.");let n,r;if(t){let i=E(0);n=i.maxLeaf,r=i.maxBranch}else n=M(e.maxLeafEntries,"maxLeafEntries",z),r=M(e.maxBranchChildren,"maxBranchChildren",$);let l=re(e.duplicateKeys),o=I([],null);return{compareKeys:e.compareKeys,maxLeafEntries:n,maxBranchChildren:r,duplicateKeys:l,minLeafEntries:Math.ceil(n/2),minBranchChildren:Math.ceil(r/2),root:o,leftmostLeaf:o,rightmostLeaf:o,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,_nextAutoScaleThreshold:t?G(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:o,index:0}}},W=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:n}=E(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=Math.ceil(t/2)),n>e.maxBranchChildren&&(e.maxBranchChildren=n,e.minBranchChildren=Math.ceil(n/2)),e._nextAutoScaleThreshold=G(e.entryCount)},Q=(e,t,n)=>{if(!e.autoScale)return;let r=E(0),l=M(t,"maxLeafEntries",z),o=M(n,"maxBranchChildren",$);if(l<r.maxLeaf||o<r.maxBranch)throw new f("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=l,e.maxBranchChildren=o,e.minLeafEntries=Math.ceil(l/2),e.minBranchChildren=Math.ceil(o/2)};var Ee=(e,t,n)=>{let r=[],l=0;for(;l<e;){let o=e-l;if(o>t&&o-t<n){let a=Math.ceil(o/2);r.push(l+a),r.push(e);break}let i=l+t<e?l+t:e;r.push(i),l=i}return r},dt=(e,t,n,r)=>{let l=Ee(t.length,e.maxLeafEntries,e.minLeafEntries),o=new Array(l.length),i=0;for(let a=0;a<l.length;a+=1){let s=l[a],c=new Array(s-i);for(let d=i;d<s;d+=1){let h=r+d;c[d-i]={key:t[d].key,entryId:h,value:t[d].value},n[d]=h,e.entryKeys!==null&&e.entryKeys.set(h,t[d].key)}o[a]=I(c,null),i=s}return o},Le=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let n=e.nextSequence;if(n+t.length>Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");let r=new Array(t.length),l=dt(e,t,r,n);e.nextSequence=n+t.length,e.entryCount=t.length;for(let o=0;o<l.length;o+=1)o>0&&(l[o].prev=l[o-1]),o<l.length-1&&(l[o].next=l[o+1]);if(e.leftmostLeaf=l[0],e.rightmostLeaf=l[l.length-1],l.length===1)e.root=l[0];else{let o=l;for(;o.length>1;){let i=Ee(o.length,e.maxBranchChildren,e.minBranchChildren),a=new Array(i.length),s=0;for(let c=0;c<i.length;c+=1)a[c]=k(o.slice(s,i[c]),null),s=i[c];o=a}e.root=o[0]}return W(e),r};var Ne=(e,t,n,r)=>{let l={key:void 0,sequence:0};if(!N(r,l))throw new u("inserted child has no min key");r.parent=t;let o=n.indexInParent-t.childOffset+1;ue(t,o,r,l),O(t)>e.maxBranchChildren&&pt(e,t)},ht=(e,t)=>{ae(t);let n=Math.ceil(t.entries.length/2),r={kind:C,entries:t.entries.splice(n),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=r:e.rightmostLeaf=r,t.next=r,t.parent===null){e.root=k([t,r],null);return}Ne(e,t.parent,t,r)},pt=(e,t)=>{R(t);let n=Math.ceil(t.children.length/2),r=k(t.children.splice(n),t.parent);if(t.keys.splice(n),t.parent===null){e.root=k([t,r],null);return}Ne(e,t.parent,t,r)},we=(e,t,n,r)=>{let l=e.nextSequence,o=B(e,t,n,l);if(e.duplicateKeys!=="allow"){let i=null;if(o>0){let a=T(t,o-1);e.compareKeys(a.key,n)===0&&(i=a)}else if(t.prev!==null&&y(t.prev)>0){let a=t.prev,s=T(a,y(a)-1);e.compareKeys(s.key,n)===0&&(i=s)}if(i!==null){if(e.duplicateKeys==="reject")throw new f("Duplicate key rejected.");return i.value=r,i.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");return e.nextSequence+=1,ie(t,o,{key:n,entryId:l,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(l,n),o===0&&t.parent!==null&&x(t),y(t)>e.maxLeafEntries&&ht(e,t),W(e),l},Ce=(e,t,n)=>{let r=K(e,t,e.nextSequence);return we(e,r,t,n)},Se=e=>{if(e.entryCount===0)return null;let t=D(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),y(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&x(e.leftmostLeaf),e.leftmostLeaf!==e.root&&y(e.leftmostLeaf)<e.minLeafEntries&&S(e,e.leftmostLeaf),t},be=e=>{if(e.entryCount===0)return null;let t=le(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&y(e.rightmostLeaf)<e.minLeafEntries&&S(e,e.rightmostLeaf),t},ge=(e,t)=>{let n=A(e,t);if(n===null)return null;let r=n.leaf,l=n.index,o=T(r,l);return Y(r,l),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(o.entryId),l===0&&y(r)>0&&r.parent!==null&&x(r),r!==e.root&&y(r)<e.minLeafEntries&&S(e,r),o},Z=(e,t,n)=>{let r=K(e,t,n),l=m(e,r,t,n);return l>=y(r)||T(r,l).entryId!==n?null:{leaf:r,index:l}},ve=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=Z(e,n,t);if(r===null)return null;let l=T(r.leaf,r.index);return Y(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(t),r.index===0&&y(r.leaf)>0&&r.leaf.parent!==null&&x(r.leaf),r.leaf!==e.root&&y(r.leaf)<e.minLeafEntries&&S(e,r.leaf),l},Ie=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=Z(e,n,t);return r===null?null:T(r.leaf,r.index)},ke=(e,t,n)=>{let r=e.entryKeys.get(t);if(r===void 0)return null;let l=Z(e,r,t);if(l===null)return null;let o=T(l.leaf,l.index);return o.value=n,o},Oe=(e,t)=>{if(t.length===0)return[];let n=e.duplicateKeys!=="allow";for(let r=1;r<t.length;r+=1){let l=e.compareKeys(t[r-1].key,t[r].key);if(n?l>=0:l>0)throw new f(n?"putMany: not sorted in strict ascending order.":"putMany: not sorted in non-descending order.")}if(e.entryCount>0){let r=new Array(t.length),l=K(e,t[0].key,e.nextSequence);for(let o=0;o<t.length;o+=1){let i=t[o],a=Te(e,l,i.key,e.nextSequence);r[o]=we(e,a,i.key,i.value),l=a}return r}return Le(e,t)};var Me=(e,t,n,r)=>{if(e.entryCount===0)return null;let l=e.compareKeys,o=l(t,n);if(o>0)return null;let i=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";if(i&&a&&o===0)return null;let s=i?Number.MAX_SAFE_INTEGER:0,c=K(e,t,s),d=i?B(e,c,t,Number.MAX_SAFE_INTEGER):m(e,c,t,0);return{leaf:c,index:d,compare:l,upperExclusive:a}},j=(e,t,n,r)=>{let l=Me(e,t,n,r);if(l===null)return 0;let o=l.leaf,i=l.index,{compare:a,upperExclusive:s}=l,c=0;for(;o!==null;){let d=y(o);if(i>=d){o=o.next,i=0;continue}let h=T(o,d-1),V=a(h.key,n);if(s?V<0:V<=0){c+=d-i,o=o.next,i=0;continue}let w=s?0:Number.MAX_SAFE_INTEGER,L=s?m(e,o,n,w):B(e,o,n,w),g=L<d?L:d;return c+=g-i,c}return c},Kt=200,mt=(e,t,n,r,l,o,i,a)=>{let s=y(t);if(s-n>=Kt&&t.next!==null){let d=T(t,s-1),h=r(d.key,i);if(l?h<0:h<=0){let V=j(e,o,i,a);return new Array(V)}}return[]},Ae=(e,t,n,r,l,o)=>{if(l)for(let i=t;i<n;i+=1)r[o++]=T(e,i);else for(let i=t;i<n;i+=1)r.push(T(e,i));return o},Re=(e,t,n,r)=>{let l=Me(e,t,n,r);if(l===null)return[];let o=l.leaf,i=l.index,{compare:a,upperExclusive:s}=l,c=mt(e,o,i,a,s,t,n,r),d=0,h=c.length>0;for(;o!==null;){let V=y(o);if(i>=V){o=o.next,i=0;continue}let w=T(o,V-1),L=a(w.key,n);if(s?L<0:L<=0){d=Ae(o,i,V,c,h,d),o=o.next,i=0;continue}let g=s?0:Number.MAX_SAFE_INTEGER,v=s?m(e,o,n,g):B(e,o,n,g),$e=v<V?v:V;return Ae(o,i,$e,c,h,d),c}return c};var _e=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},Pe=e=>{let t=new Array(e.entryCount),n=e.leftmostLeaf,r=0;for(;n!==null;){let l=y(n);for(let o=0;o<l;o+=1){let i=T(n,o);t[r++]=[i.key,i.value]}n=n.next}return{version:1,config:{maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale},entries:t}},Vt=1e6,Bt=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new f(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new f("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new f("BTreeJSON: entries must be array.");if(e.entries.length>Vt)throw new f("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let n=e.entries[t];if(!Array.isArray(n)||n.length!==2)throw new f(`BTreeJSON: bad entries[${t}].`)}},xt=e=>{let t=(n,r)=>{if(!Number.isInteger(r)||r<q||r>F)throw new f(`BTreeJSON: invalid ${n}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new f(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new f("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new f("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new f("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new f("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let n=E(0);if(e.maxLeafEntries<n.maxLeaf||e.maxBranchChildren<n.maxBranch)throw new f("BTreeJSON: autoScale capacity below tier-0.")}},qe=e=>{Bt(e),xt(e.config)},Fe=(e,t)=>{let n=e.config,r={compareKeys:t,duplicateKeys:n.duplicateKeys,enableEntryIdLookup:n.enableEntryIdLookup,autoScale:n.autoScale};return n.autoScale||(r.maxLeafEntries=n.maxLeafEntries,r.maxBranchChildren=n.maxBranchChildren),r};var ee=e=>{if(p(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},P=(e,t,n,r,l)=>{let o=e(t,r);return o!==0?o:n-l},De=e=>{if(p(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:De(e.children[e.children.length-1])},_=e=>{if(!Number.isFinite(e))throw new f("compareKeys must return a finite number.");return e},Et=(e,t)=>{if(_(e(t,t))!==0)throw new f("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},Lt=(e,t,n,r)=>{let l=Math.sign(_(e(t,n))),o=Math.sign(_(e(n,r)));if(l<0&&o<0&&Math.sign(_(e(t,r)))>=0)throw new f("compareKeys must satisfy transitivity for observed key triples.");if(l>0&&o>0&&Math.sign(_(e(t,r)))<=0)throw new f("compareKeys must satisfy transitivity for observed key triples.")},Je=(e,t)=>{try{Et(e.compareKeys,t)}catch(n){throw n instanceof f?new u(n.message):n}},Xe=(e,t,n,r)=>{try{Lt(e.compareKeys,t,n,r)}catch(l){throw l instanceof f?new u(l.message):l}},Nt=(e,t,n,r)=>{if(!p(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==n)throw new u("Leaf prev pointer mismatch.");if(n!==null&&p(n)){let l=De(n),o=ee(t);if(l===null||o===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(P(e.compareKeys,l.key,l.sequence,o.key,o.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let i=y(n),a=y(t);if(e.duplicateKeys!=="allow"&&i>0&&a>0&&e.compareKeys(T(n,i-1).key,T(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},He=(e,t)=>{if(e.entryCount===0){if(!p(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let n=new Set,r=e.leftmostLeaf,l=null,o=0;for(;r!==null;)Nt(e,r,l,n),n.add(r),l=r,r=r.next,o+=1;if(l!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(o!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var wt=(e,t)=>{let n=y(t);for(let r=0;r<n;r+=1)Je(e,T(t,r).key);for(let r=1;r<n;r+=1)if(P(e.compareKeys,T(t,r-1).key,T(t,r-1).entryId,T(t,r).key,T(t,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<n;r+=1)if(e.compareKeys(T(t,r-1).key,T(t,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<n;r+=1){let l=T(t,r-2),o=T(t,r-1),i=T(t,r);Xe(e,l.key,o.key,i.key)}if(n>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Ct=(e,t,n)=>{wt(e,t);let r=y(t),l=e.autoScale?Math.ceil(E(0).maxLeaf/2):e.minLeafEntries;if(t!==e.root&&r<l)throw new u("Non-root leaf node violates minimum occupancy.");let o=r===0?null:T(t,0),i=r===0?null:T(t,r-1),a=o===null?null:{key:o.key,sequence:o.entryId},s=i===null?null:{key:i.key,sequence:i.entryId};return{minKey:a,maxKey:s,leafDepth:r===0?null:n,leafCount:1,branchCount:0,entryCount:r}},St=(e,t)=>{let n=t.children.length-t.childOffset;if(n===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(E(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&n<r)throw new u("Non-root branch node violates minimum occupancy.");if(n>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},bt=(e,t,n,r)=>{let l=t.children[n];if(l.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(l.indexInParent!==n)throw new u("Child indexInParent does not match actual position in parent.");let o=Ge(e,l,r+1);if(o.minKey===null||o.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let i=t.keys[n],a=ee(l);if(a===null||P(e.compareKeys,i.key,i.sequence,a.key,a.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return o},Ge=(e,t,n)=>{if(p(t))return Ct(e,t,n);St(e,t);let r=null,l=0,o=1,i=0,a=null,s=null,c=null;for(let d=t.childOffset;d<t.children.length;d+=1){let h=bt(e,t,d,n);if(r!==null&&h.leafDepth!==null&&h.leafDepth!==r)throw new u("Leaf depth mismatch detected in tree.");if(r===null&&h.leafDepth!==null&&(r=h.leafDepth),c!==null&&P(e.compareKeys,c.key,c.sequence,h.minKey.key,h.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");a===null&&(a=h.minKey),s=h.maxKey,c=h.maxKey,l+=h.leafCount,o+=h.branchCount,i+=h.entryCount}return{minKey:a,maxKey:s,leafDepth:r,leafCount:l,branchCount:o,entryCount:i}},We=e=>{let t=Ge(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");He(e,t.leafCount)};var Ue=e=>{if(p(e))return{height:1,leafCount:1,branchCount:0};let t=0,n=0,r=1;for(let l=e.childOffset;l<e.children.length;l+=1){let o=e.children[l],i=Ue(o);i.height>t&&(t=i.height),n+=i.leafCount,r+=i.branchCount}return{height:t+1,leafCount:n,branchCount:r}},ze=e=>{let t=Ue(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var te=class e{constructor(t){this.state=xe(t)}put(t,n){return Ce(this.state,t,n)}putMany(t){return Oe(this.state,t)}remove(t){return ge(this.state,t)}removeById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ve(this.state,t)}peekById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Ie(this.state,t)}updateById(t,n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ke(this.state,t,n)}popFirst(){return Se(this.state)}peekFirst(){return this.state.entryCount===0?null:T(this.state.leftmostLeaf,0)}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return T(t,y(t)-1)}popLast(){return be(this.state)}clear(){let t=I([],null);if(this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys!==null&&this.state.entryKeys.clear(),this.state.autoScale){let n=E(0);this.state.maxLeafEntries=n.maxLeaf,this.state.maxBranchChildren=n.maxBranch,this.state.minLeafEntries=Math.ceil(n.maxLeaf/2),this.state.minBranchChildren=Math.ceil(n.maxBranch/2),this.state._nextAutoScaleThreshold=G(0)}}get(t){let n=A(this.state,t);return n===null?null:T(n.leaf,n.index).value}hasKey(t){return ce(this.state,t)}findFirst(t){let n=A(this.state,t);return n===null?null:T(n.leaf,n.index)}findLast(t){let n=se(this.state,t);return n===null?null:T(n.leaf,n.index)}nextHigherKey(t){return fe(this.state,t)}nextLowerKey(t){return de(this.state,t)}getPairOrNextLower(t){let n=he(this.state,t);return n===null?null:T(n.leaf,n.index)}count(t,n,r){return j(this.state,t,n,r)}deleteRange(t,n,r){return Be(this.state,t,n,r)}range(t,n,r){return Re(this.state,t,n,r)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield T(t,r);t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let n=y(t);for(let r=n-1;r>=0;r-=1)yield T(t,r);t=t.prev}}*keys(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield T(t,r).key;t=t.next}}*values(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield T(t,r).value;t=t.next}}[Symbol.iterator](){return this.entries()}forEach(t,n){let r=this.state.leftmostLeaf;for(;r!==null;){let l=y(r);for(let o=0;o<l;o+=1)t.call(n,T(r,o));r=r.next}}snapshot(){let t=new Array(this.state.entryCount),n=this.state.leftmostLeaf,r=0;for(;n!==null;){let l=y(n);for(let o=0;o<l;o+=1)t[r++]=T(n,o);n=n.next}return t}clone(){let t=new e(_e(this.state));if(Q(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0){let n=new Array(this.state.entryCount),r=this.state.leftmostLeaf,l=0;for(;r!==null;){let o=y(r);for(let i=0;i<o;i+=1)n[l++]=T(r,i);r=r.next}t.putMany(n)}return t}toJSON(){return Pe(this.state)}static fromJSON(t,n){qe(t);let r=t.config.duplicateKeys!=="allow";for(let o=1;o<t.entries.length;o+=1){let i=n(t.entries[o-1][0],t.entries[o][0]);if(i>0)throw new f("fromJSON: entries not sorted.");if(r&&i===0)throw new f('fromJSON: duplicate keys require duplicateKeys "allow".')}let l=new e(Fe(t,n));if(Q(l.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let o=new Array(t.entries.length);for(let i=0;i<t.entries.length;i+=1)o[i]={key:t.entries[i][0],value:t.entries[i][1]};l.putMany(o)}return l}size(){return this.state.entryCount}assertInvariants(){We(this.state)}getStats(){return ze(this.state)}};return tt(gt);})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var FrostpillarBTree=(()=>{var j=Object.defineProperty;var it=Object.getOwnPropertyDescriptor;var at=Object.getOwnPropertyNames;var ut=Object.prototype.hasOwnProperty;var yt=(e,t)=>{for(var n in t)j(e,n,{get:t[n],enumerable:!0})},st=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of at(t))!ut.call(e,o)&&o!==n&&j(e,o,{get:()=>t[o],enumerable:!(r=it(t,o))||r.enumerable});return e};var Tt=e=>st(j({},"__esModule",{value:!0}),e);var Dt={};yt(Dt,{BTreeConcurrencyError:()=>h,BTreeInvariantError:()=>u,BTreeValidationError:()=>f,ConcurrentInMemoryBTree:()=>U,InMemoryBTree:()=>R});var f=class extends Error{constructor(t){super(t),this.name="BTreeValidationError",Object.setPrototypeOf(this,new.target.prototype)}},u=class extends Error{constructor(t){super(t),this.name="BTreeInvariantError",Object.setPrototypeOf(this,new.target.prototype)}},h=class extends Error{constructor(t){super(t),this.name="BTreeConcurrencyError",Object.setPrototypeOf(this,new.target.prototype)}};var P=64,_=64,X=3,H=16384,S=0,ct=1,le=e=>{if(e===void 0)return"replace";if(e!=="allow"&&e!=="reject"&&e!=="replace")throw new f("Invalid duplicateKeys option.");return e},m=e=>e.kind===S,N=(e,t)=>{if(e.kind===S){if(e.entryOffset>=e.entries.length)return!1;let n=e.entries[e.entryOffset];return t.key=n.key,t.sequence=n.entryId,!0}return e.childOffset>=e.keys.length?!1:(t.key=e.keys[e.childOffset].key,t.sequence=e.keys[e.childOffset].sequence,!0)},F=(e,t,n)=>{if(e===void 0)return n;if(!Number.isInteger(e)||e<X||e>H)throw new f(`${t}: integer ${X}\u2013${H} required.`);return e},M=(e,t)=>({kind:S,entries:e,entryOffset:0,parent:t,indexInParent:0,prev:null,next:null}),k=(e,t)=>{let n=[],r={kind:ct,children:e,keys:n,childOffset:0,parent:t,indexInParent:0};for(let o=0;o<e.length;o+=1){let l=e[o];l.parent=r,l.indexInParent=o;let i={key:void 0,sequence:0};if(!N(l,i))throw new u("branch child has no min key");n.push(i)}return r},y=e=>e.entries.length-e.entryOffset,s=(e,t)=>e.entries[e.entryOffset+t],z=e=>{if(e.entryOffset>=e.entries.length)return;let t=e.entries[e.entryOffset];return e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0),t},ie=e=>{if(!(e.entryOffset>=e.entries.length))return e.entries.pop()},ae=(e,t)=>{e.entryOffset>0?(e.entryOffset-=1,e.entries[e.entryOffset]=t):e.entries.unshift(t)},Z=(e,t)=>{let n=e.entries.length-e.entryOffset,r=e.entryOffset+t;t<n-1-t?(e.entries.copyWithin(e.entryOffset+1,e.entryOffset,r),e.entryOffset+=1,e.entryOffset>=e.entries.length>>>1&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length-=e.entryOffset,e.entryOffset=0)):(e.entries.copyWithin(r,r+1),e.entries.length-=1)},ue=(e,t,n)=>{let r=e.entryOffset+t;e.entryOffset>0&&t<e.entries.length-e.entryOffset>>>1?(e.entries.copyWithin(e.entryOffset-1,e.entryOffset,r),e.entryOffset-=1,e.entries[r-1]=n):e.entries.splice(r,0,n)},ye=e=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0)},oe=1,q=e=>{if(e.childOffset>0){let t=e.childOffset<=oe?0:oe;e.children.copyWithin(t,e.childOffset),e.children.length-=e.childOffset-t,e.keys.copyWithin(t,e.childOffset),e.keys.length-=e.childOffset-t,e.childOffset=t;for(let n=t;n<e.children.length;n+=1)e.children[n].indexInParent=n}},O=e=>e.children.length-e.childOffset,se=(e,t,n,r)=>{let o=e.childOffset+t,l=e.children.length-e.childOffset;if(e.childOffset>0&&t<l>>>1){e.children.copyWithin(e.childOffset-1,e.childOffset,o),e.keys.copyWithin(e.childOffset-1,e.childOffset,o),e.childOffset-=1,e.children[o-1]=n,e.keys[o-1]=r;for(let i=e.childOffset;i<o;i+=1)e.children[i].indexInParent=i;n.indexInParent=o-1}else{e.children.splice(o,0,n),e.keys.splice(o,0,r);for(let i=o;i<e.children.length;i+=1)e.children[i].indexInParent=i}},Te=(e,t)=>{let n=t-e.childOffset,r=e.children.length-e.childOffset;if(n<r-1-n){e.children.copyWithin(e.childOffset+1,e.childOffset,t),e.keys.copyWithin(e.childOffset+1,e.childOffset,t),e.childOffset+=1;for(let o=e.childOffset;o<=t;o+=1)e.children[o].indexInParent=o;e.childOffset>=e.children.length>>>1&&q(e)}else{e.children.copyWithin(t,t+1),e.keys.copyWithin(t,t+1),e.children.length-=1,e.keys.length-=1;for(let o=t;o<e.children.length;o+=1)e.children[o].indexInParent=o}};var ft=(e,t,n,r)=>{let o=t.childOffset;if(o>=t.children.length)throw new u("branch has no children");let l=o,i=o,a=t.keys.length-1;for(;i<=a;){let T=i+a>>>1,c=t.keys[T],d=e(c.key,n);(d!==0?d:c.sequence-r)<=0?(l=T,i=T+1):a=T-1}return t.children[l]},K=(e,t,n)=>{let r=e.compareKeys,o=e.root;for(;o.kind!==S;)o=ft(r,o,t,n);return o},B=(e,t,n,r)=>{let o=e.compareKeys,l=t.entryOffset,i=t.entries.length;for(;l<i;){let a=l+i>>>1,T=t.entries[a],c=o(T.key,n);(c!==0?c:T.entryId-r)<0?l=a+1:i=a}return l-t.entryOffset},E=(e,t,n,r)=>{let o=e.compareKeys,l=t.entryOffset,i=t.entries.length;for(;l<i;){let a=l+i>>>1,T=t.entries[a],c=o(T.key,n);(c!==0?c:T.entryId-r)<=0?l=a+1:i=a}return l-t.entryOffset},ce=(e,t,n,r)=>{let o=e.compareKeys,l=t,i=32-Math.clz32(e.entryCount+1);for(;i>0&&l.next!==null&&y(l.next)>0;){let a=s(l.next,0),T=o(a.key,n);if(T>0||T===0&&a.entryId>r)break;l=l.next,i-=1}if(i===0&&l.next!==null&&y(l.next)>0){let a=s(l.next,0),T=o(a.key,n);if(T<0||T===0&&a.entryId<=r)return K(e,n,r)}return l},A=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,0),r=B(e,n,t,0);if(r>=y(n)&&(n.next===null||(n=n.next,r=B(e,n,t,0),r>=y(n)))||e.compareKeys(s(n,r).key,t)!==0)return null;let o=e._cursor;return o.leaf=n,o.index=r,o},fe=(e,t)=>{if(e.entryCount===0)return null;let n=K(e,t,Number.MAX_SAFE_INTEGER),r=E(e,n,t,Number.MAX_SAFE_INTEGER);if(r===0&&(n.prev===null||(n=n.prev,r=y(n),r===0))||(r-=1,e.compareKeys(s(n,r).key,t)!==0))return null;let o=e._cursor;return o.leaf=n,o.index=r,o},de=(e,t)=>A(e,t)!==null,pe=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,Number.MAX_SAFE_INTEGER),o=E(e,r,t,Number.MAX_SAFE_INTEGER);for(;r!==null;)if(o<y(r)){let l=s(r,o);if(n(l.key,t)>0)return l.key;o+=1}else r=r.next,o=0;return null},he=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),o=B(e,r,t,0),l=r,i=o-1;for(;l!==null;){for(;i>=0;){let a=s(l,i);if(n(a.key,t)<0)return a.key;i-=1}l=l.prev,l!==null&&(i=y(l)-1)}return null},me=(e,t)=>{if(e.entryCount===0)return null;let n=e.compareKeys,r=K(e,t,0),o=B(e,r,t,0),l=e._cursor;if(o<y(r)){if(n(s(r,o).key,t)===0)return l.leaf=r,l.index=o,l}else if(r.next!==null){let i=B(e,r.next,t,0);if(i<y(r.next)&&n(s(r.next,i).key,t)===0)return l.leaf=r.next,l.index=i,l}if(o>0)return l.leaf=r,l.index=o-1,l;if(r.prev!==null){let i=y(r.prev);if(i>0)return l.leaf=r.prev,l.index=i-1,l}return null};var L=e=>{let t=e;for(;t.parent!==null;){let n=t.indexInParent;if(!N(t,t.parent.keys[n])||n!==t.parent.childOffset)return;t=t.parent}},$=e=>{if(e.parent===null)throw new u("no parent during rebalance");return e.parent},Ke=e=>{if(!m(e))throw new u("expected leaf, got branch");return e},Be=e=>{if(m(e))throw new u("expected branch, got leaf");return e},G=(e,t)=>{if(t<e.childOffset||t>=e.children.length)throw new u("child index out of range");Te(e,t)},Ve=(e,t)=>{t.prev!==null?t.prev.next=t.next:t.next!==null&&(e.leftmostLeaf=t.next),t.next!==null?t.next.prev=t.prev:t.prev!==null&&(e.rightmostLeaf=t.prev),t.prev=null,t.next=null},dt=(e,t,n)=>{let r=t.children.pop();if(r===void 0)throw new u("left branch borrow failed");t.keys.pop(),r.parent=e;let o={key:void 0,sequence:0};if(!N(r,o))throw new u("borrowed child has no min key");if(e.childOffset>0)e.childOffset-=1,e.children[e.childOffset]=r,e.keys[e.childOffset]=o,r.indexInParent=e.childOffset;else{e.children.unshift(r),e.keys.unshift(o);for(let i=0;i<e.children.length;i+=1)e.children[i].indexInParent=i}let l=$(e);l.keys[n]={key:o.key,sequence:o.sequence},L(e)},pt=(e,t,n)=>{let r=t.childOffset;if(r>=t.children.length)throw new u("right branch borrow failed");let o=t.children[r];t.childOffset+=1,t.childOffset>=t.children.length>>>1&&q(t),e.children.push(o),o.parent=e;let l={key:void 0,sequence:0};if(!N(o,l))throw new u("borrowed child has no min key");e.keys.push(l),o.indexInParent=e.children.length-1;let i=$(e);N(t,i.keys[n+1])},ht=(e,t,n,r)=>{for(let l=t.childOffset;l<t.children.length;l+=1){let i=t.children[l];i.parent=n,i.indexInParent=n.children.length,n.children.push(i),n.keys.push(t.keys[l])}let o=$(t);G(o,r),W(e,o)},mt=(e,t,n,r)=>{for(let l=n.childOffset;l<n.children.length;l+=1){let i=n.children[l];i.parent=t,i.indexInParent=t.children.length,t.children.push(i),t.keys.push(n.keys[l])}let o=$(t);G(o,r+1),W(e,o)},Kt=(e,t)=>{let n=t>e.childOffset?Be(e.children[t-1]):null,r=t+1<e.children.length?Be(e.children[t+1]):null;return{left:n,right:r}},W=(e,t)=>{let n=O(t);if(t===e.root){if(n===1){let a=t.children[t.childOffset];a.parent=null,e.root=a,m(a)&&(e.leftmostLeaf=a,e.rightmostLeaf=a)}return}if(n>=e.minBranchChildren)return;let r=t.parent;if(r===null)throw new u("branch has no parent");let o=t.indexInParent,{left:l,right:i}=Kt(r,o);if(i!==null&&O(i)>e.minBranchChildren){pt(t,i,o);return}if(l!==null&&O(l)>e.minBranchChildren){dt(t,l,o);return}if(l!==null){ht(e,t,l,o);return}if(i!==null){mt(e,t,i,o);return}throw new u("no branch siblings to rebalance")};var xe=(e,t)=>{e.entryOffset>0&&(e.entries.copyWithin(0,e.entryOffset),e.entries.length=e.entries.length-e.entryOffset,e.entryOffset=0);let n=t.entries;for(let r=t.entryOffset;r<n.length;r+=1)e.entries.push(n[r])},g=(e,t)=>{if(t===e.root){e.entryCount===0&&(e.leftmostLeaf=t,e.rightmostLeaf=t);return}if(y(t)>=e.minLeafEntries)return;let n=t.parent;if(n===null)throw new u("Leaf node has no parent during rebalance.");let r=t.indexInParent,o=r>n.childOffset?Ke(n.children[r-1]):null,l=r+1<n.children.length?Ke(n.children[r+1]):null;if(l!==null&&y(l)>e.minLeafEntries){let i=z(l);if(i===void 0)throw new u("right leaf borrow failed");t.entries.push(i),N(l,n.keys[r+1]);return}if(o!==null&&y(o)>e.minLeafEntries){let i=o.entries.pop();if(i===void 0)throw new u("left leaf borrow failed");ae(t,i),n.keys[r]={key:i.key,sequence:i.entryId},L(t);return}if(o!==null){xe(o,t),Ve(e,t),G(n,r),W(e,n);return}if(l!==null){xe(t,l),Ve(e,l),G(n,r+1),W(e,n);return}throw new u("no leaf siblings to rebalance")};var Bt=(e,t,n)=>{let r=n?Number.MAX_SAFE_INTEGER:0,o=K(e,t,r),l=n?E(e,o,t,Number.MAX_SAFE_INTEGER):B(e,o,t,0);return l>=y(o)?o.next===null?null:{leaf:o.next,idx:0}:{leaf:o,idx:l}},Vt=(e,t,n,r,o)=>{let l=y(t),i=n;for(;i<l;){let a=s(t,i),T=e.compareKeys(a.key,r);if(o?T>=0:T>0)break;i+=1}return i},xt=e=>{let t=0,n=e.root;for(;!m(n);)n=n.children[n.childOffset],t+=1;return t},Et=(e,t,n,r,o)=>{if(e.entryKeys!==null)for(let c=n;c<n+r;c+=1)e.entryKeys.delete(s(t,c).entryId);let l=t.entryOffset+n;t.entries.copyWithin(l,l+r),t.entries.length-=r,e.entryCount-=r;let i=y(t)===0;n===0&&!i&&t.parent!==null&&L(t);let a=y(t),T=o;for(;T>0&&t!==e.root&&y(t)<e.minLeafEntries&&(g(e,t),!(t.parent!==null&&t.parent.children[t.indexInParent]!==t));)T-=1;return i&&y(t)>0&&t.parent!==null&&t.parent.children[t.indexInParent]===t&&L(t),a},Lt=(e,t)=>t.parent===null?t===e.root:t.parent.children[t.indexInParent]===t,Ee=(e,t,n,r)=>{if(e.entryCount===0)return 0;let o=e.compareKeys(t,n);if(o>0)return 0;let l=r?.lowerBound==="exclusive",i=r?.upperBound==="exclusive";if(l&&i&&o===0)return 0;let a=xt(e),T=0,c=!0,d=null,p=0;for(;e.entryCount>0;){if(c){let I=Bt(e,t,l);if(I===null)break;d=I.leaf,p=I.idx,c=!1}if(p>=y(d))break;let V=y(d),b=Vt(e,d,p,n,i),w=b-p;if(w===0)break;let C=Et(e,d,p,w,a);if(T+=w,b<V)break;if(!Lt(e,d)){c=!0;continue}if(y(d)>C){c=!0;continue}if(d.next===null)break;d=d.next,p=0}return T};var v=[{threshold:0,maxLeaf:32,maxBranch:32},{threshold:1e3,maxLeaf:64,maxBranch:64},{threshold:1e4,maxLeaf:128,maxBranch:128},{threshold:1e5,maxLeaf:256,maxBranch:128},{threshold:1e6,maxLeaf:512,maxBranch:256}],x=e=>{let t=v[0];for(let n=1;n<v.length&&e>=v[n].threshold;n+=1)t=v[n];return t},Q=e=>{for(let t=1;t<v.length;t+=1)if(e<v[t].threshold)return v[t].threshold;return Number.MAX_SAFE_INTEGER},Le=e=>{if(typeof e.compareKeys!="function")throw new f("compareKeys must be a function.");let t=e.autoScale===!0;if(t&&(e.maxLeafEntries!==void 0||e.maxBranchChildren!==void 0))throw new f("autoScale conflicts with explicit capacity.");let n,r;if(t){let i=x(0);n=i.maxLeaf,r=i.maxBranch}else n=F(e.maxLeafEntries,"maxLeafEntries",P),r=F(e.maxBranchChildren,"maxBranchChildren",_);let o=le(e.duplicateKeys),l=M([],null);return{compareKeys:e.compareKeys,maxLeafEntries:n,maxBranchChildren:r,duplicateKeys:o,minLeafEntries:Math.ceil(n/2),minBranchChildren:Math.ceil(r/2),root:l,leftmostLeaf:l,rightmostLeaf:l,entryCount:0,nextSequence:0,entryKeys:e.enableEntryIdLookup===!0?new Map:null,autoScale:t,_nextAutoScaleThreshold:t?Q(0):Number.MAX_SAFE_INTEGER,_cursor:{leaf:l,index:0}}},Y=e=>{if(e.entryCount<e._nextAutoScaleThreshold)return;let{maxLeaf:t,maxBranch:n}=x(e.entryCount);t>e.maxLeafEntries&&(e.maxLeafEntries=t,e.minLeafEntries=Math.ceil(t/2)),n>e.maxBranchChildren&&(e.maxBranchChildren=n,e.minBranchChildren=Math.ceil(n/2)),e._nextAutoScaleThreshold=Q(e.entryCount)},ee=(e,t,n)=>{if(!e.autoScale)return;let r=x(0),o=F(t,"maxLeafEntries",P),l=F(n,"maxBranchChildren",_);if(o<r.maxLeaf||l<r.maxBranch)throw new f("autoScale capacity snapshot must be >= tier-0 capacities.");e.maxLeafEntries=o,e.maxBranchChildren=l,e.minLeafEntries=Math.ceil(o/2),e.minBranchChildren=Math.ceil(l/2)};var we=(e,t,n)=>{let r=[],o=0;for(;o<e;){let l=e-o;if(l>t&&l-t<n){let a=Math.ceil(l/2);r.push(o+a),r.push(e);break}let i=o+t<e?o+t:e;r.push(i),o=i}return r},wt=(e,t,n,r)=>{let o=we(t.length,e.maxLeafEntries,e.minLeafEntries),l=new Array(o.length),i=0;for(let a=0;a<o.length;a+=1){let T=o[a],c=new Array(T-i);for(let d=i;d<T;d+=1){let p=r+d;c[d-i]={key:t[d].key,entryId:p,value:t[d].value},n[d]=p,e.entryKeys!==null&&e.entryKeys.set(p,t[d].key)}l[a]=M(c,null),i=T}return l},Ne=(e,t)=>{if(e.entryCount!==0)throw new u("bulk load requires empty tree");let n=e.nextSequence;if(n+t.length>Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");let r=new Array(t.length),o=wt(e,t,r,n);e.nextSequence=n+t.length,e.entryCount=t.length;for(let l=0;l<o.length;l+=1)l>0&&(o[l].prev=o[l-1]),l<o.length-1&&(o[l].next=o[l+1]);if(e.leftmostLeaf=o[0],e.rightmostLeaf=o[o.length-1],o.length===1)e.root=o[0];else{let l=o;for(;l.length>1;){let i=we(l.length,e.maxBranchChildren,e.minBranchChildren),a=new Array(i.length),T=0;for(let c=0;c<i.length;c+=1)a[c]=k(l.slice(T,i[c]),null),T=i[c];l=a}e.root=l[0]}return Y(e),r};var be=(e,t,n,r)=>{let o={key:void 0,sequence:0};if(!N(r,o))throw new u("inserted child has no min key");r.parent=t;let l=n.indexInParent-t.childOffset+1;se(t,l,r,o),O(t)>e.maxBranchChildren&&bt(e,t)},Nt=(e,t)=>{ye(t);let n=Math.ceil(t.entries.length/2),r={kind:S,entries:t.entries.splice(n),entryOffset:0,parent:t.parent,indexInParent:0,prev:t,next:t.next};if(t.next!==null?t.next.prev=r:e.rightmostLeaf=r,t.next=r,t.parent===null){e.root=k([t,r],null);return}be(e,t.parent,t,r)},bt=(e,t)=>{q(t);let n=Math.ceil(t.children.length/2),r=k(t.children.splice(n),t.parent);if(t.keys.splice(n),t.parent===null){e.root=k([t,r],null);return}be(e,t.parent,t,r)},Se=(e,t,n,r)=>{let o=e.nextSequence,l=E(e,t,n,o);if(e.duplicateKeys!=="allow"){let i=null;if(l>0){let a=s(t,l-1);e.compareKeys(a.key,n)===0&&(i=a)}else if(t.prev!==null&&y(t.prev)>0){let a=t.prev,T=s(a,y(a)-1);e.compareKeys(T.key,n)===0&&(i=T)}if(i!==null){if(e.duplicateKeys==="reject")throw new f("Duplicate key rejected.");return i.value=r,i.entryId}}if(e.nextSequence>=Number.MAX_SAFE_INTEGER)throw new f("Sequence overflow.");return e.nextSequence+=1,ue(t,l,{key:n,entryId:o,value:r}),e.entryCount+=1,e.entryKeys!==null&&e.entryKeys.set(o,n),l===0&&t.parent!==null&&L(t),y(t)>e.maxLeafEntries&&Nt(e,t),Y(e),o},ge=(e,t,n)=>{let r=K(e,t,e.nextSequence);return Se(e,r,t,n)},ve=e=>{if(e.entryCount===0)return null;let t=z(e.leftmostLeaf);if(t===void 0)throw new u("leftmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),y(e.leftmostLeaf)>0&&e.leftmostLeaf.parent!==null&&L(e.leftmostLeaf),e.leftmostLeaf!==e.root&&y(e.leftmostLeaf)<e.minLeafEntries&&g(e,e.leftmostLeaf),t},Ce=e=>{if(e.entryCount===0)return null;let t=ie(e.rightmostLeaf);if(t===void 0)throw new u("rightmost leaf empty but count > 0");return e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(t.entryId),e.rightmostLeaf!==e.root&&y(e.rightmostLeaf)<e.minLeafEntries&&g(e,e.rightmostLeaf),t},Ie=(e,t)=>{let n=A(e,t);if(n===null)return null;let r=n.leaf,o=n.index,l=s(r,o);return Z(r,o),e.entryCount-=1,e.entryKeys!==null&&e.entryKeys.delete(l.entryId),o===0&&y(r)>0&&r.parent!==null&&L(r),r!==e.root&&y(r)<e.minLeafEntries&&g(e,r),l},te=(e,t,n)=>{let r=K(e,t,n),o=B(e,r,t,n);return o>=y(r)||s(r,o).entryId!==n?null:{leaf:r,index:o}},Me=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=te(e,n,t);if(r===null)return null;let o=s(r.leaf,r.index);return Z(r.leaf,r.index),e.entryCount-=1,e.entryKeys.delete(t),r.index===0&&y(r.leaf)>0&&r.leaf.parent!==null&&L(r.leaf),r.leaf!==e.root&&y(r.leaf)<e.minLeafEntries&&g(e,r.leaf),o},ke=(e,t)=>{let n=e.entryKeys.get(t);if(n===void 0)return null;let r=te(e,n,t);return r===null?null:s(r.leaf,r.index)},Oe=(e,t,n)=>{let r=e.entryKeys.get(t);if(r===void 0)return null;let o=te(e,r,t);if(o===null)return null;let l=s(o.leaf,o.index);return l.value=n,l},Ae=(e,t)=>{if(t.length===0)return[];let n=e.duplicateKeys!=="allow";for(let r=1;r<t.length;r+=1){let o=e.compareKeys(t[r-1].key,t[r].key);if(n?o>=0:o>0)throw new f(n?"putMany: not sorted in strict ascending order.":"putMany: not sorted in non-descending order.")}if(e.entryCount>0){let r=new Array(t.length),o=K(e,t[0].key,e.nextSequence);for(let l=0;l<t.length;l+=1){let i=t[l],a=ce(e,o,i.key,e.nextSequence);r[l]=Se(e,a,i.key,i.value),o=a}return r}return Ne(e,t)};var Pe=(e,t,n,r)=>{if(e.entryCount===0)return null;let o=e.compareKeys,l=o(t,n);if(l>0)return null;let i=r?.lowerBound==="exclusive",a=r?.upperBound==="exclusive";if(i&&a&&l===0)return null;let T=i?Number.MAX_SAFE_INTEGER:0,c=K(e,t,T),d=i?E(e,c,t,Number.MAX_SAFE_INTEGER):B(e,c,t,0);return{leaf:c,index:d,compare:o,upperExclusive:a}},ne=(e,t,n,r)=>{let o=Pe(e,t,n,r);if(o===null)return 0;let l=o.leaf,i=o.index,{compare:a,upperExclusive:T}=o,c=0;for(;l!==null;){let d=y(l);if(i>=d){l=l.next,i=0;continue}let p=s(l,d-1),V=a(p.key,n);if(T?V<0:V<=0){c+=d-i,l=l.next,i=0;continue}let b=T?0:Number.MAX_SAFE_INTEGER,w=T?B(e,l,n,b):E(e,l,n,b),C=w<d?w:d;return c+=C-i,c}return c},St=200,gt=(e,t,n,r,o,l,i,a)=>{let T=y(t);if(T-n>=St&&t.next!==null){let d=s(t,T-1),p=r(d.key,i);if(o?p<0:p<=0){let V=ne(e,l,i,a);return new Array(V)}}return[]},Re=(e,t,n,r,o,l)=>{if(o)for(let i=t;i<n;i+=1)r[l++]=s(e,i);else for(let i=t;i<n;i+=1)r.push(s(e,i));return l},_e=(e,t,n,r)=>{let o=Pe(e,t,n,r);if(o===null)return[];let l=o.leaf,i=o.index,{compare:a,upperExclusive:T}=o,c=gt(e,l,i,a,T,t,n,r),d=0,p=c.length>0;for(;l!==null;){let V=y(l);if(i>=V){l=l.next,i=0;continue}let b=s(l,V-1),w=a(b.key,n);if(T?w<0:w<=0){d=Re(l,i,V,c,p,d),l=l.next,i=0;continue}let C=T?0:Number.MAX_SAFE_INTEGER,I=T?B(e,l,n,C):E(e,l,n,C),lt=I<V?I:V;return Re(l,i,lt,c,p,d),c}return c};var Fe=e=>{let t={compareKeys:e.compareKeys,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale};return e.autoScale||(t.maxLeafEntries=e.maxLeafEntries,t.maxBranchChildren=e.maxBranchChildren),t},qe=e=>{let t=new Array(e.entryCount),n=e.leftmostLeaf,r=0;for(;n!==null;){let o=y(n);for(let l=0;l<o;l+=1){let i=s(n,l);t[r++]=[i.key,i.value]}n=n.next}return{version:1,config:{maxLeafEntries:e.maxLeafEntries,maxBranchChildren:e.maxBranchChildren,duplicateKeys:e.duplicateKeys,enableEntryIdLookup:e.entryKeys!==null,autoScale:e.autoScale},entries:t}},vt=1e6,Ct=e=>{if(typeof e!="object"||e===null||e.version!==1)throw new f(`BTreeJSON: expected version 1, got ${String(e?.version)}.`);if(typeof e.config!="object"||e.config===null)throw new f("BTreeJSON: invalid config.");if(!Array.isArray(e.entries))throw new f("BTreeJSON: entries must be array.");if(e.entries.length>vt)throw new f("BTreeJSON: entry count exceeds maximum.");for(let t=0;t<e.entries.length;t+=1){let n=e.entries[t];if(!Array.isArray(n)||n.length!==2)throw new f(`BTreeJSON: bad entries[${t}].`)}},It=e=>{let t=(n,r)=>{if(!Number.isInteger(r)||r<X||r>H)throw new f(`BTreeJSON: invalid ${n}.`)};if(e.duplicateKeys!=="allow"&&e.duplicateKeys!=="reject"&&e.duplicateKeys!=="replace")throw new f(`BTreeJSON: invalid duplicateKeys: ${String(e.duplicateKeys)}.`);if(typeof e.enableEntryIdLookup!="boolean")throw new f("BTreeJSON: invalid enableEntryIdLookup.");if(typeof e.autoScale!="boolean")throw new f("BTreeJSON: invalid autoScale.");if(typeof e.maxLeafEntries!="number")throw new f("BTreeJSON: invalid maxLeafEntries.");if(typeof e.maxBranchChildren!="number")throw new f("BTreeJSON: invalid maxBranchChildren.");if(t("maxLeafEntries",e.maxLeafEntries),t("maxBranchChildren",e.maxBranchChildren),e.autoScale){let n=x(0);if(e.maxLeafEntries<n.maxLeaf||e.maxBranchChildren<n.maxBranch)throw new f("BTreeJSON: autoScale capacity below tier-0.")}},De=e=>{Ct(e),It(e.config)},Je=(e,t)=>{let n=e.config,r={compareKeys:t,duplicateKeys:n.duplicateKeys,enableEntryIdLookup:n.enableEntryIdLookup,autoScale:n.autoScale};return n.autoScale||(r.maxLeafEntries=n.maxLeafEntries,r.maxBranchChildren=n.maxBranchChildren),r};var re=e=>{if(m(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entryOffset];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.keys.length?null:{key:e.keys[e.childOffset].key,sequence:e.keys[e.childOffset].sequence}},J=(e,t,n,r,o)=>{let l=e(t,r);return l!==0?l:n-o},Ue=e=>{if(m(e)){if(e.entryOffset>=e.entries.length)return null;let t=e.entries[e.entries.length-1];return{key:t.key,sequence:t.entryId}}return e.childOffset>=e.children.length?null:Ue(e.children[e.children.length-1])},D=e=>{if(!Number.isFinite(e))throw new f("compareKeys must return a finite number.");return e},Mt=(e,t)=>{if(D(e(t,t))!==0)throw new f("compareKeys must satisfy reflexivity: compare(x, x) must return 0.")},kt=(e,t,n,r)=>{let o=Math.sign(D(e(t,n))),l=Math.sign(D(e(n,r)));if(o<0&&l<0&&Math.sign(D(e(t,r)))>=0)throw new f("compareKeys must satisfy transitivity for observed key triples.");if(o>0&&l>0&&Math.sign(D(e(t,r)))<=0)throw new f("compareKeys must satisfy transitivity for observed key triples.")},Xe=(e,t)=>{try{Mt(e.compareKeys,t)}catch(n){throw n instanceof f?new u(n.message):n}},He=(e,t,n,r)=>{try{kt(e.compareKeys,t,n,r)}catch(o){throw o instanceof f?new u(o.message):o}},Ot=(e,t,n,r)=>{if(!m(t))throw new u("Leaf linkage cursor reached non-leaf node.");if(r.has(t))throw new u("Cycle detected in leaf linkage.");if(t.prev!==n)throw new u("Leaf prev pointer mismatch.");if(n!==null&&m(n)){let o=Ue(n),l=re(t);if(o===null||l===null)throw new u("Non-empty tree leaf chain contains empty leaf node.");if(J(e.compareKeys,o.key,o.sequence,l.key,l.sequence)>0)throw new u("Adjacent leaf key ranges are out of order.");let i=y(n),a=y(t);if(e.duplicateKeys!=="allow"&&i>0&&a>0&&e.compareKeys(s(n,i-1).key,s(t,0).key)===0)throw new u("Duplicate user key detected across adjacent leaves with uniqueness policy.")}},ze=(e,t)=>{if(e.entryCount===0){if(!m(e.root))throw new u("Empty tree root must be a leaf node.");if(e.leftmostLeaf!==e.root||e.rightmostLeaf!==e.root)throw new u("Empty tree leaf pointers must reference root leaf.");return}if(e.leftmostLeaf.prev!==null)throw new u("Leftmost leaf prev pointer must be null.");if(e.rightmostLeaf.next!==null)throw new u("Rightmost leaf next pointer must be null.");let n=new Set,r=e.leftmostLeaf,o=null,l=0;for(;r!==null;)Ot(e,r,o,n),n.add(r),o=r,r=r.next,l+=1;if(o!==e.rightmostLeaf)throw new u("Rightmost leaf pointer mismatch.");if(l!==t)throw new u("Leaf chain count mismatch with tree traversal count.")};var At=(e,t)=>{let n=y(t);for(let r=0;r<n;r+=1)Xe(e,s(t,r).key);for(let r=1;r<n;r+=1)if(J(e.compareKeys,s(t,r-1).key,s(t,r-1).entryId,s(t,r).key,s(t,r).entryId)>=0)throw new u("Leaf entries are not strictly ordered.");if(e.duplicateKeys!=="allow"){for(let r=1;r<n;r+=1)if(e.compareKeys(s(t,r-1).key,s(t,r).key)===0)throw new u("Duplicate user key detected in tree with uniqueness policy.")}for(let r=2;r<n;r+=1){let o=s(t,r-2),l=s(t,r-1),i=s(t,r);He(e,o.key,l.key,i.key)}if(n>e.maxLeafEntries)throw new u("Leaf node exceeds maximum occupancy.")},Rt=(e,t,n)=>{At(e,t);let r=y(t),o=e.autoScale?Math.ceil(x(0).maxLeaf/2):e.minLeafEntries;if(t!==e.root&&r<o)throw new u("Non-root leaf node violates minimum occupancy.");let l=r===0?null:s(t,0),i=r===0?null:s(t,r-1),a=l===null?null:{key:l.key,sequence:l.entryId},T=i===null?null:{key:i.key,sequence:i.entryId};return{minKey:a,maxKey:T,leafDepth:r===0?null:n,leafCount:1,branchCount:0,entryCount:r}},Pt=(e,t)=>{let n=t.children.length-t.childOffset;if(n===0)throw new u("Branch node has zero children.");let r=e.autoScale?Math.ceil(x(0).maxBranch/2):e.minBranchChildren;if(t!==e.root&&n<r)throw new u("Non-root branch node violates minimum occupancy.");if(n>e.maxBranchChildren)throw new u("Branch node exceeds maximum occupancy.");if(t.keys.length!==t.children.length)throw new u("Branch keys array length does not match children array length.")},_t=(e,t,n,r)=>{let o=t.children[n];if(o.parent!==t)throw new u("Child-parent pointer mismatch in branch node.");if(o.indexInParent!==n)throw new u("Child indexInParent does not match actual position in parent.");let l=Ge(e,o,r+1);if(l.minKey===null||l.maxKey===null)throw new u("Branch child must not be empty in non-root branch tree.");let i=t.keys[n],a=re(o);if(a===null||J(e.compareKeys,i.key,i.sequence,a.key,a.sequence)!==0)throw new u("Branch cached key does not match actual child minimum key.");return l},Ge=(e,t,n)=>{if(m(t))return Rt(e,t,n);Pt(e,t);let r=null,o=0,l=1,i=0,a=null,T=null,c=null;for(let d=t.childOffset;d<t.children.length;d+=1){let p=_t(e,t,d,n);if(r!==null&&p.leafDepth!==null&&p.leafDepth!==r)throw new u("Leaf depth mismatch detected in tree.");if(r===null&&p.leafDepth!==null&&(r=p.leafDepth),c!==null&&J(e.compareKeys,c.key,c.sequence,p.minKey.key,p.minKey.sequence)>=0)throw new u("Branch child key ranges are not strictly ordered.");a===null&&(a=p.minKey),T=p.maxKey,c=p.maxKey,o+=p.leafCount,l+=p.branchCount,i+=p.entryCount}return{minKey:a,maxKey:T,leafDepth:r,leafCount:o,branchCount:l,entryCount:i}},We=e=>{let t=Ge(e,e.root,0);if(t.entryCount!==e.entryCount)throw new u("Index entry count mismatch between tree traversal and tracked state.");ze(e,t.leafCount)};var $e=e=>{if(m(e))return{height:1,leafCount:1,branchCount:0};let t=0,n=0,r=1;for(let o=e.childOffset;o<e.children.length;o+=1){let l=e.children[o],i=$e(l);i.height>t&&(t=i.height),n+=i.leafCount,r+=i.branchCount}return{height:t+1,leafCount:n,branchCount:r}},Qe=e=>{let t=$e(e.root);return{height:t.height,leafCount:t.leafCount,branchCount:t.branchCount,entryCount:e.entryCount}};var R=class e{constructor(t){this.state=Le(t)}put(t,n){return ge(this.state,t,n)}putMany(t){return Ae(this.state,t)}remove(t){return Ie(this.state,t)}removeById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Me(this.state,t)}peekById(t){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return ke(this.state,t)}updateById(t,n){if(this.state.entryKeys===null)throw new f("Requires enableEntryIdLookup: true.");return Oe(this.state,t,n)}popFirst(){return ve(this.state)}peekFirst(){return this.state.entryCount===0?null:s(this.state.leftmostLeaf,0)}peekLast(){if(this.state.entryCount===0)return null;let t=this.state.rightmostLeaf;return s(t,y(t)-1)}popLast(){return Ce(this.state)}clear(){let t=M([],null);if(this.state.root=t,this.state.leftmostLeaf=t,this.state.rightmostLeaf=t,this.state.entryCount=0,this.state._cursor.leaf=t,this.state._cursor.index=0,this.state.entryKeys!==null&&this.state.entryKeys.clear(),this.state.autoScale){let n=x(0);this.state.maxLeafEntries=n.maxLeaf,this.state.maxBranchChildren=n.maxBranch,this.state.minLeafEntries=Math.ceil(n.maxLeaf/2),this.state.minBranchChildren=Math.ceil(n.maxBranch/2),this.state._nextAutoScaleThreshold=Q(0)}}get(t){let n=A(this.state,t);return n===null?null:s(n.leaf,n.index).value}hasKey(t){return de(this.state,t)}findFirst(t){let n=A(this.state,t);return n===null?null:s(n.leaf,n.index)}findLast(t){let n=fe(this.state,t);return n===null?null:s(n.leaf,n.index)}nextHigherKey(t){return pe(this.state,t)}nextLowerKey(t){return he(this.state,t)}getPairOrNextLower(t){let n=me(this.state,t);return n===null?null:s(n.leaf,n.index)}count(t,n,r){return ne(this.state,t,n,r)}deleteRange(t,n,r){return Ee(this.state,t,n,r)}range(t,n,r){return _e(this.state,t,n,r)}*entries(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield s(t,r);t=t.next}}*entriesReversed(){let t=this.state.rightmostLeaf;for(;t!==null;){let n=y(t);for(let r=n-1;r>=0;r-=1)yield s(t,r);t=t.prev}}*keys(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield s(t,r).key;t=t.next}}*values(){let t=this.state.leftmostLeaf;for(;t!==null;){let n=y(t);for(let r=0;r<n;r+=1)yield s(t,r).value;t=t.next}}[Symbol.iterator](){return this.entries()}forEach(t,n){let r=this.state.leftmostLeaf;for(;r!==null;){let o=y(r);for(let l=0;l<o;l+=1)t.call(n,s(r,l));r=r.next}}snapshot(){let t=new Array(this.state.entryCount),n=this.state.leftmostLeaf,r=0;for(;n!==null;){let o=y(n);for(let l=0;l<o;l+=1)t[r++]=s(n,l);n=n.next}return t}clone(){let t=new e(Fe(this.state));if(ee(t.state,this.state.maxLeafEntries,this.state.maxBranchChildren),this.state.entryCount>0){let n=new Array(this.state.entryCount),r=this.state.leftmostLeaf,o=0;for(;r!==null;){let l=y(r);for(let i=0;i<l;i+=1)n[o++]=s(r,i);r=r.next}t.putMany(n)}return t}toJSON(){return qe(this.state)}static fromJSON(t,n){De(t);let r=t.config.duplicateKeys!=="allow";for(let l=1;l<t.entries.length;l+=1){let i=n(t.entries[l-1][0],t.entries[l][0]);if(i>0)throw new f("fromJSON: entries not sorted.");if(r&&i===0)throw new f('fromJSON: duplicate keys require duplicateKeys "allow".')}let o=new e(Je(t,n));if(ee(o.state,t.config.maxLeafEntries,t.config.maxBranchChildren),t.entries.length>0){let l=new Array(t.entries.length);for(let i=0;i<t.entries.length;i+=1)l[i]={key:t.entries[i][0],value:t.entries[i][1]};o.putMany(l)}return o}size(){return this.state.entryCount}assertInvariants(){We(this.state)}getStats(){return Qe(this.state)}};var Ft=16,Ye=1024,qt=1e5,je=1e6,Ze=e=>{let t=e.autoScale===!0,n=t?x(0):void 0;return JSON.stringify({duplicateKeys:e.duplicateKeys??"replace",maxLeafEntries:e.maxLeafEntries??(n?n.maxLeaf:P),maxBranchChildren:e.maxBranchChildren??(n?n.maxBranch:_),enableEntryIdLookup:e.enableEntryIdLookup===!0,autoScale:t})},et=e=>{let t=e;throw new h(`Unsupported mutation type from shared store: ${String(t.type)}`)},tt=e=>{if(e===void 0)return Ft;if(!Number.isInteger(e)||e<1||e>Ye)throw new h(`maxRetries: integer 1\u2013${Ye} required.`);return e},nt=e=>{if(e===void 0)return qt;if(!Number.isInteger(e)||e<1||e>je)throw new h(`maxSyncMutationsPerBatch: integer 1\u2013${je} required.`);return e},rt=e=>{if(e===void 0)return"strong";if(e!=="strong"&&e!=="local")throw new h("readMode: must be 'strong' or 'local'.");return e};function ot(e,t){if(typeof t!="object"||t===null)throw new h("Store contract: append() must return {applied, version}.");let n=t;if(typeof n.applied!="boolean")throw new h("Store contract: applied must be boolean.");if(typeof n.version!="bigint")throw new h("Store contract: version must be bigint.");if(n.applied&&n.version<=e)throw new h("Store contract: applied version must exceed expected.");if(!n.applied&&n.version<e)throw new h("Store contract: rejected version must be >= expected.")}var U=class{constructor(t){this.store=t.store,this.maxRetries=tt(t.maxRetries),this.maxSyncMutationsPerBatch=nt(t.maxSyncMutationsPerBatch),this.duplicateKeys=t.duplicateKeys??"replace",this.readMode=rt(t.readMode),this.configFingerprint=Ze(t),this.tree=new R({compareKeys:t.compareKeys,maxLeafEntries:t.maxLeafEntries,maxBranchChildren:t.maxBranchChildren,duplicateKeys:t.duplicateKeys,enableEntryIdLookup:t.enableEntryIdLookup,autoScale:t.autoScale}),this.currentVersion=0n,this.operationQueue=Promise.resolve(),this.initSeen=!1}async sync(){await this.runExclusive(async()=>{await this.syncUnlocked()})}async syncUnlocked(){let t=await this.store.getLogEntriesSince(this.currentVersion);if(typeof t.version!="bigint")throw new h("Store contract: version must be bigint.");if(!Array.isArray(t.mutations))throw new h("Store contract: mutations must be an array.");if(t.mutations.length>this.maxSyncMutationsPerBatch)throw new h(`Sync batch exceeded limit (${String(this.maxSyncMutationsPerBatch)}).`);if(!(t.version<=this.currentVersion)){for(let n of t.mutations)this.applyMutationLocal(n);this.currentVersion=t.version}}applyMutationLocal(t){switch(t.type){case"init":if(t.configFingerprint!==this.configFingerprint)throw new h("Config mismatch: store peers must share identical tree config.");return this.initSeen=!0,null;case"put":return this.tree.put(t.key,t.value);case"remove":return this.tree.remove(t.key);case"removeById":return this.tree.removeById(t.entryId);case"updateById":return this.tree.updateById(t.entryId,t.value);case"popFirst":return this.tree.popFirst();case"popLast":return this.tree.popLast();default:return et(t)}}runExclusive(t){let n=async()=>t(),r=this.operationQueue.then(n,n);return this.operationQueue=r.then(()=>{},()=>{}),r}readOp(t){return this.runExclusive(async()=>(this.readMode==="strong"&&await this.syncUnlocked(),t(this.tree)))}async appendMutationAndApplyUnlocked(t){for(let n=0;n<this.maxRetries;n+=1){await this.syncUnlocked();let r=t(this.tree);if(r===null)return null;let o=this.currentVersion,l=this.initSeen?[r]:[{type:"init",configFingerprint:this.configFingerprint},r],i=await this.store.append(o,l);if(ot(o,i),i.applied){for(let T of l){if(T===r)break;this.applyMutationLocal(T)}let a=this.applyMutationLocal(r);return this.currentVersion=i.version,a}}throw new h(`Mutation failed after ${String(this.maxRetries)} retries.`)}async put(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(r=>{if(this.duplicateKeys==="reject"&&r.hasKey(t))throw new f("Duplicate key rejected.");return{type:"put",key:t,value:n}}))}async remove(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(n=>n.hasKey(t)?{type:"remove",key:t}:null))}async removeById(t){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(n=>n.peekById(t)!==null?{type:"removeById",entryId:t}:null))}async updateById(t,n){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(r=>r.peekById(t)!==null?{type:"updateById",entryId:t,value:n}:null))}async popFirst(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(t=>t.peekFirst()!==null?{type:"popFirst"}:null))}async get(t){return this.readOp(n=>n.get(t))}async hasKey(t){return this.readOp(n=>n.hasKey(t))}async findFirst(t){return this.readOp(n=>n.findFirst(t))}async findLast(t){return this.readOp(n=>n.findLast(t))}async range(t,n,r){return this.readOp(o=>o.range(t,n,r))}async snapshot(){return this.readOp(t=>t.snapshot())}async size(){return this.readOp(t=>t.size())}async assertInvariants(){await this.readOp(t=>t.assertInvariants())}async getStats(){return this.readOp(t=>t.getStats())}async peekFirst(){return this.readOp(t=>t.peekFirst())}async peekLast(){return this.readOp(t=>t.peekLast())}async popLast(){return this.runExclusive(async()=>this.appendMutationAndApplyUnlocked(t=>t.peekLast()!==null?{type:"popLast"}:null))}async peekById(t){return this.readOp(n=>n.peekById(t))}async count(t,n,r){return this.readOp(o=>o.count(t,n,r))}async nextHigherKey(t){return this.readOp(n=>n.nextHigherKey(t))}async nextLowerKey(t){return this.readOp(n=>n.nextLowerKey(t))}async getPairOrNextLower(t){return this.readOp(n=>n.getPairOrNextLower(t))}};return Tt(Dt);})();
|