@based/db 0.0.70 → 0.1.0

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 (263) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +105 -77
  3. package/dist/lib/darwin_aarch64/include/libdeflate.h +23 -0
  4. package/dist/lib/darwin_aarch64/include/selva/db.h +0 -41
  5. package/dist/lib/darwin_aarch64/include/selva/fast_linear_search.h +6 -2
  6. package/dist/lib/darwin_aarch64/include/selva/fields.h +52 -37
  7. package/dist/lib/darwin_aarch64/include/selva/gmtime.h +137 -0
  8. package/dist/lib/darwin_aarch64/include/selva/hll.h +1 -1
  9. package/dist/lib/darwin_aarch64/include/selva/mblen.h +40 -0
  10. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +8 -5
  11. package/dist/lib/darwin_aarch64/include/selva/thread.h +37 -0
  12. package/dist/lib/darwin_aarch64/include/selva/types.h +18 -4
  13. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  14. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  15. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  16. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  17. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  18. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  19. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  20. package/dist/lib/linux_aarch64/include/libdeflate.h +23 -0
  21. package/dist/lib/linux_aarch64/include/selva/db.h +0 -41
  22. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +6 -2
  23. package/dist/lib/linux_aarch64/include/selva/fields.h +52 -37
  24. package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
  25. package/dist/lib/linux_aarch64/include/selva/hll.h +1 -1
  26. package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
  27. package/dist/lib/linux_aarch64/include/selva/selva_string.h +8 -5
  28. package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
  29. package/dist/lib/linux_aarch64/include/selva/types.h +18 -4
  30. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  31. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  32. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  33. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  34. package/dist/lib/linux_aarch64/libselva.so +0 -0
  35. package/dist/lib/linux_x86_64/include/libdeflate.h +23 -0
  36. package/dist/lib/linux_x86_64/include/selva/db.h +0 -41
  37. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +6 -2
  38. package/dist/lib/linux_x86_64/include/selva/fields.h +52 -37
  39. package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
  40. package/dist/lib/linux_x86_64/include/selva/hll.h +1 -1
  41. package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
  42. package/dist/lib/linux_x86_64/include/selva/selva_string.h +8 -5
  43. package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
  44. package/dist/lib/linux_x86_64/include/selva/types.h +18 -4
  45. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  46. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  47. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  48. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  49. package/dist/lib/linux_x86_64/libselva.so +0 -0
  50. package/dist/src/client/index.d.ts +16 -21
  51. package/dist/src/client/index.js +48 -59
  52. package/dist/src/client/modify/Ctx.d.ts +38 -0
  53. package/dist/src/client/modify/Ctx.js +30 -0
  54. package/dist/src/client/modify/Tmp.d.ts +19 -0
  55. package/dist/src/client/modify/Tmp.js +67 -0
  56. package/dist/src/client/modify/create/index.d.ts +6 -0
  57. package/dist/src/client/modify/create/index.js +191 -0
  58. package/dist/src/client/modify/create/mark.d.ts +7 -0
  59. package/dist/src/client/modify/create/mark.js +42 -0
  60. package/dist/src/client/modify/cursor.d.ts +10 -0
  61. package/dist/src/client/modify/cursor.js +48 -0
  62. package/dist/src/client/modify/delete/index.d.ts +2 -0
  63. package/dist/src/client/modify/delete/index.js +39 -0
  64. package/dist/src/client/modify/drain.d.ts +7 -0
  65. package/dist/src/client/modify/drain.js +77 -0
  66. package/dist/src/client/modify/edges/binary.d.ts +3 -0
  67. package/dist/src/client/modify/edges/binary.js +28 -0
  68. package/dist/src/client/modify/edges/cardinality.d.ts +3 -0
  69. package/dist/src/client/modify/edges/cardinality.js +15 -0
  70. package/dist/src/client/modify/edges/header.d.ts +5 -0
  71. package/dist/src/client/modify/edges/header.js +21 -0
  72. package/dist/src/client/modify/edges/index.d.ts +3 -0
  73. package/dist/src/client/modify/edges/index.js +128 -0
  74. package/dist/src/client/modify/edges/reference.d.ts +3 -0
  75. package/dist/src/client/modify/edges/reference.js +31 -0
  76. package/dist/src/client/modify/edges/references.d.ts +3 -0
  77. package/dist/src/client/modify/edges/references.js +38 -0
  78. package/dist/src/client/modify/edges/separate.d.ts +3 -0
  79. package/dist/src/client/modify/edges/separate.js +24 -0
  80. package/dist/src/client/modify/edges/string.d.ts +3 -0
  81. package/dist/src/client/modify/edges/string.js +30 -0
  82. package/dist/src/client/modify/error.d.ts +7 -0
  83. package/dist/src/client/modify/error.js +68 -0
  84. package/dist/src/client/modify/expire/index.d.ts +2 -0
  85. package/dist/src/client/modify/expire/index.js +31 -0
  86. package/dist/src/client/modify/props/alias.d.ts +3 -0
  87. package/dist/src/client/modify/props/alias.js +26 -0
  88. package/dist/src/client/modify/props/binary.d.ts +5 -0
  89. package/dist/src/client/modify/props/binary.js +52 -0
  90. package/dist/src/client/modify/props/cardinality.d.ts +4 -0
  91. package/dist/src/client/modify/props/cardinality.js +46 -0
  92. package/dist/src/client/modify/props/delete.d.ts +3 -0
  93. package/dist/src/client/modify/props/delete.js +13 -0
  94. package/dist/src/client/modify/props/fixed.d.ts +4 -0
  95. package/dist/src/client/modify/props/fixed.js +105 -0
  96. package/dist/src/client/modify/props/increment.d.ts +3 -0
  97. package/dist/src/client/modify/props/increment.js +28 -0
  98. package/dist/src/client/modify/props/json.d.ts +3 -0
  99. package/dist/src/client/modify/props/json.js +5 -0
  100. package/dist/src/client/modify/props/main.d.ts +4 -0
  101. package/dist/src/client/modify/props/main.js +22 -0
  102. package/dist/src/client/modify/props/object.d.ts +3 -0
  103. package/dist/src/client/modify/props/object.js +37 -0
  104. package/dist/src/client/modify/props/reference.d.ts +6 -0
  105. package/dist/src/client/modify/props/reference.js +92 -0
  106. package/dist/src/client/modify/props/references.d.ts +3 -0
  107. package/dist/src/client/modify/props/references.js +231 -0
  108. package/dist/src/client/modify/props/separate.d.ts +3 -0
  109. package/dist/src/client/modify/props/separate.js +43 -0
  110. package/dist/src/client/modify/props/string.d.ts +5 -0
  111. package/dist/src/client/modify/props/string.js +51 -0
  112. package/dist/src/client/modify/props/text.d.ts +3 -0
  113. package/dist/src/client/modify/props/text.js +35 -0
  114. package/dist/src/client/modify/props/vector.d.ts +3 -0
  115. package/dist/src/client/modify/props/vector.js +30 -0
  116. package/dist/src/client/modify/resize.d.ts +3 -0
  117. package/dist/src/client/modify/resize.js +20 -0
  118. package/dist/src/client/modify/types.d.ts +3 -4
  119. package/dist/src/client/modify/types.js +2 -1
  120. package/dist/src/client/modify/uint.d.ts +7 -0
  121. package/dist/src/client/modify/uint.js +27 -0
  122. package/dist/src/client/modify/update/index.d.ts +3 -0
  123. package/dist/src/client/modify/update/index.js +75 -0
  124. package/dist/src/client/modify/upsert/index.d.ts +3 -0
  125. package/dist/src/client/modify/{upsert.js → upsert/index.js} +4 -4
  126. package/dist/src/client/modify/validate.d.ts +6 -0
  127. package/dist/src/client/modify/validate.js +24 -0
  128. package/dist/src/client/query/BasedDbQuery.d.ts +18 -15
  129. package/dist/src/client/query/BasedDbQuery.js +93 -96
  130. package/dist/src/client/query/{BasedIterable.d.ts → BasedQueryResponse.d.ts} +5 -4
  131. package/dist/src/client/query/{BasedIterable.js → BasedQueryResponse.js} +15 -10
  132. package/dist/src/client/query/aggregates/aggregation.d.ts +5 -3
  133. package/dist/src/client/query/aggregates/aggregation.js +105 -48
  134. package/dist/src/client/query/aggregates/types.d.ts +34 -12
  135. package/dist/src/client/query/aggregates/types.js +30 -12
  136. package/dist/src/client/query/debug.js +1 -1
  137. package/dist/src/client/query/display.d.ts +1 -1
  138. package/dist/src/client/query/display.js +21 -4
  139. package/dist/src/client/query/filter/FilterBranch.js +2 -2
  140. package/dist/src/client/query/filter/convertFilter.d.ts +3 -2
  141. package/dist/src/client/query/filter/convertFilter.js +13 -1
  142. package/dist/src/client/query/filter/createFixedFilterBuffer.js +16 -30
  143. package/dist/src/client/query/filter/createReferenceFilter.js +6 -20
  144. package/dist/src/client/query/filter/createVariableFilterBuffer.js +15 -32
  145. package/dist/src/client/query/filter/parseFilterValue.js +3 -11
  146. package/dist/src/client/query/filter/primitiveFilter.js +1 -1
  147. package/dist/src/client/query/filter/toBuffer.js +9 -15
  148. package/dist/src/client/query/filter/types.d.ts +6 -6
  149. package/dist/src/client/query/filter/types.js +8 -8
  150. package/dist/src/client/query/include/include.d.ts +3 -0
  151. package/dist/src/client/query/include/include.js +58 -0
  152. package/dist/src/client/query/include/props.d.ts +7 -7
  153. package/dist/src/client/query/include/props.js +26 -21
  154. package/dist/src/client/query/include/toByteCode.js +188 -0
  155. package/dist/src/client/query/include/utils.d.ts +3 -1
  156. package/dist/src/client/query/include/utils.js +13 -0
  157. package/dist/src/client/query/include/walk.d.ts +2 -2
  158. package/dist/src/client/query/include/walk.js +38 -23
  159. package/dist/src/client/query/query.d.ts +2 -3
  160. package/dist/src/client/query/query.js +2 -3
  161. package/dist/src/client/query/queryDef.d.ts +4 -2
  162. package/dist/src/client/query/queryDef.js +3 -5
  163. package/dist/src/client/query/queryDefToReadSchema.d.ts +3 -0
  164. package/dist/src/client/query/queryDefToReadSchema.js +126 -0
  165. package/dist/src/client/query/registerQuery.js +15 -4
  166. package/dist/src/client/query/search/index.d.ts +2 -1
  167. package/dist/src/client/query/search/index.js +9 -2
  168. package/dist/src/client/query/sort.js +3 -0
  169. package/dist/src/client/query/subscription/index.d.ts +1 -1
  170. package/dist/src/client/query/subscription/index.js +2 -2
  171. package/dist/src/client/query/subscription/types.d.ts +1 -1
  172. package/dist/src/client/query/thresholds.d.ts +1 -1
  173. package/dist/src/client/query/thresholds.js +1 -1
  174. package/dist/src/client/query/toByteCode/{toBuffer.js → toByteCode.js} +8 -8
  175. package/dist/src/client/query/types.d.ts +40 -25
  176. package/dist/src/client/query/types.js +2 -6
  177. package/dist/src/client/query/validation.d.ts +6 -0
  178. package/dist/src/client/query/validation.js +32 -8
  179. package/dist/src/client/setLocalClientSchema.js +6 -10
  180. package/dist/src/client/string.d.ts +3 -7
  181. package/dist/src/client/string.js +18 -47
  182. package/dist/src/hooks.d.ts +2 -3
  183. package/dist/src/hooks.js +14 -9
  184. package/dist/src/index.d.ts +3 -5
  185. package/dist/src/index.js +4 -6
  186. package/dist/src/native.d.ts +3 -2
  187. package/dist/src/native.js +9 -8
  188. package/dist/src/server/IoWorker.js +2 -3
  189. package/dist/src/server/QueryWorker.js +1 -1
  190. package/dist/src/server/blocks.js +5 -4
  191. package/dist/src/server/dbHash.js +1 -1
  192. package/dist/src/server/index.d.ts +4 -6
  193. package/dist/src/server/index.js +63 -96
  194. package/dist/src/server/migrate/index.d.ts +2 -4
  195. package/dist/src/server/migrate/index.js +31 -16
  196. package/dist/src/server/migrate/worker.js +2 -27
  197. package/dist/src/server/save.js +10 -8
  198. package/dist/src/server/schema.js +17 -25
  199. package/dist/src/server/start.d.ts +1 -0
  200. package/dist/src/server/start.js +18 -10
  201. package/dist/src/server/tree.d.ts +20 -0
  202. package/dist/src/server/tree.js +17 -2
  203. package/dist/src/server/workers/DbWorker.d.ts +1 -0
  204. package/dist/src/server/workers/DbWorker.js +8 -1
  205. package/dist/src/server/workers/io_worker.js +1 -1
  206. package/dist/src/server/workers/worker.js +9 -4
  207. package/dist/src/table.d.ts +4 -0
  208. package/dist/src/table.js +97 -0
  209. package/dist/src/types.d.ts +1 -1
  210. package/package.json +18 -17
  211. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +0 -29
  212. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +0 -29
  213. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +0 -29
  214. package/dist/src/client/flushModify.d.ts +0 -36
  215. package/dist/src/client/flushModify.js +0 -197
  216. package/dist/src/client/modify/ModifyRes.d.ts +0 -23
  217. package/dist/src/client/modify/ModifyRes.js +0 -101
  218. package/dist/src/client/modify/alias.d.ts +0 -4
  219. package/dist/src/client/modify/alias.js +0 -57
  220. package/dist/src/client/modify/binary.d.ts +0 -6
  221. package/dist/src/client/modify/binary.js +0 -80
  222. package/dist/src/client/modify/cardinality.d.ts +0 -6
  223. package/dist/src/client/modify/cardinality.js +0 -63
  224. package/dist/src/client/modify/create.d.ts +0 -5
  225. package/dist/src/client/modify/create.js +0 -228
  226. package/dist/src/client/modify/delete.d.ts +0 -4
  227. package/dist/src/client/modify/delete.js +0 -47
  228. package/dist/src/client/modify/expire.d.ts +0 -3
  229. package/dist/src/client/modify/expire.js +0 -25
  230. package/dist/src/client/modify/fixed.d.ts +0 -5
  231. package/dist/src/client/modify/fixed.js +0 -235
  232. package/dist/src/client/modify/index.d.ts +0 -4
  233. package/dist/src/client/modify/index.js +0 -4
  234. package/dist/src/client/modify/json.d.ts +0 -4
  235. package/dist/src/client/modify/json.js +0 -26
  236. package/dist/src/client/modify/modify.d.ts +0 -5
  237. package/dist/src/client/modify/modify.js +0 -134
  238. package/dist/src/client/modify/references/appendEdgeRefs.d.ts +0 -4
  239. package/dist/src/client/modify/references/appendEdgeRefs.js +0 -33
  240. package/dist/src/client/modify/references/edge.d.ts +0 -5
  241. package/dist/src/client/modify/references/edge.js +0 -322
  242. package/dist/src/client/modify/references/getEdgeSize.d.ts +0 -3
  243. package/dist/src/client/modify/references/getEdgeSize.js +0 -27
  244. package/dist/src/client/modify/references/reference.d.ts +0 -11
  245. package/dist/src/client/modify/references/reference.js +0 -112
  246. package/dist/src/client/modify/references/references.d.ts +0 -16
  247. package/dist/src/client/modify/references/references.js +0 -308
  248. package/dist/src/client/modify/setCursor.d.ts +0 -5
  249. package/dist/src/client/modify/setCursor.js +0 -33
  250. package/dist/src/client/modify/string.d.ts +0 -5
  251. package/dist/src/client/modify/string.js +0 -71
  252. package/dist/src/client/modify/text.d.ts +0 -8
  253. package/dist/src/client/modify/text.js +0 -69
  254. package/dist/src/client/modify/update.d.ts +0 -6
  255. package/dist/src/client/modify/update.js +0 -90
  256. package/dist/src/client/modify/upsert.d.ts +0 -3
  257. package/dist/src/client/modify/vector.d.ts +0 -4
  258. package/dist/src/client/modify/vector.js +0 -53
  259. package/dist/src/client/query/include/toBuffer.js +0 -123
  260. package/dist/src/client/query/read/read.d.ts +0 -9
  261. package/dist/src/client/query/read/read.js +0 -483
  262. /package/dist/src/client/query/include/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
  263. /package/dist/src/client/query/toByteCode/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
