@based/db 0.0.19 → 0.0.21

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 (103) hide show
  1. package/basedDbNative.cjs +6 -2
  2. package/dist/lib/darwin_aarch64/include/selva/db.h +92 -9
  3. package/dist/lib/darwin_aarch64/include/selva/fast_memcmp.h +18 -0
  4. package/dist/lib/darwin_aarch64/include/selva/fields.h +31 -32
  5. package/dist/lib/darwin_aarch64/include/selva/types.h +0 -10
  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 +92 -9
  15. package/dist/lib/linux_aarch64/include/selva/fast_memcmp.h +18 -0
  16. package/dist/lib/linux_aarch64/include/selva/fields.h +31 -32
  17. package/dist/lib/linux_aarch64/include/selva/types.h +0 -10
  18. package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
  19. package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
  20. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  21. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  22. package/dist/lib/linux_aarch64/libselva.so +0 -0
  23. package/dist/lib/linux_x86_64/include/selva/db.h +92 -9
  24. package/dist/lib/linux_x86_64/include/selva/fast_memcmp.h +18 -0
  25. package/dist/lib/linux_x86_64/include/selva/fields.h +31 -32
  26. package/dist/lib/linux_x86_64/include/selva/types.h +0 -10
  27. package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
  28. package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
  29. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  30. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  31. package/dist/lib/linux_x86_64/libselva.so +0 -0
  32. package/dist/src/client/bitWise.js +0 -1
  33. package/dist/src/client/crc32.d.ts +1 -1
  34. package/dist/src/client/modify/alias.js +3 -3
  35. package/dist/src/client/modify/binary.d.ts +1 -1
  36. package/dist/src/client/modify/binary.js +7 -6
  37. package/dist/src/client/modify/cardinality.d.ts +3 -1
  38. package/dist/src/client/modify/cardinality.js +8 -4
  39. package/dist/src/client/modify/create.js +41 -5
  40. package/dist/src/client/modify/delete.js +9 -8
  41. package/dist/src/client/modify/expire.js +1 -1
  42. package/dist/src/client/modify/modify.js +3 -3
  43. package/dist/src/client/modify/references/edge.js +15 -1
  44. package/dist/src/client/modify/setCursor.js +0 -2
  45. package/dist/src/client/modify/string.d.ts +1 -1
  46. package/dist/src/client/modify/string.js +5 -3
  47. package/dist/src/client/modify/text.d.ts +1 -1
  48. package/dist/src/client/modify/text.js +52 -9
  49. package/dist/src/client/modify/types.d.ts +5 -1
  50. package/dist/src/client/modify/types.js +2 -1
  51. package/dist/src/client/modify/vector.js +4 -4
  52. package/dist/src/client/query/filter/convertFilter.js +3 -1
  53. package/dist/src/client/query/filter/createFixedFilterBuffer.js +1 -1
  54. package/dist/src/client/query/filter/createVariableFilterBuffer.js +10 -4
  55. package/dist/src/client/query/filter/parseFilterValue.js +13 -4
  56. package/dist/src/client/query/filter/primitiveFilter.js +13 -3
  57. package/dist/src/client/query/filter/toBuffer.js +13 -3
  58. package/dist/src/client/query/filter/types.d.ts +5 -3
  59. package/dist/src/client/query/filter/types.js +12 -0
  60. package/dist/src/client/query/read/read.js +7 -1
  61. package/dist/src/client/query/search/index.js +25 -19
  62. package/dist/src/client/query/serialize.d.ts +4 -0
  63. package/dist/src/client/query/serialize.js +26 -0
  64. package/dist/src/client/query/sort.d.ts +1 -1
  65. package/dist/src/client/query/sort.js +5 -5
  66. package/dist/src/client/query/subscription/run.js +1 -1
  67. package/dist/src/client/query/types.d.ts +6 -2
  68. package/dist/src/client/query/validation.js +14 -32
  69. package/dist/src/client/string.d.ts +2 -2
  70. package/dist/src/client/string.js +32 -29
  71. package/dist/src/client/tmpBuffer.d.ts +3 -0
  72. package/dist/src/client/tmpBuffer.js +20 -0
  73. package/dist/src/client/xxHash64.d.ts +1 -1
  74. package/dist/src/client/xxHash64.js +1 -1
  75. package/dist/src/index.d.ts +2 -0
  76. package/dist/src/index.js +2 -0
  77. package/dist/src/native.d.ts +14 -13
  78. package/dist/src/native.js +41 -25
  79. package/dist/src/server/csmt/draw-dot.js +2 -1
  80. package/dist/src/server/csmt/memebership-proof.d.ts +1 -1
  81. package/dist/src/server/csmt/tree.d.ts +2 -1
  82. package/dist/src/server/csmt/tree.js +10 -9
  83. package/dist/src/server/csmt/types.d.ts +4 -3
  84. package/dist/src/server/index.d.ts +5 -4
  85. package/dist/src/server/index.js +5 -6
  86. package/dist/src/server/save.js +4 -3
  87. package/dist/src/server/start.d.ts +1 -0
  88. package/dist/src/server/start.js +43 -16
  89. package/dist/src/server/tree.d.ts +1 -1
  90. package/dist/src/server/tree.js +17 -2
  91. package/dist/src/server/worker.js +2 -1
  92. package/dist/src/utils.d.ts +4 -0
  93. package/dist/src/utils.js +84 -0
  94. package/package.json +4 -4
  95. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  96. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  97. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  98. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  99. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  100. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  101. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  102. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  103. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
