@frostpillar/frostpillar-btree 0.2.5 → 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,41 +1,25 @@
1
- import { type BTreeEntry, type BTreeStats, type EntryId, type RangeBounds } from '../InMemoryBTree.js';
1
+ import { InMemoryBTree, type BTreeEntry, type BTreeJSON, type BTreeStats, type EntryId, type RangeBounds } from '../InMemoryBTree.js';
2
+ import type { KeyComparator } from '../btree/types.js';
2
3
  import type { ConcurrentInMemoryBTreeConfig } from './types.js';
3
4
  export declare class ConcurrentInMemoryBTree<TKey, TValue> {
4
- private readonly store;
5
- private readonly maxRetries;
6
- private readonly maxSyncMutationsPerBatch;
5
+ private readonly coord;
6
+ private readonly compareKeys;
7
7
  private readonly duplicateKeys;
8
- private readonly configFingerprint;
9
- private readonly readMode;
10
- private readonly tree;
11
- private currentVersion;
12
- private operationQueue;
13
- private initSeen;
14
8
  constructor(config: ConcurrentInMemoryBTreeConfig<TKey, TValue>);
15
9
  sync(): Promise<void>;
16
- private syncUnlocked;
17
- private applyMutationLocal;
18
- private runExclusive;
19
- private readOp;
20
- /**
21
- * Appends a mutation to the shared store using optimistic concurrency and,
22
- * on success, applies the same mutation locally in this method.
23
- *
24
- * The `evaluate` callback is called against the current (synced) local tree to
25
- * decide whether and what mutation to append. If the store append fails due to a
26
- * concurrent write, the tree is re-synced and `evaluate` is invoked again.
27
- *
28
- * The callback MUST be a pure function: it must not produce side effects and must
29
- * return the same logical result for equivalent tree states, because it may be
30
- * called multiple times across retries.
31
- *
32
- */
33
- private appendMutationAndApplyUnlocked;
10
+ syncThenRead<TResult>(fn: (tree: InMemoryBTree<TKey, TValue>) => TResult): Promise<TResult>;
34
11
  put(key: TKey, value: TValue): Promise<EntryId>;
35
12
  remove(key: TKey): Promise<BTreeEntry<TKey, TValue> | null>;
36
13
  removeById(entryId: EntryId): Promise<BTreeEntry<TKey, TValue> | null>;
37
14
  updateById(entryId: EntryId, value: TValue): Promise<BTreeEntry<TKey, TValue> | null>;
38
15
  popFirst(): Promise<BTreeEntry<TKey, TValue> | null>;
16
+ popLast(): Promise<BTreeEntry<TKey, TValue> | null>;
17
+ putMany(entries: readonly {
18
+ key: TKey;
19
+ value: TValue;
20
+ }[]): Promise<EntryId[]>;
21
+ deleteRange(startKey: TKey, endKey: TKey, options?: RangeBounds): Promise<number>;
22
+ clear(): Promise<void>;
39
23
  get(key: TKey): Promise<TValue | null>;
40
24
  hasKey(key: TKey): Promise<boolean>;
41
25
  findFirst(key: TKey): Promise<BTreeEntry<TKey, TValue> | null>;
@@ -47,10 +31,19 @@ export declare class ConcurrentInMemoryBTree<TKey, TValue> {
47
31
  getStats(): Promise<BTreeStats>;
48
32
  peekFirst(): Promise<BTreeEntry<TKey, TValue> | null>;
49
33
  peekLast(): Promise<BTreeEntry<TKey, TValue> | null>;
50
- popLast(): Promise<BTreeEntry<TKey, TValue> | null>;
51
34
  peekById(entryId: EntryId): Promise<BTreeEntry<TKey, TValue> | null>;
52
35
  count(startKey: TKey, endKey: TKey, options?: RangeBounds): Promise<number>;
53
36
  nextHigherKey(key: TKey): Promise<TKey | null>;
54
37
  nextLowerKey(key: TKey): Promise<TKey | null>;
55
38
  getPairOrNextLower(key: TKey): Promise<BTreeEntry<TKey, TValue> | null>;
39
+ entries(): Promise<BTreeEntry<TKey, TValue>[]>;
40
+ entriesReversed(): Promise<BTreeEntry<TKey, TValue>[]>;
41
+ keys(): Promise<TKey[]>;
42
+ values(): Promise<TValue[]>;
43
+ forEach(callback: (entry: BTreeEntry<TKey, TValue>) => void): Promise<void>;
44
+ forEachRange(startKey: TKey, endKey: TKey, callback: (entry: BTreeEntry<TKey, TValue>) => void, options?: RangeBounds): Promise<void>;
45
+ [Symbol.asyncIterator](): AsyncIterableIterator<BTreeEntry<TKey, TValue>>;
46
+ clone(): Promise<InMemoryBTree<TKey, TValue>>;
47
+ toJSON(): Promise<BTreeJSON<TKey, TValue>>;
48
+ static fromJSON<TKey, TValue>(json: BTreeJSON<TKey, TValue>, compareKeys: KeyComparator<TKey>): InMemoryBTree<TKey, TValue>;
56
49
  }
@@ -0,0 +1,21 @@
1
+ import { InMemoryBTree } from '../InMemoryBTree.js';
2
+ import type { BTreeMutation, ReadMode, SharedTreeStore } from './types.js';
3
+ import { type MutationResult } from './helpers.js';
4
+ export declare class Coordinator<TKey, TValue> {
5
+ readonly tree: InMemoryBTree<TKey, TValue>;
6
+ readonly store: SharedTreeStore<TKey, TValue>;
7
+ readonly maxRetries: number;
8
+ readonly maxSyncMutationsPerBatch: number;
9
+ readonly configFingerprint: string;
10
+ readonly readMode: ReadMode;
11
+ currentVersion: bigint;
12
+ operationQueue: Promise<void>;
13
+ initSeen: boolean;
14
+ corrupted: boolean;
15
+ constructor(tree: InMemoryBTree<TKey, TValue>, store: SharedTreeStore<TKey, TValue>, maxRetries: number, maxSyncMutationsPerBatch: number, configFingerprint: string, readMode: ReadMode);
16
+ syncUnlocked(): Promise<void>;
17
+ runExclusive<TResult>(operation: () => Promise<TResult>): Promise<TResult>;
18
+ readOp<TResult>(fn: (tree: InMemoryBTree<TKey, TValue>) => TResult): Promise<TResult>;
19
+ appendAndApply<TMutation extends BTreeMutation<TKey, TValue>>(evaluate: (tree: InMemoryBTree<TKey, TValue>) => TMutation | null): Promise<MutationResult<TKey, TValue, TMutation> | null>;
20
+ writeOp<TMutation extends BTreeMutation<TKey, TValue>>(evaluator: (tree: InMemoryBTree<TKey, TValue>) => TMutation | null): Promise<MutationResult<TKey, TValue, TMutation> | null>;
21
+ }
@@ -6,6 +6,8 @@ export type MutationResult<TKey, TValue, TMutation extends BTreeMutation<TKey, T
6
6
  } ? null : TMutation extends {
7
7
  type: 'put';
8
8
  } ? EntryId : TMutation extends {
9
+ type: 'putMany';
10
+ } ? EntryId[] : TMutation extends {
9
11
  type: 'remove';
10
12
  } ? BTreeEntry<TKey, TValue> | null : TMutation extends {
11
13
  type: 'removeById';
@@ -15,8 +17,12 @@ export type MutationResult<TKey, TValue, TMutation extends BTreeMutation<TKey, T
15
17
  type: 'popFirst';
16
18
  } ? BTreeEntry<TKey, TValue> | null : TMutation extends {
17
19
  type: 'popLast';
18
- } ? BTreeEntry<TKey, TValue> | null : never;
19
- export type AnyMutationResult<TKey, TValue> = EntryId | BTreeEntry<TKey, TValue> | null;
20
+ } ? BTreeEntry<TKey, TValue> | null : TMutation extends {
21
+ type: 'deleteRange';
22
+ } ? number : TMutation extends {
23
+ type: 'clear';
24
+ } ? null : never;
25
+ export type AnyMutationResult<TKey, TValue> = EntryId | EntryId[] | BTreeEntry<TKey, TValue> | number | null;
20
26
  export declare const assertNeverMutation: (mutation: never) => never;
