@based/db 0.0.63 → 0.0.66

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 (76) hide show
  1. package/dist/lib/darwin_aarch64/include/selva/db.h +30 -4
  2. package/dist/lib/darwin_aarch64/include/selva/fields.h +36 -24
  3. package/dist/lib/darwin_aarch64/include/selva/hll.h +5 -3
  4. package/dist/lib/darwin_aarch64/include/selva/membar.h +23 -0
  5. package/dist/lib/darwin_aarch64/include/selva/types.h +1 -1
  6. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +19 -3
  7. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  8. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  9. package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
  10. package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
  11. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  12. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  13. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  14. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  15. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  16. package/dist/lib/linux_aarch64/include/selva/db.h +30 -4
  17. package/dist/lib/linux_aarch64/include/selva/fields.h +36 -24
  18. package/dist/lib/linux_aarch64/include/selva/hll.h +5 -3
  19. package/dist/lib/linux_aarch64/include/selva/membar.h +23 -0
  20. package/dist/lib/linux_aarch64/include/selva/types.h +1 -1
  21. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +19 -3
  22. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  23. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  24. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  25. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  26. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  27. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  28. package/dist/lib/linux_aarch64/libselva.so +0 -0
  29. package/dist/lib/linux_x86_64/include/selva/db.h +30 -4
  30. package/dist/lib/linux_x86_64/include/selva/fields.h +36 -24
  31. package/dist/lib/linux_x86_64/include/selva/hll.h +5 -3
  32. package/dist/lib/linux_x86_64/include/selva/membar.h +23 -0
  33. package/dist/lib/linux_x86_64/include/selva/types.h +1 -1
  34. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +19 -3
  35. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  36. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  37. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  38. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  39. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  40. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  41. package/dist/lib/linux_x86_64/libselva.so +0 -0
  42. package/dist/src/client/flushModify.js +2 -2
  43. package/dist/src/client/modify/create.js +1 -1
  44. package/dist/src/client/modify/delete.js +2 -2
  45. package/dist/src/client/modify/expire.js +1 -1
  46. package/dist/src/client/modify/fixed.js +1 -1
  47. package/dist/src/client/modify/update.js +1 -1
  48. package/dist/src/client/query/BasedDbQuery.d.ts +3 -2
  49. package/dist/src/client/query/BasedDbQuery.js +16 -0
  50. package/dist/src/client/query/aggregates/types.d.ts +2 -1
  51. package/dist/src/client/query/read/read.js +6 -3
  52. package/dist/src/client/query/subscription/index.js +6 -1
  53. package/dist/src/client/query/validation.d.ts +3 -0
  54. package/dist/src/client/query/validation.js +9 -0
  55. package/dist/src/hooks.js +1 -1
  56. package/dist/src/index.d.ts +1 -1
  57. package/dist/src/native.d.ts +5 -2
  58. package/dist/src/native.js +16 -5
  59. package/dist/src/server/DbWorker.d.ts +2 -2
  60. package/dist/src/server/DbWorker.js +3 -1
  61. package/dist/src/server/index.d.ts +1 -0
  62. package/dist/src/server/index.js +2 -1
  63. package/dist/src/server/migrate/index.js +2 -1
  64. package/dist/src/server/migrate/worker.js +3 -0
  65. package/dist/src/server/save.js +5 -5
  66. package/dist/src/server/start.js +2 -2
  67. package/package.json +2 -2
  68. package/dist/lib/darwin_aarch64/include/selva/find.h +0 -47
  69. package/dist/lib/darwin_aarch64/include/selva/queue_r.h +0 -190
  70. package/dist/lib/darwin_aarch64/include/selva/traverse.h +0 -65
  71. package/dist/lib/linux_aarch64/include/selva/find.h +0 -47
  72. package/dist/lib/linux_aarch64/include/selva/queue_r.h +0 -190
  73. package/dist/lib/linux_aarch64/include/selva/traverse.h +0 -65
  74. package/dist/lib/linux_x86_64/include/selva/find.h +0 -47
  75. package/dist/lib/linux_x86_64/include/selva/queue_r.h +0 -190
  76. package/dist/lib/linux_x86_64/include/selva/traverse.h +0 -65
@@ -42,24 +42,24 @@ SELVA_EXPORT
42
42
  int selva_dump_save_common(struct SelvaDb *db, const char *filename) __attribute__((nonnull));
43
43
 
44
44
  /**
45
- * Save a range of nodes from te.
45
+ * Save a nodes block starting from start.
46
46
  */
47
47
  SELVA_EXPORT
48
- int selva_dump_save_range(struct SelvaDb *db, struct SelvaTypeEntry *te, const char *filename, node_id_t start, node_id_t end, selva_hash128_t *range_hash_out) __attribute__((nonnull));
48
+ int selva_dump_save_block(struct SelvaDb *db, struct SelvaTypeEntry *te, const char *filename, node_id_t start, selva_hash128_t *range_hash_out) __attribute__((nonnull));
49
49
 