Binary file
@@ -31,7 +31,7 @@ void selva_db_destroy(struct SelvaDb *db) __attribute__((nonnull));
31
31
  * @param type must not exist before.
32
32
  */
33
33
  SELVA_EXPORT
34
- int selva_db_schema_create(struct SelvaDb *db, node_type_t type, const char *schema_buf, size_t schema_len) __attribute__((nonnull));
34
+ int selva_db_create_type(struct SelvaDb *db, node_type_t type, const char *schema_buf, size_t schema_len) __attribute__((nonnull));
35
35
 
36
36
  /**
37
37
  * Save the common/shared data of the database.
@@ -54,10 +54,10 @@ int selva_dump_save_range(struct SelvaDb *db, struct SelvaTypeEntry *te, const c
54
54
  * ```
55
55
  */
56
56
  SELVA_EXPORT
57
- int selva_dump_load_common(struct SelvaDb *db, const char *filename) __attribute__((nonnull));
57
+ int selva_dump_load_common(struct SelvaDb *db, const char *filename, char *errlog_buf, size_t errlog_size) __attribute__((nonnull));
58
58
 
59
59
  SELVA_EXPORT
60
- int selva_dump_load_range(struct SelvaDb *db, const char *filename) __attribute__((nonnull));
60
+ int selva_dump_load_range(struct SelvaDb *db, const char *filename, char *errlog_buf, size_t errlog_size) __attribute__((nonnull));
61
61
 
62
62
  /**
63
63
  * Find a type by type id.
@@ -75,28 +75,92 @@ struct SelvaTypeEntry *selva_get_type_by_node(const struct SelvaDb *db, struct S
75
75
  * Get the node schema for type.
76
76
  */
77
77
  SELVA_EXPORT
78
- const struct SelvaNodeSchema *selva_get_ns_by_te(const struct SelvaTypeEntry *te) __attribute__((nonnull, pure));
78
+ __attribute__((nonnull, pure))
79
+ inline const struct SelvaNodeSchema *selva_get_ns_by_te(const struct SelvaTypeEntry *te)
80
+ #if !__zig
81
+ {
82
+ return &te->ns;
83
+ }
84
+ #else
85
+ ;
86
+ #endif
79
87
 
80
88
  SELVA_EXPORT
81
- const struct SelvaFieldSchema *get_fs_by_fields_schema_field(const struct SelvaFieldsSchema *fields_schema, field_t field) __attribute__((nonnull, pure));
89
+ __attribute__((nonnull, pure))
90
+ inline const struct SelvaFieldSchema *get_fs_by_fields_schema_field(const struct SelvaFieldsSchema *fields_schema, field_t field)
91
+ #if !__zig
92
+ {
93
+ if (field >= fields_schema->nr_fields) {
94
+ return nullptr;
95
+ }
96
+
97
+ return &fields_schema->field_schemas[field];
98
+ }
99
+ #else
100
+ ;
101
+ #endif
82
102
 
83
103
  /**
84
104
  * Get the field schema for field.
85
105
  */
86
106
  SELVA_EXPORT
87
- const struct SelvaFieldSchema *selva_get_fs_by_ns_field(const struct SelvaNodeSchema *ns, field_t field) __attribute__((nonnull, pure));
107
+ __attribute__((nonnull, pure))
108
+ inline const struct SelvaFieldSchema *selva_get_fs_by_te_field(const struct SelvaTypeEntry *te, field_t field)
109
+ #if !__zig
110
+ {
111
+ return get_fs_by_fields_schema_field(&te->ns.fields_schema, field);
112
+ }
113
+ #else
114
+ ;
115
+ #endif
88
116
 
89
117
  /**
90
118
  * Get the field schema for field.
91
119
  */
92
120
  SELVA_EXPORT