21
27
  export declare const validateMutationBatch: <TKey, TValue>(mutations: BTreeMutation<TKey, TValue>[], expectedConfigFingerprint?: string) => void;
22
28
  export declare const normalizeMaxRetries: (value: number | undefined) => number;
@@ -0,0 +1,2 @@
1
+ import type { SharedTreeLog } from './types.js';
2
+ export declare const validateSyncLog: <TKey, TValue>(log: SharedTreeLog<TKey, TValue>, maxSyncMutationsPerBatch: number) => void;
@@ -1,4 +1,4 @@
1
- import type { EntryId, InMemoryBTreeConfig } from '../InMemoryBTree.js';
1
+ import type { EntryId, InMemoryBTreeConfig, RangeBounds } from '../InMemoryBTree.js';
2
2
  export type BTreeMutation<TKey, TValue> = {
3
3
  type: 'init';
4
4
  configFingerprint: string;
@@ -6,6 +6,12 @@ export type BTreeMutation<TKey, TValue> = {
6
6
  type: 'put';
7
7
  key: TKey;
8
8
  value: TValue;
9
+ } | {
10
+ type: 'putMany';
11
+ entries: readonly {
12
+ key: TKey;
13
+ value: TValue;
14
+ }[];
9
15
  } | {
10
16
  type: 'remove';
11
17
  key: TKey;
@@ -20,6 +26,13 @@ export type BTreeMutation<TKey, TValue> = {
20
26
  type: 'popFirst';
21
27
  } | {
22
28
  type: 'popLast';
29
+ } | {
30
+ type: 'deleteRange';
31
+ startKey: TKey;
32
+ endKey: TKey;
33
+ options?: RangeBounds;
34
+ } | {
35
+ type: 'clear';
23
36
  };
24
37
  export interface SharedTreeLog<TKey, TValue> {
25
38
  version: bigint;
@@ -0,0 +1,48 @@
1
+ import { InMemoryBTree, type EntryId, type RangeBounds } from '../InMemoryBTree.js';
2
+ import type { KeyComparator, DuplicateKeyPolicy } from '../btree/types.js';
3
+ import type { BTreeMutation } from './types.js';
4
+ import { type AnyMutationResult } from './helpers.js';
5
+ export declare const applyMutationLocal: <TKey, TValue>(tree: InMemoryBTree<TKey, TValue>, mutation: BTreeMutation<TKey, TValue>, onInit: () => void) => AnyMutationResult<TKey, TValue>;
6
+ export declare const createPutEvaluator: <TKey, TValue>(duplicateKeys: DuplicateKeyPolicy, key: TKey, value: TValue) => ((tree: InMemoryBTree<TKey, TValue>) => {
7
+ type: "put";
8
+ key: TKey;
9
+ value: TValue;
10
+ });
11
+ export declare const createRemoveEvaluator: <TKey, TValue>(key: TKey) => ((tree: InMemoryBTree<TKey, TValue>) => {
12
+ type: "remove";
13
+ key: TKey;
14
+ } | null);
15
+ export declare const createRemoveByIdEvaluator: <TKey, TValue>(entryId: EntryId) => ((tree: InMemoryBTree<TKey, TValue>) => {
16
+ type: "removeById";
17
+ entryId: EntryId;
18
+ } | null);
19
+ export declare const createUpdateByIdEvaluator: <TKey, TValue>(entryId: EntryId, value: TValue) => ((tree: InMemoryBTree<TKey, TValue>) => {
20
+ type: "updateById";
21
+ entryId: EntryId;
22
+ value: TValue;
23
+ } | null);
24
+ export declare const createPopFirstEvaluator: <TKey, TValue>() => ((tree: InMemoryBTree<TKey, TValue>) => {
25
+ type: "popFirst";
26
+ } | null);
27
+ export declare const createPopLastEvaluator: <TKey, TValue>() => ((tree: InMemoryBTree<TKey, TValue>) => {
28
+ type: "popLast";
29
+ } | null);
30
+ export declare const createPutManyEvaluator: <TKey, TValue>(entries: readonly {
31
+ key: TKey;
32
+ value: TValue;
33
+ }[], duplicateKeys: DuplicateKeyPolicy, compareKeys: KeyComparator<TKey>) => ((tree: InMemoryBTree<TKey, TValue>) => {
34
+ type: "putMany";
35
+ entries: readonly {
36
+ key: TKey;
37
+ value: TValue;
38
+ }[];
39
+ });
40
+ export declare const createDeleteRangeEvaluator: <TKey, TValue>(startKey: TKey, endKey: TKey, options: RangeBounds | undefined) => ((tree: InMemoryBTree<TKey, TValue>) => {
41
+ type: "deleteRange";
42
+ startKey: TKey;
43
+ endKey: TKey;
44
+ options?: RangeBounds;
45
+ } | null);
46
+ export declare const createClearEvaluator: <TKey, TValue>() => ((tree: InMemoryBTree<TKey, TValue>) => {
47
+ type: "clear";
48
+ });