50
50
  /**
51
51
  * **Usage:**
52
52
  * ```c
53
53
  * struct SelvaDb *db = selva_db_create();
54
54
  * selva_dump_load_common(db, filename_common);
55
- * selva_dump_load_range(db, filename_range_n);
55
+ * selva_dump_load_block(db, filename_range_n);
56
56
  * ```
57
57
  */
58
58
  SELVA_EXPORT
59
59
  int selva_dump_load_common(struct SelvaDb *db, const char *filename, char *errlog_buf, size_t errlog_size) __attribute__((nonnull));
60
60
 
61
61
  SELVA_EXPORT
62
- int selva_dump_load_range(struct SelvaDb *db, const char *filename, char *errlog_buf, size_t errlog_size) __attribute__((nonnull));
62
+ int selva_dump_load_block(struct SelvaDb *db, const char *filename, char *errlog_buf, size_t errlog_size) __attribute__((nonnull));
63
63
 
64
64
  /**
65
65
  * Find a type by type id.
@@ -83,6 +83,32 @@ inline block_id_t selva_get_block_capacity(const struct SelvaTypeEntry *te)
83
83
  ;
84
84
  #endif
85
85
 
86
+ inline block_id_t selva_node_id2block_i3(block_id_t block_capacity, node_id_t node_id)
87
+ {
88
+ assert(node_id > 0);
89
+ return ((node_id - 1) - ((node_id - 1) % block_capacity)) / block_capacity;
90
+ }
91
+
92
+ SELVA_EXPORT
93
+ inline block_id_t selva_node_id2block_i(const struct SelvaTypeBlocks *blocks, node_id_t node_id)
94
+ #ifndef __zig
95
+ {
96
+ return selva_node_id2block_i3(blocks->block_capacity, node_id);
97
+ }
98
+ #else
99
+ ;
100
+ #endif
101
+
102
+ SELVA_EXPORT
103
+ inline block_id_t selva_node_id2block_i2(const struct SelvaTypeEntry *te, node_id_t node_id)
104
+ #ifndef __zig
105
+ {
106
+ return selva_node_id2block_i(te->blocks, node_id);
107
+ }
108
+ #else
109
+ ;
110
+ #endif
111
+
86
112
  /**
87
113
  * Get the node schema for type.
88
114
  */
@@ -46,7 +46,7 @@ struct SelvaNodeReferences {
46
46
  };
47
47
 
