datalevin-node 1.0.2 → 1.2.10
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/README.md +470 -31
- package/jars/datalevin-runtime-1.2.10.jar +4 -0
- package/package.json +1 -1
- package/src/client.js +5 -3
- package/src/connection.js +87 -40
- package/src/convert.js +111 -7
- package/src/database.js +13 -0
- package/src/entity.js +4 -0
- package/src/form.js +408 -0
- package/src/index.js +38 -1
- package/src/interop.js +119 -7
- package/src/kv.js +27 -1
- package/src/native.js +172 -0
- package/src/prepared.js +88 -0
- package/src/query.js +1352 -0
- package/src/resource.js +2 -0
- package/src/transaction.js +312 -0
- package/src/udf-value.js +267 -0
- package/src/udf.js +128 -76
- package/jars/datalevin-runtime-1.0.2.jar +0 -4
package/src/resource.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import util from "node:util";
|
|
2
|
+
import { currentNativeRegistry } from "./native.js";
|
|
2
3
|
|
|
3
4
|
export class ResourceWrapper {
|
|
4
5
|
constructor(handle, closeFn, closedFn, label) {
|
|
6
|
+
this._nativeRegistry = currentNativeRegistry();
|
|
5
7
|
this._handle = handle;
|
|
6
8
|
this._closeFn = closeFn;
|
|
7
9
|
this._closedFn = closedFn;
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { Form, Keyword, RawForm, formData, immutableSnapshot } from "./form.js";
|
|
2
|
+
import { kw } from "./query.js";
|
|
3
|
+
import { UdfDescriptor, udfReference } from "./udf-value.js";
|
|
4
|
+
|
|
5
|
+
const MISSING = Symbol("missing");
|
|
6
|
+
const PATCH_UPDATE_OPERATIONS = new Set([
|
|
7
|
+
"conj",
|
|
8
|
+
"merge",
|
|
9
|
+
"assoc",
|
|
10
|
+
"dissoc",
|
|
11
|
+
"inc",
|
|
12
|
+
"dec"
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
function attribute(value) {
|
|
16
|
+
return typeof value === "string" ? kw(value) : value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function callable(value) {
|
|
20
|
+
return typeof value === "string" ? kw(value) : value;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function patchPath(path) {
|
|
24
|
+
const vectorPath = Array.isArray(path);
|
|
25
|
+
if (!vectorPath && typeof path !== "string" && !(path instanceof Keyword)) {
|
|
26
|
+
throw new TypeError(
|
|
27
|
+
"An idoc patch path must be a string, keyword, or array."
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
const segments = vectorPath ? path : [path];
|
|
31
|
+
if (segments.length === 0) {
|
|
32
|
+
throw new TypeError("An idoc patch path must not be empty.");
|
|
33
|
+
}
|
|
34
|
+
for (const segment of segments) {
|
|
35
|
+
const integer = (
|
|
36
|
+
(typeof segment === "number" && Number.isSafeInteger(segment))
|
|
37
|
+
|| typeof segment === "bigint"
|
|
38
|
+
);
|
|
39
|
+
if (integer) {
|
|
40
|
+
if (segment < 0) {
|
|
41
|
+
throw new TypeError("An idoc patch path index must be non-negative.");
|
|
42
|
+
}
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (segment instanceof Keyword) {
|
|
46
|
+
if (segment.name === "?" || segment.name === "*") {
|
|
47
|
+
throw new TypeError("An idoc patch path does not allow keyword wildcards.");
|
|
48
|
+
}
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (typeof segment !== "string") {
|
|
52
|
+
throw new TypeError(
|
|
53
|
+
"Idoc patch path segments must be strings, keywords, or integers."
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return vectorPath ? immutableSnapshot(path) : path;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function patchUpdateOperation(operation) {
|
|
61
|
+
const result = typeof operation === "string" ? kw(operation) : operation;
|
|
62
|
+
if (!(result instanceof Keyword)) {
|
|
63
|
+
throw new TypeError(
|
|
64
|
+
"An idoc patch update operation must be a keyword or keyword name."
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (!PATCH_UPDATE_OPERATIONS.has(result.name)) {
|
|
68
|
+
throw new TypeError(
|
|
69
|
+
`Unknown idoc patch update operation ${result}; expected one of ${
|
|
70
|
+
[...PATCH_UPDATE_OPERATIONS].sort().join(", ")
|
|
71
|
+
}.`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export class TxItem extends Form {
|
|
78
|
+
constructor(form) {
|
|
79
|
+
super();
|
|
80
|
+
this.form = immutableSnapshot(form);
|
|
81
|
+
Object.freeze(this);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
toForm() {
|
|
85
|
+
return this.form;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class LookupRef extends Form {
|
|
90
|
+
constructor(attributeName, value) {
|
|
91
|
+
super();
|
|
92
|
+
this.attribute = immutableSnapshot(attribute(attributeName));
|
|
93
|
+
this.value = immutableSnapshot(value);
|
|
94
|
+
Object.freeze(this);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
toForm() {
|
|
98
|
+
return immutableSnapshot([this.attribute, this.value]);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
asData() {
|
|
102
|
+
return formData(this);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class PatchOp extends Form {
|
|
107
|
+
constructor(form) {
|
|
108
|
+
super();
|
|
109
|
+
this.form = immutableSnapshot([...form]);
|
|
110
|
+
Object.freeze(this);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
toForm() {
|
|
114
|
+
return this.form;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
asData() {
|
|
118
|
+
return formData(this);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export class TxData extends Form {
|
|
123
|
+
constructor(items = []) {
|
|
124
|
+
super();
|
|
125
|
+
this.items = immutableSnapshot([...items]);
|
|
126
|
+
Object.freeze(this);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get length() {
|
|
130
|
+
return this.items.length;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
at(index) {
|
|
134
|
+
return this.items.at(index);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
[Symbol.iterator]() {
|
|
138
|
+
return this.items[Symbol.iterator]();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
toForm() {
|
|
142
|
+
return immutableSnapshot(
|
|
143
|
+
this.items.map((item) => item instanceof Form ? item.toForm() : item)
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
asData() {
|
|
148
|
+
return formData(this);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function data(...items) {
|
|
153
|
+
if (items.length === 1 && Array.isArray(items[0])) {
|
|
154
|
+
items = items[0];
|
|
155
|
+
}
|
|
156
|
+
return new TxData(items);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function entity(dbId = null, attrs = MISSING) {
|
|
160
|
+
if (attrs === MISSING) {
|
|
161
|
+
if (dbId instanceof Map || (
|
|
162
|
+
dbId !== null
|
|
163
|
+
&& typeof dbId === "object"
|
|
164
|
+
&& (Object.getPrototypeOf(dbId) === Object.prototype
|
|
165
|
+
|| Object.getPrototypeOf(dbId) === null)
|
|
166
|
+
)) {
|
|
167
|
+
attrs = dbId;
|
|
168
|
+
dbId = null;
|
|
169
|
+
} else {
|
|
170
|
+
attrs = {};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (!(attrs instanceof Map) && (
|
|
174
|
+
attrs === null
|
|
175
|
+
|| typeof attrs !== "object"
|
|
176
|
+
|| (Object.getPrototypeOf(attrs) !== Object.prototype
|
|
177
|
+
&& Object.getPrototypeOf(attrs) !== null)
|
|
178
|
+
)) {
|
|
179
|
+
throw new TypeError("Transaction entity attributes must be an object or Map.");
|
|
180
|
+
}
|
|
181
|
+
const entries = attrs instanceof Map ? attrs.entries() : Object.entries(attrs);
|
|
182
|
+
const result = new Map();
|
|
183
|
+
for (const [key, value] of entries) {
|
|
184
|
+
result.set(attribute(key), value);
|
|
185
|
+
}
|
|
186
|
+
if (dbId !== null && dbId !== undefined) {
|
|
187
|
+
result.set(kw("db/id"), dbId);
|
|
188
|
+
}
|
|
189
|
+
return new TxItem(result);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function lookupRef(attributeName, value) {
|
|
193
|
+
return new LookupRef(attributeName, value);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function add(entityId, attributeName, value) {
|
|
197
|
+
return new TxItem([kw("db/add"), entityId, attribute(attributeName), value]);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function retract(entityId, attributeName, value) {
|
|
201
|
+
return new TxItem([kw("db/retract"), entityId, attribute(attributeName), value]);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function retractAttribute(entityId, attributeName) {
|
|
205
|
+
return new TxItem([kw("db.fn/retractAttribute"), entityId, attribute(attributeName)]);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function retractEntity(entityId) {
|
|
209
|
+
return new TxItem([kw("db/retractEntity"), entityId]);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function compareAndSwap(entityId, attributeName, oldValue, newValue) {
|
|
213
|
+
return new TxItem([
|
|
214
|
+
kw("db.fn/cas"),
|
|
215
|
+
entityId,
|
|
216
|
+
attribute(attributeName),
|
|
217
|
+
oldValue,
|
|
218
|
+
newValue
|
|
219
|
+
]);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export const cas = compareAndSwap;
|
|
223
|
+
|
|
224
|
+
export function call(functionValue, ...args) {
|
|
225
|
+
return new TxItem([kw("db.fn/call"), callable(functionValue), ...args]);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function callUdf(reference, ...args) {
|
|
229
|
+
return call(udfReference(reference), ...args);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function invoke(functionValue, ...args) {
|
|
233
|
+
return new TxItem([callable(functionValue), ...args]);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function ensure(predicateValue, ...args) {
|
|
237
|
+
return new TxItem([kw("db/ensure"), udfReference(predicateValue), ...args]);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function installUdf(descriptor) {
|
|
241
|
+
const normalized = UdfDescriptor.from(descriptor, { defaultLang: "java" });
|
|
242
|
+
return entity({
|
|
243
|
+
"db/ident": kw(normalized.udfId),
|
|
244
|
+
"db/udf": normalized
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function uninstallUdf(ident) {
|
|
249
|
+
const normalized = typeof ident === "string" ? kw(ident) : ident;
|
|
250
|
+
return retractAttribute(lookupRef("db/ident", normalized), "db/udf");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function patchSet(path, value) {
|
|
254
|
+
return new PatchOp([kw("set"), patchPath(path), value]);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function patchUnset(path) {
|
|
258
|
+
return new PatchOp([kw("unset"), patchPath(path)]);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export function patchUpdate(path, operation, ...args) {
|
|
262
|
+
return new PatchOp([
|
|
263
|
+
kw("update"),
|
|
264
|
+
patchPath(path),
|
|
265
|
+
patchUpdateOperation(operation),
|
|
266
|
+
...args
|
|
267
|
+
]);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function patchIdoc(
|
|
271
|
+
entityId,
|
|
272
|
+
attributeName,
|
|
273
|
+
patch,
|
|
274
|
+
{ oldValue = MISSING } = {}
|
|
275
|
+
) {
|
|
276
|
+
if (patch instanceof PatchOp) {
|
|
277
|
+
patch = [patch];
|
|
278
|
+
}
|
|
279
|
+
const result = [kw("db.fn/patchIdoc"), entityId, attribute(attributeName)];
|
|
280
|
+
if (oldValue !== MISSING) {
|
|
281
|
+
result.push(oldValue);
|
|
282
|
+
}
|
|
283
|
+
result.push(patch);
|
|
284
|
+
return new TxItem(result);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export function raw(form) {
|
|
288
|
+
return new RawForm(form);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export const tx = Object.freeze({
|
|
292
|
+
add,
|
|
293
|
+
call,
|
|
294
|
+
callUdf,
|
|
295
|
+
cas,
|
|
296
|
+
compareAndSwap,
|
|
297
|
+
data,
|
|
298
|
+
ensure,
|
|
299
|
+
entity,
|
|
300
|
+
invoke,
|
|
301
|
+
installUdf,
|
|
302
|
+
lookupRef,
|
|
303
|
+
patchIdoc,
|
|
304
|
+
patchSet,
|
|
305
|
+
patchUnset,
|
|
306
|
+
patchUpdate,
|
|
307
|
+
raw,
|
|
308
|
+
retract,
|
|
309
|
+
retractAttribute,
|
|
310
|
+
retractEntity,
|
|
311
|
+
uninstallUdf
|
|
312
|
+
});
|
package/src/udf-value.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DatalogSymbol,
|
|
3
|
+
Form,
|
|
4
|
+
Keyword,
|
|
5
|
+
formData,
|
|
6
|
+
immutableSnapshot
|
|
7
|
+
} from "./form.js";
|
|
8
|
+
|
|
9
|
+
export const UDF_KINDS = Object.freeze(new Set([
|
|
10
|
+
"query-fn",
|
|
11
|
+
"predicate",
|
|
12
|
+
"tx-fn",
|
|
13
|
+
"analyzer",
|
|
14
|
+
"query-analyzer",
|
|
15
|
+
"order-fn",
|
|
16
|
+
"serializer",
|
|
17
|
+
"deserializer"
|
|
18
|
+
]));
|
|
19
|
+
|
|
20
|
+
const ALIASES = new Map([
|
|
21
|
+
[":udf/lang", "lang"],
|
|
22
|
+
["lang", "lang"],
|
|
23
|
+
[":udf/kind", "kind"],
|
|
24
|
+
["kind", "kind"],
|
|
25
|
+
[":udf/id", "id"],
|
|
26
|
+
["id", "id"],
|
|
27
|
+
["udfId", "id"],
|
|
28
|
+
[":udf/version", "version"],
|
|
29
|
+
["version", "version"]
|
|
30
|
+
]);
|
|
31
|
+
const MISSING = Symbol("missing-udf-value");
|
|
32
|
+
const DESCRIPTORS = new Map();
|
|
33
|
+
const DESCRIPTOR_FINALIZER = new FinalizationRegistry(({ key, reference }) => {
|
|
34
|
+
if (DESCRIPTORS.get(key) === reference) {
|
|
35
|
+
DESCRIPTORS.delete(key);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
function descriptorIdentity(data) {
|
|
40
|
+
const version = data[":udf/version"];
|
|
41
|
+
const versionType = version instanceof Keyword ? "keyword" : typeof version;
|
|
42
|
+
return JSON.stringify([
|
|
43
|
+
data[":udf/lang"],
|
|
44
|
+
data[":udf/kind"],
|
|
45
|
+
data[":udf/id"],
|
|
46
|
+
versionType,
|
|
47
|
+
version === undefined ? null : String(version)
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function keywordText(value, field) {
|
|
52
|
+
let text;
|
|
53
|
+
if (value instanceof Keyword) {
|
|
54
|
+
text = value.name;
|
|
55
|
+
} else if (typeof value === "string") {
|
|
56
|
+
text = value.startsWith(":") ? value.slice(1) : value;
|
|
57
|
+
} else {
|
|
58
|
+
throw new TypeError(`UDF descriptor ${field} must be a keyword or string.`);
|
|
59
|
+
}
|
|
60
|
+
if (text.length === 0) {
|
|
61
|
+
throw new TypeError(`UDF descriptor ${field} must not be empty.`);
|
|
62
|
+
}
|
|
63
|
+
if (/\s/.test(text)) {
|
|
64
|
+
throw new TypeError(`UDF descriptor ${field} must not contain whitespace.`);
|
|
65
|
+
}
|
|
66
|
+
return `:${text}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function plainObject(value) {
|
|
70
|
+
if (value === null || typeof value !== "object") {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
const prototype = Object.getPrototypeOf(value);
|
|
74
|
+
return prototype === Object.prototype || prototype === null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function descriptorEntries(value) {
|
|
78
|
+
if (value instanceof UdfDescriptor) {
|
|
79
|
+
return Object.entries(value.asData());
|
|
80
|
+
}
|
|
81
|
+
if (value instanceof Map) {
|
|
82
|
+
return Array.from(value.entries());
|
|
83
|
+
}
|
|
84
|
+
if (plainObject(value)) {
|
|
85
|
+
return Object.entries(value);
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function descriptorFields(value) {
|
|
91
|
+
const entries = descriptorEntries(value);
|
|
92
|
+
if (entries === null) {
|
|
93
|
+
return { id: value };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const fields = {};
|
|
97
|
+
const unknown = [];
|
|
98
|
+
for (const [key, item] of entries) {
|
|
99
|
+
const keyText = key instanceof Keyword ? key.toString() : key;
|
|
100
|
+
const canonical = ALIASES.get(keyText);
|
|
101
|
+
if (canonical === undefined) {
|
|
102
|
+
unknown.push(key);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (Object.hasOwn(fields, canonical) && fields[canonical] !== item) {
|
|
106
|
+
throw new TypeError(`Conflicting UDF descriptor values for ${canonical}.`);
|
|
107
|
+
}
|
|
108
|
+
fields[canonical] = item;
|
|
109
|
+
}
|
|
110
|
+
if (unknown.length > 0) {
|
|
111
|
+
throw new TypeError(`Unsupported UDF descriptor key(s): ${unknown.join(", ")}.`);
|
|
112
|
+
}
|
|
113
|
+
return fields;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function descriptorData(value, {
|
|
117
|
+
kind = "query-fn",
|
|
118
|
+
lang = "java",
|
|
119
|
+
version = MISSING
|
|
120
|
+
} = {}) {
|
|
121
|
+
const fields = descriptorFields(value);
|
|
122
|
+
if (fields.id === null || fields.id === undefined) {
|
|
123
|
+
throw new TypeError("udf id is required");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const normalizedLang = keywordText(fields.lang ?? lang, ":udf/lang");
|
|
127
|
+
const normalizedKind = keywordText(fields.kind ?? kind, ":udf/kind");
|
|
128
|
+
if (!UDF_KINDS.has(normalizedKind.slice(1))) {
|
|
129
|
+
throw new TypeError(
|
|
130
|
+
`Unsupported UDF kind ${normalizedKind}; expected one of ${
|
|
131
|
+
[...UDF_KINDS].sort().join(", ")
|
|
132
|
+
}.`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
const normalizedId = keywordText(fields.id, ":udf/id");
|
|
136
|
+
const normalizedVersion = Object.hasOwn(fields, "version")
|
|
137
|
+
? fields.version
|
|
138
|
+
: version;
|
|
139
|
+
if (normalizedVersion !== MISSING && normalizedVersion !== null && normalizedVersion !== undefined) {
|
|
140
|
+
const integer = (
|
|
141
|
+
(typeof normalizedVersion === "number" && Number.isSafeInteger(normalizedVersion))
|
|
142
|
+
|| typeof normalizedVersion === "bigint"
|
|
143
|
+
);
|
|
144
|
+
if (!(normalizedVersion instanceof Keyword)
|
|
145
|
+
&& typeof normalizedVersion !== "string"
|
|
146
|
+
&& !integer) {
|
|
147
|
+
throw new TypeError(
|
|
148
|
+
"UDF descriptor :udf/version must be a keyword, string, integer, or null."
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const result = {
|
|
154
|
+
":udf/lang": normalizedLang,
|
|
155
|
+
":udf/kind": normalizedKind,
|
|
156
|
+
":udf/id": normalizedId
|
|
157
|
+
};
|
|
158
|
+
if (normalizedVersion !== MISSING && normalizedVersion !== null && normalizedVersion !== undefined) {
|
|
159
|
+
result[":udf/version"] = normalizedVersion;
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export class UdfDescriptor extends Form {
|
|
165
|
+
constructor(value = null, {
|
|
166
|
+
kind = "query-fn",
|
|
167
|
+
lang = "javascript",
|
|
168
|
+
version = MISSING
|
|
169
|
+
} = {}) {
|
|
170
|
+
super();
|
|
171
|
+
const data = descriptorData(value, { kind, lang, version });
|
|
172
|
+
const identity = descriptorIdentity(data);
|
|
173
|
+
const existing = DESCRIPTORS.get(identity)?.deref();
|
|
174
|
+
if (existing !== undefined) {
|
|
175
|
+
return existing;
|
|
176
|
+
}
|
|
177
|
+
const formEntries = Object.entries(data).map(([key, item]) => {
|
|
178
|
+
let formItem = item;
|
|
179
|
+
if (key !== ":udf/version"
|
|
180
|
+
|| (typeof item === "string" && item.startsWith(":"))) {
|
|
181
|
+
formItem = item instanceof Keyword ? item : new Keyword(item);
|
|
182
|
+
}
|
|
183
|
+
return [new Keyword(key), formItem];
|
|
184
|
+
});
|
|
185
|
+
this.lang = data[":udf/lang"];
|
|
186
|
+
this.kind = data[":udf/kind"];
|
|
187
|
+
this.udfId = data[":udf/id"];
|
|
188
|
+
this.version = data[":udf/version"] ?? null;
|
|
189
|
+
this.data = immutableSnapshot(data);
|
|
190
|
+
this.form = immutableSnapshot(new Map(formEntries));
|
|
191
|
+
Object.freeze(this);
|
|
192
|
+
const reference = new WeakRef(this);
|
|
193
|
+
DESCRIPTORS.set(identity, reference);
|
|
194
|
+
DESCRIPTOR_FINALIZER.register(this, { key: identity, reference });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
toForm() {
|
|
198
|
+
return this.form;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
asData() {
|
|
202
|
+
return formData(this.data);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
static of(kind, udfId, { lang = "javascript", version = MISSING } = {}) {
|
|
206
|
+
return new UdfDescriptor(udfId, { kind, lang, version });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
static queryFn(udfId, options = {}) {
|
|
210
|
+
return UdfDescriptor.of("query-fn", udfId, options);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static predicate(udfId, options = {}) {
|
|
214
|
+
return UdfDescriptor.of("predicate", udfId, options);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
static txFn(udfId, options = {}) {
|
|
218
|
+
return UdfDescriptor.of("tx-fn", udfId, options);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static analyzer(udfId, options = {}) {
|
|
222
|
+
return UdfDescriptor.of("analyzer", udfId, options);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
static queryAnalyzer(udfId, options = {}) {
|
|
226
|
+
return UdfDescriptor.of("query-analyzer", udfId, options);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static orderFn(udfId, options = {}) {
|
|
230
|
+
return UdfDescriptor.of("order-fn", udfId, options);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static serializer(udfId, options = {}) {
|
|
234
|
+
return UdfDescriptor.of("serializer", udfId, options);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static deserializer(udfId, options = {}) {
|
|
238
|
+
return UdfDescriptor.of("deserializer", udfId, options);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static from(value, { defaultLang = "javascript" } = {}) {
|
|
242
|
+
return value instanceof UdfDescriptor
|
|
243
|
+
? value
|
|
244
|
+
: new UdfDescriptor(value, { lang: defaultLang });
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function udfReference(value) {
|
|
249
|
+
if (typeof value === "string") {
|
|
250
|
+
return new Keyword(value);
|
|
251
|
+
}
|
|
252
|
+
if (value instanceof Map || plainObject(value)) {
|
|
253
|
+
return UdfDescriptor.from(value, { defaultLang: "java" });
|
|
254
|
+
}
|
|
255
|
+
if (
|
|
256
|
+
value instanceof Keyword
|
|
257
|
+
|| value instanceof DatalogSymbol
|
|
258
|
+
|| value instanceof Form
|
|
259
|
+
) {
|
|
260
|
+
return value;
|
|
261
|
+
}
|
|
262
|
+
throw new TypeError(
|
|
263
|
+
"A UDF reference must be a descriptor, keyword id, or query variable."
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export { MISSING as UDF_VALUE_MISSING };
|