93
- const struct SelvaFieldSchema *selva_get_fs_by_node(struct SelvaDb *db, struct SelvaNode *node, field_t field) __attribute__((nonnull, pure));
121
+ __attribute__((nonnull, pure))
122
+ inline const struct SelvaFieldSchema *selva_get_fs_by_ns_field(const struct SelvaNodeSchema *ns, field_t field)
123
+ #if !__zig
124
+ {
125
+ return get_fs_by_fields_schema_field(&ns->fields_schema, field);
126
+ }
127
+ #else
128
+ ;
129
+ #endif
130
+
131
+ /**
132
+ * Get the field schema for field.
133
+ */
134
+ SELVA_EXPORT
135
+ __attribute__((nonnull, pure))
136
+ inline const struct SelvaFieldSchema *selva_get_fs_by_node(struct SelvaDb *db, struct SelvaNode *node, field_t field)
137
+ #if !__zig
138
+ {
139
+ struct SelvaTypeEntry *type;
140
+
141
+ type = selva_get_type_by_node(db, node);
142
+ if (!type) {
143
+ return nullptr;
144
+ }
145
+
146
+ return selva_get_fs_by_ns_field(&type->ns, field);
147
+ }
148
+ #else
149
+ ;
150
+ #endif
94
151
 
95
152
  SELVA_EXPORT
96
153
  #if __has_c_attribute(reproducible)
97
154
  [[reproducible]]
98
155
  #endif
99
- enum SelvaFieldType selva_get_fs_type(const struct SelvaFieldSchema *fs);
156
+ inline enum SelvaFieldType selva_get_fs_type(const struct SelvaFieldSchema *fs)
157
+ #if !__zig
158
+ {
159
+ return fs->type;
160
+ }
161
+ #else
162
+ ;
163
+ #endif
100
164
 
101
165
  /**
102
166
  * Get the EdgeFieldConstraint from a ref field schema.
@@ -105,7 +169,17 @@ enum SelvaFieldType selva_get_fs_type(const struct SelvaFieldSchema *fs);
105
169
  * struct SelvaFieldSchema *dst_fs = selva_get_fs_by_node(db, dst, efc->inverse_field);
106
170
  */
107
171
  SELVA_EXPORT
108
- const struct EdgeFieldConstraint *selva_get_edge_field_constraint(const struct SelvaFieldSchema *fs) __attribute__((nonnull));
172
+ __attribute__((nonnull))
173
+ inline const struct EdgeFieldConstraint *selva_get_edge_field_constraint(const struct SelvaFieldSchema *fs)
174
+ #if !__zig
175
+ {
176
+ return (fs->type == SELVA_FIELD_TYPE_REFERENCE || fs->type == SELVA_FIELD_TYPE_REFERENCES)
177
+ ? &fs->edge_constraint
178
+ : nullptr;
179
+ }
180
+ #else
181
+ ;
182
+ #endif
109
183
 
110
184
  SELVA_EXPORT
111
185
  const struct SelvaFieldsSchema *selva_get_edge_field_fields_schema(struct SelvaDb *db, const struct EdgeFieldConstraint *efc);