48
48
  struct SelvaNodeWeakReference {
49
- /* THe type can be found from the schema. */
49
+ /* The type can be found from the schema. */
50
50
  #if 0
51
51
  node_type_t dst_type;
52
52
  #endif
@@ -99,11 +99,15 @@ __purefn
99
99
  #endif
100
100
  size_t selva_fields_get_data_size(const struct SelvaFieldSchema *fs);
101
101
 
102
+ SELVA_EXPORT
102
103
  #if __has_c_attribute(reproducible)
103
104
  [[reproducible]]
104
105
  #endif
105
106
  void *selva_fields_nfo2p(struct SelvaFields *fields, const struct SelvaFieldInfo *nfo);
106
107
 
108
+ SELVA_EXPORT
109
+ struct SelvaFields *selva_fields_node2fields(struct SelvaNode *node);
110
+
107
111
  void selva_fields_ensure_ref_meta(
108
112
  struct SelvaDb *db,
109
113
  struct SelvaNode *node,
@@ -118,19 +122,7 @@ SELVA_EXPORT
118
122
  int selva_fields_set(
119
123
  struct SelvaNode *node,
120
124
  const struct SelvaFieldSchema *fs,
121
- const void *value, size_t len);
122
-
123
- /**
124
- * Set field value by a `fields` pointer.
125
- * @param fields can be either `node->fields` or any other `fields` structure
126
- * associated with the given node (currently edge).
127
- */
128
- SELVA_EXPORT
129
- int fields_set2(
130
- struct SelvaNode *node,
131
- const struct SelvaFieldSchema *fs,
132
- struct SelvaFields *fields,
133
- const void *value, size_t len);
125
+ const void *value, size_t len) __attribute__((deprecated));
134
126
 
135
127
  SELVA_EXPORT
136
128
  int selva_fields_get_mutable_string(
@@ -163,10 +155,6 @@ struct selva_string *selva_fields_ensure_string2(
163
155
 
164
156
  /**
165
157
  * Set reference to fields.
166
- * @param dirty_nodes returns the nodes that were changed, apart from src and dst.
167
- * [n].id = 0 = nil;
168
- * [0] = the node src was pointing to previously (same type as dst);
169
- * [1] = the node dst was pointing to previously (same type as src).
170
158
  */
171
159
  SELVA_EXPORT
172
160
  int selva_fields_reference_set(
@@ -175,8 +163,9 @@ int selva_fields_reference_set(
175
163
  const struct SelvaFieldSchema *fs_src,
176
164
  struct SelvaNode * restrict dst,
177
165
  struct SelvaNodeReference **ref_out,
178
- node_id_t dirty_nodes[static 2])
179
- __attribute__((access(write_only, 5), access(write_only, 6)));
166
+ selva_dirty_node_cb_t dirty_cb,
167
+ void *dirty_ctx);
168
+ // __attribute__((access(write_only, 5), access(write_only, 6)));
180
169
 
181
170
  /**
182
171
  * @param index 0 = first; -1 = last.
@@ -241,7 +230,7 @@ int selva_fields_set_reference_meta(
241
230
  struct SelvaNode *node,
242
231
  struct SelvaNodeReference *ref,
243
232
  const struct EdgeFieldConstraint *efc,
244
- field_t field,
233
+ const struct SelvaFieldSchema *efs,
245
234
  const void *value, size_t len);
246
235
 
247
236
  SELVA_EXPORT
@@ -267,7 +256,6 @@ SELVA_EXPORT
267
256
  int selva_fields_set_string(
268
257
  struct SelvaNode *node,
269
258
  const struct SelvaFieldSchema *fs,
270
- struct SelvaFieldInfo *nfo,
271
259
  const char *str,
272
260
  size_t len);
273
261
 
@@ -304,6 +292,24 @@ int selva_fields_get_text(
304
292
  const char **str,
305
293
  size_t *len);
306
294
 
295
+ SELVA_EXPORT
296
+ int selva_fields_set_micro_buffer(struct SelvaFields *fields, const struct SelvaFieldSchema *fs, const void *value, size_t len);
297
+
298
+ SELVA_EXPORT
299
+ int selva_fields_set_micro_buffer2(struct SelvaNode *node, const struct SelvaFieldSchema *fs, const void *value, size_t len);
300
+
301
+ SELVA_EXPORT
302
+ int selva_fields_set_weak_reference(struct SelvaNode *node, const struct SelvaFieldSchema *fs, node_id_t dst);
303
+
304
+ SELVA_EXPORT
305
+ int selva_fields_set_weak_reference2(struct SelvaFields *fields, const struct SelvaFieldSchema *fs, node_id_t dst);
306
+
307
+ SELVA_EXPORT
308
+ int selva_fields_set_weak_references2(struct SelvaFields *fields, const struct SelvaFieldSchema *fs, node_id_t dst[], size_t nr_dsts);
309
+
310
+ SELVA_EXPORT
311
+ int selva_fields_set_weak_references(struct SelvaNode *node, const struct SelvaFieldSchema *fs, node_id_t dst[], size_t nr_dsts);
312
+
307
313
  SELVA_EXPORT
308
314
  struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs)
309
315
  __attribute__((nonnull));
@@ -313,11 +319,11 @@ struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, stru
313
319
  __attribute__((nonnull));
314
320
 
315
321
  SELVA_EXPORT
316
- struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaDb *db, struct SelvaFields *fields, field_t field)
322
+ struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaFields *fields, field_t field)
317
323
  __attribute__((nonnull));
318
324
 
319
325
  SELVA_EXPORT
320
- struct SelvaNodeWeakReferences selva_fields_get_weak_references(struct SelvaDb *db, struct SelvaFields *fields, field_t field)
326
+ struct SelvaNodeWeakReferences selva_fields_get_weak_references(struct SelvaFields *fields, field_t field)
321
327
  __attribute__((nonnull));
322
328
 
323
329
  SELVA_EXPORT
@@ -339,6 +345,9 @@ SELVA_EXPORT
339
345
  struct selva_string *selva_fields_get_selva_string(struct SelvaNode *node, const struct SelvaFieldSchema *fs)
340
346
  __attribute__((nonnull));
341
347
 
348
+ SELVA_EXPORT
349
+ struct SelvaFieldInfo *selva_field_get_nfo(struct SelvaFields *fields, const struct SelvaFieldSchema *fs);
350
+
342
351
  SELVA_EXPORT
343
352
  struct SelvaFieldsPointer selva_fields_get_raw2(struct SelvaFields *fields, const struct SelvaFieldSchema *fs)
344
353
  __attribute__((nonnull));
@@ -375,6 +384,9 @@ SELVA_EXPORT
375
384
  void selva_fields_init(const struct SelvaFieldsSchema *schema, struct SelvaFields *fields)
376
385
  __attribute__((nonnull));
377
386
 
387
+ void selva_fields_init_node(struct SelvaTypeEntry *te, struct SelvaNode *node)
388
+ __attribute__((nonnull));
389
+
378
390
  /**
379
391
  * Destroy all fields of a node.
380
392
  */
@@ -17,10 +17,12 @@ void hll_add(struct selva_string *hllss, uint64_t element);
17
17
  SELVA_EXPORT
18
18
  uint8_t *hll_count(struct selva_string *hllss);
19
19
  SELVA_EXPORT
20
- struct selva_string hll_array_union(struct selva_string *hll_array, size_t count);
20
+ void hll_array_union(struct selva_string *res, struct selva_string *hll_array, size_t count);
21
+ SELVA_EXPORT
22
+ void hll_union(char*, size_t, const char*, size_t);
21
23
 
22
24
  // size_t actual_cols[13] = { 79, 159, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 };
23
- // float raw_estimate_data[13][200] =
25
+ // float raw_estimate_data[13][200] =
24
26
  // {
25
27
  // // precision 4
26
28
  // { 11, 11.717, 12.207, 12.7896, 13.2882, 13.8204, 14.3772, 14.9342, 15.5202, 16.161, 16.7722,17.4636, 18.0396, 18.6766, 19.3566, 20.0454, 20.7936, 21.4856, 22.2666, 22.9946, 23.766,24.4692, 25.3638, 26.0764, 26.7864, 27.7602, 28.4814, 29.433, 30.2926, 31.0664, 31.9996,32.7956, 33.5366, 34.5894, 35.5738, 36.2698, 37.3682, 38.0544, 39.2342, 40.0108, 40.7966,41.9298, 42.8704, 43.6358, 44.5194, 45.773, 46.6772, 47.6174, 48.4888, 49.3304, 50.2506,51.4996, 52.3824, 53.3078, 54.3984, 55.5838, 56.6618, 57.2174, 58.3514, 59.0802, 60.1482,61.0376, 62.3598, 62.8078, 63.9744, 64.914, 65.781, 67.1806, 68.0594, 68.8446, 69.7928,70.8248, 71.8324, 72.8598, 73.6246, 74.7014, 75.393, 76.6708, 77.2394, },
@@ -78,4 +80,4 @@ struct selva_string hll_array_union(struct selva_string *hll_array, size_t count
78
80
  // // precision 16
79
81
  // { 47270, 46423.3584, 45585.7074, 44757.152, 43938.8416, 43130.9514, 42330.03, 41540.407,40759.6348, 39988.206, 39226.5144, 38473.2096, 37729.795, 36997.268, 36272.6448, 35558.665,34853.0248, 34157.4472, 33470.5204, 32793.5742, 32127.0194, 31469.4182, 30817.6136,30178.6968, 29546.8908, 28922.8544, 28312.271, 27707.0924, 27114.0326, 26526.692, 25948.6336,25383.7826, 24823.5998, 24272.2974, 23732.2572, 23201.4976, 22674.2796, 22163.6336, 21656.515,21161.7362, 20669.9368, 20189.4424, 19717.3358, 19256.3744, 18795.9638, 18352.197, 17908.5738,17474.391, 17052.918, 16637.2236, 16228.4602, 15823.3474, 15428.6974, 15043.0284, 14667.6278,14297.4588, 13935.2882, 13578.5402, 13234.6032, 12882.1578, 12548.0728, 12219.231, 11898.0072,11587.2626, 11279.9072, 10973.5048, 10678.5186, 10392.4876, 10105.2556, 9825.766, 9562.5444,9294.2222, 9038.2352, 8784.848, 8533.2644, 8301.7776, 8058.30859999999, 7822.94579999999,7599.11319999999, 7366.90779999999, 7161.217, 6957.53080000001, 6736.212, 6548.21220000001,6343.06839999999, 6156.28719999999, 5975.15419999999, 5791.75719999999, 5621.32019999999,5451.66, 5287.61040000001, 5118.09479999999, 4957.288, 4798.4246, 4662.17559999999,4512.05900000001, 4364.68539999999, 4220.77720000001, 4082.67259999999, 3957.19519999999,3842.15779999999, 3699.3328, 3583.01180000001, 3473.8964, 3338.66639999999, 3233.55559999999,3117.799, 3008.111, 2909.69140000001, 2814.86499999999, 2719.46119999999, 2624.742,2532.46979999999, 2444.7886, 2370.1868, 2272.45259999999, 2196.19260000001, 2117.90419999999,2023.2972, 1969.76819999999, 1885.58979999999, 1833.2824, 1733.91200000001, 1682.54920000001,1604.57980000001, 1556.11240000001, 1491.3064, 1421.71960000001, 1371.22899999999, 1322.1324,1264.7892, 1196.23920000001, 1143.8474, 1088.67240000001, 1073.60380000001, 1023.11660000001,959.036400000012, 927.433199999999, 906.792799999996, 853.433599999989, 841.873800000001,791.1054, 756.899999999994, 704.343200000003, 672.495599999995, 622.790399999998,611.254799999995, 567.283200000005, 519.406599999988, 519.188400000014, 495.312800000014,451.350799999986, 443.973399999988, 431.882199999993, 392.027000000002, 380.924200000009,345.128999999986, 298.901400000002, 287.771999999997, 272.625, 247.253000000026,222.490600000019, 223.590000000026, 196.407599999977, 176.425999999978, 134.725199999986,132.4804, 110.445599999977, 86.7939999999944, 56.7038000000175, 64.915399999998,38.3726000000024, 37.1606000000029, 46.170999999973, 49.1716000000015, 15.3362000000197,6.71639999997569,-34.8185999999987,-39.4476000000141, 12.6830000000191,-12.3331999999937,-50.6565999999875,-59.9538000000175,-65.1054000000004,-70.7576000000117,-106.325200000021,-126.852200000023,-110.227599999984,-132.885999999999,-113.897200000007,-142.713800000027,-151.145399999979,-150.799200000009,-177.756200000003,-156.036399999983,-182.735199999996,-177.259399999981,-198.663600000029,-174.577600000019,-193.84580000001, },
80
82
  // };
81
- #endif
83
+ #endif
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright (c) 2025 SAULX
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+ #include "selva/_export.h"
6
+
7
+ /**
8
+ * Read memory barrier.
9
+ * Call this function before read in case another thread has potentially
10
+ * written to the memory addresses the current thread is going to access.
11
+ */
12
+ SELVA_EXPORT
13
+ __attribute__((no_reorder))
14
+ void membar_sync_read(void);
15
+
16
+ /**
17
+ * Write memory barrier.
18
+ * Call this function after write to memory when another thread is expected to
19
+ * read the written data.
20
+ */
21
+ SELVA_EXPORT
22
+ __attribute__((no_reorder))
23
+ void membar_sync_write(void);
@@ -82,7 +82,7 @@ struct SelvaFieldsSchema {
82
82
  size_t len;
83
83
  size_t fixed_data_size;
84
84
  } field_map_template;
85
- struct SelvaFieldSchema field_schemas[] __counted_by(nr_fields);
85
+ struct SelvaFieldSchema field_schemas[255];
86
86
  };
87
87
 
88
88
  struct SelvaNodeSchema {
@@ -1,13 +1,29 @@
1
1
  /*
2
- * Copyright (c) 2024 SAULX
2
+ * Copyright (c) 2024-2025 SAULX
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
  #pragma once
6
6
 
7
7
  #include "selva/_export.h"
8
+ #include "libdeflate.h"
8
9
 
9
10
  SELVA_EXPORT
10
- void worker_ctx_init();
11
+ void worker_ctx_init(void);
11
12
 
12
13
  SELVA_EXPORT
13
- void worker_ctx_deinit();
14
+ void worker_ctx_deinit(void);
15
+
16
+ SELVA_EXPORT
17
+ enum libdeflate_result worker_ctx_libdeflate_decompress(
18
+ const void *in, size_t in_nbytes,
19
+ void *out, size_t out_nbytes_avail,
20
+ size_t *actual_out_nbytes_ret);
21
+
22
+ SELVA_EXPORT
23
+ enum libdeflate_result worker_ctx_libdeflate_decompress_stream(
24
+ const char *in_buf, size_t in_len,
25
+ libdeflate_decompress_stream_cb_t cb, void *ctx,
26
+ int *result);
27
+
28
+ SELVA_EXPORT
29
+ bool worker_ctx_libdeflate_block_state_growbuf(void);
Binary file
@@ -169,12 +169,12 @@ export const startDrain = (db) => {
169
169
  db.isDraining = true;
170
170
  if (db.flushTime === 0) {
171
171
  process.nextTick(() => {
172
- flushBuffer(db);
172
+ void flushBuffer(db);
173
173
  });
174
174
  }
175
175
  else {
176
176
  setTimeout(() => {
177
- flushBuffer(db);
177
+ void flushBuffer(db);
178
178
  }, db.flushTime);
179
179
  }
180
180
  };
@@ -145,7 +145,7 @@ export function create(db, type, obj, opts) {
145
145
  if (pos === 8) {
146
146
  throw new Error('!No range available');
147
147
  }
148
- flushBuffer(db);
148
+ void flushBuffer(db);
149
149
  return db.create(type, obj, opts);
150
150
  }
151
151
  res.error = err;
@@ -15,7 +15,7 @@ export const deleteFn = (db, type, id, opts) => {
15
15
  if (separate) {
16
16
  const size = 11 /* SIZE.DEFAULT_CURSOR */ + 2 + separate.length * 12;
17
17
  if (ctx.len + size > ctx.max) {
18
- flushBuffer(db);
18
+ void flushBuffer(db);
19
19
  return deleteFn(db, type, id);
20
20
  }
21
21
  setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
@@ -28,7 +28,7 @@ export const deleteFn = (db, type, id, opts) => {
28
28
  }
29
29
  else {
30
30
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 2 > ctx.max) {
31
- flushBuffer(db);
31
+ void flushBuffer(db);
32
32
  return deleteFn(db, type, id);
33
33
  }
34
34
  setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
@@ -9,7 +9,7 @@ export function expire(db, type, id, seconds) {
9
9
  }
10
10
  const ctx = db.modifyCtx;
11
11
  if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 > ctx.max) {
12
- flushBuffer(db);
12
+ void flushBuffer(db);
13
13
  return expire(db, type, id, seconds);
14
14
  }
15
15
  setCursor(ctx, def, 0, MICRO_BUFFER, id, EXPIRE);
@@ -98,7 +98,7 @@ map[TIMESTAMP] = (ctx, val, def) => {
98
98
  ctx.len += 8;
99
99
  // Todo use new utils and store as uint64
100
100
  view.setFloat64(0, parsedValue, true);
101
- const ts = view.getFloat64(0);
101
+ // const ts = view.getFloat64(0)
102
102
  };
103
103
  map[UINT32] = (ctx, val, def) => {
104
104
  if (ctx.len + 4 > ctx.max) {
@@ -70,7 +70,7 @@ export const update = (db, type, id, obj, opts) => {
70
70
  if (pos === 8) {
71
71
  throw new Error('out of range');
72
72
  }
73
- flushBuffer(db);
73
+ void flushBuffer(db);
74
74
  return update(db, type, id, obj, opts);
75
75
  }
76
76
  res.error = err;
@@ -1,7 +1,7 @@
1
1
  import { QueryDef, QueryTarget, Operator, QueryByAliasObj } from './query.js';
2
2
  import { BasedQueryResponse } from './BasedIterable.js';
3
3
  import { Search } from './search/index.js';
4
- import { OnData, OnError, OnClose } from './subscription/index.js';
4
+ import { OnData, OnError } from './subscription/index.js';
5
5
  import { DbClient } from '../index.js';
6
6
  import { LangName } from '@based/schema';
7
7
  import { FilterAst, FilterBranchFn, FilterOpts } from './filter/types.js';
@@ -25,6 +25,7 @@ export declare class QueryBranch<T> {
25
25
  groupBy(field: string): T;
26
26
  count(field?: string): T;
27
27
  sum(...fields: (string | string[])[]): T;
28
+ cardinality(...fields: (string | string[])[]): T;
28
29
  or(fn: FilterBranchFn): T;
29
30
  or(field: string, operator?: Operator | boolean, value?: any, opts?: FilterOpts): T;
30
31
  range(start: number, end?: number): T;
@@ -48,6 +49,6 @@ export declare class BasedDbQuery extends QueryBranch<BasedDbQuery> {
48
49
  buffer: Uint8Array;
49
50
  register(): void;
50
51
  locale(locale: LangName): this;
51
- subscribe(onData: OnData, onError?: OnError): OnClose;
52
+ subscribe(onData: OnData, onError?: OnError): import("./subscription/types.js").OnClose;
52
53
  _getSync(dbCtxExternal: any): BasedQueryResponse;
53
54
  }
@@ -173,6 +173,22 @@ export class QueryBranch {
173
173
  // @ts-ignore
174
174
  return this;
175
175
  }
176
+ cardinality(...fields) {
177
+ if (fields.length === 0) {
178
+ throw new Error('Empty cardinality() called');
179
+ }
180
+ if (this.queryCommands) {
181
+ this.queryCommands.push({
182
+ method: 'cardinality',
183
+ args: fields,
184
+ });
185
+ }
186
+ else {
187
+ addAggregate(3 /* AggregateType.CARDINALITY */, this.def, fields);
188
+ }
189
+ // @ts-ignore
190
+ return this;
191
+ }
176
192
  or(field, operator, value, opts) {
177
193
  if (this.queryCommands) {
178
194
  this.queryCommands.push({
@@ -1,6 +1,7 @@
1
1
  export declare const enum AggregateType {
2
2
  SUM = 1,
3
- COUNT = 2
3
+ COUNT = 2,
4
+ CARDINALITY = 3
4
5
  }
5
6
  export declare const enum GroupBy {
6
7
  NONE = 0,
@@ -10,6 +10,7 @@ const readAggregate = (q, result, offset, len) => {
10
10
  let i = offset;
11
11
  while (i < len) {
12
12
  let key = '';
13
+ let keyLen = 0;
13
14
  if (result[i] == 0) {
14
15
  if (q.aggregate.groupBy.default) {
15
16
  key = q.aggregate.groupBy.default;
@@ -17,11 +18,14 @@ const readAggregate = (q, result, offset, len) => {
17
18
  else {
18
19
  key = `$undefined`;
19
20
  }
21
+ i += 2;
20
22
  }
21
23
  else {
22
- key = DECODER.decode(result.subarray(i, i + 2));
24
+ keyLen = readUint16(result, i);
25
+ i += 2;
26
+ key = DECODER.decode(result.subarray(i, i + keyLen));
27
+ i += keyLen;
23
28
  }
24
- i += 2;
25
29
  const resultKey = (results[key] = {});
26
30
  for (const aggregatesArray of q.aggregate.aggregates.values()) {
27
31
  for (const agg of aggregatesArray) {
@@ -414,7 +418,6 @@ export const readAllFields = (q, result, offset, end, item, id) => {
414
418
  handleUndefinedProps(id, q, item);
415
419
  return i - offset;
416
420
  };
417
- let cnt = 0;
418
421
  export const resultToObject = (q, result, end, offset = 0) => {
419
422
  if (q.aggregate) {
420
423
  return readAggregate(q, result, 0, result.byteLength - 4);
@@ -32,7 +32,9 @@ export class SubStore {
32
32
  };
33
33
  let killed = false;
34
34
  if (!q.db.schema) {
35
- q.db.schemaIsSet().then(() => {
35
+ q.db
36
+ .schemaIsSet()
37
+ .then(() => {
36
38
  if (!killed) {
37
39
  try {
38
40
  registerQuery(q);
@@ -42,6 +44,9 @@ export class SubStore {
42
44
  onError(err);
43
45
  }
44
46
  }
47
+ })
48
+ .catch((err) => {
49
+ onError(err);
45
50
  });
46
51
  this.onClose = () => {
47
52
  killed = true;
@@ -31,6 +31,7 @@ export declare const ERR_SEARCH_TYPE = 22;
31
31
  export declare const ERR_SEARCH_INCORRECT_VALUE = 23;
32
32
  export declare const ERR_SORT_LANG = 24;
33
33
  export declare const ERR_AGG_ENOENT = 25;
34
+ export declare const ERR_AGG_TYPE = 26;
34
35
  declare const messages: {
35
36
  1: (p: any) => string;
36
37
  2: (p: any) => string;
@@ -57,6 +58,7 @@ declare const messages: {
57
58
  23: (p: any) => string;
58
59
  24: (p: any) => string;
59
60
  25: (p: any) => string;
61
+ 26: (p: any) => string;
60
62
  };
61
63
  export type ErrorCode = keyof typeof messages;
62
64
  export declare const searchIncorrecQueryValue: (def: QueryDef, payload: any) => void;
@@ -85,4 +87,5 @@ export declare const ERROR_STRING: PropDef;
85
87
  export declare const ERROR_VECTOR: PropDef;
86
88
  export declare const EMPTY_SCHEMA_DEF: SchemaTypeDef;
87
89
  export declare const aggregationFieldDoesNotExist: (def: QueryDef, field: string) => void;
90
+ export declare const aggregationFieldNotNumber: (def: QueryDef, field: string) => void;
88
91
  export {};
@@ -29,6 +29,7 @@ export const ERR_SEARCH_TYPE = 22;
29
29
  export const ERR_SEARCH_INCORRECT_VALUE = 23;
30
30
  export const ERR_SORT_LANG = 24;
31
31
  export const ERR_AGG_ENOENT = 25;
32
+ export const ERR_AGG_TYPE = 26;
32
33
  const messages = {
33
34
  [ERR_TARGET_INVAL_TYPE]: (p) => `Type "${p}" does not exist`,
34
35
  [ERR_TARGET_INVAL_ALIAS]: (p) => {
@@ -61,6 +62,7 @@ const messages = {
61
62
  [ERR_SEARCH_INCORRECT_VALUE]: (p) => `Search: incorrect query on field "${safeStringify(p)}"`,
62
63
  [ERR_SORT_LANG]: (p) => `Sort: invalid lang`,
63
64
  [ERR_AGG_ENOENT]: (p) => `Field \"${p}\" in the aggregate function is invalid or unreacheable.`,
65
+ [ERR_AGG_TYPE]: (p) => `Aggregate: incorrect type "${p.path.join('.')}"`,
64
66
  };
65
67
  export const searchIncorrecQueryValue = (def, payload) => {
66
68
  def.errors.push({ code: ERR_SEARCH_INCORRECT_VALUE, payload });
@@ -451,4 +453,11 @@ export const aggregationFieldDoesNotExist = (def, field) => {
451
453
  });
452
454
  handleErrors(def);
453
455
  };
456
+ export const aggregationFieldNotNumber = (def, field) => {
457
+ def.errors.push({
458
+ code: ERR_AGG_TYPE,
459
+ payload: field,
460
+ });
461
+ handleErrors(def);
462
+ };
454
463
  //# sourceMappingURL=validation.js.map
package/dist/src/hooks.js CHANGED
@@ -25,7 +25,7 @@ export const getDefaultHooks = (server, subInterval = 200) => {
25
25
  }
26
26
  timer = setTimeout(poll, subInterval);
27
27
  };
28
- poll();
28
+ void poll();
29
29
  return () => {
30
30
  clearTimeout(timer);
31
31
  killed = true;
@@ -19,7 +19,7 @@ export * from './client/query/BasedDbQuery.js';
19
19
  export * from './client/query/BasedIterable.js';
20
20
  export * from './server/save.js';
21
21
  export * from './hooks.js';
22
- export { getDefaultHooks };
22
+ export { getDefaultHooks, BasedDbOpts };
23
23
  export declare class BasedDb extends Emitter {
24
24
  client: DbClient;
25
25
  server: DbServer;
@@ -1,4 +1,5 @@
1
1
  declare const native: {
2
+ threadCtx: any;
2
3
  historyAppend(history: any, typeId: number, nodeId: number, dbCtx: any): any;
3
4
  historyCreate(pathname: string, mainLen: number): any;
4
5
  workerCtxInit: () => void;
@@ -9,9 +10,9 @@ declare const native: {
9
10
  start: () => any;
10
11
  stop: (dbCtx: any) => any;
11
12
  saveCommon: (path: string, dbCtx: any) => number;
12
- saveRange: (path: string, typeCode: number, start: number, end: number, dbCtx: any, hashOut: Uint8Array) => number;
13
+ saveBlock: (path: string, typeCode: number, start: number, dbCtx: any, hashOut: Uint8Array) => number;
13
14
  loadCommon: (path: string, dbCtx: any) => void;
14
- loadRange: (path: string, dbCtx: any) => void;
15
+ loadBlock: (path: string, dbCtx: any) => void;
15
16
  updateSchemaType: (prefix: number, buf: Uint8Array, dbCtx: any) => any;
16
17
  getTypeInfo: (typeId: number, dbCtx: any) => any;
17
18
  getNodeRangeHash: (typeId: number, start: number, end: number, bufOut: Uint8Array, dbCtx: any) => any;
@@ -22,5 +23,7 @@ declare const native: {
22
23
  destroySortIndex: (buf: Uint8Array, dbCtx: any) => any;
23
24
  xxHash64: (buf: Uint8Array, target: Uint8Array, index: number) => any;
24
25
  equals: (a: Uint8Array, b: Uint8Array) => boolean;
26
+ membarSyncRead: () => void;
27
+ membarSyncWrite: () => void;
25
28
  };
26
29
  export default native;
@@ -4,14 +4,19 @@ import db from '../../basedDbNative.cjs';
4
4
  const DECODER = new TextDecoder('utf-8');
5
5
  const ENCODER = new TextEncoder();
6
6
  const selvaIoErrlog = new Uint8Array(256);
7
- var compressor = db.createCompressor();
7
+ var compressor = db.createCompressor(); // put on threadCtx
8
8
  var decompressor = db.createDecompressor();
9
9
  function SelvaIoErrlogToString(buf) {
10
10
  let i;
11
11
  let len = (i = buf.indexOf(0)) >= 0 ? i : buf.byteLength;
12
12
  return DECODER.decode(selvaIoErrlog.slice(0, len));
13
13
  }
14
+ // add worker CTX HERE
15
+ // then add it to every function
16
+ // worker should allways be here
17
+ // then add ThreadCtx to modify ctx and query ctx
14
18
  const native = {
19
+ threadCtx: null, // add compressors here as well!
15
20
  historyAppend(history, typeId, nodeId, dbCtx) {
16
21
  return db.historyAppend(history, typeId, nodeId, dbCtx);
17
22
  },
@@ -45,9 +50,9 @@ const native = {
45
50
  const pathBuf = ENCODER.encode(path + '\0');
46
51
  return db.saveCommon(pathBuf, dbCtx);
47
52
  },
48
- saveRange: (path, typeCode, start, end, dbCtx, hashOut) => {
53
+ saveBlock: (path, typeCode, start, dbCtx, hashOut) => {
49
54
  const pathBuf = ENCODER.encode(path + '\0');
50
- return db.saveRange(pathBuf, typeCode, start, end, dbCtx, hashOut);
55
+ return db.saveBlock(pathBuf, typeCode, start, dbCtx, hashOut);
51
56
  },
52
57
  loadCommon: (path, dbCtx) => {
53
58
  const pathBuf = ENCODER.encode(path + '\0');
@@ -56,9 +61,9 @@ const native = {
56
61
  throw new Error(`Failed to load common. selvaError: ${err} cause:\n${SelvaIoErrlogToString(selvaIoErrlog)}`);
57
62
  }
58
63
  },
59
- loadRange: (path, dbCtx) => {
64
+ loadBlock: (path, dbCtx) => {
60
65
  const pathBuf = ENCODER.encode(path + '\0');
61
- const err = db.loadRange(pathBuf, dbCtx, selvaIoErrlog);
66
+ const err = db.loadBlock(pathBuf, dbCtx, selvaIoErrlog);
62
67
  if (err) {
63
68
  throw new Error(`Failed to load a range. selvaError: ${err} cause:\n${SelvaIoErrlogToString(selvaIoErrlog)}`);
64
69
  }
@@ -93,6 +98,12 @@ const native = {
93
98
  equals: (a, b) => {
94
99
  return !!db.equals(a, b);
95
100
  },
101
+ membarSyncRead: () => {
102
+ db.membarSyncRead();
103
+ },
104
+ membarSyncWrite: () => {
105
+ db.membarSyncWrite();
106
+ },
96
107
  };
97
108
  global.__basedDb__native__ = native;
98
109
  export default native;