@based/db 0.0.28 → 0.0.30

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 (115) hide show
  1. package/README.md +1 -399
  2. package/dist/lib/darwin_aarch64/include/selva/db.h +8 -4
  3. package/dist/lib/darwin_aarch64/include/selva/fields.h +10 -2
  4. package/dist/lib/darwin_aarch64/include/selva/selva_hash128.h +17 -7
  5. package/dist/lib/darwin_aarch64/include/selva/sort.h +21 -14
  6. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  7. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  8. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  9. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  10. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  11. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  12. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  13. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  14. package/dist/lib/linux_aarch64/include/selva/db.h +8 -4
  15. package/dist/lib/linux_aarch64/include/selva/fields.h +10 -2
  16. package/dist/lib/linux_aarch64/include/selva/selva_hash128.h +17 -7
  17. package/dist/lib/linux_aarch64/include/selva/sort.h +21 -14
  18. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  19. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  20. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  21. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  22. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  23. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  24. package/dist/lib/linux_aarch64/libselva.so +0 -0
  25. package/dist/lib/linux_x86_64/include/selva/db.h +8 -4
  26. package/dist/lib/linux_x86_64/include/selva/fields.h +10 -2
  27. package/dist/lib/linux_x86_64/include/selva/selva_hash128.h +17 -7
  28. package/dist/lib/linux_x86_64/include/selva/sort.h +21 -14
  29. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  30. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  31. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  32. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  33. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  34. package/dist/lib/linux_x86_64/libselva.so +0 -0
  35. package/dist/src/client/flushModify.d.ts +1 -1
  36. package/dist/src/client/flushModify.js +15 -19
  37. package/dist/src/client/index.d.ts +8 -10
  38. package/dist/src/client/index.js +19 -8
  39. package/dist/src/client/modify/ModifyRes.d.ts +2 -4
  40. package/dist/src/client/modify/ModifyRes.js +15 -21
  41. package/dist/src/client/modify/create.js +1 -2
  42. package/dist/src/client/modify/delete.js +1 -2
  43. package/dist/src/client/modify/fixed.js +43 -8
  44. package/dist/src/client/modify/modify.js +0 -5
  45. package/dist/src/client/modify/references/{appendRefs.d.ts → appendEdgeRefs.d.ts} +1 -1
  46. package/dist/src/client/modify/references/{appendRefs.js → appendEdgeRefs.js} +5 -2
  47. package/dist/src/client/modify/references/edge.js +182 -175
  48. package/dist/src/client/modify/references/reference.js +4 -8
  49. package/dist/src/client/modify/references/references.js +18 -14
  50. package/dist/src/client/modify/setCursor.js +1 -1
  51. package/dist/src/client/modify/string.js +0 -3
  52. package/dist/src/client/modify/text.js +11 -3
  53. package/dist/src/client/modify/types.d.ts +11 -0
  54. package/dist/src/client/modify/types.js +10 -0
  55. package/dist/src/client/modify/update.js +5 -3
  56. package/dist/src/client/modify/vector.js +13 -4
  57. package/dist/src/client/query/BasedDbQuery.d.ts +1 -1
  58. package/dist/src/client/query/BasedDbQuery.js +2 -2
  59. package/dist/src/client/query/BasedIterable.d.ts +1 -1
  60. package/dist/src/client/query/BasedIterable.js +7 -2
  61. package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +2 -1
  62. package/dist/src/client/query/filter/createFixedFilterBuffer.js +11 -28
  63. package/dist/src/client/query/filter/createReferenceFilter.d.ts +2 -1
  64. package/dist/src/client/query/filter/createReferenceFilter.js +10 -9
  65. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -1
  66. package/dist/src/client/query/filter/createVariableFilterBuffer.js +8 -10
  67. package/dist/src/client/query/filter/parseFilterValue.js +1 -1
  68. package/dist/src/client/query/filter/primitiveFilter.js +9 -9
  69. package/dist/src/client/query/filter/toBuffer.js +0 -15
  70. package/dist/src/client/query/filter/types.d.ts +1 -0
  71. package/dist/src/client/query/filter/types.js +1 -0
  72. package/dist/src/client/query/include/walk.js +0 -1
  73. package/dist/src/client/query/read/read.js +16 -7
  74. package/dist/src/client/query/search/index.js +11 -15
  75. package/dist/src/client/query/subscription/index.d.ts +1 -2
  76. package/dist/src/client/query/subscription/index.js +3 -50
  77. package/dist/src/client/query/subscription/markers.js +1 -2
  78. package/dist/src/client/query/subscription/run.js +0 -2
  79. package/dist/src/client/query/subscription/types.d.ts +1 -29
  80. package/dist/src/client/query/subscription/types.js +8 -1
  81. package/dist/src/client/query/thresholds.d.ts +0 -2
  82. package/dist/src/client/query/thresholds.js +0 -2
  83. package/dist/src/client/query/toBuffer.js +16 -42
  84. package/dist/src/client/query/types.d.ts +3 -2
  85. package/dist/src/client/query/validation.d.ts +1 -3
  86. package/dist/src/client/query/validation.js +6 -18
  87. package/dist/src/client/string.d.ts +2 -0
  88. package/dist/src/client/string.js +10 -14
  89. package/dist/src/index.d.ts +1 -1
  90. package/dist/src/index.js +11 -15
  91. package/dist/src/native.d.ts +3 -3
  92. package/dist/src/native.js +6 -4
  93. package/dist/src/server/csmt/draw-dot.js +2 -2
  94. package/dist/src/server/csmt/tree.js +57 -6
  95. package/dist/src/server/csmt/types.d.ts +5 -0
  96. package/dist/src/server/index.d.ts +4 -3
  97. package/dist/src/server/index.js +44 -44
  98. package/dist/src/server/migrate/index.js +47 -29
  99. package/dist/src/server/migrate/worker.js +2 -2
  100. package/dist/src/server/save.js +40 -28
  101. package/dist/src/server/start.js +7 -19
  102. package/dist/src/server/tree.d.ts +2 -0
  103. package/dist/src/server/tree.js +34 -2
  104. package/dist/src/server/worker.js +3 -3
  105. package/dist/src/utils.d.ts +3 -1
  106. package/dist/src/utils.js +43 -19
  107. package/package.json +9 -3
  108. package/dist/lib/darwin_aarch64/include/selva/base64.h +0 -59
  109. package/dist/lib/darwin_aarch64/include/selva/base64url.h +0 -59
  110. package/dist/lib/linux_aarch64/include/selva/base64.h +0 -59
  111. package/dist/lib/linux_aarch64/include/selva/base64url.h +0 -59
  112. package/dist/lib/linux_x86_64/include/selva/base64.h +0 -59
  113. package/dist/lib/linux_x86_64/include/selva/base64url.h +0 -59
  114. package/dist/src/client/timestamp.d.ts +0 -1
  115. package/dist/src/client/timestamp.js +0 -68
