@based/db 0.0.44 → 0.0.46

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.
Files changed (33) hide show
  1. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  2. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  3. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  4. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  5. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  6. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  7. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  8. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  9. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  10. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  11. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  12. package/dist/lib/linux_aarch64/libselva.so +0 -0
  13. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  14. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  15. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  16. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  17. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  18. package/dist/lib/linux_x86_64/libselva.so +0 -0
  19. package/dist/src/client/modify/create.js +1 -1
  20. package/dist/src/client/modify/update.js +1 -1
  21. package/dist/src/client/query/BasedDbQuery.js +15 -1
  22. package/dist/src/client/query/debug.js +3 -0
  23. package/dist/src/client/query/display.js +1 -1
  24. package/dist/src/client/query/include/toBuffer.js +0 -1
  25. package/dist/src/client/query/read/read.js +51 -40
  26. package/dist/src/client/query/registerQuery.js +0 -1
  27. package/dist/src/client/query/toBuffer.js +54 -26
  28. package/dist/src/client/query/types.d.ts +7 -0
  29. package/dist/src/client/query/types.js +1 -0
  30. package/dist/src/index.js +10 -0
  31. package/dist/src/server/index.js +4 -0
  32. package/dist/src/server/worker.js +1 -2
  33. package/package.json +1 -1
