datalevin-node 0.10.16

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/src/interop.js ADDED
@@ -0,0 +1,314 @@
1
+ import { toEdnForm, toJava, toJs, toQueryInput } from "./convert.js";
2
+ import { callJavaMethod, classes, jvmStarted, startJvm } from "./jvm.js";
3
+
4
+ async function unwrapInteropHandle(value) {
5
+ if (typeof value?.rawHandle === "function") {
6
+ return value.rawHandle();
7
+ }
8
+ return value;
9
+ }
10
+
11
+ async function normalizeInteropArgs(args = []) {
12
+ const normalized = [];
13
+ for (const arg of args) {
14
+ normalized.push(await unwrapInteropHandle(arg));
15
+ }
16
+ return normalized;
17
+ }
18
+
19
+ class InteropBindings {
20
+ async apiInfoRaw() {
21
+ const cls = await classes();
22
+ return callJavaMethod(cls.datalevin, "apiInfo");
23
+ }
24
+
25
+ async execJson(op, args = null) {
26
+ const cls = await classes();
27
+ if (args === null || args === undefined) {
28
+ return callJavaMethod(cls.datalevin, "exec", op);
29
+ }
30
+ return callJavaMethod(cls.datalevin, "exec", op, await toJava(args));
31
+ }
32
+
33
+ async coreInvoke(functionName, args = null) {
34
+ const cls = await classes();
35
+ return callJavaMethod(
36
+ cls.interop,
37
+ "coreInvokeBridge",
38
+ functionName,
39
+ await toJava(await normalizeInteropArgs([...(args || [])]))
40
+ );
41
+ }
42
+
43
+ async coreInvokeRaw(functionName, args = null) {
44
+ const cls = await classes();
45
+ return callJavaMethod(
46
+ cls.interop,
47
+ "coreInvoke",
48
+ functionName,
49
+ await toJava(await normalizeInteropArgs([...(args || [])]))
50
+ );
51
+ }
52
+
53
+ async clientInvoke(functionName, args = null) {
54
+ const cls = await classes();
55
+ return callJavaMethod(
56
+ cls.interop,
57
+ "clientInvokeBridge",
58
+ functionName,
59
+ await toJava(await normalizeInteropArgs([...(args || [])]))
60
+ );
61
+ }
62
+
63
+ async createConnection(dir = null, schema = null, opts = null, { shared = false } = {}) {
64
+ const cls = await classes();
65
+ const target = shared ? "getConn" : "createConn";
66
+
67
+ if (opts !== null && opts !== undefined) {
68
+ return callJavaMethod(cls.datalevin, target, dir, await toJava(schema), await toJava(opts));
69
+ }
70
+ if (schema !== null && schema !== undefined) {
71
+ return callJavaMethod(cls.datalevin, target, dir, await toJava(schema));
72
+ }
73
+ if (dir !== null && dir !== undefined) {
74
+ return callJavaMethod(cls.datalevin, target, dir);
75
+ }
76
+ return callJavaMethod(cls.datalevin, target);
77
+ }
78
+
79
+ async closeConnection(handle) {
80
+ await callJavaMethod(handle, "close");
81
+ }
82
+
83
+ async connectionClosed(handle) {
84
+ return Boolean(await callJavaMethod(handle, "closed"));
85
+ }
86
+
87
+ async connectionDb(handle) {
88
+ const cls = await classes();
89
+ return callJavaMethod(cls.interop, "connectionDb", handle);
90
+ }
91
+
92
+ async openKeyValue(dir, opts = null) {
93
+ const cls = await classes();
94
+ if (opts !== null && opts !== undefined) {
95
+ return callJavaMethod(cls.datalevin, "openKV", dir, await toJava(opts));
96
+ }
97
+ return callJavaMethod(cls.datalevin, "openKV", dir);
98
+ }
99
+
100
+ async closeKeyValue(handle) {
101
+ await callJavaMethod(handle, "close");
102
+ }
103
+
104
+ async keyValueClosed(handle) {
105
+ return Boolean(await callJavaMethod(handle, "closed"));
106
+ }
107
+
108
+ async newClient(uri, opts = null) {
109
+ const cls = await classes();
110
+ if (opts !== null && opts !== undefined) {
111
+ return callJavaMethod(cls.datalevin, "newClient", uri, await toJava(opts));
112
+ }
113
+ return callJavaMethod(cls.datalevin, "newClient", uri);
114
+ }
115
+
116
+ async closeClient(handle) {
117
+ await callJavaMethod(handle, "close");
118
+ }
119
+
120
+ async clientDisconnected(handle) {
121
+ return Boolean(await callJavaMethod(handle, "disconnected"));
122
+ }
123
+
124
+ async bridgeResult(value) {
125
+ const cls = await classes();
126
+ return callJavaMethod(cls.interop, "bridgeResult", value);
127
+ }
128
+
129
+ async readEdn(edn) {
130
+ const cls = await classes();
131
+ return callJavaMethod(cls.interop, "readEdn", edn);
132
+ }
133
+
134
+ async keyword(value) {
135
+ const cls = await classes();
136
+ return callJavaMethod(cls.interop, "keyword", value);
137
+ }
138
+
139
+ async symbol(value) {
140
+ const cls = await classes();
141
+ return callJavaMethod(cls.interop, "symbol", value);
142
+ }
143
+
144
+ async schema(schema) {
145
+ if (schema === null || schema === undefined) {
146
+ return null;
147
+ }
148
+ const cls = await classes();
149
+ return callJavaMethod(cls.interop, "schema", await toJava(schema));
150
+ }
151
+
152
+ async options(opts) {
153
+ if (opts === null || opts === undefined) {
154
+ return null;
155
+ }
156
+ const cls = await classes();
157
+ return callJavaMethod(cls.interop, "options", await toJava(opts));
158
+ }
159
+
160
+ async udfDescriptor(descriptor) {
161
+ if (descriptor === null || descriptor === undefined) {
162
+ return null;
163
+ }
164
+ const cls = await classes();
165
+ return callJavaMethod(cls.interop, "udfDescriptor", await toJava(descriptor));
166
+ }
167
+
168
+ async createUdfRegistry() {
169
+ const cls = await classes();
170
+ return callJavaMethod(cls.interop, "createUdfRegistry");
171
+ }
172
+
173
+ async registerUdf(registry, descriptor, fn) {
174
+ const cls = await classes();
175
+ return callJavaMethod(cls.interop, "registerUdf", registry, await toJava(descriptor), fn);
176
+ }
177
+
178
+ async unregisterUdf(registry, descriptor) {
179
+ const cls = await classes();
180
+ return callJavaMethod(cls.interop, "unregisterUdf", registry, await toJava(descriptor));
181
+ }
182
+
183
+ async registeredUdf(registry, descriptor) {
184
+ const cls = await classes();
185
+ return Boolean(
186
+ await callJavaMethod(cls.interop, "registeredUdf", registry, await toJava(descriptor))
187
+ );
188
+ }
189
+
190
+ async renameMap(renameMap) {
191
+ if (renameMap === null || renameMap === undefined) {
192
+ return null;
193
+ }
194
+ const cls = await classes();
195
+ return callJavaMethod(cls.interop, "renameMap", await toJava(renameMap));
196
+ }
197
+
198
+ async deleteAttrs(attrs) {
199
+ if (attrs === null || attrs === undefined) {
200
+ return null;
201
+ }
202
+ const cls = await classes();
203
+ return callJavaMethod(cls.interop, "deleteAttrs", await toJava([...attrs]));
204
+ }
205
+
206
+ async lookupRef(value) {
207
+ if (value === null || value === undefined) {
208
+ return null;
209
+ }
210
+ const cls = await classes();
211
+ return callJavaMethod(cls.interop, "lookupRef", await toJava(value));
212
+ }
213
+
214
+ async txData(txData) {
215
+ if (txData === null || txData === undefined) {
216
+ return null;
217
+ }
218
+ const cls = await classes();
219
+ return callJavaMethod(cls.interop, "txData", await toJava(txData));
220
+ }
221
+
222
+ async kvTxs(txs) {
223
+ if (txs === null || txs === undefined) {
224
+ return null;
225
+ }
226
+ const cls = await classes();
227
+ return callJavaMethod(cls.interop, "kvTxs", await toJava(txs));
228
+ }
229
+
230
+ async kvTxsWithTypes(txs, kType = null, vType = null) {
231
+ if (txs === null || txs === undefined) {
232
+ return null;
233
+ }
234
+ const cls = await classes();
235
+ return callJavaMethod(
236
+ cls.interop,
237
+ "kvTxs",
238
+ await toJava(txs),
239
+ await toJava(kType),
240
+ await toJava(vType)
241
+ );
242
+ }
243
+
244
+ async kvInput(value, type) {
245
+ if (value === null || value === undefined) {
246
+ return null;
247
+ }
248
+ const cls = await classes();
249
+ return callJavaMethod(cls.interop, "kvInput", await toJava(value), await toJava(type));
250
+ }
251
+
252
+ async kvRange(range, type) {
253
+ if (range === null || range === undefined) {
254
+ return null;
255
+ }
256
+ const cls = await classes();
257
+ return callJavaMethod(cls.interop, "kvRange", await toEdnForm(range), await toJava(type));
258
+ }
259
+
260
+ async kvType(value) {
261
+ if (value === null || value === undefined) {
262
+ return null;
263
+ }
264
+ const cls = await classes();
265
+ return callJavaMethod(cls.interop, "kvType", await toJava(value));
266
+ }
267
+
268
+ async databaseType(value) {
269
+ const cls = await classes();
270
+ return callJavaMethod(cls.interop, "databaseType", value);
271
+ }
272
+
273
+ async role(role) {
274
+ const cls = await classes();
275
+ return callJavaMethod(cls.interop, "role", role);
276
+ }
277
+
278
+ async permissionKeyword(value) {
279
+ const cls = await classes();
280
+ return callJavaMethod(cls.interop, "permissionKeyword", value);
281
+ }
282
+
283
+ async permissionTarget(objectType, target) {
284
+ const cls = await classes();
285
+ return callJavaMethod(cls.interop, "permissionTarget", objectType, await toJava(target));
286
+ }
287
+ }
288
+
289
+ export const _BINDINGS = new InteropBindings();
290
+
291
+ export async function apiInfo() {
292
+ return toJs(await _BINDINGS.apiInfoRaw());
293
+ }
294
+
295
+ export async function execJson(op, args = null) {
296
+ return toJs(await _BINDINGS.execJson(op, args));
297
+ }
298
+
299
+ export async function connect(dir = null, { schema = null, opts = null, shared = false } = {}) {
300
+ const { Connection } = await import("./connection.js");
301
+ return new Connection(await _BINDINGS.createConnection(dir, schema, opts, { shared }));
302
+ }
303
+
304
+ export async function openKv(dir, opts = null) {
305
+ const { KV } = await import("./kv.js");
306
+ return new KV(await _BINDINGS.openKeyValue(dir, opts));
307
+ }
308
+
309
+ export async function newClient(uri, opts = null) {
310
+ const { Client } = await import("./client.js");
311
+ return new Client(await _BINDINGS.newClient(uri, opts));
312
+ }
313
+
314
+ export { jvmStarted, startJvm, toEdnForm, toJava, toJs, toQueryInput };
package/src/jvm.js ADDED
@@ -0,0 +1,318 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ import {
7
+ DatalevinConfigurationError,
8
+ DatalevinJavaError,
9
+ DatalevinJvmError
10
+ } from "./errors.js";
11
+
12
+ export const DATALEVIN_JAR_ENV = "DATALEVIN_JAR";
13
+ export const DATALEVIN_CLASSPATH_ENV = "DATALEVIN_CLASSPATH";
14
+ export const DATALEVIN_JVM_ARGS_ENV = "DATALEVIN_JVM_ARGS";
15
+ export const DATALEVIN_JAVACPP_CACHEDIR_ENV = "DATALEVIN_JAVACPP_CACHEDIR";
16
+ export const DEFAULT_JAVACPP_CACHEDIR = "/tmp/datalevin-javacpp-cache";
17
+
18
+ const TARGET_JAR_PATTERNS = [
19
+ /^datalevin-runtime-.*\.jar$/,
20
+ /^datalevin-python-runtime-.*\.jar$/,
21
+ /^datalevin-java-.*\.jar$/
22
+ ];
23
+
24
+ const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
25
+ const jarsDir = path.join(packageRoot, "jars");
26
+ const repoRoot = path.resolve(packageRoot, "../..");
27
+ const targetDir = path.join(repoRoot, "target");
28
+
29
+ let bridgeModulePromise = null;
30
+ let startJvmPromise = null;
31
+ let classesPromise = null;
32
+ let started = false;
33
+
34
+ function expandPath(value) {
35
+ if (value.startsWith("~")) {
36
+ return path.resolve(os.homedir(), value.slice(1));
37
+ }
38
+ return path.resolve(value);
39
+ }
40
+
41
+ function findRuntimeJars(dir) {
42
+ if (!fs.existsSync(dir)) {
43
+ return [];
44
+ }
45
+
46
+ return fs.readdirSync(dir)
47
+ .filter((entry) => TARGET_JAR_PATTERNS.some((pattern) => pattern.test(entry)))
48
+ .sort()
49
+ .map((entry) => path.join(dir, entry));
50
+ }
51
+
52
+ function splitShellWords(input) {
53
+ if (!input) {
54
+ return [];
55
+ }
56
+
57
+ const words = [];
58
+ let current = "";
59
+ let quote = null;
60
+ let escape = false;
61
+
62
+ for (const ch of input) {
63
+ if (escape) {
64
+ current += ch;
65
+ escape = false;
66
+ continue;
67
+ }
68
+
69
+ if (ch === "\\") {
70
+ escape = true;
71
+ continue;
72
+ }
73
+
74
+ if (quote !== null) {
75
+ if (ch === quote) {
76
+ quote = null;
77
+ } else {
78
+ current += ch;
79
+ }
80
+ continue;
81
+ }
82
+
83
+ if (ch === "'" || ch === "\"") {
84
+ quote = ch;
85
+ continue;
86
+ }
87
+
88
+ if (/\s/.test(ch)) {
89
+ if (current) {
90
+ words.push(current);
91
+ current = "";
92
+ }
93
+ continue;
94
+ }
95
+
96
+ current += ch;
97
+ }
98
+
99
+ if (escape || quote !== null) {
100
+ throw new DatalevinConfigurationError(
101
+ `Invalid ${DATALEVIN_JVM_ARGS_ENV} shell-style argument string.`
102
+ );
103
+ }
104
+
105
+ if (current) {
106
+ words.push(current);
107
+ }
108
+
109
+ return words;
110
+ }
111
+
112
+ function ensureJavacppCachedirArg(jvmArgs) {
113
+ for (const arg of jvmArgs) {
114
+ if (
115
+ arg.startsWith("-Dorg.bytedeco.javacpp.cachedir=") ||
116
+ arg.startsWith("-Dorg.bytedeco.javacpp.cacheDir=")
117
+ ) {
118
+ return jvmArgs;
119
+ }
120
+ }
121
+
122
+ const cacheDir = process.env[DATALEVIN_JAVACPP_CACHEDIR_ENV] || DEFAULT_JAVACPP_CACHEDIR;
123
+ fs.mkdirSync(cacheDir, { recursive: true });
124
+ jvmArgs.push(`-Dorg.bytedeco.javacpp.cachedir=${cacheDir}`);
125
+ return jvmArgs;
126
+ }
127
+
128
+ function resolveJvmArgs(jvmArgs = null) {
129
+ if (jvmArgs && jvmArgs.length > 0) {
130
+ return ensureJavacppCachedirArg([...jvmArgs]);
131
+ }
132
+
133
+ const envArgs = process.env[DATALEVIN_JVM_ARGS_ENV];
134
+ return ensureJavacppCachedirArg(splitShellWords(envArgs));
135
+ }
136
+
137
+ export function resolveClasspath(classpath = null) {
138
+ if (classpath && classpath.length > 0) {
139
+ return classpath.map((entry) => expandPath(entry));
140
+ }
141
+
142
+ const envClasspath = process.env[DATALEVIN_CLASSPATH_ENV];
143
+ if (envClasspath) {
144
+ return envClasspath
145
+ .split(path.delimiter)
146
+ .filter(Boolean)
147
+ .map((entry) => expandPath(entry));
148
+ }
149
+
150
+ const envJar = process.env[DATALEVIN_JAR_ENV];
151
+ if (envJar) {
152
+ return [expandPath(envJar)];
153
+ }
154
+
155
+ const vendored = findRuntimeJars(jarsDir);
156
+ if (vendored.length > 0) {
157
+ return vendored;
158
+ }
159
+
160
+ const repoLocal = findRuntimeJars(targetDir);
161
+ if (repoLocal.length > 0) {
162
+ return repoLocal;
163
+ }
164
+
165
+ throw new DatalevinConfigurationError(
166
+ "Unable to resolve a Datalevin runtime jar. "
167
+ + `Set ${DATALEVIN_JAR_ENV}, set ${DATALEVIN_CLASSPATH_ENV}, `
168
+ + "or vendor a jar under bindings/javascript/jars/."
169
+ );
170
+ }
171
+
172
+ export function jvmStarted() {
173
+ return started;
174
+ }
175
+
176
+ export async function javaBridgeModule() {
177
+ if (bridgeModulePromise !== null) {
178
+ return bridgeModulePromise;
179
+ }
180
+
181
+ bridgeModulePromise = import("java-bridge")
182
+ .then((module) => module.default ?? module)
183
+ .catch((error) => {
184
+ bridgeModulePromise = null;
185
+ throw new DatalevinConfigurationError(
186
+ "The `java-bridge` package is required for the Datalevin Node bindings.",
187
+ { cause: error }
188
+ );
189
+ });
190
+ return bridgeModulePromise;
191
+ }
192
+
193
+ function javaClassName(value) {
194
+ try {
195
+ return value.getClassSync().getNameSync();
196
+ } catch {
197
+ return null;
198
+ }
199
+ }
200
+
201
+ function javaMessage(value) {
202
+ try {
203
+ return value.getMessageSync();
204
+ } catch {
205
+ return null;
206
+ }
207
+ }
208
+
209
+ function coerceJavaError(error) {
210
+ const cause = error?.cause && typeof error.cause.getClassSync === "function" ? error.cause : null;
211
+ const javaClass = cause === null ? null : javaClassName(cause);
212
+ const typeName =
213
+ cause !== null && typeof cause.getErrorTypeSync === "function"
214
+ ? cause.getErrorTypeSync()
215
+ : null;
216
+ const data =
217
+ cause !== null && typeof cause.getDataSync === "function"
218
+ ? cause.getDataSync()
219
+ : null;
220
+
221
+ return new DatalevinJavaError(
222
+ javaMessage(cause) || error?.message || "Java call failed.",
223
+ {
224
+ javaClass,
225
+ typeName,
226
+ data,
227
+ cause: error
228
+ }
229
+ );
230
+ }
231
+
232
+ export async function callJava(target, ...args) {
233
+ try {
234
+ return await target(...args);
235
+ } catch (error) {
236
+ throw coerceJavaError(error);
237
+ }
238
+ }
239
+
240
+ export async function callJavaMethod(receiver, methodName, ...args) {
241
+ return callJava(receiver[methodName].bind(receiver), ...args);
242
+ }
243
+
244
+ export async function startJvm({ classpath = null, jvmArgs = null } = {}) {
245
+ if (startJvmPromise !== null) {
246
+ return startJvmPromise;
247
+ }
248
+
249
+ startJvmPromise = (async () => {
250
+ const bridge = await javaBridgeModule();
251
+ const resolvedClasspath = resolveClasspath(classpath);
252
+ const resolvedJvmArgs = resolveJvmArgs(jvmArgs);
253
+
254
+ try {
255
+ bridge.ensureJvm({
256
+ classpath: resolvedClasspath,
257
+ opts: resolvedJvmArgs
258
+ });
259
+ started = true;
260
+ } catch (error) {
261
+ startJvmPromise = null;
262
+ throw new DatalevinJvmError(
263
+ "Failed to start the JVM for Datalevin. "
264
+ + "Check that Java 21+ is installed and that the runtime jar is valid.",
265
+ { cause: error }
266
+ );
267
+ }
268
+ })();
269
+
270
+ return startJvmPromise;
271
+ }
272
+
273
+ export async function classes() {
274
+ if (classesPromise !== null) {
275
+ return classesPromise;
276
+ }
277
+
278
+ classesPromise = (async () => {
279
+ await startJvm();
280
+ const { importClass } = await javaBridgeModule();
281
+ const opts = { asyncJavaExceptionObjects: true };
282
+ const load = (className) => importClass(className, opts);
283
+
284
+ return {
285
+ datalevin: load("datalevin.Datalevin"),
286
+ interop: load("datalevin.DatalevinInterop"),
287
+ udfFunction: load("datalevin.UdfFunction"),
288
+ datalevinException: load("datalevin.DatalevinException"),
289
+ linkedHashMap: load("java.util.LinkedHashMap"),
290
+ linkedHashSet: load("java.util.LinkedHashSet"),
291
+ arrayList: load("java.util.ArrayList"),
292
+ longClass: load("java.lang.Long"),
293
+ uuid: load("java.util.UUID"),
294
+ instant: load("java.time.Instant"),
295
+ date: load("java.util.Date"),
296
+ bigInteger: load("java.math.BigInteger"),
297
+ bigDecimal: load("java.math.BigDecimal"),
298
+ mapType: load("java.util.Map"),
299
+ listType: load("java.util.List"),
300
+ setType: load("java.util.Set"),
301
+ collectionType: load("java.util.Collection"),
302
+ keywordType: load("clojure.lang.Keyword"),
303
+ symbolType: load("clojure.lang.Symbol")
304
+ };
305
+ })().catch((error) => {
306
+ classesPromise = null;
307
+ throw error;
308
+ });
309
+
310
+ return classesPromise;
311
+ }
312
+
313
+ export const __testing = {
314
+ findRuntimeJars,
315
+ splitShellWords,
316
+ resolveJvmArgs,
317
+ ensureJavacppCachedirArg
318
+ };