@@ -1,6 +1,6 @@
1
1
  import { ModifyError, ModifyState } from '../ModifyRes.js';
2
2
  import { setCursor } from '../setCursor.js';
3
- import { DELETE, RANGE_ERR } from '../types.js';
3
+ import { DELETE, RANGE_ERR, EDGE_NOINDEX_TMPID, NOEDGE_NOINDEX_TMPID, EDGE_NOINDEX_REALID, NOEDGE_INDEX_REALID, } from '../types.js';
4
4
  import { writeEdges } from './edge.js';
5
5
  import { dbUpdateFromUpsert } from './references.js';
6
6
  function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges, isTmpId) {
@@ -11,10 +11,10 @@ function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges, isTmpId) {
11
11
  setCursor(ctx, schema, def.prop, def.typeIndex, parentId, modifyOp);
12
12
  ctx.buf[ctx.len++] = modifyOp;
13
13
  if (isTmpId) {
14
- ctx.buf[ctx.len++] = hasEdges ? 2 : 3;
14
+ ctx.buf[ctx.len++] = hasEdges ? EDGE_NOINDEX_TMPID : NOEDGE_NOINDEX_TMPID;
15
15
  }
16
16
  else {
17
- ctx.buf[ctx.len++] = hasEdges ? 1 : 0;
17
+ ctx.buf[ctx.len++] = hasEdges ? EDGE_NOINDEX_REALID : NOEDGE_INDEX_REALID;
18
18
  }
19
19
  ctx.buf[ctx.len++] = id;
20
20
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -42,7 +42,6 @@ function singleReferenceEdges(ref, ctx, schema, def, parentId, modifyOp) {
42
42
  // TODO SINGLE REF
43
43
  if (id > 0) {
44
44
  if (def.edgesSeperateCnt === 0 && def.edgeMainLen === 0) {
45
- // edgeMainLen, edgesSeperateCnt
46
45
  return writeRef(id, ctx, schema, def, parentId, modifyOp, false, isTmpId);
47
46
  }
48
47
  else {
@@ -50,10 +49,7 @@ function singleReferenceEdges(ref, ctx, schema, def, parentId, modifyOp) {
50
49
  if (err) {
51
50
  return err;
52
51
  }
53
- // def.edgeMainLen
54
- // const edgesLen = getEdgeSize(def, ref)
55
- // edgeMainLen can be done better
56
- // TODO REOMVE - SEEMS REDUNDANT
52
+ // TODO REMOVE - SEEMS REDUNDANT
57
53
  if (ctx.len + 4 > ctx.max) {
58
54
  return RANGE_ERR;
59
55
  }
@@ -1,7 +1,7 @@
1
1
  import { REFERENCES } from '@based/schema/def';
2
2
  import { ModifyError, ModifyState } from '../ModifyRes.js';
3
3
  import { setCursor } from '../setCursor.js';
4
- import { DELETE, RANGE_ERR } from '../types.js';
4
+ import { DELETE, EDGE_INDEX_REALID, EDGE_INDEX_TMPID, EDGE_NOINDEX_REALID, EDGE_NOINDEX_TMPID, NOEDGE_INDEX_REALID, NOEDGE_INDEX_TMPID, NOEDGE_NOINDEX_REALID, NOEDGE_NOINDEX_TMPID, RANGE_ERR, REF_OP_UPDATE, } from '../types.js';
5
5
  import { writeEdges } from './edge.js';
6
6
  export function writeReferences(value, ctx, schema, def, res, mod) {
7
7
  if (typeof value !== 'object') {
@@ -28,13 +28,9 @@ export function writeReferences(value, ctx, schema, def, res, mod) {
28
28
  err = deleteRefs(def, ctx, schema, mod, val, res.tmpId);
29
29
  }
30
30
  else if (key === 'update') {
31
- // and add add: []
32
- // replace this with update
33
31
  err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
34
32
  }
35
33
  else if (key === 'add') {
36
- // and add add: []
37
- // replace this with update
38
34
  err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
39
35
  }
40
36
  else if (key === 'upsert') {
@@ -104,7 +100,6 @@ function updateRefs(def, ctx, schema, mod, refs, parentId, op) {
104
100
  if (nrOrErr) {
105
101
  if (typeof nrOrErr === 'number') {
106
102
  if (nrOrErr === refs.length) {
107
- // reset
108
103
  ctx.len = initpos;
109
104
  }
110
105
  else if (ctx.len + 2 > ctx.max) {
@@ -128,7 +123,8 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
128
123
  let i = refs.length - remaining;
129
124
  let totalpos = ctx.len;
130
125
  ctx.len += 4;
131
- ctx.buf[ctx.len++] = i === 0 ? op : 1; // if it just did a PUT, it should ADD not overwrite the remaining
126
+ // if it just did a PUT, it should ADD not overwrite the remaining
127
+ ctx.buf[ctx.len++] = i === 0 ? op : REF_OP_UPDATE;
132
128
  ctx.buf[ctx.len++] = remaining;
133
129
  ctx.buf[ctx.len++] = remaining >>>= 8;
134
130
  ctx.buf[ctx.len++] = remaining >>>= 8;
@@ -183,12 +179,12 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
183
179
  if (!def.validation(id, def)) {
184
180
  return new ModifyError(def, refs);
185
181
  }
186
- if (hasEdges) {
182
+ if (hasEdges && typeof ref === 'object' && !(ref instanceof ModifyState)) {
187
183
  if (index === undefined) {
188
184
  if (ctx.len + 9 > ctx.max) {
189
185
  return RANGE_ERR;
190
186
  }
191
- ctx.buf[ctx.len++] = isTmpId ? 5 : 1;
187
+ ctx.buf[ctx.len++] = isTmpId ? EDGE_NOINDEX_TMPID : EDGE_NOINDEX_REALID;
192
188
  ctx.buf[ctx.len++] = id;
193
189
  ctx.buf[ctx.len++] = id >>>= 8;
194
190
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -198,7 +194,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
198
194
  if (ctx.len + 13 > ctx.max) {
199
195
  return RANGE_ERR;
200
196
  }
201
- ctx.buf[ctx.len++] = isTmpId ? 6 : 2;
197
+ ctx.buf[ctx.len++] = isTmpId ? EDGE_INDEX_TMPID : EDGE_INDEX_REALID;
202
198
  ctx.buf[ctx.len++] = id;
203
199
  ctx.buf[ctx.len++] = id >>>= 8;
204
200
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -227,7 +223,9 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
227
223
  if (ctx.len + 5 > ctx.max) {
228
224
  return RANGE_ERR;
229
225
  }
230
- ctx.buf[ctx.len++] = isTmpId ? 4 : 0;
226
+ ctx.buf[ctx.len++] = isTmpId
227
+ ? NOEDGE_NOINDEX_TMPID
228
+ : NOEDGE_NOINDEX_REALID;
231
229
  ctx.buf[ctx.len++] = id;
232
230
  ctx.buf[ctx.len++] = id >>>= 8;
233
231
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -237,7 +235,7 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
237
235
  if (ctx.len + 9 > ctx.max) {
238
236
  return RANGE_ERR;
239
237
  }
240
- ctx.buf[ctx.len++] = isTmpId ? 7 : 3;
238
+ ctx.buf[ctx.len++] = isTmpId ? NOEDGE_INDEX_TMPID : NOEDGE_INDEX_REALID;
241
239
  ctx.buf[ctx.len++] = id;
242
240
  ctx.buf[ctx.len++] = id >>>= 8;
243
241
  ctx.buf[ctx.len++] = id >>>= 8;
@@ -260,6 +258,9 @@ function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
260
258
  }
261
259
  function putRefs(def, ctx, modifyOp, refs, op) {
262
260
  let size = refs.length * 4 + 1;
261
+ if (refs.length === 0) {
262
+ return 0;
263
+ }
263
264
  ctx.buf[ctx.len++] = modifyOp;
264
265
  ctx.buf[ctx.len++] = size;
265
266
  ctx.buf[ctx.len++] = size >>>= 8;
@@ -272,7 +273,7 @@ function putRefs(def, ctx, modifyOp, refs, op) {
272
273
  let ref = refs[i];
273
274
  if (typeof ref === 'number') {
274
275
  if (!def.validation(ref, def)) {
275
- break;
276
+ return new ModifyError(def, ref);
276
277
  }
277
278
  else {
278
279
  ctx.buf[ctx.len++] = ref;
@@ -286,9 +287,12 @@ function putRefs(def, ctx, modifyOp, refs, op) {
286
287
  return ref.error;
287
288
  }
288
289
  ref = ref.getId();
289
- if (!def.validation(ref, def)) {
290
+ if (!ref) {
290
291
  break;
291
292
  }
293
+ if (!def.validation(ref, def)) {
294
+ return new ModifyError(def, ref);
295
+ }
292
296
  ctx.buf[ctx.len++] = ref;
293
297
  ctx.buf[ctx.len++] = ref >>>= 8;
294
298
  ctx.buf[ctx.len++] = ref >>>= 8;
@@ -1,5 +1,5 @@
1
1
  import { CREATE, SWITCH_FIELD, SWITCH_ID_CREATE, SWITCH_ID_UPDATE, SWITCH_TYPE, } from './types.js';
2
- export const setCursor = (ctx, schema, field, // PROPDEF
2
+ export const setCursor = (ctx, schema, field, // TODO pass propdef better
3
3
  typeIndex, id, modifyOp, ignoreField) => {
4
4
  const prefix0 = schema.idUint8[0];
5
5
  const prefix1 = schema.idUint8[1];
@@ -25,9 +25,6 @@ export function writeString(lang, value, ctx, def, t, parentId, modifyOp) {
25
25
  ? value.byteLength
26
26
  : ENCODER.encode(value).byteLength + 6;
27
27
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 11 + size > ctx.max) {
28
- // +10 OR +11, the original check was +20 but
29
- // there are 10 addtional bytes in this scope
30
- // 5 compression size
31
28
  return RANGE_ERR;
32
29
  }
33
30
  if (modifyOp === CREATE) {
@@ -1,4 +1,3 @@
1
- import { langCodesMap } from '@based/schema';
2
1
  import { CREATE, UPDATE, RANGE_ERR, DELETE, } from './types.js';
3
2
  import { writeString } from './string.js';
4
3
  import { ModifyError } from './ModifyRes.js';
@@ -14,9 +13,18 @@ export function writeText(value, ctx, def, t, res, parentId, modifyOp) {
14
13
  }
15
14
  return;
16
15
  }
17
- // todo proper fallback as well
18
16
  if ((value && typeof value !== 'object') || value === null) {
19
- const locale = res.locale ?? langCodesMap.get('en'); // TODO: Add def lang option...
17
+ let locale = res.locale;
18
+ if (!locale) {
19
+ // TODO: Add def lang option...
20
+ for (const localeCode of def.seperateTextSort.localeToIndex.keys()) {
21
+ locale = localeCode;
22
+ break;
23
+ }
24
+ }
25
+ if (!def.seperateTextSort.localeToIndex.has(locale)) {
26
+ return new ModifyError(t, locale, 'Invalid locale');
27
+ }
20
28
  if (value == null) {
21
29
  // @ts-ignore
22
30
  value = '';
@@ -26,3 +26,14 @@ export type ModifyOpts = {
26
26
  locale?: LangName;
27
27
  overwrite?: boolean;
28
28
  };
29
+ export declare const NOEDGE_NOINDEX_REALID = 0;
30
+ export declare const EDGE_NOINDEX_REALID = 1;
31
+ export declare const EDGE_INDEX_REALID = 2;
32
+ export declare const NOEDGE_INDEX_REALID = 3;
33
+ export declare const NOEDGE_NOINDEX_TMPID = 4;
34
+ export declare const EDGE_NOINDEX_TMPID = 5;
35
+ export declare const EDGE_INDEX_TMPID = 6;
36
+ export declare const NOEDGE_INDEX_TMPID = 7;
37
+ export declare const REF_OP_UPDATE = 1;
38
+ export declare const REF_OP_OVERWRITE = 0;
39
+ export type REF_OP = typeof REF_OP_OVERWRITE | typeof REF_OP_UPDATE;
@@ -14,4 +14,14 @@ export const SWITCH_TYPE = 2;
14
14
  export const SWITCH_FIELD = 0;
15
15
  export const SWITCH_ID_CREATE = 9;
16
16
  export const SWITCH_ID_UPDATE = 1;
17
+ export const NOEDGE_NOINDEX_REALID = 0;
18
+ export const EDGE_NOINDEX_REALID = 1;
19
+ export const EDGE_INDEX_REALID = 2;
20
+ export const NOEDGE_INDEX_REALID = 3;
21
+ export const NOEDGE_NOINDEX_TMPID = 4;
22
+ export const EDGE_NOINDEX_TMPID = 5;
23
+ export const EDGE_INDEX_TMPID = 6;
24
+ export const NOEDGE_INDEX_TMPID = 7;
25
+ export const REF_OP_UPDATE = 1;
26
+ export const REF_OP_OVERWRITE = 0;
17
27
  //# sourceMappingURL=types.js.map
@@ -1,11 +1,10 @@
1
- import { MICRO_BUFFER } from '@based/schema/def';
1
+ import { MICRO_BUFFER, isValidId, } from '@based/schema/def';
2
2
  import { startDrain, flushBuffer } from '../flushModify.js';
3
3
  import { setCursor } from './setCursor.js';
4
4
  import { modify } from './modify.js';
5
5
  import { ModifyState } from './ModifyRes.js';
6
6
  import { RANGE_ERR, UPDATE } from './types.js';
7
7
  import { appendFixedValue } from './fixed.js';
8
- import { getSubscriptionMarkers } from '../query/subscription/index.js';
9
8
  const appendUpdate = (ctx, def, obj, res, overwrite) => {
10
9
  const err = modify(ctx, res, obj, def, UPDATE, def.tree, overwrite);
11
10
  if (err) {
@@ -57,9 +56,12 @@ export const update = (db, type, id, obj, opts) => {
57
56
  if (!def) {
58
57
  throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
59
58
  }
59
+ if (!isValidId(id)) {
60
+ throw new Error(`Update ${id} is not a valid id`);
61
+ }
60
62
  const ctx = db.modifyCtx;
61
63
  const pos = ctx.len;
62
- const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, false), opts, true);
64
+ const res = new ModifyState(def.id, id, db, opts, true);
63
65
  const err = appendUpdate(ctx, def, obj, res, opts?.overwrite);
64
66
  if (err) {
65
67
  ctx.prefix0 = -1; // force a new cursor
@@ -2,14 +2,23 @@ import { UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
2
  import { ModifyError } from './ModifyRes.js';
3
3
  import { setCursor } from './setCursor.js';
4
4
  function write(value, ctx, fieldSize) {
5
- const size = Math.min(value.byteLength, fieldSize);
6
- let tmp = size;
7
- // 16-bits would be enough but the zig code now expects 32-bits
5
+ let size = Math.min(value.byteLength, fieldSize);
6
+ let padding = 0;
7
+ if (ctx.len % 4 != 0) {
8
+ padding = ctx.len % 4;
9
+ }
10
+ size -= padding;
11
+ let tmp = size + 4;
12
+ // 16-bits would be enough but the zig expects 32-bits
8
13
  ctx.buf[ctx.len++] = tmp;
9
14
  ctx.buf[ctx.len++] = tmp >>>= 8;
10
15
  ctx.buf[ctx.len++] = tmp >>>= 8;
11
16
  ctx.buf[ctx.len++] = tmp >>>= 8;
12
- ctx.buf.set(new Uint8Array(value.buffer).subarray(0, size), ctx.len);
17
+ ctx.buf[ctx.len++] = padding;
18
+ ctx.buf[ctx.len++] = 0;
19
+ ctx.buf[ctx.len++] = 0;
20
+ ctx.buf[ctx.len++] = 0;
21
+ ctx.buf.set(new Uint8Array(value.buffer).subarray(0, size), ctx.len - padding);
13
22
  ctx.len += size;
14
23
  }
15
24
  export function writeVector(value, ctx, schema, t, parentId, modifyOp) {
@@ -28,7 +28,7 @@ export declare class BasedDbReferenceQuery extends QueryBranch<BasedDbReferenceQ
28
28
  declare class GetPromise extends Promise<BasedQueryResponse> {
29
29
  toObject(): Promise<any>;
30
30
  toJSON(): Promise<string>;
31
- inspect(depth?: number): GetPromise;
31
+ inspect(depth?: number, raw?: boolean): GetPromise;
32
32
  }
33
33
  export declare class BasedDbQuery extends QueryBranch<BasedDbQuery> {
34
34
  #private;
@@ -188,8 +188,8 @@ class GetPromise extends Promise {
188
188
  toJSON() {
189
189
  return this.then(resToJSON);
190
190
  }
191
- inspect(depth) {
192
- return this.then((res) => new GetPromise((resolve) => resolve(res.inspect(depth))));
191
+ inspect(depth, raw) {
192
+ return this.then((res) => new GetPromise((resolve) => resolve(res.inspect(depth, raw))));
193
193
  }
194
194
  }
195
195
  export class BasedDbQuery extends QueryBranch {
@@ -15,7 +15,7 @@ export declare class BasedQueryResponse {
15
15
  debug(): this;
16
16
  node(index?: number): any;
17
17
  [Symbol.iterator](): Generator<Partial<Item>, void, unknown>;
18
- inspect(depth?: number): this;
18
+ inspect(depth?: number, raw?: boolean): this;
19
19
  forEach(fn: (item: any, key: number) => void): void;
20
20
  map(fn: (item: any, key: number) => any): any[];
21
21
  get checksum(): number;
@@ -78,8 +78,13 @@ export class BasedQueryResponse {
78
78
  yield item;
79
79
  }
80
80
  }
81
- inspect(depth = 2) {
82
- console.log(this[inspect.custom](depth));
81
+ inspect(depth = 2, raw) {
82
+ if (raw) {
83
+ console.dir(this.toObject(), { depth });
84
+ }
85
+ else {
86
+ console.log(this[inspect.custom](depth));
87
+ }
83
88
  return this;
84
89
  }
85
90
  forEach(fn) {
@@ -1,4 +1,5 @@
1
1
  import { PropDef, PropDefEdge } from '@based/schema/def';
2
2
  import { FilterCtx } from './types.js';
3
+ import { FilterCondition } from '../types.js';
3
4
  export declare const writeFixed: (prop: PropDef | PropDefEdge, buf: Uint8Array, value: any, size: number, offset: number) => void;
4
- export declare const createFixedFilterBuffer: (prop: PropDef | PropDefEdge, size: number, ctx: FilterCtx, value: any, sort: boolean) => Uint8Array;
5
+ export declare const createFixedFilterBuffer: (prop: PropDef | PropDefEdge, size: number, ctx: FilterCtx, value: any, sort: boolean) => FilterCondition;
@@ -1,20 +1,7 @@
1
1
  import { BINARY, STRING, REFERENCES, } from '@based/schema/def';
2
- import { EQUAL, MODE_AND_FIXED, MODE_DEFAULT, MODE_OR_FIXED, } from './types.js';
2
+ import { ALIGNMENT_NOT_SET, EQUAL, MODE_AND_FIXED, MODE_DEFAULT, MODE_OR_FIXED, } from './types.js';
3
3
  import { parseFilterValue } from './parseFilterValue.js';
4
4
  import { ENCODER } from '../../../utils.js';
5
- // -------------------------------------------
6
- // conditions normal
7
- // field, [size 2]
8
- // [or = 0] [size 2] [start 2], [op], value[size]
9
- // -------------------------------------------
10
- // conditions or fixed
11
- // field, [size 2]
12
- // [or = 1] [size 2] [start 2] [op], [repeat 2], value[size] value[size] value[size]
13
- // -------------------------------------------
14
- // conditions or variable
15
- // field, [size 2]
16
- // [or = 2] [size 2] [start 2], [op], [size 2], value[size], [size 2], value[size]
17
- // -------------------------------------------
18
5
  export const writeFixed = (prop, buf, value, size, offset) => {
19
6
  if (prop.typeIndex === BINARY || prop.typeIndex === STRING) {
20
7
  if (typeof value === 'string') {
@@ -32,7 +19,8 @@ export const writeFixed = (prop, buf, value, size, offset) => {
32
19
  buf[offset] = value;
33
20
  }
34
21
  else {
35
- if (size === 8) { // RFE no int64 for u?
22
+ if (size === 8) {
23
+ // RFE no int64 for u? prob important to add...
36
24
  const view = new DataView(buf.buffer, buf.byteOffset);
37
25
  view.setFloat64(offset, value, true);
38
26
  }
@@ -51,17 +39,12 @@ export const writeFixed = (prop, buf, value, size, offset) => {
51
39
  }
52
40
  }
53
41
  };
54
- // Modes
55
- // default = 0,
56
- // orFixed = 1,
57
- // orVar = 2,
58
42
  export const createFixedFilterBuffer = (prop, size, ctx, value, sort) => {
59
- let buf;
60
43
  const start = prop.start;
61
44
  if (Array.isArray(value)) {
62
- // [or = 1] [size 2] [start 2] [op], [repeat 2], value[size] value[size] value[size]
63
45
  const len = value.length;
64
- buf = new Uint8Array(10 + len * size);
46
+ // Add 8 extra bytes for alignment
47
+ const buf = new Uint8Array(18 + len * size);
65
48
  buf[0] = ctx.type;
66
49
  buf[1] =
67
50
  prop.typeIndex === REFERENCES && ctx.operation === EQUAL
@@ -73,14 +56,14 @@ export const createFixedFilterBuffer = (prop, size, ctx, value, sort) => {
73
56
  buf[5] = start;
74
57
  buf[6] = start >>> 8;
75
58
  buf[7] = ctx.operation;
76
- // buf[7] = prop.typeIndex
77
59
  buf[8] = len;
78
60
  buf[9] = len >>> 8;
61
+ buf[10] = ALIGNMENT_NOT_SET;
79
62
  if (sort) {
80
63
  value = new Uint32Array(value.map((v) => parseFilterValue(prop, v)));
81
64
  value.sort();
82
65
  for (let i = 0; i < len; i++) {
83
- const off = 10 + i * size;
66
+ const off = 18 + i * size;
84
67
  const val = value[i];
85
68
  buf[off] = val;
86
69
  buf[off + 1] = val >>> 8;
@@ -90,13 +73,13 @@ export const createFixedFilterBuffer = (prop, size, ctx, value, sort) => {
90
73
  }
91
74
  else {
92
75
  for (let i = 0; i < len; i++) {
93
- writeFixed(prop, buf, parseFilterValue(prop, value[i]), size, 10 + i * size);
76
+ writeFixed(prop, buf, parseFilterValue(prop, value[i]), size, 18 + i * size);
94
77
  }
95
78
  }
79
+ return buf;
96
80
  }
97
81
  else {
98
- // [or = 0] [size 2] [start 2], [op], value[size]
99
- buf = new Uint8Array(8 + size);
82
+ const buf = new Uint8Array(8 + size);
100
83
  buf[0] = ctx.type;
101
84
  buf[1] = MODE_DEFAULT;
102
85
  buf[2] = prop.typeIndex;
@@ -106,7 +89,7 @@ export const createFixedFilterBuffer = (prop, size, ctx, value, sort) => {
106
89
  buf[6] = start >>> 8;
107
90
  buf[7] = ctx.operation;
108
91
  writeFixed(prop, buf, parseFilterValue(prop, value), size, 8);
92
+ return buf;
109
93
  }
110
- return buf;
111
94
  };
112
95
  //# sourceMappingURL=createFixedFilterBuffer.js.map
@@ -1,3 +1,4 @@
1
1
  import { PropDef, PropDefEdge } from '@based/schema/def';
2
2
  import { FilterCtx } from './types.js';
3
- export declare const createReferenceFilter: (prop: PropDef | PropDefEdge, ctx: FilterCtx, value: any) => Uint8Array;
3
+ import { FilterCondition } from '../types.js';
4
+ export declare const createReferenceFilter: (prop: PropDef | PropDefEdge, ctx: FilterCtx, value: any) => FilterCondition;
@@ -1,23 +1,24 @@
1
- import { MODE_REFERENCE } from './types.js';
1
+ import { ALIGNMENT_NOT_SET, MODE_REFERENCE } from './types.js';
2
2
  export const createReferenceFilter = (prop, ctx, value) => {
3
- const len = Array.isArray(value) ? value.length : 1;
4
- const buf = new Uint8Array(11 + len * 8);
3
+ const isArray = Array.isArray(value);
4
+ const len = isArray ? value.length : 1;
5
+ const buf = new Uint8Array(11 + (isArray ? 8 : 0) + len * 4);
5
6
  buf[0] = ctx.type;
6
7
  buf[1] = MODE_REFERENCE;
7
8
  buf[2] = prop.typeIndex;
8
- buf[3] = 8;
9
- buf[4] = 8 >>> 8;
9
+ // size (4)
10
+ buf[3] = 4;
11
+ buf[4] = 0;
10
12
  buf[5] = len;
11
13
  buf[6] = len >>> 8;
12
14
  buf[7] = ctx.operation;
13
- // buf[7] = prop.typeIndex
14
- // REF TYPE (only 1 exists now...)
15
15
  buf[8] = 0;
16
16
  buf[9] = prop.inverseTypeId;
17
17
  buf[10] = prop.inverseTypeId >>> 8;
18
- if (Array.isArray(value)) {
18
+ if (isArray) {
19
+ buf[11] = ALIGNMENT_NOT_SET;
19
20
  for (let i = 0; i < len; i++) {
20
- let off = 11 + i * 8;
21
+ let off = 19 + i * 4;
21
22
  const v = value[i];
22
23
  buf[off++] = v;
23
24
  buf[off++] = v >>> 8;
@@ -1,4 +1,5 @@
1
1
  import { PropDef, PropDefEdge } from '@based/schema/def';
2
2
  import { FilterCtx } from './types.js';
3
3
  import { LangCode } from '@based/schema';
4
- export declare const createVariableFilterBuffer: (value: any, prop: PropDef | PropDefEdge, ctx: FilterCtx, lang: LangCode) => Uint8Array;
4
+ import { FilterCondition } from '../types.js';
5
+ export declare const createVariableFilterBuffer: (value: any, prop: PropDef | PropDefEdge, ctx: FilterCtx, lang: LangCode) => FilterCondition;
@@ -6,7 +6,6 @@ import { ENCODER, concatUint8Arr } from '../../../utils.js';
6
6
  const DEFAULT_SCORE = new Uint8Array(new Float32Array([0.5]).buffer);
7
7
  const parseValue = (value, prop, ctx, lang) => {
8
8
  let val = value;
9
- // TODO should we do .normalize('NFKD') for all strings?
10
9
  if (ctx.operation === HAS_TO_LOWER_CASE && typeof val === 'string') {
11
10
  val = val.toLowerCase();
12
11
  }
@@ -31,7 +30,7 @@ const parseValue = (value, prop, ctx, lang) => {
31
30
  !prop.separate ||
32
31
  ctx.operation !== EQUAL) {
33
32
  if (typeof val === 'string') {
34
- val = ENCODER.encode(val);
33
+ val = ENCODER.encode(val.normalize('NFKD'));
35
34
  }
36
35
  if (prop.typeIndex === TEXT) {
37
36
  const tmp = new Uint8Array(val.byteLength + 1);
@@ -48,7 +47,7 @@ const parseValue = (value, prop, ctx, lang) => {
48
47
  }
49
48
  if (ctx.operation === LIKE && prop.typeIndex !== VECTOR) {
50
49
  const tmp = new Uint8Array(val.byteLength + 1);
51
- tmp.set((val instanceof ArrayBuffer) ? new Uint8Array(val) : val);
50
+ tmp.set(val instanceof ArrayBuffer ? new Uint8Array(val) : val);
52
51
  tmp[tmp.byteLength - 1] = ctx.opts.score ?? 2;
53
52
  val = tmp;
54
53
  }
@@ -57,7 +56,7 @@ const parseValue = (value, prop, ctx, lang) => {
57
56
  export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
58
57
  let mode = MODE_DEFAULT_VAR;
59
58
  let val;
60
- let buf;
59
+ let parsedCondition;
61
60
  if (Array.isArray(value)) {
62
61
  if (ctx.operation !== EQUAL || !prop.separate) {
63
62
  mode = MODE_OR_VAR;
@@ -103,25 +102,24 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
103
102
  v[6] = len >>> 16;
104
103
  v[7] = len >>> 24;
105
104
  v[8] = val[val.length - 1];
106
- buf = writeVarFilter(mode, v, ctx, prop, 0, 0);
105
+ parsedCondition = writeVarFilter(mode, v, ctx, prop, 0, 0);
107
106
  }
108
107
  else {
109
- buf = createFixedFilterBuffer(prop, 8, { operation: EQUAL_CRC32, type: ctx.type, opts: ctx.opts }, val, false);
108
+ parsedCondition = createFixedFilterBuffer(prop, 8, { operation: EQUAL_CRC32, type: ctx.type, opts: ctx.opts }, val, false);
110
109
  }
111
110
  }
112
111
  else {
113
112
  if (val instanceof ArrayBuffer) {
114
113
  val = new Uint8Array(val);
115
114
  }
116
- buf = writeVarFilter(mode, val, ctx, prop, 0, 0);
115
+ parsedCondition = writeVarFilter(mode, val, ctx, prop, 0, 0);
117
116
  }
118
117
  }
119
118
  else {
120
- // RFE is val always an Uint8Array?
121
- buf = writeVarFilter(mode, val, ctx, prop, prop.start, prop.len);
119
+ parsedCondition = writeVarFilter(mode, val, ctx, prop, prop.start, prop.len);
122
120
  }
123
121
  }
124
- return buf;
122
+ return parsedCondition;
125
123
  };
126
124
  function writeVarFilter(mode, val, ctx, prop, start, len) {
127
125
  const size = val.byteLength;
@@ -1,6 +1,6 @@
1
1
  import { TIMESTAMP, ENUM, BOOLEAN, STRING, BINARY, TEXT, } from '@based/schema/def';
2
2
  import { crc32 } from '../../crc32.js';
3
- import { convertToTimestamp } from '../../timestamp.js';
3
+ import { convertToTimestamp } from '@saulx/utils';
4
4
  import { ENCODER } from '../../../utils.js';
5
5
  // -------------------------------------------
6
6
  // conditions normal
@@ -6,10 +6,10 @@ import { createReferenceFilter } from './createReferenceFilter.js';
6
6
  import { validateFilter } from '../validation.js';
7
7
  export const primitiveFilter = (def, prop, filter, conditions, lang) => {
8
8
  if (validateFilter(def, prop, filter)) {
9
- return;
9
+ return 0;
10
10
  }
11
11
  let [, ctx, value] = filter;
12
- let buf;
12
+ let parsedCondition;
13
13
  const fieldIndexChar = prop.prop;
14
14
  const bufferMap = prop.__isEdge ? conditions.edges : conditions.conditions;
15
15
  if (ctx.operation === EXISTS) {
@@ -29,22 +29,22 @@ export const primitiveFilter = (def, prop, filter, conditions, lang) => {
29
29
  }
30
30
  const propSize = REVERSE_SIZE_MAP[prop.typeIndex];
31
31
  if (prop.typeIndex === REFERENCE) {
32
- buf = createReferenceFilter(prop, ctx, value);
32
+ parsedCondition = createReferenceFilter(prop, ctx, value);
33
33
  }
34
34
  else if (prop.typeIndex === REFERENCES) {
35
35
  if (ctx.operation === EQUAL && !isArray) {
36
36
  value = [value];
37
37
  }
38
- buf = createFixedFilterBuffer(prop, 4, ctx, value, !isNumerical(ctx.operation));
38
+ parsedCondition = createFixedFilterBuffer(prop, 4, ctx, value, !isNumerical(ctx.operation));
39
39
  }
40
40
  else if (prop.typeIndex === CARDINALITY) {
41
- buf = createFixedFilterBuffer(prop, 2, ctx, value, false);
41
+ parsedCondition = createFixedFilterBuffer(prop, 2, ctx, value, false);
42
42
  }
43
43
  else if (propSize) {
44
- buf = createFixedFilterBuffer(prop, propSize, ctx, value, false);
44
+ parsedCondition = createFixedFilterBuffer(prop, propSize, ctx, value, false);
45
45
  }
46
46
  else {
47
- buf = createVariableFilterBuffer(value, prop, ctx, lang);
47
+ parsedCondition = createVariableFilterBuffer(value, prop, ctx, lang);
48
48
  }
49
49
  // ADD OR if array for value
50
50
  let arr = bufferMap.get(fieldIndexChar);
@@ -53,8 +53,8 @@ export const primitiveFilter = (def, prop, filter, conditions, lang) => {
53
53
  arr = [];
54
54
  bufferMap.set(fieldIndexChar, arr);
55
55
  }
56
- size += buf.byteLength;
57
- arr.push(buf);
56
+ size += parsedCondition.byteLength;
57
+ arr.push(parsedCondition);
58
58
  return size;
59
59
  };
60
60
  //# sourceMappingURL=primitiveFilter.js.map
@@ -1,19 +1,4 @@
1
1
  import { META_EDGE, META_EXISTS, META_OR_BRANCH, META_REFERENCE, TYPE_DEFAULT, TYPE_NEGATE, } from './types.js';
2
- // or
3
- // [meta = 253] [size 2] [next 4]
4
- // -------------------------------------------
5
- // edge
6
- // [meta = 252] [size 2]
7
- // -------------------------------------------
8
- // ref
9
- // [meta = 254] [field] [typeId 2] [size 2]
10
- // -------------------------------------------
11
- // conditions normal
12
- // -------------------------------------------
13
- // conditions or fixed
14
- // -------------------------------------------
15
- // conditions or variable
16
- // -------------------------------------------
17
2
  const writeConditions = (result, k, offset, conditions) => {
18
3
  let lastWritten = offset;
19
4
  result[lastWritten] = k;
@@ -57,3 +57,4 @@ export declare const VECTOR_EUCLIDEAN_DIST = 3;
57
57
  export declare const getVectorFn: (optsFn?: FilterOpts["fn"]) => 0 | 1 | 2 | 3;
58
58
  export declare const toFilterCtx: (def: QueryDef, op: Operator, opts?: FilterOpts) => FilterCtx;
59
59
  export declare const operatorReverseMap: Record<OPERATOR, string>;
60
+ export declare const ALIGNMENT_NOT_SET = 255;
@@ -142,4 +142,5 @@ export const operatorReverseMap = {
142
142
  [EQUAL_CRC32]: '= (crc32)',
143
143
  [EXISTS]: 'exists',
144
144
  };
145
+ export const ALIGNMENT_NOT_SET = 255;
145
146
  //# sourceMappingURL=types.js.map