vector-index 0.0.1 → 0.1.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 +201 -21
- package/README.md +1447 -12
- package/build-browser.js +67 -0
- package/index.js +38 -6
- package/package.json +70 -6
- package/src/async.js +458 -0
- package/src/clustering.js +226 -0
- package/src/embeddings/als.js +211 -0
- package/src/embeddings/bpr.js +149 -0
- package/src/embeddings/embeddings.js +222 -0
- package/src/embeddings/evaluate.js +193 -0
- package/src/embeddings/graph.js +376 -0
- package/src/embeddings/idmap.js +120 -0
- package/src/embeddings/interactions.js +123 -0
- package/src/embeddings/memory.js +52 -0
- package/src/embeddings/node2vec.js +142 -0
- package/src/embeddings/sgns.js +191 -0
- package/src/embeddings/skipgram.js +128 -0
- package/src/embeddings/trainer.js +147 -0
- package/src/embeddings/walker.js +208 -0
- package/src/errors.js +41 -0
- package/src/heap.js +82 -0
- package/src/index/block-codec.js +191 -0
- package/src/index/diskann-block-engine.js +891 -0
- package/src/index/diskann.js +331 -0
- package/src/index/engine-common.js +159 -0
- package/src/index/flat.js +49 -0
- package/src/index/hnsw.js +282 -0
- package/src/index/ivf.js +232 -0
- package/src/index/prune.js +70 -0
- package/src/index/vector-index.js +391 -0
- package/src/lru.js +32 -0
- package/src/math.js +666 -0
- package/src/metric.js +62 -0
- package/src/quant/bq.js +172 -0
- package/src/quant/pq.js +418 -0
- package/src/quant/scan.js +52 -0
- package/src/quant/sq.js +274 -0
- package/src/quant/turboquant.js +527 -0
- package/src/reduce/pca.js +253 -0
- package/src/results.js +86 -0
- package/src/search.js +52 -0
- package/src/sparse/sparse-index.js +349 -0
- package/src/store.js +476 -0
- package/src/typed.js +23 -0
- package/src/validate.js +130 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* vector-index — Apache-2.0 — https://github.com/colocohen/vector-index
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// ===========================
|
|
6
|
+
// PCA — principal component analysis: fewer dimensions, most of the signal
|
|
7
|
+
//
|
|
8
|
+
// embeddings often carry their information in far fewer directions than
|
|
9
|
+
// they have dimensions. PCA finds those directions (the eigenvectors of the
|
|
10
|
+
// covariance with the largest eigenvalues) and projects onto them: 1536
|
|
11
|
+
// dimensions → 256 is 6× less memory and faster search, usually for little
|
|
12
|
+
// recall. standalone — not tied to any index.
|
|
13
|
+
//
|
|
14
|
+
// API (same shape as every quantizer in this package):
|
|
15
|
+
// every operation exists as name(..., cb) and nameSync(...)
|
|
16
|
+
// train(vectors) learns the mean and the top `components` directions
|
|
17
|
+
// transform(vector) → Float32Array(components): the coordinates along them
|
|
18
|
+
// inverse(reduced) → Float32Array(dimensions): back to the original space
|
|
19
|
+
// events: 'progress' during train
|
|
20
|
+
// getModel() → { mean, components, variance, explainedVarianceRatio }
|
|
21
|
+
//
|
|
22
|
+
// options: { dimensions, components, iterations: 4, whiten: false, seed, model }
|
|
23
|
+
// model: what getModel() returned — the PCA starts trained with it (keep
|
|
24
|
+
// the model next to an index built from reduced vectors, and load it here).
|
|
25
|
+
// whiten: true — each output coordinate is divided by its standard
|
|
26
|
+
// deviation (unit variance per component).
|
|
27
|
+
//
|
|
28
|
+
// how: randomized subspace iteration (Halko, Martinsson & Tropp 2011). the
|
|
29
|
+
// d × d covariance is never formed (1536² cells, and n·d² work to fill it):
|
|
30
|
+
// a block of l = components + 10 random directions V is pushed through
|
|
31
|
+
// V ← orth( Xᵀ (X V) ) a few times, each pass costing 2·n·d·l, which turns V
|
|
32
|
+
// towards the top eigenvectors. the small l × l problem left at the end
|
|
33
|
+
// (Rayleigh–Ritz) is solved exactly (symmetricEigen, math.js).
|
|
34
|
+
// ===========================
|
|
35
|
+
|
|
36
|
+
import { createRng, gaussianSample, orthonormalizeColumnsSteps, symmetricEigenSteps } from '../math.js';
|
|
37
|
+
import { ERR, createError } from '../errors.js';
|
|
38
|
+
import { pairedApi, createEmitter } from '../async.js';
|
|
39
|
+
import { checkVector, checkVectorsJob, checkPositiveInt } from '../validate.js';
|
|
40
|
+
|
|
41
|
+
const OVERSAMPLE = 10;
|
|
42
|
+
|
|
43
|
+
function createPCA(opts) {
|
|
44
|
+
opts = opts || {};
|
|
45
|
+
const dimensions = checkPositiveInt(opts.dimensions, 'dimensions');
|
|
46
|
+
const k = checkPositiveInt(opts.components, 'components');
|
|
47
|
+
if (k > dimensions) throw createError(ERR.INVALID_ARGUMENT, 'components cannot exceed dimensions');
|
|
48
|
+
const iterations = opts.iterations == null ? 4 : opts.iterations;
|
|
49
|
+
if (!Number.isInteger(iterations) || iterations < 0) {
|
|
50
|
+
throw createError(ERR.INVALID_ARGUMENT, 'iterations must be a non-negative integer');
|
|
51
|
+
}
|
|
52
|
+
const whiten = !!opts.whiten;
|
|
53
|
+
const seed = opts.seed;
|
|
54
|
+
const d = dimensions;
|
|
55
|
+
const emitter = createEmitter();
|
|
56
|
+
|
|
57
|
+
// the model — committed together at the end of train
|
|
58
|
+
let mean = null; // Float32Array(d)
|
|
59
|
+
let comps = null; // Float32Array(k × d), row j = component j (unit length)
|
|
60
|
+
let variance = null; // Float32Array(k), eigenvalues: variance along each component
|
|
61
|
+
let ratio = null; // Float32Array(k), share of the total variance
|
|
62
|
+
let scale = null; // Float32Array(k): 1 / √variance when whitening, else 1
|
|
63
|
+
|
|
64
|
+
function adopt(m) {
|
|
65
|
+
const ok = m && m.mean instanceof Float32Array && m.mean.length === d &&
|
|
66
|
+
m.components instanceof Float32Array && m.components.length === k * d &&
|
|
67
|
+
m.variance instanceof Float32Array && m.variance.length === k;
|
|
68
|
+
if (!ok) {
|
|
69
|
+
throw createError(ERR.INVALID_ARGUMENT,
|
|
70
|
+
'model must be { mean: Float32Array(' + d + '), components: Float32Array(' + (k * d) + '), variance: Float32Array(' + k + ') } as getModel() returns');
|
|
71
|
+
}
|
|
72
|
+
mean = Float32Array.from(m.mean);
|
|
73
|
+
comps = Float32Array.from(m.components);
|
|
74
|
+
variance = Float32Array.from(m.variance);
|
|
75
|
+
ratio = m.explainedVarianceRatio instanceof Float32Array && m.explainedVarianceRatio.length === k
|
|
76
|
+
? Float32Array.from(m.explainedVarianceRatio) : new Float32Array(k);
|
|
77
|
+
scale = new Float32Array(k);
|
|
78
|
+
for (let c = 0; c < k; c++) scale[c] = whiten ? (variance[c] > 0 ? 1 / Math.sqrt(variance[c]) : 0) : 1;
|
|
79
|
+
}
|
|
80
|
+
if (opts.model != null) adopt(opts.model);
|
|
81
|
+
|
|
82
|
+
function requireTrained() {
|
|
83
|
+
if (!mean) throw createError(ERR.NOT_TRAINED, 'PCA is not trained yet — call train() first');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ===========================
|
|
87
|
+
// train — a generator (time-sliced by train(cb), run in one go by trainSync)
|
|
88
|
+
// ===========================
|
|
89
|
+
function* trainJob(vectors) {
|
|
90
|
+
yield* checkVectorsJob(vectors, d);
|
|
91
|
+
const n = vectors.length;
|
|
92
|
+
if (n < 2) throw createError(ERR.INVALID_ARGUMENT, 'need at least 2 vectors to train');
|
|
93
|
+
const l = Math.min(d, k + OVERSAMPLE);
|
|
94
|
+
const rng = createRng(seed);
|
|
95
|
+
emitter.emit('progress', { phase: 'start', percent: 0 });
|
|
96
|
+
|
|
97
|
+
// mean, and the centered sample as one block (Float32, n × d)
|
|
98
|
+
const mu = new Float64Array(d);
|
|
99
|
+
for (let i = 0; i < n; i++) {
|
|
100
|
+
const v = vectors[i];
|
|
101
|
+
for (let j = 0; j < d; j++) mu[j] += v[j];
|
|
102
|
+
if ((i & 127) === 127) yield true;
|
|
103
|
+
}
|
|
104
|
+
for (let j = 0; j < d; j++) mu[j] /= n;
|
|
105
|
+
const X = new Float32Array(n * d);
|
|
106
|
+
let total = 0; // Σ ‖x − μ‖²
|
|
107
|
+
for (let i = 0; i < n; i++) {
|
|
108
|
+
const v = vectors[i], o = i * d;
|
|
109
|
+
for (let j = 0; j < d; j++) {
|
|
110
|
+
const c = v[j] - mu[j];
|
|
111
|
+
X[o + j] = c;
|
|
112
|
+
total += c * c;
|
|
113
|
+
}
|
|
114
|
+
if ((i & 127) === 127) yield true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Z = X V (n × l) and W = Xᵀ Z (d × l), row by row: work ≈ n·d·l each
|
|
118
|
+
const V = new Float64Array(d * l);
|
|
119
|
+
for (let i = 0; i < V.length; i++) V[i] = gaussianSample(rng);
|
|
120
|
+
yield* orthonormalizeColumnsSteps(V, d, l);
|
|
121
|
+
const Z = new Float64Array(n * l);
|
|
122
|
+
const zRow = new Float64Array(l);
|
|
123
|
+
const budget = 1 << 18; // multiply-adds between yields
|
|
124
|
+
function* project() { // Z ← X V
|
|
125
|
+
let work = 0;
|
|
126
|
+
for (let i = 0; i < n; i++) {
|
|
127
|
+
const xo = i * d, zo = i * l;
|
|
128
|
+
zRow.fill(0);
|
|
129
|
+
for (let j = 0; j < d; j++) {
|
|
130
|
+
const x = X[xo + j];
|
|
131
|
+
if (x === 0) continue;
|
|
132
|
+
const vo = j * l;
|
|
133
|
+
for (let c = 0; c < l; c++) zRow[c] += x * V[vo + c];
|
|
134
|
+
}
|
|
135
|
+
Z.set(zRow, zo);
|
|
136
|
+
work += d * l;
|
|
137
|
+
if (work > budget) { work = 0; yield true; }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
for (let it = 0; it < iterations; it++) {
|
|
141
|
+
yield* project();
|
|
142
|
+
V.fill(0); // V ← Xᵀ Z
|
|
143
|
+
let work = 0;
|
|
144
|
+
for (let i = 0; i < n; i++) {
|
|
145
|
+
const xo = i * d, zo = i * l;
|
|
146
|
+
for (let j = 0; j < d; j++) {
|
|
147
|
+
const x = X[xo + j];
|
|
148
|
+
if (x === 0) continue;
|
|
149
|
+
const vo = j * l;
|
|
150
|
+
for (let c = 0; c < l; c++) V[vo + c] += x * Z[zo + c];
|
|
151
|
+
}
|
|
152
|
+
work += d * l;
|
|
153
|
+
if (work > budget) { work = 0; yield true; }
|
|
154
|
+
}
|
|
155
|
+
yield* orthonormalizeColumnsSteps(V, d, l);
|
|
156
|
+
emitter.emit('progress', {
|
|
157
|
+
phase: 'iteration', iteration: it + 1, totalIterations: iterations,
|
|
158
|
+
percent: Math.round((it + 1) / (iterations + 1) * 100)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Rayleigh–Ritz: H = Zᵀ Z / (n − 1) with Z = X V, eigen-decomposed exactly
|
|
163
|
+
yield* project();
|
|
164
|
+
const H = new Float64Array(l * l);
|
|
165
|
+
let hWork = 0;
|
|
166
|
+
for (let i = 0; i < n; i++) {
|
|
167
|
+
const zo = i * l;
|
|
168
|
+
for (let a = 0; a < l; a++) {
|
|
169
|
+
const za = Z[zo + a];
|
|
170
|
+
for (let b = a; b < l; b++) H[a * l + b] += za * Z[zo + b];
|
|
171
|
+
}
|
|
172
|
+
hWork += l * l / 2;
|
|
173
|
+
if (hWork > budget) { hWork = 0; yield true; }
|
|
174
|
+
}
|
|
175
|
+
for (let a = 0; a < l; a++) {
|
|
176
|
+
for (let b = a; b < l; b++) {
|
|
177
|
+
H[a * l + b] /= (n - 1);
|
|
178
|
+
H[b * l + a] = H[a * l + b];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const { values, vectors: E } = yield* symmetricEigenSteps(H, l);
|
|
182
|
+
|
|
183
|
+
// components = (V E)ᵀ, top k
|
|
184
|
+
const newComps = new Float32Array(k * d);
|
|
185
|
+
for (let c = 0; c < k; c++) {
|
|
186
|
+
for (let j = 0; j < d; j++) {
|
|
187
|
+
let s = 0;
|
|
188
|
+
for (let m = 0; m < l; m++) s += V[j * l + m] * E[m * l + c];
|
|
189
|
+
newComps[c * d + j] = s;
|
|
190
|
+
}
|
|
191
|
+
yield true;
|
|
192
|
+
}
|
|
193
|
+
const totalVar = total / (n - 1);
|
|
194
|
+
const newVar = new Float32Array(k), newRatio = new Float32Array(k), newScale = new Float32Array(k);
|
|
195
|
+
for (let c = 0; c < k; c++) {
|
|
196
|
+
newVar[c] = Math.max(0, values[c]);
|
|
197
|
+
newRatio[c] = totalVar > 0 ? newVar[c] / totalVar : 0;
|
|
198
|
+
newScale[c] = whiten ? (newVar[c] > 0 ? 1 / Math.sqrt(newVar[c]) : 0) : 1;
|
|
199
|
+
}
|
|
200
|
+
mean = Float32Array.from(mu);
|
|
201
|
+
comps = newComps;
|
|
202
|
+
variance = newVar;
|
|
203
|
+
ratio = newRatio;
|
|
204
|
+
scale = newScale;
|
|
205
|
+
emitter.emit('progress', { phase: 'done', percent: 100 });
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function transformSync(vector) {
|
|
209
|
+
requireTrained();
|
|
210
|
+
checkVector(vector, d);
|
|
211
|
+
const out = new Float32Array(k);
|
|
212
|
+
for (let c = 0; c < k; c++) {
|
|
213
|
+
let s = 0;
|
|
214
|
+
const o = c * d;
|
|
215
|
+
for (let j = 0; j < d; j++) s += (vector[j] - mean[j]) * comps[o + j];
|
|
216
|
+
out[c] = s * scale[c];
|
|
217
|
+
}
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function inverseSync(reduced) {
|
|
222
|
+
requireTrained();
|
|
223
|
+
checkVector(reduced, k, 'reduced');
|
|
224
|
+
const out = Float32Array.from(mean);
|
|
225
|
+
for (let c = 0; c < k; c++) {
|
|
226
|
+
const y = scale[c] === 0 ? 0 : reduced[c] / scale[c];
|
|
227
|
+
const o = c * d;
|
|
228
|
+
for (let j = 0; j < d; j++) out[j] += y * comps[o + j];
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return Object.assign(
|
|
234
|
+
pairedApi({
|
|
235
|
+
train: trainJob,
|
|
236
|
+
transform: transformSync,
|
|
237
|
+
inverse: inverseSync
|
|
238
|
+
}),
|
|
239
|
+
{
|
|
240
|
+
on: emitter.on,
|
|
241
|
+
off: emitter.off,
|
|
242
|
+
// config
|
|
243
|
+
dimensions,
|
|
244
|
+
components: k,
|
|
245
|
+
whiten,
|
|
246
|
+
requiresTraining: true,
|
|
247
|
+
isTrained: () => !!mean,
|
|
248
|
+
getModel: () => mean && { mean, components: comps, variance, explainedVarianceRatio: ratio }
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export { createPCA };
|
package/src/results.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* vector-index — Apache-2.0 — https://github.com/colocohen/vector-index
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// ===========================
|
|
6
|
+
// results — combining result lists. pure, synchronous, no index needed.
|
|
7
|
+
//
|
|
8
|
+
// fuseResults(lists, opts) lists from DIFFERENT searches (vector + keyword,
|
|
9
|
+
// two embedding models…): Reciprocal Rank Fusion.
|
|
10
|
+
// scores on different scales can't be added, so
|
|
11
|
+
// only the rank counts:
|
|
12
|
+
// score(id) = Σ weight_i / (k + rank_i(id))
|
|
13
|
+
// (Cormack, Clarke & Buettcher, SIGIR 2009; k = 60)
|
|
14
|
+
//
|
|
15
|
+
// mergeResults(lists, k) lists from the SAME search on different shards:
|
|
16
|
+
// their scores are comparable, so the global top k
|
|
17
|
+
// is a plain merge. an id found on several shards
|
|
18
|
+
// keeps its best entry.
|
|
19
|
+
//
|
|
20
|
+
// a result is any object with an `id`; mergeResults also needs `score`
|
|
21
|
+
// (higher = closer) or `distance` (lower = closer), the two shapes
|
|
22
|
+
// index.search returns.
|
|
23
|
+
// ===========================
|
|
24
|
+
|
|
25
|
+
import { ERR, createError } from './errors.js';
|
|
26
|
+
|
|
27
|
+
function checkLists(lists, name) {
|
|
28
|
+
if (!Array.isArray(lists) || !lists.every(Array.isArray)) {
|
|
29
|
+
throw createError(ERR.INVALID_ARGUMENT, name + ': expected an array of result arrays');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// fuseResults([listA, listB, ...], { k = 60, weights, limit }) → [{ id, score }]
|
|
34
|
+
// weights: one number per list (default 1 each)
|
|
35
|
+
// limit: how many to return (default: all ids seen)
|
|
36
|
+
function fuseResults(lists, opts) {
|
|
37
|
+
checkLists(lists, 'fuseResults');
|
|
38
|
+
opts = opts || {};
|
|
39
|
+
const k = opts.k == null ? 60 : opts.k;
|
|
40
|
+
if (!(k >= 0) || !Number.isFinite(k)) throw createError(ERR.INVALID_ARGUMENT, 'fuseResults: k must be a number ≥ 0');
|
|
41
|
+
const weights = opts.weights;
|
|
42
|
+
if (weights != null && (!Array.isArray(weights) || weights.length !== lists.length)) {
|
|
43
|
+
throw createError(ERR.INVALID_ARGUMENT, 'fuseResults: weights must have one number per list');
|
|
44
|
+
}
|
|
45
|
+
const score = new Map();
|
|
46
|
+
lists.forEach((list, li) => {
|
|
47
|
+
const w = weights ? weights[li] : 1;
|
|
48
|
+
const seen = new Set(); // an id counts once per list, at its best rank
|
|
49
|
+
for (let r = 0; r < list.length; r++) {
|
|
50
|
+
const id = list[r].id;
|
|
51
|
+
if (seen.has(id)) continue;
|
|
52
|
+
seen.add(id);
|
|
53
|
+
score.set(id, (score.get(id) || 0) + w / (k + r + 1));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const [id, s] of score) out.push({ id, score: s });
|
|
58
|
+
out.sort((a, b) => b.score - a.score);
|
|
59
|
+
return opts.limit == null ? out : out.slice(0, opts.limit);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// mergeResults([shard1, shard2, ...], k) → the global top k, same shape as
|
|
63
|
+
// the inputs ({ id, score } or { id, distance })
|
|
64
|
+
function mergeResults(lists, k) {
|
|
65
|
+
checkLists(lists, 'mergeResults');
|
|
66
|
+
if (!Number.isInteger(k) || k <= 0) throw createError(ERR.INVALID_ARGUMENT, 'mergeResults: k must be a positive integer');
|
|
67
|
+
const first = lists.find(l => l.length > 0);
|
|
68
|
+
if (!first) return [];
|
|
69
|
+
const byDistance = typeof first[0].distance === 'number';
|
|
70
|
+
const key = byDistance ? 'distance' : 'score';
|
|
71
|
+
const best = new Map();
|
|
72
|
+
for (const list of lists) {
|
|
73
|
+
for (const r of list) {
|
|
74
|
+
if (typeof r[key] !== 'number') {
|
|
75
|
+
throw createError(ERR.INVALID_ARGUMENT, 'mergeResults: every result needs a numeric ' + key + ' (lists of one shape only)');
|
|
76
|
+
}
|
|
77
|
+
const cur = best.get(r.id);
|
|
78
|
+
if (!cur || (byDistance ? r.distance < cur.distance : r.score > cur.score)) best.set(r.id, r);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const out = [...best.values()];
|
|
82
|
+
out.sort(byDistance ? (a, b) => a.distance - b.distance : (a, b) => b.score - a.score);
|
|
83
|
+
return out.slice(0, k);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { fuseResults, mergeResults };
|
package/src/search.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* vector-index — Apache-2.0 — https://github.com/colocohen/vector-index
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// ===========================
|
|
6
|
+
// search-call plumbing shared by every index (vector and sparse):
|
|
7
|
+
// checkSearchOptions(options) → the caller's filter, wrapped, or null
|
|
8
|
+
// batchJob(queries, checkQuery, searchOne) → a generator for searchBatch:
|
|
9
|
+
// validates every query, then one search per query with a yield between
|
|
10
|
+
// ===========================
|
|
11
|
+
|
|
12
|
+
import { ERR, createError } from './errors.js';
|
|
13
|
+
|
|
14
|
+
// search options → the caller's filter wrapped (or null): called with the
|
|
15
|
+
// id, truthy = may be returned. synchronous, like a comparator to sort. an
|
|
16
|
+
// exception from it is reported as INVALID_ARGUMENT (original on err.cause)
|
|
17
|
+
function checkSearchOptions(options) {
|
|
18
|
+
if (options == null) return null;
|
|
19
|
+
if (typeof options !== 'object') {
|
|
20
|
+
throw createError(ERR.INVALID_ARGUMENT, 'search options must be an object, got ' + typeof options);
|
|
21
|
+
}
|
|
22
|
+
const f = options.filter;
|
|
23
|
+
if (f == null) return null;
|
|
24
|
+
if (typeof f !== 'function') {
|
|
25
|
+
throw createError(ERR.INVALID_ARGUMENT, 'options.filter must be a function, got ' + typeof f);
|
|
26
|
+
}
|
|
27
|
+
return function accept(id) {
|
|
28
|
+
try {
|
|
29
|
+
return !!f(id);
|
|
30
|
+
} catch (err) {
|
|
31
|
+
throw createError(ERR.INVALID_ARGUMENT, 'options.filter threw: ' + (err && err.message || err), err);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function* batchJob(queries, checkQuery, searchOne) {
|
|
37
|
+
if (!Array.isArray(queries)) {
|
|
38
|
+
throw createError(ERR.INVALID_ARGUMENT, 'searchBatch: queries must be an array');
|
|
39
|
+
}
|
|
40
|
+
for (let i = 0; i < queries.length; i++) {
|
|
41
|
+
checkQuery(queries[i], 'queries[' + i + ']');
|
|
42
|
+
if ((i & 255) === 255) yield;
|
|
43
|
+
}
|
|
44
|
+
const out = new Array(queries.length);
|
|
45
|
+
for (let i = 0; i < queries.length; i++) {
|
|
46
|
+
out[i] = searchOne(queries[i]);
|
|
47
|
+
yield true;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { checkSearchOptions, batchJob };
|