@frostpillar/frostpillar-btree 0.2.6 → 0.2.7

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.
@@ -1,4 +1,7 @@
1
1
  import { type BTreeEntry, type BTreeState, type RangeBounds } from './types.js';
2
2
  export declare function isEmptyRange<TKey>(compare: (a: TKey, b: TKey) => number, startKey: TKey, endKey: TKey, options?: RangeBounds): boolean;
3
3
  export declare const countRangeEntries: <TKey, TValue>(state: BTreeState<TKey, TValue>, startKey: TKey, endKey: TKey, options?: RangeBounds) => number;
4
- export declare const rangeQueryEntries: <TKey, TValue>(state: BTreeState<TKey, TValue>, startKey: TKey, endKey: TKey, options?: RangeBounds) => BTreeEntry<TKey, TValue>[];
4
+ /** Single-pass range query that produces public entries (via freezeEntry) inline. */
5
+ export declare const rangeQueryPublicEntries: <TKey, TValue>(state: BTreeState<TKey, TValue>, startKey: TKey, endKey: TKey, options?: RangeBounds) => BTreeEntry<TKey, TValue>[];
6
+ /** Streaming range iteration — invokes callback for each entry without array allocation. */
7
+ export declare const forEachRangeEntries: <TKey, TValue>(state: BTreeState<TKey, TValue>, startKey: TKey, endKey: TKey, callback: (entry: BTreeEntry<TKey, TValue>) => void, options?: RangeBounds) => void;
@@ -0,0 +1,4 @@
1
+ import { type BTreeNode, type BTreeState, type BranchNode } from './types.js';
2
+ export declare const updateMinKeyInAncestors: <TKey, TValue>(node: BTreeNode<TKey, TValue>) => void;
3
+ export declare const removeChildFromBranch: <TKey, TValue>(branch: BranchNode<TKey, TValue>, childIndex: number) => void;
4
+ export declare const rebalanceAfterBranchRemoval: <TKey, TValue>(state: BTreeState<TKey, TValue>, branch: BranchNode<TKey, TValue>) => void;
@@ -1,4 +1,7 @@
1
- import { type BTreeNode, type BTreeState, type LeafNode } from './types.js';
2
- declare const updateMinKeyInAncestors: <TKey, TValue>(node: BTreeNode<TKey, TValue>) => void;
1
+ import { type BTreeState, type LeafNode } from './types.js';
2
+ import { updateMinKeyInAncestors } from './rebalance-branch.js';
3
3
  export { updateMinKeyInAncestors };
4
+ /** Applies the lazy divisor to a minimum-occupancy value. */
5
+ export declare const applyLazyThreshold: (min: number) => number;
6
+ export declare const leafRebalanceThreshold: <TKey, TValue>(state: BTreeState<TKey, TValue>) => number;
4
7
  export declare const rebalanceAfterLeafRemoval: <TKey, TValue>(state: BTreeState<TKey, TValue>, leaf: LeafNode<TKey, TValue>) => void;
