kci-vec 0.0.0-stage → 0.2.31
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.md +100 -2
- package/build.js +36 -0
- package/example.js +38 -0
- package/index.d.ts +83 -0
- package/index.js +274 -0
- package/install.js +21 -0
- package/kci_vec_node.c +378 -0
- package/package.json +46 -4
- package/prebuilds/darwin-arm64/kci_vec.node +0 -0
- package/prebuilds/linux-x64/kci_vec.node +0 -0
- package/prebuilds/win32-x64/kci_vec.node +0 -0
- package/src/kci_crypt.h +302 -0
- package/src/kci_sparse.c +1995 -0
- package/src/kci_sparse.h +208 -0
- package/src/kci_vec.c +5954 -0
- package/src/kci_vec.h +573 -0
- package/src/kci_win_compat.h +381 -0
- package/store.js +225 -0
- package/where.js +93 -0
package/where.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Conditions on the fields you keep beside your vectors: the same rules as kci_vec.where of the Python
|
|
3
|
+
// client. matcher(cond)(record) -> true / false; ids(fieldsById, cond) -> the ids that pass, for search({ filter }).
|
|
4
|
+
// { genre: 'scifi' } the field has that value
|
|
5
|
+
// { year: { $gte: 2000, $lt: 2010 } } comparisons: $eq $ne $gt $gte $lt $lte $in $nin $contains $exists
|
|
6
|
+
// { $or: [ {...}, {...} ] }, $and, $not combine conditions
|
|
7
|
+
// A field that holds an array matches when one of its items does. A record without the field matches only $ne, $nin
|
|
8
|
+
// and { $exists: false }. Values of another kind never match (1 is not '1', true is not 1).
|
|
9
|
+
const OPS = ['$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in', '$nin', '$contains', '$exists'];
|
|
10
|
+
const kind = (x) => (typeof x === 'boolean' ? 'b' : typeof x === 'number' && !Number.isNaN(x) ? 'n' : typeof x === 'bigint' ? 'n' : typeof x === 'string' ? 's' : null);
|
|
11
|
+
const items = (x) => (Array.isArray(x) ? x : [x]);
|
|
12
|
+
const same = (x, v) => items(x).some((y) => kind(y) === kind(v) && y == v); // == : 5 and 5n are the same number
|
|
13
|
+
const has = (m, f) => m != null && Object.prototype.hasOwnProperty.call(m, f) && m[f] != null;
|
|
14
|
+
const show = (x) => { try { return JSON.stringify(x, (k, v) => (typeof v === 'bigint' ? String(v) : v)); } catch (e) { return String(x); } };
|
|
15
|
+
const plain = (x) => x !== null && typeof x === 'object' && !Array.isArray(x);
|
|
16
|
+
|
|
17
|
+
function one(field, op, v) {
|
|
18
|
+
if (!OPS.includes(op)) throw new RangeError(`where: ${show(op)} is not known (field ${show(field)}); the comparisons are ${OPS.join(', ')}`);
|
|
19
|
+
if (op === '$eq' || op === '$ne') {
|
|
20
|
+
if (kind(v) === null) throw new TypeError(`where: ${show(field)} ${op} needs a string, a number or true / false, not ${show(v)}`);
|
|
21
|
+
return op === '$eq' ? (m) => has(m, field) && same(m[field], v) : (m) => !(has(m, field) && same(m[field], v));
|
|
22
|
+
}
|
|
23
|
+
if (op === '$gt' || op === '$gte' || op === '$lt' || op === '$lte') {
|
|
24
|
+
if (kind(v) !== 'n' && kind(v) !== 's') throw new TypeError(`where: ${show(field)} ${op} needs a number or a string, not ${show(v)}`);
|
|
25
|
+
const cmp = { $gt: (a, b) => a > b, $gte: (a, b) => a >= b, $lt: (a, b) => a < b, $lte: (a, b) => a <= b }[op];
|
|
26
|
+
return (m) => has(m, field) && items(m[field]).some((y) => kind(y) === kind(v) && cmp(y, v));
|
|
27
|
+
}
|
|
28
|
+
if (op === '$in' || op === '$nin') {
|
|
29
|
+
const vs = v instanceof Set ? Array.from(v) : v;
|
|
30
|
+
if (!Array.isArray(vs) || vs.some((y) => kind(y) === null)) throw new TypeError(`where: ${show(field)} ${op} needs an array of strings, numbers or true / false, not ${show(v)}`);
|
|
31
|
+
const hit = (m) => has(m, field) && vs.some((y) => same(m[field], y));
|
|
32
|
+
return op === '$in' ? hit : (m) => !hit(m);
|
|
33
|
+
}
|
|
34
|
+
if (op === '$contains') {
|
|
35
|
+
if (kind(v) === null) throw new TypeError(`where: ${show(field)} $contains needs a string, a number or true / false, not ${show(v)}`);
|
|
36
|
+
return (m) => has(m, field) && (typeof m[field] === 'string' ? typeof v === 'string' && m[field].includes(v) : Array.isArray(m[field]) && same(m[field], v));
|
|
37
|
+
}
|
|
38
|
+
if (typeof v !== 'boolean') throw new TypeError(`where: ${show(field)} $exists needs true or false, not ${show(v)}`);
|
|
39
|
+
return (m) => has(m, field) === v;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function compile(cond, names) {
|
|
43
|
+
if (!plain(cond) || Object.keys(cond).length === 0) throw new TypeError(`where: a condition must be an object such as { genre: 'scifi' }, not ${show(cond)}`);
|
|
44
|
+
const parts = [];
|
|
45
|
+
for (const [key, v] of Object.entries(cond)) {
|
|
46
|
+
if (key === '$and' || key === '$or') {
|
|
47
|
+
if (!Array.isArray(v) || v.length === 0) throw new TypeError(`where: ${key} needs an array of conditions, not ${show(v)}`);
|
|
48
|
+
const subs = v.map((c) => compile(c, names));
|
|
49
|
+
parts.push(key === '$and' ? (m) => subs.every((s) => s(m)) : (m) => subs.some((s) => s(m)));
|
|
50
|
+
} else if (key === '$not') {
|
|
51
|
+
const sub = compile(v, names); parts.push((m) => !sub(m));
|
|
52
|
+
} else if (key.startsWith('$')) {
|
|
53
|
+
throw new RangeError(OPS.includes(key) ? `where: ${show(key)} cannot stand here; a comparison goes inside a field: { year: { ${key}: ... } }`
|
|
54
|
+
: `where: ${show(key)} is not known ($and, $or, $not combine conditions)`);
|
|
55
|
+
} else if (plain(v)) {
|
|
56
|
+
if (Object.keys(v).length === 0) throw new TypeError(`where: field ${show(key)} has an empty comparison`);
|
|
57
|
+
for (const [op, x] of Object.entries(v)) { parts.push(one(key, op, x)); if (op !== '$exists') names.add(key); }
|
|
58
|
+
} else {
|
|
59
|
+
if (v == null) throw new TypeError(`where: to ask for records without ${show(key)} write { ${key}: { $exists: false } }`);
|
|
60
|
+
parts.push(one(key, '$eq', v)); names.add(key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return parts.length === 1 ? parts[0] : (m) => parts.every((p) => p(m));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The condition as a function: matcher(cond)(record) -> true / false. */
|
|
67
|
+
function matcher(cond) { return compile(cond, new Set()); }
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The ids whose fields pass the condition, for search(query, k, { filter }). fieldsById: a Map (id -> fields) or a
|
|
71
|
+
* plain object (its keys are then taken as numbers). A field name that no record has throws: a mistyped name would
|
|
72
|
+
* otherwise answer nothing, silently.
|
|
73
|
+
*/
|
|
74
|
+
function ids(fieldsById, cond) {
|
|
75
|
+
const names = new Set(), ok = compile(cond, names);
|
|
76
|
+
const isMap = fieldsById instanceof Map;
|
|
77
|
+
if (!isMap && !plain(fieldsById)) throw new TypeError('where: the fields must be a Map (id -> fields) or an object');
|
|
78
|
+
const entries = isMap ? Array.from(fieldsById.entries()) : Object.entries(fieldsById).map(([k, v]) => {
|
|
79
|
+
const n = Number(k);
|
|
80
|
+
if (!Number.isSafeInteger(n) || n < 0 || String(n) !== k) throw new TypeError(`where: the key ${show(k)} is not an id (a whole number); use a Map for BigInt ids`);
|
|
81
|
+
return [n, v];
|
|
82
|
+
});
|
|
83
|
+
const known = new Set();
|
|
84
|
+
for (const [, rec] of entries) if (rec != null) for (const f of Object.keys(rec)) known.add(f);
|
|
85
|
+
const bad = Array.from(names).filter((n) => !known.has(n)).sort();
|
|
86
|
+
if (bad.length) {
|
|
87
|
+
const some = Array.from(known).sort();
|
|
88
|
+
throw new RangeError(`where: no record has the field ${show(bad[0])}` + (some.length ? `; the fields are ${some.slice(0, 20).map(show).join(', ')}${some.length > 20 ? ' ...' : ''}` : '; no record has any field'));
|
|
89
|
+
}
|
|
90
|
+
return entries.filter(([, rec]) => ok(rec)).map(([id]) => id);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { matcher, ids };
|