@@ -15,15 +15,6 @@ struct selva_string;
15
15
  #include "selva/types.h"
16
16
  #include "selva_lang_code.h"
17
17
 
18
- /**
19
- * Reserved (N/A) field id.
20
- * Can be used to mark that a field doesn't exist.
21
- * E.g. if created and updated fields don't exist their ids can be set to this
22
- * value.
23
- * Technically fields 251..255 are all reserved.
24
- */
25
- #define SELVA_FIELDS_RESERVED 255
26
-
27
18
  struct XXH3_state_s; /* RFE Not good? */
28
19
 
29
20
  #ifndef __zig
@@ -33,17 +24,51 @@ struct SelvaTextField {
33
24
  } __packed;
34
25
  #endif
35
26
 
36
- struct SelvaNodeReference {
27
+ struct SelvaNodeSmallReference {
28
+ struct SelvaNode *dst;
29
+ };
30
+
31
+ struct SelvaNodeLargeReference {
37
32
  struct SelvaNode *dst;
38
33
  struct SelvaFields *meta;
39
34
  };
40
35
 
36
+ enum SelvaNodeReferenceType {
37
+ SELVA_NODE_REFERENCE_NULL = 0,
38
+ SELVA_NODE_REFERENCE_SMALL = 1,
39
+ SELVA_NODE_REFERENCE_LARGE = 2,
40
+ } __packed;
41
+
42
+ struct SelvaNodeReferenceAny {
43
+ enum SelvaNodeReferenceType type;
44
+ union {
45
+ void *any;
46
+ struct SelvaNodeSmallReference *small;
47
+ struct SelvaNodeLargeReference *large;
48
+ }
49
+ #ifdef __zig
50
+ p
51
+ #endif
52
+ ;
53
+ };
54
+
55
+ static_assert(offsetof(struct SelvaNodeLargeReference, dst) == offsetof(struct SelvaNodeSmallReference, dst));
56
+
41
57
  struct SelvaNodeReferences {
42
58
  uint32_t nr_refs;
43
59
  uint16_t offset;
44
- struct SelvaNodeReference *refs __pcounted_by(nr_refs);
60
+ enum SelvaNodeReferenceType size;
61
+
62
+ union {
63
+ void *any;
64
+ struct SelvaNodeLargeReference *large __pcounted_by(nr_refs);
65
+ struct SelvaNodeSmallReference *small __pcounted_by(nr_refs);
66
+ };
45
67
  node_id_t *index __pcounted_by(nr_refs); /*!< Sorted index of all nodes in `.refs`. */
46
68
  };
69
+ static_assert(offsetof(struct SelvaNodeReferences, any) == offsetof(struct SelvaNodeReferences, small));
70
+ static_assert(offsetof(struct SelvaNodeReferences, any) == offsetof(struct SelvaNodeReferences, large));
71
+ static_assert(offsetof(struct SelvaNodeReferences, small) == offsetof(struct SelvaNodeReferences, large));
47
72
 
48
73
  struct SelvaNodeWeakReference {
49
74
  /* The type can be found from the schema. */
@@ -59,23 +84,6 @@ struct SelvaNodeWeakReferences {
59
84
  struct SelvaNodeWeakReference *refs __pcounted_by(nr_refs);
60
85
  };
61
86
 
62
- /**
63
- * Type helper to determine the size of statically (constant/fixed) sized fields.
64
- */
65
- union SelvaStaticFields {
66
- bool boolean; /*!< SELVA_FIELD_TYPE_BOOLEAN */
67
- double number; /*!< SELVA_FIELD_TYPE_NUMBER */
68
- struct selva_string *string; /*!< SELVA_FIELD_TYPE_STRING */
69
- int8_t int8; /* SELVA_FIELD_TYPE_INT8 */
70
- uint8_t uint8; /*!< SELVA_FIELD_TYPE_UINT8 */
71
- int16_t int16; /*!< SELVA_FIELD_TYPE_INT16 */
72
- uint16_t uint16; /*!< SELVA_FIELD_TYPE_UINT16 */
73
- int32_t int32; /*!< SELVA_FIELD_TYPE_INT32 */
74
- uint32_t uint32; /*!< SELVA_FIELD_TYPE_UINT32 */
75
- uint8_t enu; /*!< SELVA_FIELD_TYPE_ENUM */
76
- struct SelvaNodeWeakReference weak_reference; /*!< SELVA_FIELD_TYPE_WEAK_REFERENCE */
77
- };
78
-
79
87
  struct SelvaFieldsPointer {
80
88
  uint8_t *ptr;
81
89
  size_t off;
@@ -111,7 +119,7 @@ struct SelvaFields *selva_fields_node2fields(struct SelvaNode *node);
111
119
  void selva_fields_ensure_ref_meta(
112
120
  struct SelvaDb *db,
113
121
  struct SelvaNode *node,
114
- struct SelvaNodeReference *ref,
122
+ struct SelvaNodeLargeReference *ref,
115
123
  const struct EdgeFieldConstraint *efc)
116
124
  __attribute__((nonnull));
117
125
 
@@ -140,7 +148,7 @@ struct selva_string *selva_fields_ensure_string2(
140
148
  struct SelvaDb *db,
141
149
  struct SelvaNode *node,
142
150
  const struct EdgeFieldConstraint *efc,
143
- struct SelvaNodeReference *ref,
151
+ struct SelvaNodeLargeReference *ref,
144
152
  const struct SelvaFieldSchema *fs,
145
153
  size_t initial_len);
146
154
 
@@ -153,7 +161,7 @@ int selva_fields_reference_set(
153
161
  struct SelvaNode * restrict src,
154
162
  const struct SelvaFieldSchema *fs_src,
155
163
  struct SelvaNode * restrict dst,
156
- struct SelvaNodeReference **ref_out,
164
+ struct SelvaNodeReferenceAny *ref_out,
157
165
  selva_dirty_node_cb_t dirty_cb,
158
166
  void *dirty_ctx);
159
167
  // __attribute__((access(write_only, 5), access(write_only, 6)));
@@ -171,7 +179,7 @@ int selva_fields_references_insert(
171
179
  bool reorder,
172
180
  struct SelvaTypeEntry *te_dst,
173
181
  struct SelvaNode * restrict dst,
174
- struct SelvaNodeReference **ref_out)
182
+ struct SelvaNodeReferenceAny *ref_out)
175
183
  __attribute__((access(write_only, 8)));
176
184
 
177
185
  /**
@@ -179,7 +187,7 @@ int selva_fields_references_insert(
179
187
  * @returns nr_refs.
180
188
  */
181
189
  SELVA_EXPORT
182
- size_t selva_fields_prealloc_refs(struct SelvaNode *node, const struct SelvaFieldSchema *fs, size_t nr_refs_min);
190
+ size_t selva_fields_prealloc_refs(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs, size_t nr_refs_min);
183
191
 
184
192
  SELVA_EXPORT
185
193
  int selva_fields_references_insert_tail_wupsert(
@@ -219,7 +227,7 @@ SELVA_EXPORT
219
227
  int selva_fields_set_reference_meta(
220
228
  struct SelvaDb *db,
221
229
  struct SelvaNode *node,
222
- struct SelvaNodeReference *ref,
230
+ struct SelvaNodeLargeReference *ref,
223
231
  const struct EdgeFieldConstraint *efc,
224
232
  const struct SelvaFieldSchema *efs,
225
233
  const void *value, size_t len);
@@ -228,7 +236,7 @@ SELVA_EXPORT
228
236
  int selva_fields_get_reference_meta_mutable_string(
229
237
  struct SelvaDb *db,
230
238
  struct SelvaNode *node,
231
- struct SelvaNodeReference *ref,
239
+ struct SelvaNodeLargeReference *ref,
232
240
  const struct EdgeFieldConstraint *efc,
233
241
  const struct SelvaFieldSchema *efs,
234
242
  size_t len,
@@ -266,6 +274,13 @@ int selva_fields_set_text(
266
274
  const char *str,
267
275
  size_t len);
268
276
 
277
+ int selva_fields_get_mutable_text(
278
+ struct SelvaNode *node,
279
+ const struct SelvaFieldSchema *fs,
280
+ enum selva_lang_code lang,
281
+ size_t len,
282
+ struct selva_string **out);
283
+
269
284
  /**
270
285
  * Get text field translation.
271
286
  *
@@ -302,7 +317,7 @@ SELVA_EXPORT
302
317
  int selva_fields_set_weak_references(struct SelvaNode *node, const struct SelvaFieldSchema *fs, node_id_t dst[], size_t nr_dsts);
303
318
 
304
319
  SELVA_EXPORT
305
- struct SelvaNodeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs)
320
+ struct SelvaNodeLargeReference *selva_fields_get_reference(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFieldSchema *fs)
306
321
  __attribute__((nonnull));
307
322
 
308
323
  SELVA_EXPORT
@@ -325,7 +340,7 @@ struct SelvaNode *selva_fields_resolve_weak_reference(
325
340
  __attribute__((nonnull));
326
341
 
327
342
  SELVA_EXPORT
328
- struct selva_string *selva_fields_get_selva_string3(struct SelvaNodeReference *ref, const struct SelvaFieldSchema *fs)
343
+ struct selva_string *selva_fields_get_selva_string3(struct SelvaDb *db, struct SelvaNodeLargeReference *ref, const struct SelvaFieldSchema *fs)
329
344
  __attribute__((nonnull));
330
345
 
331
346
  SELVA_EXPORT
@@ -0,0 +1,137 @@
1
+ /**
2
+ *******************************************************************************
3
+ * @file gmtime.h
4
+ * @author Olli Vanhoja
5
+ * @brief Calendar calculations.
6
+ * @section LICENSE
7
+ * Copyright (c) 2025 Saulx
8
+ * SPDX-License-Identifier: MIT
9
+ *******************************************************************************
10
+ */
11
+
12
+ #pragma once
13
+ #include <stdint.h>
14
+ #include "selva/_export.h"
15
+
16
+ #define SELVA_TM_SUNDAY 0
17
+ #define SELVA_TM_MONDAY 1
18
+ #define SELVA_TM_TUESDAY 2
19
+ #define SELVA_TM_WEDNESDAY 3
20
+ #define SELVA_TM_THURSDAY 4
21
+ #define SELVA_TM_FRIDAY 5
22
+ #define SELVA_TM_SATURDAY 6
23
+
24
+ #define SELVA_TM_JANUARY 0
25
+ #define SELVA_TM_FEBRUARY 1
26
+ #define SELVA_TM_MARCH 2
27
+ #define SELVA_TM_APRIL 3
28
+ #define SELVA_TM_MAY 4
29
+ #define SELVA_TM_JUNE 5
30
+ #define SELVA_TM_JULY 6
31
+ #define SELVA_TM_AUGUST 7
32
+ #define SELVA_TM_SEPTEMBER 8
33
+ #define SELVA_TM_OCTOBER 9
34
+ #define SELVA_TM_NOVEMBER 10
35
+ #define SELVA_TM_DECEMBER 11
36
+
37
+ #define SELVA_EPOCH_YEAR 1970
38
+
39
+ /**
40
+ * Gregorian Calendar.
41
+ */
42
+ struct selva_tm {
43
+ int32_t tm_sec; /*!< Seconds [0,60]. */
44
+ int32_t tm_min; /*!< Minutes [0,59]. */
45
+ int32_t tm_hour; /*!< Hour [0,23]. */
46
+ int32_t tm_mday; /*!< Day of month [1,31]. */
47
+ int32_t tm_mon; /*!< Month of year [0,11]. */
48
+ int32_t tm_year; /*!< Year. */
49
+ int32_t tm_wday; /*!< Day of week [0,6] (Sunday =0). */
50
+ int32_t tm_yday; /*!< Day of year [0,365]. */
51
+ bool tm_yleap; /*!< Is leap year. */
52
+ };
53
+
54
+ /**
55
+ * ISO-8601 Week Date.
56
+ */
57
+ struct selva_iso_week {
58
+ int32_t iso_year; /*!< ISO year. */
59
+ int32_t iso_week; /*!< ISO week [1,54]. */
60
+ };
61
+
62
+ static inline int32_t selva_gmtime_wday2iso_wday(int32_t wday)
63
+ {
64
+ /* Same as (tm.tm_wday + 6) % 7 + 1 but fewer instructions. */
65
+ return wday ? wday : 7;
66
+ }
67
+
68
+ /**
69
+ * Calculate struct selva_tm from ts and tmz in Gregorian calendar.
70
+ * @param ts in ms.
71
+ * @param tmz in minutes.
72
+ */
73
+ SELVA_EXPORT
74
+ void selva_gmtime(struct selva_tm *result, int64_t ts, int64_t tmz);
75
+
76
+ /**
77
+ * Calculate Gregorian year from ts and tmz.
78
+ * @param ts in ms.
79
+ * @param tmz in minutes.
80
+ * @returns Gregorian year.
81
+ */
82
+ SELVA_EXPORT
83
+ int32_t selva_gmtime_year(int64_t ts, int64_t tmz);
84
+
85
+ /**
86
+ * Calculate Gregorian month from ts and tmz.
87
+ * @param ts in ms.
88
+ * @param tmz in minutes.
89
+ * @returns Gregorian month of year [0,11].
90
+ */
91
+ SELVA_EXPORT
92
+ int32_t selva_gmtime_mon(int64_t ts, int64_t tmz);
93
+
94
+ /**
95
+ * Calculate Gregorian day of year from ts and tmz.
96
+ * @param ts in ms.
97
+ * @param tmz in minutes.
98
+ * @returns Gregorian day of year [0,365].
99
+ */
100
+ SELVA_EXPORT
101
+ int32_t selva_gmtime_yday(int64_t ts, int64_t tmz);
102
+
103
+ /**
104
+ * Calculate Gregorian day of week from ts and tmz.
105
+ * @param ts in ms.
106
+ * @param tmz in minutes.
107
+ * @returns Gregorian day of week [0,6] (Sunday =0).
108
+ */
109
+ SELVA_EXPORT
110
+ int32_t selva_gmtime_wday(int64_t ts, int64_t tmz);
111
+
112
+ /**
113
+ * Calculate Gregorian day of month from ts and tmz.
114
+ * @param ts in ms.
115
+ * @param tmz in minutes.
116
+ * @returns Gregorian day of month [1,31].
117
+ */
118
+ SELVA_EXPORT
119
+ int32_t selva_gmtime_mday(int64_t ts, int64_t tmz);
120
+
121
+ /**
122
+ * Calculate hour from ts and tmz.
123
+ * @param ts in ms.
124
+ * @param tmz in minutes.
125
+ * @returns Hour [0, 23].
126
+ */
127
+ SELVA_EXPORT
128
+ int32_t selva_gmtime_hour(int64_t ts, int64_t tmz);
129
+
130
+ /**
131
+ * Calculate the ISO-8601 week date from ts and tmz.
132
+ * @param ts in ms.
133
+ * @param tmz in minutes.
134
+ * @returns wyear.
135
+ */
136
+ SELVA_EXPORT
137
+ struct selva_iso_week *selva_gmtime_iso_wyear(struct selva_iso_week *wyear, int64_t ts, int64_t tmz);
@@ -19,7 +19,7 @@ uint8_t *hll_count(struct selva_string *hllss);
19
19
  SELVA_EXPORT
20
20
  void hll_array_union(struct selva_string *res, struct selva_string *hll_array, size_t count);
21
21
  SELVA_EXPORT
22
- void hll_union(char*, size_t, const char*, size_t);
22
+ void hll_union(struct selva_string *, struct selva_string *);
23
23
 
24
24
  // size_t actual_cols[13] = { 79, 159, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200 };
25
25
  // float raw_estimate_data[13][200] =
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Copyright (c) 2025 SAULX
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+ #pragma once
6
+ #include "selva/_export.h"
7
+ #include <stdint.h>
8
+
9
+ /***
10
+ * Get the length of multibyte char.
11
+ */
12
+ SELVA_EXPORT
13
+ #if defined(__clang__)
14
+ /*
15
+ * optnone is needed at least on clang to not optimize out the clz.
16
+ * Otherwise this function will always return 0.
17
+ */
18
+ __attribute__((optnone))
19
+ #endif
20
+ inline unsigned selva_mblen(char first_byte)
21
+ #ifndef __zig
22
+ {
23
+ uint8_t x = first_byte;
24
+ unsigned l = 0;
25
+
26
+ if (x & 0x80) {
27
+ #if __has_builtin(__builtin_clzg)
28
+ l = __builtin_clzg((uint8_t)~x, 0) - 1;
29
+ #elif __has_builtin(__builtin_clz)
30
+ l = __builtin_clz((unsigned)(~x << 24)) - 1;
31
+ #else
32
+ #error "No luck"
33
+ #endif
34
+ }
35
+
36
+ return l;
37
+ }
38
+ #else
39
+ ;
40
+ #endif /* __zig */
@@ -10,6 +10,8 @@
10
10
 
11
11
  struct finalizer;
12
12
  struct selva_string;
13
+ struct libdeflate_compressor;
14
+ struct libdeflate_decompressor;
13
15
 
14
16
  /**
15
17
  * Selva string flags.
@@ -165,8 +167,8 @@ struct selva_string *selva_string_fread(FILE *fp, size_t size, enum selva_string
165
167
  */
166
168
  [[nodiscard]]
167
169
  SELVA_EXPORT
168
- struct selva_string *selva_string_createz(const char *in_str, size_t in_len, enum selva_string_flags flags)
169
- __attribute__((access(read_only, 1, 2)));
170
+ struct selva_string *selva_string_createz(struct libdeflate_compressor *compressor, const char *in_str, size_t in_len, enum selva_string_flags flags)
171
+ __attribute__((access(read_only, 2, 3)));
170
172
 
171
173
  /**
172
174
  * Decompress a compressed string.
@@ -178,8 +180,8 @@ struct selva_string *selva_string_createz(const char *in_str, size_t in_len, enu
178
180
  * SELVA_EINVAL if the string cannot be decompressed.
179
181
  */
180
182
  SELVA_EXPORT
181
- int selva_string_decompress(const struct selva_string * restrict s, char * restrict buf)
182
- __attribute__((access(read_only, 1), access(write_only, 2)));
183
+ int selva_string_decompress(struct libdeflate_decompressor *decompressor, const struct selva_string * restrict s, char * restrict buf)
184
+ __attribute__((access(read_only, 2), access(write_only, 3)));
183
185
 
184
186
  /**
185
187
  * Duplicate a string.
@@ -395,6 +397,7 @@ void selva_string_set_compress(struct selva_string *s)
395
397
  /**
396
398
  * Compare two strings.
397
399
  * This function works correctly with compressed strings.
400
+ * @param decompress can be nullptr if strings are not compressed.
398
401
  * @param a is a pointer to the first string to be compared.
399
402
  * @param b is a pointer to the second strings to be compared.
400
403
  * @returns < 0 if the first character that does not match has a lower value in ptr1 than in ptr2;
@@ -402,7 +405,7 @@ void selva_string_set_compress(struct selva_string *s)
402
405
  * > 0 if the first character that does not match has a greater value in ptr1 than in ptr2.
403
406
  */
404
407
  SELVA_EXPORT
405
- int selva_string_cmp(const struct selva_string *a, const struct selva_string *b)
408
+ int selva_string_cmp(struct libdeflate_decompressor *decompressor, const struct selva_string *a, const struct selva_string *b)
406
409
  __attribute__((access(read_only, 1), access(read_only, 2)));
407
410
 
408
411
  /**
@@ -0,0 +1,37 @@
1
+ /*
2
+ * Copyright (c) 2025 SAULX
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+ #pragma once
6
+ #include <stdint.h>
7
+
8
+ static inline uint64_t selva_get_thread_id(void);
9
+
10
+ #ifdef __linux__
11
+ #define _GNU_SOURCE
12
+ #include <unistd.h>
13
+
14
+ /*
15
+ * Sometimes the symbol for getid not there even if _GNU_SOURCE should now
16
+ * expose it on glibc 2.30+.
17
+ */
18
+ pid_t gettid(void);
19
+
20
+ static inline uint64_t selva_get_thread_id(void)
21
+ {
22
+ return (uint64_t)gettid();
23
+ }
24
+ #elifdef __APPLE__
25
+ #include <pthread.h>
26
+
27
+ static inline uint64_t selva_get_thread_id(void)
28
+ {
29
+ uint64_t tid;
30
+
31
+ (void)pthread_threadid_np((void *)0, &tid);
32
+
33
+ return tid;
34
+ }
35
+ #else
36
+ #error "Platform not supported"
37
+ #endif
@@ -8,11 +8,21 @@
8
8
  #include <stdint.h>
9
9
  #include "selva/_export.h"
10
10
 
11
+ #define SELVA_FIELDS_MAX 249
12
+
13
+ /**
14
+ * Reserved (N/A) field id.
15
+ * Can be used to mark that a field doesn't exist.
16
+ * E.g. if created and updated fields don't exist their ids can be set to this
17
+ * value.
18
+ * Technically fields 251..255 are all reserved.
19
+ */
20
+ #define SELVA_FIELDS_RESERVED 255
21
+
11
22
  typedef uint32_t block_id_t;
12
23
  typedef uint8_t field_t;
13
24
  typedef uint32_t node_id_t;
14
25
  typedef uint16_t node_type_t;
15
- typedef int32_t cursor_id_t;
16
26
  #ifndef __zig
17
27
  typedef unsigned _BitInt(128) selva_hash128_t;
18
28
  #else
@@ -81,14 +91,18 @@ struct SelvaFieldSchema {
81
91
  } __designated_init;
82
92
 
83
93
  struct SelvaFieldsSchema {
84
- field_t nr_fields; /*!< The total number of fields for this node type. */
85
- field_t nr_fixed_fields; /*!< Number of fixed fields that are always allocated. */
94
+ field_t nr_fields; /*!< The total number of fields in this schema. */
95
+ field_t nr_fixed_fields; /*!< The number of fixed fields that are always allocated. */
96
+ field_t nr_virtual_fields; /*!< The number of fields that are not included in fields.fields_map. These must be the last field ids used. */
97
+ /**
98
+ * Template for fields->fields_map.
99
+ */
86
100
  struct {
87
101
  void *buf;
88
102
  size_t len;
89
103
  size_t fixed_data_size;
90
104
  } field_map_template;
91
- struct SelvaFieldSchema field_schemas[255];
105
+ struct SelvaFieldSchema field_schemas[SELVA_FIELDS_MAX];
92
106
  };
93
107
 
94
108
  struct SelvaNodeSchema {
Binary file
Binary file
@@ -1,14 +1,11 @@
1
- import { Schema } from '@based/schema';
2
- import { CreateObj } from './modify/create.js';
3
- import { ModifyCtx } from './flushModify.js';
1
+ import { MigrateFns, Schema } from '@based/schema';
4
2
  import { BasedDbQuery, QueryByAliasObj } from './query/BasedDbQuery.js';
5
- import { ModifyRes } from './modify/ModifyRes.js';
6
- import { TransformFns } from '../server/migrate/index.js';
7
- import { ModifyOpts } from './modify/types.js';
8
3
  import { SubStore } from './query/subscription/index.js';
9
4
  import { DbShared } from '../shared/DbBase.js';
10
5
  import { DbClientHooks } from '../hooks.js';
11
6
  import { SchemaChecksum } from '../schema.js';
7
+ import { ModifyOpts } from './modify/types.js';
8
+ import { Ctx } from './modify/Ctx.js';
12
9
  type DbClientOpts = {
13
10
  hooks: DbClientHooks;
14
11
  maxModifySize?: number;
@@ -20,30 +17,28 @@ export declare class DbClient extends DbShared {
20
17
  subs: Map<BasedDbQuery, SubStore>;
21
18
  hooks: DbClientHooks;
22
19
  flushTime: number;
23
- flushReady: () => void;
24
- flushIsReady: Promise<void>;
25
20
  writeTime: number;
26
21
  isDraining: boolean;
27
- modifyCtx: ModifyCtx;
22
+ modifyCtx: Ctx;
28
23
  maxModifySize: number;
29
24
  upserting: Map<number, {
30
25
  o: Record<string, any>;
31
- p: Promise<number | ModifyRes>;
26
+ p: Promise<number>;
32
27
  }>;
33
28
  schemaIsSet(): Promise<void>;
34
- setSchema(schema: Schema, transformFns?: TransformFns): Promise<SchemaChecksum>;
35
- create(type: string, obj?: CreateObj, opts?: ModifyOpts): ModifyRes;
36
- copy(type: string, target: number | ModifyRes, objOrTransformFn?: Record<string, any> | ((item: Record<string, any>) => Promise<any>)): Promise<ModifyRes>;
37
- query(type: string, id?: number | ModifyRes | (number | ModifyRes)[] | QueryByAliasObj | QueryByAliasObj[] | Uint32Array): BasedDbQuery;
29
+ setSchema(schema: Schema, transformFns?: MigrateFns): Promise<SchemaChecksum>;
30
+ create(type: string, obj?: {}, opts?: ModifyOpts): Promise<number>;
31
+ copy(type: string, target: number, objOrTransformFn?: Record<string, any> | ((item: Record<string, any>) => Promise<any>)): Promise<number>;
32
+ query(type: string, id?: number | Promise<number> | (number | Promise<number>)[] | QueryByAliasObj | QueryByAliasObj[] | Uint32Array): BasedDbQuery;
38
33
  query(): BasedDbQuery;
39
- update(type: string, id: number | ModifyRes, value: any, opts?: ModifyOpts): ModifyRes;
34
+ update(type: string, id: number | Promise<number>, value: any, opts?: ModifyOpts): Promise<number>;
40
35
  update(type: string, value: Record<string, any> & {
41
- id: number | ModifyRes;
42
- }, opts?: ModifyOpts): ModifyRes;
43
- update(value: Record<string, any>, opts?: ModifyOpts): ModifyRes;
44
- upsert(type: string, obj: Record<string, any>, opts?: ModifyOpts): Promise<any>;
45
- delete(type: string, id: number | ModifyRes): ModifyRes;
46
- expire(type: string, id: number | ModifyRes, seconds: number): void;
36
+ id: number;
37
+ }, opts?: ModifyOpts): Promise<number>;
38
+ update(value: Record<string, any>, opts?: ModifyOpts): Promise<number>;
39
+ upsert(type: string, obj: Record<string, any>, opts?: ModifyOpts): Promise<number>;
40
+ delete(type: string, id: number | Promise<number>): any;
41
+ expire(type: string, id: number, seconds: number): Promise<number>;
47
42
  destroy(): void;
48
43
  stop(): void;
49
44
  drain(): Promise<number>;