@@ -1,4 +1,4 @@
1
- import { type BTreeState, type DuplicateKeyPolicy, type InMemoryBTreeConfig, type KeyComparator } from './types.js';
1
+ import { type BTreeState, type DeleteRebalancePolicy, type DuplicateKeyPolicy, type InMemoryBTreeConfig, type KeyComparator } from './types.js';
2
2
  export interface BTreeJSON<TKey, TValue> {
3
3
  version: number;
4
4
  config: {
@@ -7,10 +7,12 @@ export interface BTreeJSON<TKey, TValue> {
7
7
  duplicateKeys: DuplicateKeyPolicy;
8
8
  enableEntryIdLookup: boolean;
9
9
  autoScale: boolean;
10
+ deleteRebalancePolicy?: DeleteRebalancePolicy;
10
11
  };
11
12
  entries: [TKey, TValue][];
12
13
  }
13
14
  export declare const buildConfigFromState: <TKey, TValue>(state: BTreeState<TKey, TValue>) => InMemoryBTreeConfig<TKey>;
14
15
  export declare const serializeToJSON: <TKey, TValue>(state: BTreeState<TKey, TValue>) => BTreeJSON<TKey, TValue>;
15
16
  export declare const validateBTreeJSON: <TKey, TValue>(json: BTreeJSON<TKey, TValue>) => void;
17
+ export declare const validateBTreeJSONSortOrder: <TKey, TValue>(json: BTreeJSON<TKey, TValue>, compareKeys: KeyComparator<TKey>) => void;
16
18
  export declare const buildConfigFromJSON: <TKey>(json: BTreeJSON<TKey, unknown>, compareKeys: KeyComparator<TKey>) => InMemoryBTreeConfig<TKey>;
@@ -0,0 +1,3 @@
1
+ import { type BTreeState, type BranchNode, type LeafNode } from './types.js';
2
+ export declare const splitLeaf: <TKey, TValue>(state: BTreeState<TKey, TValue>, leaf: LeafNode<TKey, TValue>) => void;
3
+ export declare const splitBranch: <TKey, TValue>(state: BTreeState<TKey, TValue>, branch: BranchNode<TKey, TValue>) => void;
@@ -0,0 +1,7 @@
1
+ import { type BTreeEntry, type BTreeState } from './types.js';
2
+ /** Collect all entries into a pre-allocated array, frozen for safe external use. */
3
+ export declare const snapshotEntries: <TKey, TValue>(state: BTreeState<TKey, TValue>) => BTreeEntry<TKey, TValue>[];
4
+ /** Collect all internal entries (no freeze) for internal use (clone, serialize). */
5
+ export declare const collectInternalEntries: <TKey, TValue>(state: BTreeState<TKey, TValue>) => BTreeEntry<TKey, TValue>[];
6
+ /** Iterate all entries, invoking callback with frozen entries. */
7
+ export declare const forEachEntry: <TKey, TValue>(state: BTreeState<TKey, TValue>, callback: (entry: BTreeEntry<TKey, TValue>) => void, thisArg?: unknown) => void;
@@ -6,6 +6,7 @@ export declare const NODE_LEAF: 0;
6
6
  export declare const NODE_BRANCH: 1;
7
7
  export type KeyComparator<TKey> = (left: TKey, right: TKey) => number;
8
8
  export type DuplicateKeyPolicy = 'allow' | 'reject' | 'replace';
9
+ export type DeleteRebalancePolicy = 'standard' | 'lazy';
9
10
  /**
10
11
  * Defines the inclusivity of the lower and upper bounds for a key range scan.
11
12
  * Both bounds default to `'inclusive'` when omitted.
@@ -17,6 +18,7 @@ export interface RangeBounds {
17
18
  upperBound?: 'inclusive' | 'exclusive';
18
19
  }
19
20
  export declare const normalizeDuplicateKeyPolicy: (value: DuplicateKeyPolicy | undefined) => DuplicateKeyPolicy;
21
+ export declare const normalizeDeleteRebalancePolicy: (value: DeleteRebalancePolicy | undefined) => DeleteRebalancePolicy;
20
22
  export type EntryId = number & {
21
23
  readonly __brand: 'EntryId';
22
24
  };
@@ -36,10 +38,17 @@ export interface LeafEntry<TKey, TValue> {
36
38
  value: TValue;
37
39
  }
38
40
  /**
39
- * Shallow-copies a `LeafEntry` into a plain `BTreeEntry` for safe public API return.
40
- * Prevents callers from holding a reference that mutates when `updateById` is called.
41
+ * Freezes and returns an internal entry for safe exposure via the public API.
42
+ * Idempotent: re-freezing an already-frozen object is a no-op in V8.
43
+ * All entries are frozen at creation via createEntry, so this is a zero-allocation cast.
41
44
  */
42
- export declare const toPublicEntry: <TKey, TValue>(entry: LeafEntry<TKey, TValue>) => BTreeEntry<TKey, TValue>;
45
+ export declare const freezeEntry: <TKey, TValue>(entry: LeafEntry<TKey, TValue>) => BTreeEntry<TKey, TValue>;
46
+ /**
47
+ * Creates a frozen LeafEntry with a canonical property order.
48
+ * All entry creation MUST go through this function to guarantee a single
49
+ * V8 hidden class across all entries in the tree.
50
+ */
51
+ export declare const createEntry: <TKey, TValue>(key: TKey, entryId: EntryId, value: TValue) => LeafEntry<TKey, TValue>;
43
52
  export interface LeafNode<TKey, TValue> {
44
53
  kind: typeof NODE_LEAF;
45
54
  entries: LeafEntry<TKey, TValue>[];
@@ -72,6 +81,7 @@ export interface BTreeState<TKey, TValue> {
72
81
  minBranchChildren: number;
73
82
  entryKeys: Map<EntryId, TKey> | null;
74
83
  autoScale: boolean;
84
+ deleteRebalancePolicy: DeleteRebalancePolicy;
75
85
  _nextAutoScaleThreshold: number;
76
86
  /** @internal Shared return object for navigation functions — never store a reference across calls. */
77
87
  _cursor: {
@@ -86,6 +96,7 @@ export interface InMemoryBTreeConfig<TKey> {
86
96
  duplicateKeys?: DuplicateKeyPolicy;
87
97
  enableEntryIdLookup?: boolean;
88
98
  autoScale?: boolean;
99
+ deleteRebalancePolicy?: DeleteRebalancePolicy;
89
100
  }
90
101
  export interface BTreeStats {
91
102
  height: number;