Binary file
Binary file
@@ -142,7 +142,7 @@ export function create(db, type, obj, opts) {
142
142
  ctx.prefix0 = -1; // Force a new cursor
143
143
  ctx.len = pos;
144
144
  if (err === RANGE_ERR) {
145
- if (pos === 0) {
145
+ if (pos === 8) {
146
146
  throw new Error('!No range available');
147
147
  }
148
148
  flushBuffer(db);
@@ -67,7 +67,7 @@ export const update = (db, type, id, obj, opts) => {
67
67
  ctx.prefix0 = -1; // force a new cursor
68
68
  ctx.len = pos;
69
69
  if (err === RANGE_ERR) {
70
- if (pos === 0) {
70
+ if (pos === 8) {
71
71
  throw new Error('out of range');
72
72
  }
73
73
  flushBuffer(db);
@@ -285,6 +285,7 @@ export class BasedDbQuery extends QueryBranch {
285
285
  throw new Error('Query: No schema yet - use await db.schemaIsSet()');
286
286
  }
287
287
  const def = createQueryDef(db, QueryDefType.Root, target, skipValidation);
288
+ def.schemaChecksum = db.schema?.hash || 0;
288
289
  super(db, def);
289
290
  this.db = db;
290
291
  this.skipValidation = skipValidation;
@@ -295,6 +296,7 @@ export class BasedDbQuery extends QueryBranch {
295
296
  this.id = undefined;
296
297
  this.buffer = undefined;
297
298
  const def = createQueryDef(this.db, QueryDefType.Root, this.target, this.skipValidation);
299
+ def.schemaChecksum = this.db.schema?.hash || 0;
298
300
  this.def = def;
299
301
  const q = this.queryCommands;
300
302
  this.queryCommands = [];
@@ -316,8 +318,20 @@ export class BasedDbQuery extends QueryBranch {
316
318
  }
317
319
  const d = performance.now();
318
320
  await this.db.isModified();
321
+ if (this.db.schema?.hash !== this.def.schemaChecksum) {
322
+ this.reBuildQuery();
323
+ return this.#getInternal(resolve, reject);
324
+ }
319
325
  const res = await this.db.hooks.getQueryBuf(buf);
320
- if (res instanceof Error) {
326
+ if (res.byteLength === 1) {
327
+ if (res[0] === 0) {
328
+ reject(new Error('schema mismatch'));
329
+ }
330
+ else {
331
+ reject(new Error('unexpected error'));
332
+ }
333
+ }
334
+ else if (res instanceof Error) {
321
335
  reject(res);
322
336
  }
323
337
  else {
@@ -101,6 +101,9 @@ export const debug = (x, start = 0, end = 0, label) => {
101
101
  if (a[j + i * w] === 252) {
102
102
  return picocolors.red(v);
103
103
  }
104
+ if (a[j + i * w] === 250) {
105
+ return picocolors.redBright(v);
106
+ }
104
107
  return v;
105
108
  })
106
109
  .join(' '));
@@ -281,7 +281,7 @@ export const inspectData = (q, def, level, top, depth, hasId = false) => {
281
281
  str = prefix + '[';
282
282
  }
283
283
  if (def.aggregate) {
284
- str += inspectObject(q.toObject(), def, '', level + 1, i === max - 1, i === 0, false, depth);
284
+ str += inspectObject('toObject' in q ? q.toObject() : q, def, '', level + 1, i === max - 1, i === 0, false, depth);
285
285
  return str;
286
286
  }
287
287
  for (const x of q) {
@@ -18,7 +18,6 @@ export const includeToBuffer = (db, def) => {
18
18
  walkDefs(db, def, f);
19
19
  }
20
20
  }
21
- // main
22
21
  if (def.include.main.len > 0) {
23
22
  // if (def.target.)
24
23
  const len = def.type === QueryDefType.Edge
@@ -3,7 +3,43 @@ import { QueryDefType } from '../types.js';
3
3
  import { read, readUtf8 } from '../../string.js';
4
4
  import { DECODER, readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, setByPath, } from '@saulx/utils';
5
5
  import { inverseLangMap } from '@based/schema';
6
- import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES, } from '../types.js';
6
+ import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES, READ_AGGREGATION, } from '../types.js';
7
+ const readAggregate = (q, result, offset, len) => {
8
+ const results = {};
9
+ if (q.aggregate.groupBy) {
10
+ let i = offset;
11
+ while (i < len) {
12
+ let key = '';
13
+ if (result[i] == 0) {
14
+ if (q.aggregate.groupBy.default) {
15
+ key = q.aggregate.groupBy.default;
16
+ }
17
+ else {
18
+ key = `$undefined`;
19
+ }
20
+ }
21
+ else {
22
+ key = DECODER.decode(result.subarray(i, i + 2));
23
+ }
24
+ i += 2;
25
+ const resultKey = (results[key] = {});
26
+ for (const aggregatesArray of q.aggregate.aggregates.values()) {
27
+ for (const agg of aggregatesArray) {
28
+ setByPath(resultKey, agg.propDef.path, readUint32(result, agg.resultPos + i));
29
+ }
30
+ }
31
+ i += q.aggregate.totalResultsPos;
32
+ }
33
+ }
34
+ else {
35
+ for (const aggregatesArray of q.aggregate.aggregates.values()) {
36
+ for (const agg of aggregatesArray) {
37
+ setByPath(results, agg.propDef.path, readUint32(result, agg.resultPos + offset));
38
+ }
39
+ }
40
+ }
41
+ return results;
42
+ };
7
43
  const addField = (p, value, item, defaultOnly = false, lang = 0) => {
8
44
  let i = p.__isEdge === true ? 1 : 0;
9
45
  // TODO OPTMIZE
@@ -175,7 +211,19 @@ export const readAllFields = (q, result, offset, end, item, id) => {
175
211
  handleUndefinedProps(id, q, item);
176
212
  return i - offset;
177
213
  }
178
- if (index === READ_EDGE) {
214
+ if (index === READ_AGGREGATION) {
215
+ // also for edges at some point!
216
+ let field = result[i];
217
+ i++;
218
+ const size = readUint32(result, i);
219
+ i += 4;
220
+ const ref = q.references.get(field);
221
+ addField(
222
+ // @ts-ignore
223
+ ref.target.propDef, readAggregate(ref, result, i, i + size), item);
224
+ i += size;
225
+ }
226
+ else if (index === READ_EDGE) {
179
227
  let prop = result[i];
180
228
  if (prop === READ_REFERENCE) {
181
229
  i++;
@@ -369,44 +417,7 @@ export const readAllFields = (q, result, offset, end, item, id) => {
369
417
  let cnt = 0;
370
418
  export const resultToObject = (q, result, end, offset = 0) => {
371
419
  if (q.aggregate) {
372
- const results = {};
373
- // range for numbers
374
- if (q.aggregate.groupBy) {
375
- // key size = 2 for now... not perfect...
376
- let i = 0;
377
- while (i < result.byteLength - 4) {
378
- // if group = 0
379
- // add extra thing for the keys maybe?
380
- let key = '';
381
- if (result[i] == 0) {
382
- if (q.aggregate.groupBy.default) {
383
- key = q.aggregate.groupBy.default;
384
- }
385
- else {
386
- key = `$undefined`;
387
- }
388
- }
389
- else {
390
- key = DECODER.decode(result.subarray(i, i + 2));
391
- }
392
- i += 2;
393
- const resultKey = (results[key] = {});
394
- for (const aggregatesArray of q.aggregate.aggregates.values()) {
395
- for (const agg of aggregatesArray) {
396
- setByPath(resultKey, agg.propDef.path, readUint32(result, agg.resultPos + i));
397
- }
398
- }
399
- i += q.aggregate.totalResultsPos;
400
- }
401
- }
402
- else {
403
- for (const aggregatesArray of q.aggregate.aggregates.values()) {
404
- for (const agg of aggregatesArray) {
405
- setByPath(results, agg.propDef.path, readUint32(result, agg.resultPos));
406
- }
407
- }
408
- }
409
- return results;
420
+ return readAggregate(q, result, 0, result.byteLength - 4);
410
421
  }
411
422
  const len = readUint32(result, offset);
412
423
  if (len === 0) {
@@ -4,7 +4,6 @@ import { defToBuffer } from './toBuffer.js';
4
4
  import { handleErrors } from './validation.js';
5
5
  export const registerQuery = (q) => {
6
6
  if (!q.id) {
7
- q.def.schemaChecksum = q.db.schema.hash;
8
7
  const b = defToBuffer(q.db, q.def);
9
8
  const buf = concatUint8Arr(b);
10
9
  let id = native.crc32(buf);
@@ -39,30 +39,58 @@ export function defToBuffer(db, def) {
39
39
  throw new Error('Wrong aggregate size (0)');
40
40
  }
41
41
  const filterSize = def.filter.size || 0;
42
- const buf = new Uint8Array(16 + filterSize + aggregateSize);
43
- buf[0] = isRootCountOnly(def, filterSize)
44
- ? QueryType.aggregatesCountType
45
- : QueryType.aggregates;
46
- buf[1] = def.schema.idUint8[0];
47
- buf[2] = def.schema.idUint8[1];
48
- buf[3] = def.range.offset;
49
- buf[4] = def.range.offset >>> 8;
50
- buf[5] = def.range.offset >>> 16;
51
- buf[6] = def.range.offset >>> 24;
52
- buf[7] = def.range.limit;
53
- buf[8] = def.range.limit >>> 8;
54
- buf[9] = def.range.limit >>> 16;
55
- buf[10] = def.range.limit >>> 24;
56
- buf[11] = filterSize;
57
- buf[12] = filterSize >>> 8;
58
- if (filterSize) {
59
- buf.set(filterToBuffer(def.filter), 13);
42
+ if (def.type === QueryDefType.References) {
43
+ const buf = new Uint8Array(13 + filterSize + aggregateSize);
44
+ const sz = 10 + filterSize + aggregateSize;
45
+ buf[0] = 251 /* includeOp.REFERENCES_AGGREGATION */;
46
+ buf[1] = sz;
47
+ buf[2] = sz >>> 8;
48
+ // ---
49
+ buf[3] = filterSize;
50
+ buf[4] = filterSize >>> 8;
51
+ buf[5] = def.range.offset;
52
+ buf[6] = def.range.offset >>> 8;
53
+ buf[7] = def.range.offset >>> 16;
54
+ buf[8] = def.range.offset >>> 24;
55
+ if (filterSize) {
56
+ buf.set(filterToBuffer(def.filter), 9);
57
+ }
58
+ // required to get typeEntry and fieldSchema
59
+ buf[9 + filterSize] = def.schema.idUint8[0]; // typeId
60
+ buf[9 + 1 + filterSize] = def.schema.idUint8[1]; // typeId
61
+ buf[9 + 2 + filterSize] = def.target.propDef.prop; // refField
62
+ const aggregateBuffer = aggregateToBuffer(def.aggregate);
63
+ buf.set(aggregateBuffer, 9 + 3 + filterSize);
64
+ // buf[12 + filterSize] = aggregateSize
65
+ // buf[12 + 1 + filterSize] = aggregateSize >>> 8
66
+ result.push(buf);
67
+ }
68
+ else {
69
+ const buf = new Uint8Array(16 + filterSize + aggregateSize);
70
+ buf[0] = isRootCountOnly(def, filterSize)
71
+ ? QueryType.aggregatesCountType
72
+ : QueryType.aggregates;
73
+ buf[1] = def.schema.idUint8[0];
74
+ buf[2] = def.schema.idUint8[1];
75
+ buf[3] = def.range.offset;
76
+ buf[4] = def.range.offset >>> 8;
77
+ buf[5] = def.range.offset >>> 16;
78
+ buf[6] = def.range.offset >>> 24;
79
+ buf[7] = def.range.limit;
80
+ buf[8] = def.range.limit >>> 8;
81
+ buf[9] = def.range.limit >>> 16;
82
+ buf[10] = def.range.limit >>> 24;
83
+ buf[11] = filterSize;
84
+ buf[12] = filterSize >>> 8;
85
+ if (filterSize) {
86
+ buf.set(filterToBuffer(def.filter), 13);
87
+ }
88
+ const aggregateBuffer = aggregateToBuffer(def.aggregate);
89
+ buf[14 + filterSize] = aggregateSize;
90
+ buf[15 + filterSize] = aggregateSize >>> 8;
91
+ buf.set(aggregateBuffer, 16 + filterSize);
92
+ result.push(buf);
60
93
  }
61
- const aggregateBuffer = aggregateToBuffer(def.aggregate);
62
- buf[14 + filterSize] = aggregateSize;
63
- buf[15 + filterSize] = aggregateSize >>> 8;
64
- buf.set(aggregateBuffer, 16 + filterSize);
65
- result.push(buf);
66
94
  // ignore this for now...
67
95
  // result.push(...include)
68
96
  if (def.type === QueryDefType.Root) {
@@ -206,7 +234,7 @@ export function defToBuffer(db, def) {
206
234
  const modsSize = filterSize + sortSize;
207
235
  const meta = new Uint8Array(modsSize + 10 + 8);
208
236
  const sz = size + 7 + modsSize + 8;
209
- meta[0] = 254;
237
+ meta[0] = 254 /* includeOp.REFERENCES */;
210
238
  meta[1] = sz;
211
239
  meta[2] = sz >>> 8;
212
240
  meta[3] = filterSize;
@@ -235,7 +263,7 @@ export function defToBuffer(db, def) {
235
263
  else if (def.type === QueryDefType.Reference) {
236
264
  const meta = new Uint8Array(6);
237
265
  const sz = size + 3;
238
- meta[0] = 255;
266
+ meta[0] = 255 /* includeOp.REFERENCE */;
239
267
  meta[1] = sz;
240
268
  meta[2] = sz >>> 8;
241
269
  meta[3] = def.schema.idUint8[0];
@@ -246,7 +274,7 @@ export function defToBuffer(db, def) {
246
274
  result.push(...include);
247
275
  if (edges) {
248
276
  const metaEdgeBuffer = new Uint8Array(3);
249
- metaEdgeBuffer[0] = 252;
277
+ metaEdgeBuffer[0] = 252 /* includeOp.EDGE */;
250
278
  metaEdgeBuffer[1] = edgesSize;
251
279
  metaEdgeBuffer[2] = edgesSize >>> 8;
252
280
  result.push(metaEdgeBuffer, ...edges);
@@ -140,3 +140,10 @@ export declare const READ_ID = 255;
140
140
  export declare const READ_EDGE = 252;
141
141
  export declare const READ_REFERENCES = 253;
142
142
  export declare const READ_REFERENCE = 254;
143
+ export declare const READ_AGGREGATION = 250;
144
+ export declare const enum includeOp {
145
+ REFERENCES_AGGREGATION = 251,
146
+ EDGE = 252,
147
+ REFERENCES = 254,
148
+ REFERENCE = 255
149
+ }
@@ -31,4 +31,5 @@ export const READ_ID = 255;
31
31
  export const READ_EDGE = 252;
32
32
  export const READ_REFERENCES = 253;
33
33
  export const READ_REFERENCE = 254;
34
+ export const READ_AGGREGATION = 250;
34
35
  //# sourceMappingURL=types.js.map
package/dist/src/index.js CHANGED
@@ -53,6 +53,7 @@ export class BasedDb {
53
53
  maxModifySize,
54
54
  hooks: {
55
55
  subscribe(q, onData, onError) {
56
+ let killed = false;
56
57
  let timer;
57
58
  let prevChecksum;
58
59
  let lastLen = 0;
@@ -63,8 +64,16 @@ export class BasedDb {
63
64
  q.reBuildQuery();
64
65
  registerQuery(q);
65
66
  response = undefined;
67
+ timer = setTimeout(get, 0);
68
+ return;
69
+ }
70
+ if (killed) {
71
+ return;
66
72
  }
67
73
  const res = await server.getQueryBuf(q.buffer);
74
+ if (killed) {
75
+ return;
76
+ }
68
77
  if (!response) {
69
78
  response = new BasedQueryResponse(q.id, q.def, res, 0);
70
79
  }
@@ -82,6 +91,7 @@ export class BasedDb {
82
91
  };
83
92
  get();
84
93
  return () => {
94
+ killed = true;
85
95
  clearTimeout(timer);
86
96
  };
87
97
  },
@@ -469,6 +469,10 @@ export class DbServer {
469
469
  console.error('Db is stopped - trying to query', buf.byteLength);
470
470
  return Promise.resolve(new Uint8Array(8));
471
471
  }
472
+ const schemaChecksum = readUint64(buf, buf.byteLength - 8);
473
+ if (schemaChecksum !== this.schema?.hash) {
474
+ return Promise.resolve(new Uint8Array(1));
475
+ }
472
476
  if (this.modifyQueue.length) {
473
477
  return new Promise((resolve) => {
474
478
  this.addToQueryQueue(resolve, buf);
@@ -1,13 +1,12 @@
1
1
  import { isMainThread, receiveMessageOnPort, workerData, } from 'node:worker_threads';
2
2
  import native from '../native.js';
3
- let workerCtx;
4
3
  if (isMainThread) {
5
4
  console.warn('running query worker.ts in mainthread');
6
5
  }
7
6
  else {
8
7
  let { address, channel } = workerData;
9
8
  let dbCtx = native.externalFromInt(address);
10
- workerCtx = native.workerCtxInit();
9
+ native.workerCtxInit();
11
10
  // const transferList = new Array(1)
12
11
  const handleMsg = (msg) => {
13
12
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.44",
3
+ "version": "0.0.46",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",