@@ -173,6 +247,11 @@ struct SelvaNode *selva_prev_node(struct SelvaTypeEntry *type, struct SelvaNode
173
247
  SELVA_EXPORT
174
248
  struct SelvaNode *selva_next_node(struct SelvaTypeEntry *type, struct SelvaNode *node) __attribute__((nonnull));
175
249
 
250
+ /**
251
+ * \addtogroup db_cursor
252
+ * @{
253
+ */
254
+
176
255
  /**
177
256
  * Create a new cursor pointing to node.
178
257
  * If the node is deleted later then the cursor is updated to point to the next
@@ -205,6 +284,10 @@ void selva_cursor_del(struct SelvaTypeEntry *type, cursor_id_t id) __attribute__
205
284
  SELVA_EXPORT
206
285
  size_t selva_cursor_count(const struct SelvaTypeEntry *type) __attribute__((nonnull));
207
286
 
287
+ /**
288
+ * @}
289
+ */
290
+
208
291
  /**
209
292
  * Total count of nodes of type.
210
293
  */
@@ -0,0 +1,18 @@
1
+ /*
2
+ * Copyright (c) 2025 SAULX
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+ #pragma once
6
+
7
+ #include <stddef.h>
8
+ #include "selva/_export.h"
9
+
10
+ /**
11
+ * Fast buffer equals comparison.
12
+ * @param a is an array of length len.
13
+ * @param b is an array of length len.
14
+ * @param len length of a and b in bytes, must be greater than 0.
15
+ * @returns true if a and b contains the same byte sequence; Otherwise false.
16
+ */
17
+ SELVA_EXPORT
18
+ bool fast_memcmp(const void *restrict a, const void *restrict b, size_t len);
@@ -58,30 +58,21 @@ struct SelvaNodeWeakReferences {
58
58
  struct SelvaNodeWeakReference *refs __pcounted_by(nr_refs);
59
59
  };
60
60
 
61
- struct SelvaFieldsAny {
62
- enum SelvaFieldType type; /*!< Type of the value. */
63
- union {
64
- bool boolean; /*!< SELVA_FIELD_TYPE_BOOLEAN */
65
- double number; /*!< SELVA_FIELD_TYPE_NUMBER */
66
- int64_t timestamp; /*!< SELVA_FIELD_TYPE_TIMESTAMP, should fit time_t */
67
- struct selva_string *string; /*!< SELVA_FIELD_TYPE_STRING */
68
- int8_t int8; /* SELVA_FIELD_TYPE_INT8 */
69
- uint8_t uint8; /*!< SELVA_FIELD_TYPE_UINT8 */
70
- int16_t int16; /*!< SELVA_FIELD_TYPE_INT16 */
71
- uint16_t uint16; /*!< SELVA_FIELD_TYPE_UINT16 */
72
- int32_t int32; /*!< SELVA_FIELD_TYPE_INT32 */
73
- uint32_t uint32; /*!< SELVA_FIELD_TYPE_UINT32 */
74
- int64_t int64; /* SELVA_FIELD_TYPE_INT64 */
75
- uint64_t uint64; /*!< SELVA_FIELD_TYPE_UINT64 */
76
- uint8_t enu; /*!< SELVA_FIELD_TYPE_ENUM */
77
- #if 0
78
- struct SelvaTextField *text; /*!< SELVA_FIELD_TYPE_TEXT */
79
- struct SelvaNodeReference *reference; /*!< SELVA_FIELD_TYPE_REFERENCE */
80
- struct SelvaNodeReferences *references; /*!< SELVA_FIELD_TYPE_REFERENCES */
81
- struct SelvaNodeWeakReference weak_reference; /*!< SELVA_FIELD_TYPE_WEAK_REFERENCE */
82
- struct SelvaNodeWeakReferences weak_references; /*!< SELVA_FIELD_TYPE_WEAK_REFERENCES */
83
- #endif
84
- };
61
+ /**
62
+ * Type helper to determine the size of statically (constant/fixed) sized fields.
63
+ */
64
+ union SelvaStaticFields {
65
+ bool boolean; /*!< SELVA_FIELD_TYPE_BOOLEAN */
66
+ double number; /*!< SELVA_FIELD_TYPE_NUMBER */
67
+ struct selva_string *string; /*!< SELVA_FIELD_TYPE_STRING */
68
+ int8_t int8; /* SELVA_FIELD_TYPE_INT8 */
69
+ uint8_t uint8; /*!< SELVA_FIELD_TYPE_UINT8 */
70
+ int16_t int16; /*!< SELVA_FIELD_TYPE_INT16 */
71
+ uint16_t uint16; /*!< SELVA_FIELD_TYPE_UINT16 */
72
+ int32_t int32; /*!< SELVA_FIELD_TYPE_INT32 */
73
+ uint32_t uint32; /*!< SELVA_FIELD_TYPE_UINT32 */
74
+ uint8_t enu; /*!< SELVA_FIELD_TYPE_ENUM */
75
+ struct SelvaNodeWeakReference weak_reference; /*!< SELVA_FIELD_TYPE_WEAK_REFERENCE */
85
76
  };
86
77
 
87
78
  struct SelvaFieldsPointer {
@@ -121,7 +112,6 @@ int selva_fields_get_mutable_string(
121
112
  __attribute__((access(write_only, 4)));
122
113
 
123
114
  /*
124
- * TODO prefix with selva_
125
115
  * TODO Document diff to get_mutable_string
126
116
  */
127
117
  SELVA_EXPORT
@@ -130,6 +120,15 @@ struct selva_string *selva_fields_ensure_string(
130
120
  const struct SelvaFieldSchema *fs,
131
121
  size_t initial_len);
132
122
 
123
+ SELVA_EXPORT
124
+ struct selva_string *selva_fields_ensure_string2(
125
+ struct SelvaDb *db,
126
+ struct SelvaNode *node,
127
+ const struct EdgeFieldConstraint *efc,
128
+ struct SelvaNodeReference *ref,
129
+ const struct SelvaFieldSchema *fs,
130
+ size_t initial_len);
131
+
133
132
  SELVA_EXPORT
134
133
  int selva_fields_reference_set(
135
134
  struct SelvaDb *db,
@@ -267,16 +266,16 @@ int selva_fields_get_text(
267
266
  size_t *len);
268
267
 
269
268
  SELVA_EXPORT
270
- struct SelvaNodeReference *selva_fields_get_reference(struct SelvaNode *node, field_t field);
269
+ struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, field_t field);
271
270
 
272
271
  SELVA_EXPORT
273
- struct SelvaNodeReferences *selva_fields_get_references(struct SelvaNode *node, field_t field);
272
+ struct SelvaNodeReferences *selva_fields_get_references(struct SelvaDb *db, struct SelvaNode *node, field_t field);
274
273
 
275
274
  SELVA_EXPORT
276
- struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaFields *fields, field_t field);
275
+ struct SelvaNodeWeakReference selva_fields_get_weak_reference(struct SelvaDb *db, struct SelvaFields *fields, field_t field);
277
276
 
278
277
  SELVA_EXPORT
279
- struct SelvaNodeWeakReferences selva_fields_get_weak_references(struct SelvaFields *fields, field_t field);
278
+ struct SelvaNodeWeakReferences selva_fields_get_weak_references(struct SelvaDb *db, struct SelvaFields *fields, field_t field);
280
279
 
281
280
  SELVA_EXPORT
282
281
  struct SelvaNode *selva_fields_resolve_weak_reference(
@@ -284,6 +283,9 @@ struct SelvaNode *selva_fields_resolve_weak_reference(
284
283
  const struct SelvaFieldSchema *fs,
285
284
  const struct SelvaNodeWeakReference *weak_ref);
286
285
 
286
+ SELVA_EXPORT
287
+ struct selva_string *selva_fields_get_selva_string3(struct SelvaNodeReference *ref, const struct SelvaFieldSchema *fs);
288
+
287
289
  SELVA_EXPORT
288
290
  struct selva_string *selva_fields_get_selva_string2(struct SelvaFields *fields, const struct SelvaFieldSchema *fs);
289
291
 
@@ -326,9 +328,6 @@ void selva_fields_init(const struct SelvaFieldsSchema *schema, struct SelvaField
326
328
  SELVA_EXPORT
327
329
  void selva_fields_destroy(struct SelvaDb *db, struct SelvaNode *node);
328
330
 
329
- SELVA_EXPORT
330
- int selva_fields_get_text_crc(const struct SelvaNode *node, const struct SelvaFieldSchema *fs, enum selva_lang_code lang, uint32_t *crc);
331
-
332
331
  SELVA_EXPORT
333
332
  void selva_fields_hash_update(struct XXH3_state_s *hash_state, struct SelvaDb *db, const struct SelvaFieldsSchema *schema, const struct SelvaFields *fields);
334
333
 
@@ -23,23 +23,13 @@ struct SelvaFieldsSchema;
23
23
 
24
24
  enum SelvaFieldType {
25
25
  SELVA_FIELD_TYPE_NULL = 0,
26
- SELVA_FIELD_TYPE_TIMESTAMP = 1,
27
- #if 0
28
- SELVA_FIELD_TYPE_SPARE1 = 2,
29
- SELVA_FIELD_TYPE_SPARE2 = 3,
30
- #endif
31
26
  SELVA_FIELD_TYPE_NUMBER = 4,
32
- #if 0
33
- SELVA_FIELD_TYPE_SPARE3 = 5,
34
- #endif
35
27
  SELVA_FIELD_TYPE_INT8 = 20,
36
28
  SELVA_FIELD_TYPE_UINT8 = 6,
37
29
  SELVA_FIELD_TYPE_INT16 = 21,
38
30
  SELVA_FIELD_TYPE_UINT16 = 22,
39
31
  SELVA_FIELD_TYPE_INT32 = 23,
40
32
  SELVA_FIELD_TYPE_UINT32 = 7,
41
- SELVA_FIELD_TYPE_INT64 = 24,
42
- SELVA_FIELD_TYPE_UINT64 = 8,
43
33
  SELVA_FIELD_TYPE_BOOLEAN = 9,
44
34
  SELVA_FIELD_TYPE_ENUM = 10,
45
35
  SELVA_FIELD_TYPE_STRING = 11,
Binary file
@@ -79,5 +79,4 @@ export const concatUint8Arr = (arrs) => {
79
79
  });
80
80
  return result;
81
81
  };
82
- const textDecoder = new TextDecoder('utf-8');
83
82
  //# sourceMappingURL=bitWise.js.map
@@ -1 +1 @@
1
- export declare const crc32: (buf: Buffer) => any;
1
+ export declare const crc32: (buf: Uint8Array) => any;
@@ -5,7 +5,7 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
5
5
  if (typeof value === 'string') {
6
6
  if (value.length === 0) {
7
7
  if (modifyOp === UPDATE) {
8
- if (ctx.len + 11 > ctx.max) {
8
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ > ctx.max) {
9
9
  return RANGE_ERR;
10
10
  }
11
11
  setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
@@ -14,7 +14,7 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
14
14
  }
15
15
  else {
16
16
  let size = Buffer.byteLength(value, 'utf8');
17
- if (ctx.len + 15 + size > ctx.max) {
17
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + size > ctx.max) {
18
18
  // 5 compression size
19
19
  return RANGE_ERR;
20
20
  }
@@ -33,7 +33,7 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
33
33
  }
34
34
  else if (value === null) {
35
35
  if (modifyOp === UPDATE) {
36
- if (ctx.len + 11 > ctx.max) {
36
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
37
37
  return RANGE_ERR;
38
38
  }
39
39
  setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
@@ -1,6 +1,6 @@
1
1
  import { ModifyCtx } from '../../index.js';
2
2
  import { PropDef, SchemaTypeDef } from '@based/schema/def';
3
3
  import { ModifyOp, ModifyErr } from './types.js';
4
- export declare function getBuffer(value: any): Buffer;
4
+ export declare function getBuffer(value: any): Uint8Array | undefined;
5
5
  export declare function writeBinaryRaw(value: Buffer, ctx: ModifyCtx): void;
6
6
  export declare function writeBinary(value: any, ctx: ModifyCtx, schema: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,18 +1,19 @@
1
- import { UPDATE, RANGE_ERR, DELETE } from './types.js';
1
+ import { UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
2
  import { ModifyError } from './ModifyRes.js';
3
3
  import { setCursor } from './setCursor.js';
4
4
  import native from '../../native.js';
5
+ const ENCODER = new TextEncoder();
5
6
  export function getBuffer(value) {
6
7
  if (typeof value === 'object') {
7
- if (value instanceof Buffer) {
8
+ if (value instanceof Uint8Array) {
8
9
  return value;
9
10
  }
10
11
  if (value.buffer instanceof ArrayBuffer) {
11
- return Buffer.from(value.buffer);
12
+ return new Uint8Array(value.buffer, 0, value.byteLength);
12
13
  }
13
14
  }
14
15
  else if (typeof value === 'string') {
15
- return Buffer.from(value);
16
+ return ENCODER.encode(value);
16
17
  }
17
18
  }
18
19
  export function writeBinaryRaw(value, ctx) {
@@ -45,7 +46,7 @@ export function writeBinary(value, ctx, schema, t, parentId, modifyOp) {
45
46
  }
46
47
  if (size === 0) {
47
48
  if (modifyOp === UPDATE) {
48
- if (ctx.len + 11 > ctx.max) {
49
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
49
50
  return RANGE_ERR;
50
51
  }
51
52
  setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
@@ -53,7 +54,7 @@ export function writeBinary(value, ctx, schema, t, parentId, modifyOp) {
53
54
  }
54
55
  }
55
56
  else {
56
- if (ctx.len + 15 + size > ctx.max) {
57
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + size > ctx.max) {
57
58
  return RANGE_ERR;
58
59
  }
59
60
  setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
@@ -1,4 +1,6 @@
1
1
  import { ModifyCtx } from '../../index.js';
2
2
  import { SchemaTypeDef, PropDef } from '@based/schema/def';
3
3
  import { ModifyOp, ModifyErr } from './types.js';
4
- export declare function writeHll(value: string | null | Buffer | Array<string | Buffer>, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
4
+ import { ModifyError } from './ModifyRes.js';
5
+ export declare function writeHll(value: string | null | Buffer | Uint8Array | Array<string | Buffer | Uint8Array>, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
6
+ export declare function writeHllBuf(value: (string | Buffer | Uint8Array)[], ctx: ModifyCtx, t: PropDef, len: number): ModifyError;
@@ -7,7 +7,7 @@ export function writeHll(value, ctx, def, t, parentId, modifyOp) {
7
7
  return new ModifyError(t, value);
8
8
  }
9
9
  if (value === null) {
10
- // Future hll_reset frunction
10
+ // Future hll_reset function
11
11
  return;
12
12
  }
13
13
  else if (!Array.isArray(value)) {
@@ -23,18 +23,22 @@ export function writeHll(value, ctx, def, t, parentId, modifyOp) {
23
23
  function addHll(value, ctx, def, t, parentId, modifyOp) {
24
24
  const len = value.length;
25
25
  let size = 4 + len * 8;
26
- if (ctx.len + size + 11 > ctx.max) {
26
+ if (ctx.len + size + 11 /* SIZE.DEFAULT_CURSOR */ > ctx.max) {
27
27
  return RANGE_ERR;
28
28
  }
29
29
  setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
30
30
  ctx.buf[ctx.len++] = modifyOp;
31
+ writeHllBuf(value, ctx, t, len);
32
+ }
33
+ export function writeHllBuf(value, ctx, t, len) {
31
34
  ctx.buf.writeUint32LE(len, ctx.len);
32
35
  ctx.len += 4;
33
36
  for (let val of value) {
34
37
  if (typeof val === 'string') {
35
- xxHash64(Buffer.from(val), ctx.buf, ctx.len);
38
+ xxHash64(new TextEncoder().encode(val), ctx.buf, ctx.len);
36
39
  }
37
- else if (val instanceof Buffer && val.byteLength === 8) {
40
+ else if ((val instanceof Buffer || val instanceof Uint8Array) &&
41
+ val.byteLength === 8) {
38
42
  ctx.buf.set(val, ctx.len);
39
43
  }
40
44
  else {
@@ -3,7 +3,7 @@ import { startDrain, flushBuffer } from '../operations.js';
3
3
  import { setCursor } from './setCursor.js';
4
4
  import { modify } from './modify.js';
5
5
  import { ModifyState } from './ModifyRes.js';
6
- import { CREATE, RANGE_ERR, ADD_EMPTY_SORT, } from './types.js';
6
+ import { CREATE, RANGE_ERR, ADD_EMPTY_SORT, ADD_EMPTY_SORT_TEXT, } from './types.js';
7
7
  import { writeFixedValue } from './fixed.js';
8
8
  import { getSubscriptionMarkers } from '../query/subscription/index.js';
9
9
  const appendCreate = (ctx, def, obj, res, unsafe) => {
@@ -13,7 +13,7 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
13
13
  return err;
14
14
  }
15
15
  if (ctx.len === len || def.mainLen === 0) {
16
- if (ctx.len + 10 > ctx.max) {
16
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ > ctx.max) {
17
17
  return RANGE_ERR;
18
18
  }
19
19
  setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
@@ -47,7 +47,7 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
47
47
  let sizepos = ctx.len;
48
48
  ctx.len += 2;
49
49
  for (const { prop } of def.seperateSort.props) {
50
- if (def.seperateSort.bufferTmp[prop] === 1) {
50
+ if (def.seperateSort.bufferTmp[prop] === 0) {
51
51
  if (ctx.len + 1 > ctx.max) {
52
52
  return RANGE_ERR;
53
53
  }
@@ -59,8 +59,44 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
59
59
  ctx.buf[sizepos] = size >>>= 8;
60
60
  }
61
61
  if (ctx.hasSortField !== -1) {
62
- def.seperateSort.buffer.set(def.seperateSort.bufferTmp);
62
+ def.seperateSort.bufferTmp.set(def.seperateSort.buffer, 0);
63
63
  }
64
+ // add test for this
65
+ ctx.hasSortField = -1;
66
+ }
67
+ if (def.hasSeperateTextSort) {
68
+ const buf = def.seperateTextSort.bufferTmp;
69
+ if (ctx.hasSortText !== def.seperateTextSort.size - 1) {
70
+ if (ctx.len + 3 > ctx.max) {
71
+ return RANGE_ERR;
72
+ }
73
+ ctx.buf[ctx.len++] = ADD_EMPTY_SORT_TEXT;
74
+ let sizepos = ctx.len;
75
+ ctx.len += 2;
76
+ const amount = def.localeSize + 1;
77
+ const len = amount * def.seperateTextSort.props.length;
78
+ for (const { prop } of def.seperateTextSort.props) {
79
+ const index = prop * amount;
80
+ if (buf[index] !== 0) {
81
+ ctx.buf[ctx.len++] = prop;
82
+ ctx.buf[ctx.len++] = buf[index];
83
+ for (let i = index + 1; i < len + index; i++) {
84
+ const lang = buf[i];
85
+ if (lang !== 0) {
86
+ ctx.buf[ctx.len++] = lang;
87
+ }
88
+ }
89
+ }
90
+ }
91
+ let size = ctx.len - sizepos - 2;
92
+ ctx.buf[sizepos++] = size;
93
+ ctx.buf[sizepos] = size >>>= 8;
94
+ // [size][size] [prop][len][lang][lang]
95
+ }
96
+ if (ctx.hasSortText !== -1) {
97
+ buf.set(def.seperateTextSort.buffer, 0);
98
+ }
99
+ ctx.hasSortText = -1;
64
100
  }
65
101
  };
66
102
  export function create(db, type, obj, opts) {
@@ -89,7 +125,7 @@ export function create(db, type, obj, opts) {
89
125
  ctx.len = pos;
90
126
  if (err === RANGE_ERR) {
91
127
  if (pos === 0) {
92
- throw new Error('out of range');
128
+ throw new Error('!No range available');
93
129
  }
94
130
  flushBuffer(db);
95
131
  return db.create(type, obj, opts);
@@ -1,33 +1,34 @@
1
1
  import { flushBuffer, startDrain } from '../operations.js';
2
2
  import { setCursor } from './setCursor.js';
3
- import { UPDATE } from './types.js';
3
+ import { UPDATE, DELETE_SORT_INDEX, DELETE_NODE } from './types.js';
4
4
  import { MICRO_BUFFER } from '@based/schema/def';
5
5
  export const deleteFn = (db, type, id) => {
6
6
  const ctx = db.modifyCtx;
7
7
  const schema = db.schemaTypesParsed[type];
8
8
  const separate = schema.separate;
9
+ // TODO: pretty slow actually
9
10
  if (separate) {
10
- const size = 12 + separate.length * 12;
11
+ const size = 11 /* SIZE.DEFAULT_CURSOR */ + 2 + separate.length * 12;
11
12
  if (ctx.len + size > ctx.max) {
12
13
  flushBuffer(db);
13
14
  return deleteFn(db, type, id);
14
15
  }
15
16
  setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
16
- ctx.buf[ctx.len++] = 4;
17
+ ctx.buf[ctx.len++] = DELETE_SORT_INDEX;
17
18
  for (const s of separate) {
18
19
  setCursor(ctx, schema, s.prop, s.typeIndex, id, UPDATE);
19
- ctx.buf[ctx.len++] = 4;
20
+ ctx.buf[ctx.len++] = DELETE_SORT_INDEX;
20
21
  }
21
- ctx.buf[ctx.len++] = 10;
22
+ ctx.buf[ctx.len++] = DELETE_NODE;
22
23
  }
23
24
  else {
24
- if (ctx.len + 12 > ctx.max) {
25
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 2 > ctx.max) {
25
26
  flushBuffer(db);
26
27
  return deleteFn(db, type, id);
27
28
  }
28
29
  setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
29
- ctx.buf[ctx.len++] = 4;
30
- ctx.buf[ctx.len++] = 10;
30
+ ctx.buf[ctx.len++] = DELETE_SORT_INDEX;
31
+ ctx.buf[ctx.len++] = DELETE_NODE;
31
32
  }
32
33
  if (!db.isDraining) {
33
34
  startDrain(db);
@@ -8,7 +8,7 @@ export function expire(db, type, id, seconds) {
8
8
  throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
9
9
  }
10
10
  const ctx = db.modifyCtx;
11
- if (ctx.len + 8 + 1 + 4 > ctx.max) {
11
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 > ctx.max) {
12
12
  flushBuffer(db);
13
13
  return expire(db, type, id, seconds);
14
14
  }
@@ -34,7 +34,7 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
34
34
  err = writeString(0, val, ctx, schema, def, res.tmpId, mod);
35
35
  }
36
36
  else if (type === TEXT) {
37
- err = writeText(val, ctx, schema, def, res, mod);
37
+ err = writeText(val, ctx, schema, def, res, res.tmpId, mod);
38
38
  }
39
39
  else if (type === REFERENCE) {
40
40
  err = writeReference(val, ctx, schema, def, res, mod);
@@ -59,7 +59,7 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
59
59
  }
60
60
  }
61
61
  else if (overwrite) {
62
- if (ctx.len + 15 + schema.mainLen > ctx.max) {
62
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + schema.mainLen > ctx.max) {
63
63
  return RANGE_ERR;
64
64
  }
65
65
  setCursor(ctx, schema, def.prop, MICRO_BUFFER, res.tmpId, mod, true);
@@ -87,7 +87,7 @@ function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
87
87
  if (increment === 0) {
88
88
  continue;
89
89
  }
90
- if (ctx.len + 10 > ctx.max) {
90
+ if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ > ctx.max) {
91
91
  return RANGE_ERR;
92
92
  }
93
93
  setCursor(ctx, schema, def.prop, MICRO_BUFFER, res.tmpId, mod);
@@ -1,5 +1,6 @@
1
- import { BINARY, REFERENCE, REFERENCES, STRING, } from '@based/schema/def';
1
+ import { BINARY, CARDINALITY, REFERENCE, REFERENCES, STRING, } from '@based/schema/def';
2
2
  import { write } from '../../string.js';
3
+ import { writeHllBuf } from '../cardinality.js';
3
4
  import { getBuffer, writeBinaryRaw } from '../binary.js';
4
5
  import { ModifyError, ModifyState } from '../ModifyRes.js';
5
6
  import { DECREMENT, INCREMENT, RANGE_ERR } from '../types.js';
@@ -140,6 +141,19 @@ export function writeEdges(t, ref, ctx) {
140
141
  ctx.buf[ctx.len++] = size >>>= 8;
141
142
  appendRefs(edge, ctx, value);
142
143
  }
144
+ else if (edge.typeIndex === CARDINALITY) {
145
+ if (!Array.isArray(value)) {
146
+ value = [value];
147
+ }
148
+ const len = value.length;
149
+ let size = 4 + len * 8;
150
+ if (ctx.len + size + 11 > ctx.max) {
151
+ return RANGE_ERR;
152
+ }
153
+ ctx.buf[ctx.len++] = edge.prop;
154
+ ctx.buf[ctx.len++] = CARDINALITY;
155
+ writeHllBuf(value, ctx, t, size);
156
+ }
143
157
  }
144
158
  else {
145
159
  if (ctx.len + 2 > ctx.max) {
@@ -20,8 +20,6 @@ typeIndex, id, modifyOp, ignoreField) => {
20
20
  ctx.buf[ctx.len++] = SWITCH_FIELD; // switch field
21
21
  ctx.buf[ctx.len++] = field; // actual field
22
22
  ctx.buf[ctx.len++] = typeIndex;
23
- // field === 0 ? MICRO_BUFFER : schema.reverseProps[field].typeIndex
24
- // add start and len if its main
25
23
  ctx.field = field;
26
24
  }
27
25
  if (ctx.id !== id) {