eigen-db 4.2.0 → 4.4.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/dist/eigen-db.js CHANGED
@@ -3,7 +3,7 @@ class k extends Error {
3
3
  super(`Capacity exceeded. Max vectors for this dimension size is ~${e}.`), this.name = "VectorCapacityExceededError";
4
4
  }
5
5
  }
6
- class D {
6
+ class K {
7
7
  dirHandle = null;
8
8
  dirName;
9
9
  constructor(e) {
@@ -18,34 +18,34 @@ class D {
18
18
  }
19
19
  async readAll(e) {
20
20
  try {
21
- const r = await (await (await (await this.getDir()).getFileHandle(e)).getFile()).arrayBuffer();
22
- return new Uint8Array(r);
21
+ const n = await (await (await (await this.getDir()).getFileHandle(e)).getFile()).arrayBuffer();
22
+ return new Uint8Array(n);
23
23
  } catch {
24
24
  return new Uint8Array(0);
25
25
  }
26
26
  }
27
27
  async append(e, t) {
28
- const A = await (await this.getDir()).getFileHandle(e, { create: !0 }), r = await A.createWritable({ keepExistingData: !0 }), n = await A.getFile();
29
- await r.seek(n.size), await r.write(t), await r.close();
28
+ const s = await (await this.getDir()).getFileHandle(e, { create: !0 }), n = await s.createWritable({ keepExistingData: !0 }), o = await s.getFile();
29
+ await n.seek(o.size), await n.write(t), await n.close();
30
30
  }
31
31
  async write(e, t) {
32
- const r = await (await (await this.getDir()).getFileHandle(e, { create: !0 })).createWritable({ keepExistingData: !1 });
33
- await r.write(t), await r.close();
32
+ const n = await (await (await this.getDir()).getFileHandle(e, { create: !0 })).createWritable({ keepExistingData: !1 });
33
+ await n.write(t), await n.close();
34
34
  }
35
35
  async destroy() {
36
36
  await (await navigator.storage.getDirectory()).removeEntry(this.dirName, { recursive: !0 }), this.dirHandle = null;
37
37
  }
38
38
  }
39
- class F {
39
+ class S {
40
40
  files = /* @__PURE__ */ new Map();
41
41
  async readAll(e) {
42
42
  const t = this.files.get(e);
43
43
  if (!t || t.length === 0) return new Uint8Array(0);
44
- const s = t.reduce((n, o) => n + o.byteLength, 0), A = new Uint8Array(s);
45
- let r = 0;
46
- for (const n of t)
47
- A.set(n, r), r += n.byteLength;
48
- return A;
44
+ const r = t.reduce((o, a) => o + a.byteLength, 0), s = new Uint8Array(r);
45
+ let n = 0;
46
+ for (const o of t)
47
+ s.set(o, n), n += o.byteLength;
48
+ return s;
49
49
  }
50
50
  async append(e, t) {
51
51
  this.files.has(e) || this.files.set(e, []), this.files.get(e).push(new Uint8Array(t));
@@ -57,46 +57,46 @@ class F {
57
57
  this.files.clear();
58
58
  }
59
59
  }
60
- function S(a) {
60
+ function v(c) {
61
61
  let e = 0;
62
- for (let A = 0; A < a.length; A++)
63
- e += a[A] * a[A];
62
+ for (let s = 0; s < c.length; s++)
63
+ e += c[s] * c[s];
64
64
  const t = Math.sqrt(e);
65
65
  if (t === 0) return;
66
- const s = 1 / t;
67
- for (let A = 0; A < a.length; A++)
68
- a[A] *= s;
66
+ const r = 1 / t;
67
+ for (let s = 0; s < c.length; s++)
68
+ c[s] *= r;
69
69
  }
70
- function v(a, e, t, s, A) {
71
- for (let r = 0; r < s; r++) {
72
- let n = 0;
73
- const o = r * A;
74
- for (let i = 0; i < A; i++)
75
- n += a[i] * e[o + i];
76
- t[r] = n;
70
+ function O(c, e, t, r, s) {
71
+ for (let n = 0; n < r; n++) {
72
+ let o = 0;
73
+ const a = n * s;
74
+ for (let i = 0; i < s; i++)
75
+ o += c[i] * e[a + i];
76
+ t[n] = o;
77
77
  }
78
78
  }
79
- const p = new TextEncoder(), U = new TextDecoder();
80
- function u(a) {
81
- const e = a.map((o) => p.encode(o)), t = e.reduce((o, i) => o + 4 + i.byteLength, 0), s = new ArrayBuffer(t), A = new DataView(s), r = new Uint8Array(s);
82
- let n = 0;
83
- for (const o of e)
84
- A.setUint32(n, o.byteLength, !0), n += 4, r.set(o, n), n += o.byteLength;
85
- return r;
79
+ const U = new TextEncoder(), x = new TextDecoder();
80
+ function d(c) {
81
+ const e = c.map((a) => U.encode(a)), t = e.reduce((a, i) => a + 4 + i.byteLength, 0), r = new ArrayBuffer(t), s = new DataView(r), n = new Uint8Array(r);
82
+ let o = 0;
83
+ for (const a of e)
84
+ s.setUint32(o, a.byteLength, !0), o += 4, n.set(a, o), o += a.byteLength;
85
+ return n;
86
86
  }
87
- function w(a) {
88
- const e = [], t = new DataView(a.buffer, a.byteOffset, a.byteLength);
89
- let s = 0;
90
- for (; s < a.byteLength; ) {
91
- const A = t.getUint32(s, !0);
92
- s += 4;
93
- const r = U.decode(a.subarray(s, s + A));
94
- e.push(r), s += A;
87
+ function g(c) {
88
+ const e = [], t = new DataView(c.buffer, c.byteOffset, c.byteLength);
89
+ let r = 0;
90
+ for (; r < c.byteLength; ) {
91
+ const s = t.getUint32(r, !0);
92
+ r += 4;
93
+ const n = x.decode(c.subarray(r, r + s));
94
+ e.push(n), r += s;
95
95
  }
96
96
  return e;
97
97
  }
98
- const l = 65536, I = 65536;
99
- class L {
98
+ const m = 65536, w = 65536;
99
+ class T {
100
100
  memory;
101
101
  dimensions;
102
102
  queryOffset;
@@ -104,10 +104,10 @@ class L {
104
104
  _vectorCount;
105
105
  constructor(e, t = 0) {
106
106
  this.dimensions = e, this.queryOffset = 0;
107
- const s = e * 4;
108
- this.dbOffset = Math.ceil(s / l) * l;
109
- const A = t * e * 4, r = this.dbOffset + A, n = Math.max(1, Math.ceil(r / l));
110
- this.memory = new WebAssembly.Memory({ initial: n }), this._vectorCount = t;
107
+ const r = e * 4;
108
+ this.dbOffset = Math.ceil(r / m) * m;
109
+ const s = t * e * 4, n = this.dbOffset + s, o = Math.max(1, Math.ceil(n / m));
110
+ this.memory = new WebAssembly.Memory({ initial: o }), this._vectorCount = t;
111
111
  }
112
112
  /** Current number of vectors stored */
113
113
  get vectorCount() {
@@ -126,7 +126,7 @@ class L {
126
126
  * Accounts for query buffer, DB space, and scores buffer.
127
127
  */
128
128
  get maxVectors() {
129
- const e = I * l - this.dbOffset, t = this.dimensions * 4 + 4;
129
+ const e = w * m - this.dbOffset, t = this.dimensions * 4 + 4;
130
130
  return Math.floor(e / t);
131
131
  }
132
132
  /**
@@ -134,12 +134,12 @@ class L {
134
134
  * Calls memory.grow() if needed.
135
135
  */
136
136
  ensureCapacity(e) {
137
- const t = this._vectorCount + e, s = this.dbOffset + t * this.dimensions * 4 + t * 4, A = this.memory.buffer.byteLength;
138
- if (s > A) {
139
- const r = Math.ceil((s - A) / l);
140
- if (A / l + r > I)
137
+ const t = this._vectorCount + e, r = this.dbOffset + t * this.dimensions * 4 + t * 4, s = this.memory.buffer.byteLength;
138
+ if (r > s) {
139
+ const n = Math.ceil((r - s) / m);
140
+ if (s / m + n > w)
141
141
  throw new Error("WASM memory limit exceeded");
142
- this.memory.grow(r);
142
+ this.memory.grow(n);
143
143
  }
144
144
  }
145
145
  /**
@@ -154,9 +154,9 @@ class L {
154
154
  */
155
155
  appendVectors(e) {
156
156
  const t = this.dbOffset + this._vectorCount * this.dimensions * 4;
157
- let s = t;
158
- for (const A of e)
159
- new Float32Array(this.memory.buffer, s, this.dimensions).set(A), s += this.dimensions * 4;
157
+ let r = t;
158
+ for (const s of e)
159
+ new Float32Array(this.memory.buffer, r, this.dimensions).set(s), r += this.dimensions * 4;
160
160
  return this._vectorCount += e.length, t;
161
161
  }
162
162
  /**
@@ -183,8 +183,8 @@ class L {
183
183
  * Write a vector to a specific slot in the database region.
184
184
  */
185
185
  writeVector(e, t) {
186
- const s = this.dbOffset + e * this.dimensions * 4;
187
- new Float32Array(this.memory.buffer, s, this.dimensions).set(t);
186
+ const r = this.dbOffset + e * this.dimensions * 4;
187
+ new Float32Array(this.memory.buffer, r, this.dimensions).set(t);
188
188
  }
189
189
  /**
190
190
  * Reset the vector count to zero, logically clearing the database.
@@ -201,59 +201,59 @@ class L {
201
201
  this._vectorCount = e;
202
202
  }
203
203
  }
204
- function x(a, e, t, s) {
205
- const A = a.length;
206
- if (A === 0) return [];
207
- const r = new Uint32Array(A);
208
- for (let i = 0; i < A; i++) r[i] = i;
209
- r.sort((i, c) => a[c] - a[i]);
210
- const n = Math.min(t, A), o = [];
211
- for (let i = 0; i < n; i++) {
212
- const c = r[i], h = a[c];
213
- if (s !== void 0 && h < s) break;
214
- o.push({ key: e(c), similarity: h });
215
- }
216
- return o;
204
+ function Q(c, e, t, r) {
205
+ const s = c.length;
206
+ if (s === 0) return [];
207
+ const n = new Uint32Array(s);
208
+ for (let i = 0; i < s; i++) n[i] = i;
209
+ n.sort((i, h) => c[h] - c[i]);
210
+ const o = Math.min(t, s), a = [];
211
+ for (let i = 0; i < o; i++) {
212
+ const h = n[i], y = c[h];
213
+ if (r !== void 0 && y < r) break;
214
+ a.push({ key: e(h), similarity: y });
215
+ }
216
+ return a;
217
217
  }
218
- function O(a, e, t, s) {
219
- const A = a.length;
220
- if (A === 0) return [];
221
- const r = new Uint32Array(A);
222
- for (let o = 0; o < A; o++) r[o] = o;
223
- r.sort((o, i) => a[i] - a[o]);
224
- const n = Math.min(t, A);
218
+ function V(c, e, t, r) {
219
+ const s = c.length;
220
+ if (s === 0) return [];
221
+ const n = new Uint32Array(s);
222
+ for (let a = 0; a < s; a++) n[a] = a;
223
+ n.sort((a, i) => c[i] - c[a]);
224
+ const o = Math.min(t, s);
225
225
  return {
226
226
  [Symbol.iterator]() {
227
- let o = 0;
227
+ let a = 0;
228
228
  return {
229
229
  next() {
230
- if (o >= n) return { done: !0, value: void 0 };
231
- const i = r[o++], c = a[i];
232
- return s !== void 0 && c < s ? { done: !0, value: void 0 } : {
230
+ if (a >= o) return { done: !0, value: void 0 };
231
+ const i = n[a++], h = c[i];
232
+ return r !== void 0 && h < r ? { done: !0, value: void 0 } : {
233
233
  done: !1,
234
- value: { key: e(i), similarity: c }
234
+ value: { key: e(i), similarity: h }
235
235
  };
236
236
  }
237
237
  };
238
238
  }
239
239
  };
240
240
  }
241
- const G = "AGFzbQEAAAABDgJgAn9/AGAFf39/f38AAg8BA2VudgZtZW1vcnkCAAEDAwIAAQcaAglub3JtYWxpemUAAApzZWFyY2hfYW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QYMAAsLIAcgCP3kASAJIAr95AH95AEhBwJAA0AgBiAYTw0BIAAgBkECdGohGSAVIAZBAnRqIRogByAZ/QAEACAa/QAEAP3mAf3kASEHIAZBBGohBgwACwsgB/0fACAH/R8BkiAH/R8CIAf9HwOSkiETAkADQCAGIARPDQEgACAGQQJ0aiEZIBUgBkECdGohGiATIBkqAgAgGioCAJSSIRMgBkEBaiEGDAALCyACIAVBAnRqIBM4AgALCw==";
242
- function R() {
243
- const a = atob(G), e = new Uint8Array(a.length);
244
- for (let t = 0; t < a.length; t++)
245
- e[t] = a.charCodeAt(t);
241
+ const _ = "AGFzbQEAAAABDgJgAn9/AGAFf39/f38AAg8BA2VudgZtZW1vcnkCAAEDAwIAAQcaAglub3JtYWxpemUAAApzZWFyY2hfYWxsAAEKpgQCrQIFAX8BewN9AXsCf/0MAAAAAAAAAAAAAAAAAAAAACEDIAFBfHEhCEEAIQICQANAIAIgCE8NASAAIAJBAnRqIQkgAyAJ/QAEACAJ/QAEAP3mAf3kASEDIAJBBGohAgwACwsgA/0fACAD/R8BkiAD/R8CIAP9HwOSkiEEAkADQCACIAFPDQEgACACQQJ0aiEJIAQgCSoCACAJKgIAlJIhBCACQQFqIQIMAAsLIASRIQUgBUMAAAAAWwRADwtDAACAPyAFlSEGIAb9EyEHQQAhAgJAA0AgAiAITw0BIAAgAkECdGohCSAJIAn9AAQAIAf95gH9CwQAIAJBBGohAgwACwsCQANAIAIgAU8NASAAIAJBAnRqIQkgCSAJKgIAIAaUOAIAIAJBAWohAgwACwsL9AEEAn8BewF9BX8gBEF8cSEKIARBAnQhDUEAIQUCQANAIAUgA08NASABIAUgDWxqIQn9DAAAAAAAAAAAAAAAAAAAAAAhB0EAIQYCQANAIAYgCk8NASAAIAZBAnRqIQsgCSAGQQJ0aiEMIAcgC/0ABAAgDP0ABAD95gH95AEhByAGQQRqIQYMAAsLIAf9HwAgB/0fAZIgB/0fAiAH/R8DkpIhCAJAA0AgBiAETw0BIAAgBkECdGohCyAJIAZBAnRqIQwgCCALKgIAIAwqAgCUkiEIIAZBAWohBgwACwsgAiAFQQJ0aiAIOAIAIAVBAWohBQwACwsL";
242
+ function D() {
243
+ const c = atob(_), e = new Uint8Array(c.length);
244
+ for (let t = 0; t < c.length; t++)
245
+ e[t] = c.charCodeAt(t);
246
246
  return e;
247
247
  }
248
- async function T(a, e) {
248
+ async function L(c, e) {
249
249
  const t = { env: { memory: e } };
250
- return (await WebAssembly.instantiate(a, t)).instance.exports;
250
+ return (await WebAssembly.instantiate(c, t)).instance.exports;
251
251
  }
252
- const B = "vectors.bin", Q = "keys.bin", E = 1111770949, d = 1, C = 24, H = 65536;
253
- class b {
252
+ const b = "vectors.bin", C = "keys.bin", E = 1111770949, u = 1, M = 24, F = 65536;
253
+ class I {
254
254
  memoryManager;
255
255
  storage;
256
- dimensions;
256
+ _dimensions;
257
257
  shouldNormalize;
258
258
  wasmExports;
259
259
  /** Maps key to its slot index in the vector array */
@@ -262,46 +262,98 @@ class b {
262
262
  slotToKey;
263
263
  /** Whether this instance has been closed */
264
264
  closed = !1;
265
- constructor(e, t, s, A, r, n, o) {
266
- this.memoryManager = e, this.storage = t, this.dimensions = s, this.shouldNormalize = A, this.wasmExports = r, this.keyToSlot = n, this.slotToKey = o;
265
+ constructor(e, t, r, s, n, o, a) {
266
+ this.memoryManager = e, this.storage = t, this._dimensions = r, this.shouldNormalize = s, this.wasmExports = n, this.keyToSlot = o, this.slotToKey = a;
267
267
  }
268
268
  static async open(e) {
269
- const t = e.name ?? "default", s = e.storage ?? new D(t), A = e.normalize !== !1, [r, n] = await Promise.all([s.readAll(B), s.readAll(Q)]), o = n.byteLength > 0 ? w(n) : [], i = r.byteLength / (e.dimensions * 4), c = /* @__PURE__ */ new Map(), h = [];
270
- for (let m = 0; m < o.length; m++)
271
- c.set(o[m], m), h[m] = o[m];
272
- const g = new L(e.dimensions, i);
273
- r.byteLength > 0 && g.loadVectorBytes(r, i);
274
- let f = null;
275
- const y = e.wasmBinary !== void 0 ? e.wasmBinary : R();
276
- if (y !== null)
269
+ const t = e.storage ?? new S(), r = e.normalize !== !1, [s, n] = await Promise.all([t.readAll(b), t.readAll(C)]), o = n.byteLength > 0 ? g(n) : [], a = s.byteLength / (e.dimensions * 4), i = /* @__PURE__ */ new Map(), h = [];
270
+ for (let A = 0; A < o.length; A++)
271
+ i.set(o[A], A), h[A] = o[A];
272
+ const y = new T(e.dimensions, a);
273
+ s.byteLength > 0 && y.loadVectorBytes(s, a);
274
+ let l = null;
275
+ const f = e.wasmBinary !== void 0 ? e.wasmBinary : D();
276
+ if (f !== null)
277
277
  try {
278
- f = await T(y, g.memory);
278
+ l = await L(f, y.memory);
279
279
  } catch {
280
280
  }
281
- return new b(g, s, e.dimensions, A, f, c, h);
281
+ return new I(y, t, e.dimensions, r, l, i, h);
282
282
  }
283
283
  /** Total number of key-value pairs in the database */
284
284
  get size() {
285
285
  return this.keyToSlot.size;
286
286
  }
287
+ /** Number of dimensions per vector */
288
+ get dimensions() {
289
+ return this._dimensions;
290
+ }
291
+ /**
292
+ * Check whether a key exists in the database.
293
+ * Uses the internal key-to-slot map for O(1) lookup.
294
+ */
295
+ has(e) {
296
+ return this.assertOpen(), this.keyToSlot.has(e);
297
+ }
298
+ /**
299
+ * Delete an entry by key. Returns true if the key existed, false otherwise.
300
+ * Uses swap-and-pop to avoid gaps in the underlying vector array.
301
+ */
302
+ delete(e) {
303
+ this.assertOpen();
304
+ const t = this.keyToSlot.get(e);
305
+ if (t === void 0) return !1;
306
+ const r = this.memoryManager.vectorCount - 1;
307
+ if (t !== r) {
308
+ const s = new Float32Array(this.memoryManager.readVector(r));
309
+ this.memoryManager.writeVector(t, s);
310
+ const n = this.slotToKey[r];
311
+ this.keyToSlot.set(n, t), this.slotToKey[t] = n;
312
+ }
313
+ return this.keyToSlot.delete(e), this.slotToKey.length = r, this.memoryManager.setVectorCount(r), !0;
314
+ }
315
+ /**
316
+ * Returns an iterable of all keys in the database.
317
+ */
318
+ keys() {
319
+ return this.assertOpen(), this.keyToSlot.keys();
320
+ }
321
+ /**
322
+ * Returns an iterable of [key, value] pairs.
323
+ * Values are returned as plain number array copies.
324
+ */
325
+ entries() {
326
+ this.assertOpen();
327
+ const e = this.keyToSlot, t = this.memoryManager;
328
+ return (function* () {
329
+ for (const [r, s] of e)
330
+ yield [r, Array.from(t.readVector(s))];
331
+ })();
332
+ }
333
+ /**
334
+ * Implements the iterable protocol. Same as entries().
335
+ */
336
+ [Symbol.iterator]() {
337
+ return this.entries();
338
+ }
287
339
  /**
288
340
  * Set a key-value pair. If the key already exists, its vector is overwritten (last-write-wins).
289
341
  * The value is a number[] or Float32Array of length equal to the configured dimensions.
290
342
  */
291
- set(e, t, s) {
292
- if (this.assertOpen(), t.length !== this.dimensions)
293
- throw new Error(`Vector dimension mismatch: expected ${this.dimensions}, got ${t.length}`);
294
- const A = new Float32Array(t);
295
- (s?.normalize ?? this.shouldNormalize) && this.normalizeVector(A);
296
- const n = this.keyToSlot.get(e);
297
- if (n !== void 0)
298
- this.memoryManager.writeVector(n, A);
343
+ set(e, t, r) {
344
+ if (this.assertOpen(), t.length !== this._dimensions)
345
+ throw new Error(`Vector dimension mismatch: expected ${this._dimensions}, got ${t.length}`);
346
+ const s = new Float32Array(t);
347
+ (r?.normalize ?? this.shouldNormalize) && this.normalizeVector(s);
348
+ const o = this.keyToSlot.get(e);
349
+ if (o !== void 0)
350
+ this.memoryManager.writeVector(o, s);
299
351
  else {
300
352
  if (this.memoryManager.vectorCount + 1 > this.memoryManager.maxVectors)
301
353
  throw new k(this.memoryManager.maxVectors);
302
354
  this.memoryManager.ensureCapacity(1);
303
355
  const i = this.memoryManager.vectorCount;
304
- this.memoryManager.appendVectors([A]), this.keyToSlot.set(e, i), this.slotToKey[i] = e;
356
+ this.memoryManager.appendVectors([s]), this.keyToSlot.set(e, i), this.slotToKey[i] = e;
305
357
  }
306
358
  }
307
359
  /**
@@ -318,8 +370,8 @@ class b {
318
370
  * Set multiple key-value pairs at once. Last-write-wins applies within the batch.
319
371
  */
320
372
  setMany(e) {
321
- for (const [t, s] of e)
322
- this.set(t, s);
373
+ for (const [t, r] of e)
374
+ this.set(t, r);
323
375
  }
324
376
  /**
325
377
  * Get vectors for multiple keys. Returns undefined for keys that don't exist.
@@ -329,53 +381,53 @@ class b {
329
381
  }
330
382
  query(e, t) {
331
383
  this.assertOpen();
332
- const s = t?.topK ?? 1 / 0, A = t?.minSimilarity, r = t && "iterable" in t && t.iterable;
384
+ const r = t?.topK ?? 1 / 0, s = t?.minSimilarity, n = t && "iterable" in t && t.iterable;
333
385
  if (this.size === 0)
334
386
  return [];
335
- if (e.length !== this.dimensions)
336
- throw new Error(`Query vector dimension mismatch: expected ${this.dimensions}, got ${e.length}`);
337
- const n = new Float32Array(e);
338
- (t?.normalize ?? this.shouldNormalize) && this.normalizeVector(n), this.memoryManager.writeQuery(n), this.memoryManager.ensureCapacity(0);
339
- const i = this.memoryManager.vectorCount, c = this.memoryManager.scoresOffset;
387
+ if (e.length !== this._dimensions)
388
+ throw new Error(`Query vector dimension mismatch: expected ${this._dimensions}, got ${e.length}`);
389
+ const o = new Float32Array(e);
390
+ (t?.normalize ?? this.shouldNormalize) && this.normalizeVector(o), this.memoryManager.writeQuery(o), this.memoryManager.ensureCapacity(0);
391
+ const i = this.memoryManager.vectorCount, h = this.memoryManager.scoresOffset;
340
392
  if (this.wasmExports)
341
393
  this.wasmExports.search_all(
342
394
  this.memoryManager.queryOffset,
343
395
  this.memoryManager.dbOffset,
344
- c,
396
+ h,
345
397
  i,
346
- this.dimensions
398
+ this._dimensions
347
399
  );
348
400
  else {
349
- const y = new Float32Array(
401
+ const A = new Float32Array(
350
402
  this.memoryManager.memory.buffer,
351
403
  this.memoryManager.queryOffset,
352
- this.dimensions
353
- ), m = new Float32Array(
404
+ this._dimensions
405
+ ), p = new Float32Array(
354
406
  this.memoryManager.memory.buffer,
355
407
  this.memoryManager.dbOffset,
356
- i * this.dimensions
357
- ), M = new Float32Array(this.memoryManager.memory.buffer, c, i);
358
- v(y, m, M, i, this.dimensions);
408
+ i * this._dimensions
409
+ ), B = new Float32Array(this.memoryManager.memory.buffer, h, i);
410
+ O(A, p, B, i, this._dimensions);
359
411
  }
360
- const h = new Float32Array(this.memoryManager.readScores()), g = this.slotToKey, f = (y) => g[y];
361
- return r ? O(h, f, s, A) : x(h, f, s, A);
412
+ const y = new Float32Array(this.memoryManager.readScores()), l = this.slotToKey, f = (A) => l[A];
413
+ return n ? V(y, f, r, s) : Q(y, f, r, s);
362
414
  }
363
415
  /**
364
416
  * Persist the current in-memory state to storage.
365
417
  */
366
418
  async flush() {
367
419
  this.assertOpen();
368
- const e = this.memoryManager.vectorCount, t = new Uint8Array(e * this.dimensions * 4);
420
+ const e = this.memoryManager.vectorCount, t = new Uint8Array(e * this._dimensions * 4);
369
421
  if (e > 0) {
370
- const A = new Uint8Array(
422
+ const s = new Uint8Array(
371
423
  this.memoryManager.memory.buffer,
372
424
  this.memoryManager.dbOffset,
373
- e * this.dimensions * 4
425
+ e * this._dimensions * 4
374
426
  );
375
- t.set(A);
427
+ t.set(s);
376
428
  }
377
- const s = u(this.slotToKey);
378
- await Promise.all([this.storage.write(B, t), this.storage.write(Q, s)]);
429
+ const r = d(this.slotToKey);
430
+ await Promise.all([this.storage.write(b, t), this.storage.write(C, r)]);
379
431
  }
380
432
  /**
381
433
  * Flush data to storage and release the instance.
@@ -401,23 +453,23 @@ class b {
401
453
  */
402
454
  async export() {
403
455
  this.assertOpen();
404
- const e = this.memoryManager.vectorCount, t = e * this.dimensions * 4, s = u(this.slotToKey), A = s.byteLength, r = new ArrayBuffer(C), n = new DataView(r);
405
- n.setUint32(0, E, !0), n.setUint32(4, d, !0), n.setUint32(8, this.dimensions, !0), n.setUint32(12, e, !0), n.setUint32(16, t, !0), n.setUint32(20, A, !0);
406
- const o = this.memoryManager;
407
- let i = "header", c = 0;
456
+ const e = this.memoryManager.vectorCount, t = e * this._dimensions * 4, r = d(this.slotToKey), s = r.byteLength, n = new ArrayBuffer(M), o = new DataView(n);
457
+ o.setUint32(0, E, !0), o.setUint32(4, u, !0), o.setUint32(8, this._dimensions, !0), o.setUint32(12, e, !0), o.setUint32(16, t, !0), o.setUint32(20, s, !0);
458
+ const a = this.memoryManager;
459
+ let i = "header", h = 0;
408
460
  return new ReadableStream({
409
- pull(h) {
461
+ pull(y) {
410
462
  switch (i) {
411
463
  case "header":
412
- h.enqueue(new Uint8Array(r)), i = t > 0 ? "vectors" : A > 0 ? "keys" : "done", i === "done" && h.close();
464
+ y.enqueue(new Uint8Array(n)), i = t > 0 ? "vectors" : s > 0 ? "keys" : "done", i === "done" && y.close();
413
465
  break;
414
466
  case "vectors": {
415
- const g = t - c, f = Math.min(g, H), y = new Uint8Array(f);
416
- y.set(new Uint8Array(o.memory.buffer, o.dbOffset + c, f)), h.enqueue(y), c += f, c >= t && (i = A > 0 ? "keys" : "done", i === "done" && h.close());
467
+ const l = t - h, f = Math.min(l, F), A = new Uint8Array(f);
468
+ A.set(new Uint8Array(a.memory.buffer, a.dbOffset + h, f)), y.enqueue(A), h += f, h >= t && (i = s > 0 ? "keys" : "done", i === "done" && y.close());
417
469
  break;
418
470
  }
419
471
  case "keys":
420
- h.enqueue(s), i = "done", h.close();
472
+ y.enqueue(r), i = "done", y.close();
421
473
  break;
422
474
  }
423
475
  }
@@ -432,29 +484,29 @@ class b {
432
484
  */
433
485
  async import(e) {
434
486
  this.assertOpen();
435
- const t = new V(e), s = await t.readExact(C), A = new DataView(s.buffer, s.byteOffset, s.byteLength);
436
- if (A.getUint32(0, !0) !== E)
487
+ const t = new z(e), r = await t.readExact(M), s = new DataView(r.buffer, r.byteOffset, r.byteLength);
488
+ if (s.getUint32(0, !0) !== E)
437
489
  throw new Error("Invalid import data: unrecognized format");
438
- const n = A.getUint32(4, !0);
439
- if (n !== d)
440
- throw new Error(`Unsupported import version: expected ${d}, got ${n}`);
441
- const o = A.getUint32(8, !0);
442
- if (o !== this.dimensions)
443
- throw new Error(`Import dimension mismatch: expected ${this.dimensions}, got ${o}`);
444
- const i = A.getUint32(12, !0), c = A.getUint32(16, !0), h = A.getUint32(20, !0);
490
+ const o = s.getUint32(4, !0);
491
+ if (o !== u)
492
+ throw new Error(`Unsupported import version: expected ${u}, got ${o}`);
493
+ const a = s.getUint32(8, !0);
494
+ if (a !== this._dimensions)
495
+ throw new Error(`Import dimension mismatch: expected ${this._dimensions}, got ${a}`);
496
+ const i = s.getUint32(12, !0), h = s.getUint32(16, !0), y = s.getUint32(20, !0);
445
497
  if (this.keyToSlot.clear(), this.slotToKey.length = 0, this.memoryManager.reset(), i > 0) {
446
498
  this.memoryManager.ensureCapacity(i);
447
- let g = 0;
448
- await t.readChunked(c, (f) => {
449
- new Uint8Array(this.memoryManager.memory.buffer, this.memoryManager.dbOffset + g, f.byteLength).set(
499
+ let l = 0;
500
+ await t.readChunked(h, (f) => {
501
+ new Uint8Array(this.memoryManager.memory.buffer, this.memoryManager.dbOffset + l, f.byteLength).set(
450
502
  f
451
- ), g += f.byteLength;
503
+ ), l += f.byteLength;
452
504
  }), this.memoryManager.setVectorCount(i);
453
505
  }
454
- if (h > 0) {
455
- const g = await t.readExact(h), f = w(g);
456
- for (let y = 0; y < f.length; y++)
457
- this.keyToSlot.set(f[y], y), this.slotToKey[y] = f[y];
506
+ if (y > 0) {
507
+ const l = await t.readExact(y), f = g(l);
508
+ for (let A = 0; A < f.length; A++)
509
+ this.keyToSlot.set(f[A], A), this.slotToKey[A] = f[A];
458
510
  }
459
511
  t.release();
460
512
  }
@@ -465,17 +517,17 @@ class b {
465
517
  if (this.wasmExports) {
466
518
  const t = this.memoryManager.queryOffset;
467
519
  new Float32Array(this.memoryManager.memory.buffer, t, e.length).set(e), this.wasmExports.normalize(t, e.length);
468
- const s = new Float32Array(this.memoryManager.memory.buffer, t, e.length);
469
- e.set(s);
520
+ const r = new Float32Array(this.memoryManager.memory.buffer, t, e.length);
521
+ e.set(r);
470
522
  } else
471
- S(e);
523
+ v(e);
472
524
  }
473
525
  assertOpen() {
474
526
  if (this.closed)
475
527
  throw new Error("VectorDB instance has been closed");
476
528
  }
477
529
  }
478
- class V {
530
+ class z {
479
531
  reader;
480
532
  buffer = new Uint8Array(0);
481
533
  constructor(e) {
@@ -485,36 +537,36 @@ class V {
485
537
  async readExact(e) {
486
538
  if (e === 0) return new Uint8Array(0);
487
539
  if (this.buffer.byteLength >= e) {
488
- const n = this.buffer.subarray(0, e);
489
- return this.buffer = this.buffer.subarray(e), n;
540
+ const o = this.buffer.subarray(0, e);
541
+ return this.buffer = this.buffer.subarray(e), o;
490
542
  }
491
543
  const t = [];
492
- let s = this.buffer.byteLength;
493
- for (s > 0 && (t.push(this.buffer), this.buffer = new Uint8Array(0)); s < e; ) {
494
- const { done: n, value: o } = await this.reader.read();
495
- if (n) throw new Error("Invalid import data: unexpected end of stream");
496
- t.push(o), s += o.byteLength;
544
+ let r = this.buffer.byteLength;
545
+ for (r > 0 && (t.push(this.buffer), this.buffer = new Uint8Array(0)); r < e; ) {
546
+ const { done: o, value: a } = await this.reader.read();
547
+ if (o) throw new Error("Invalid import data: unexpected end of stream");
548
+ t.push(a), r += a.byteLength;
497
549
  }
498
- const A = new Uint8Array(s);
499
- let r = 0;
500
- for (const n of t)
501
- A.set(n, r), r += n.byteLength;
502
- return s > e ? (this.buffer = A.subarray(e), A.subarray(0, e)) : A;
550
+ const s = new Uint8Array(r);
551
+ let n = 0;
552
+ for (const o of t)
553
+ s.set(o, n), n += o.byteLength;
554
+ return r > e ? (this.buffer = s.subarray(e), s.subarray(0, e)) : s;
503
555
  }
504
556
  /**
505
557
  * Read `totalBytes` from the stream in chunks, calling `onChunk` for each.
506
558
  * Avoids allocating a single large buffer for the full data.
507
559
  */
508
560
  async readChunked(e, t) {
509
- let s = e;
561
+ let r = e;
510
562
  if (this.buffer.byteLength > 0) {
511
- const A = Math.min(this.buffer.byteLength, s);
512
- t(this.buffer.subarray(0, A)), s -= A, this.buffer = this.buffer.subarray(A);
563
+ const s = Math.min(this.buffer.byteLength, r);
564
+ t(this.buffer.subarray(0, s)), r -= s, this.buffer = this.buffer.subarray(s);
513
565
  }
514
- for (; s > 0; ) {
515
- const { done: A, value: r } = await this.reader.read();
516
- if (A) throw new Error("Invalid import data: unexpected end of stream");
517
- r.byteLength <= s ? (t(r), s -= r.byteLength) : (t(r.subarray(0, s)), this.buffer = r.slice(s), s = 0);
566
+ for (; r > 0; ) {
567
+ const { done: s, value: n } = await this.reader.read();
568
+ if (s) throw new Error("Invalid import data: unexpected end of stream");
569
+ n.byteLength <= r ? (t(n), r -= n.byteLength) : (t(n.subarray(0, r)), this.buffer = n.slice(r), r = 0);
518
570
  }
519
571
  }
520
572
  /** Release the stream reader lock. */
@@ -523,9 +575,9 @@ class V {
523
575
  }
524
576
  }
525
577
  export {
526
- b as DB,
527
- F as InMemoryStorageProvider,
528
- D as OPFSStorageProvider,
578
+ I as DB,
579
+ S as InMemoryStorageProvider,
580
+ K as OPFSStorageProvider,
529
581
  k as VectorCapacityExceededError
530
582
  };
531
583
  //# sourceMappingURL=eigen-db.js.map