@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
@@ -1,308 +0,0 @@
1
- import { REFERENCES } from '@based/schema/def';
2
- import { ModifyError, ModifyState } from '../ModifyRes.js';
3
- import { setCursor } from '../setCursor.js';
4
- import { DELETE, EDGE_INDEX_REALID, EDGE_INDEX_TMPID, EDGE_NOINDEX_REALID, EDGE_NOINDEX_TMPID, NOEDGE_INDEX_REALID, NOEDGE_INDEX_TMPID, NOEDGE_NOINDEX_REALID, NOEDGE_NOINDEX_TMPID, RANGE_ERR, REF_OP_OVERWRITE, REF_OP_PUT_ADD, REF_OP_PUT_OVERWRITE, REF_OP_UPDATE, } from '../types.js';
5
- import { writeEdges } from './edge.js';
6
- export function writeReferences(value, ctx, schema, def, res, mod) {
7
- if (typeof value !== 'object') {
8
- return new ModifyError(def, value);
9
- }
10
- if (value === null || (Array.isArray(value) && value.length === 0)) {
11
- if (ctx.len + 11 > ctx.max) {
12
- return RANGE_ERR;
13
- }
14
- setCursor(ctx, schema, def.prop, def.typeIndex, res.tmpId, mod);
15
- ctx.buf[ctx.len++] = DELETE;
16
- return;
17
- }
18
- if (Array.isArray(value)) {
19
- return updateRefs(def, ctx, schema, mod, value, res.tmpId, 0);
20
- }
21
- for (const key in value) {
22
- const val = value[key];
23
- let err;
24
- if (!Array.isArray(val)) {
25
- err = new ModifyError(def, value);
26
- }
27
- else if (key === 'delete') {
28
- err = deleteRefs(def, ctx, schema, mod, val, res.tmpId);
29
- }
30
- else if (key === 'update') {
31
- err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
32
- }
33
- else if (key === 'add') {
34
- err = updateRefs(def, ctx, schema, mod, val, res.tmpId, 1);
35
- }
36
- else if (key === 'upsert') {
37
- dbUpdateFromUpsert(ctx, schema, def, res, Promise.all(val.map((val) => ctx.db.upsert(def.inverseTypeName, val))));
38
- }
39
- else {
40
- err = new ModifyError(def, value);
41
- }
42
- if (err) {
43
- return err;
44
- }
45
- }
46
- }
47
- export const dbUpdateFromUpsert = (ctx, schema, def, res, promise) => {
48
- res.promises ??= [];
49
- res.promises.push(promise.then((result) => {
50
- let payload = {};
51
- let i = 0;
52
- for (; i < def.path.length - 1; i++) {
53
- payload = payload[def.path[i]] = {};
54
- }
55
- payload[def.path[i]] = result;
56
- return ctx.db.update(schema.type, res.getId(), payload);
57
- }));
58
- };
59
- function deleteRefs(def, ctx, schema, modifyOp, refs, parentId) {
60
- let size = 4 * refs.length + 1;
61
- if (ctx.len + 10 + size > ctx.max) {
62
- return RANGE_ERR;
63
- }
64
- setCursor(ctx, schema, def.prop, def.typeIndex, parentId, modifyOp);
65
- ctx.buf[ctx.len++] = modifyOp;
66
- ctx.buf[ctx.len++] = size;
67
- ctx.buf[ctx.len++] = size >>>= 8;
68
- ctx.buf[ctx.len++] = size >>>= 8;
69
- ctx.buf[ctx.len++] = size >>>= 8;
70
- ctx.buf[ctx.len++] = 2;
71
- for (let ref of refs) {
72
- if (typeof ref === 'number') {
73
- ctx.buf[ctx.len++] = ref;
74
- ctx.buf[ctx.len++] = ref >>>= 8;
75
- ctx.buf[ctx.len++] = ref >>>= 8;
76
- ctx.buf[ctx.len++] = ref >>>= 8;
77
- }
78
- else if (ref instanceof ModifyState) {
79
- if (ref.error) {
80
- return ref.error;
81
- }
82
- ref = ref.tmpId;
83
- ctx.buf[ctx.len++] = ref;
84
- ctx.buf[ctx.len++] = ref >>>= 8;
85
- ctx.buf[ctx.len++] = ref >>>= 8;
86
- ctx.buf[ctx.len++] = ref >>>= 8;
87
- }
88
- else {
89
- return new ModifyError(def, refs);
90
- }
91
- }
92
- }
93
- function updateRefs(def, ctx, schema, mod, refs, parentId, op) {
94
- if (ctx.len + 19 + refs.length * 4 > ctx.max) {
95
- return RANGE_ERR;
96
- }
97
- setCursor(ctx, schema, def.prop, def.typeIndex, parentId, mod);
98
- const initpos = ctx.len;
99
- const nrOrErr = putRefs(def, ctx, mod, refs, op);
100
- if (nrOrErr) {
101
- if (typeof nrOrErr === 'number') {
102
- if (nrOrErr === refs.length) {
103
- ctx.len = initpos;
104
- }
105
- else if (ctx.len + 2 > ctx.max) {
106
- return RANGE_ERR;
107
- }
108
- else {
109
- ctx.buf[ctx.len++] = 0;
110
- ctx.buf[ctx.len++] = REFERENCES;
111
- }
112
- return appendRefs(def, ctx, mod, refs, op, nrOrErr);
113
- }
114
- return nrOrErr;
115
- }
116
- }
117
- function appendRefs(def, ctx, modifyOp, refs, op, remaining) {
118
- if (ctx.len + 10 > ctx.max) {
119
- return RANGE_ERR;
120
- }
121
- const hasEdges = !!def.edges;
122
- ctx.buf[ctx.len++] = modifyOp;
123
- let i = refs.length - remaining;
124
- let totalpos = ctx.len;
125
- ctx.len += 4;
126
- // if it just did a PUT, it should ADD not overwrite the remaining
127
- ctx.buf[ctx.len++] = i === 0 ? op : REF_OP_UPDATE;
128
- ctx.buf[ctx.len++] = remaining;
129
- ctx.buf[ctx.len++] = remaining >>>= 8;
130
- ctx.buf[ctx.len++] = remaining >>>= 8;
131
- ctx.buf[ctx.len++] = remaining >>>= 8;
132
- for (; i < refs.length; i++) {
133
- const ref = refs[i];
134
- let id;
135
- let index;
136
- let isTmpId;
137
- if (typeof ref === 'object') {
138
- if (ref instanceof ModifyState) {
139
- if (ref.error) {
140
- return ref.error;
141
- }
142
- const resolvedId = ref.getId();
143
- if (resolvedId) {
144
- id = resolvedId;
145
- }
146
- else {
147
- id = ref.tmpId;
148
- isTmpId = true;
149
- }
150
- }
151
- else if (ref.id instanceof ModifyState) {
152
- if (ref.id.error) {
153
- return ref.id.error;
154
- }
155
- const resolvedId = ref.id.getId();
156
- if (resolvedId) {
157
- id = resolvedId;
158
- }
159
- else {
160
- id = ref.id.tmpId;
161
- isTmpId = true;
162
- }
163
- index = ref.$index;
164
- }
165
- else if (ref.id > 0) {
166
- id = ref.id;
167
- index = ref.$index;
168
- }
169
- else {
170
- return new ModifyError(def, refs);
171
- }
172
- }
173
- else if (ref > 0) {
174
- id = ref;
175
- }
176
- else {
177
- return new ModifyError(def, refs);
178
- }
179
- if (!def.validation(id, def)) {
180
- return new ModifyError(def, refs);
181
- }
182
- if (hasEdges && typeof ref === 'object' && !(ref instanceof ModifyState)) {
183
- if (index === undefined) {
184
- if (ctx.len + 9 > ctx.max) {
185
- return RANGE_ERR;
186
- }
187
- ctx.buf[ctx.len++] = isTmpId ? EDGE_NOINDEX_TMPID : EDGE_NOINDEX_REALID;
188
- ctx.buf[ctx.len++] = id;
189
- ctx.buf[ctx.len++] = id >>>= 8;
190
- ctx.buf[ctx.len++] = id >>>= 8;
191
- ctx.buf[ctx.len++] = id >>>= 8;
192
- }
193
- else if (typeof index === 'number') {
194
- if (ctx.len + 13 > ctx.max) {
195
- return RANGE_ERR;
196
- }
197
- ctx.buf[ctx.len++] = isTmpId ? EDGE_INDEX_TMPID : EDGE_INDEX_REALID;
198
- ctx.buf[ctx.len++] = id;
199
- ctx.buf[ctx.len++] = id >>>= 8;
200
- ctx.buf[ctx.len++] = id >>>= 8;
201
- ctx.buf[ctx.len++] = id >>>= 8;
202
- ctx.buf[ctx.len++] = index;
203
- ctx.buf[ctx.len++] = index >>>= 8;
204
- ctx.buf[ctx.len++] = index >>>= 8;
205
- ctx.buf[ctx.len++] = index >>>= 8;
206
- }
207
- else {
208
- return new ModifyError(def, refs);
209
- }
210
- let sizepos = ctx.len;
211
- ctx.len += 4;
212
- const err = writeEdges(def, ref, ctx);
213
- if (err) {
214
- return err;
215
- }
216
- let size = ctx.len - sizepos - 4;
217
- ctx.buf[sizepos++] = size;
218
- ctx.buf[sizepos++] = size >>>= 8;
219
- ctx.buf[sizepos++] = size >>>= 8;
220
- ctx.buf[sizepos] = size >>>= 8;
221
- }
222
- else if (index === undefined) {
223
- if (ctx.len + 5 > ctx.max) {
224
- return RANGE_ERR;
225
- }
226
- ctx.buf[ctx.len++] = isTmpId
227
- ? NOEDGE_NOINDEX_TMPID
228
- : NOEDGE_NOINDEX_REALID;
229
- ctx.buf[ctx.len++] = id;
230
- ctx.buf[ctx.len++] = id >>>= 8;
231
- ctx.buf[ctx.len++] = id >>>= 8;
232
- ctx.buf[ctx.len++] = id >>>= 8;
233
- }
234
- else if (typeof index === 'number') {
235
- if (ctx.len + 9 > ctx.max) {
236
- return RANGE_ERR;
237
- }
238
- ctx.buf[ctx.len++] = isTmpId ? NOEDGE_INDEX_TMPID : NOEDGE_INDEX_REALID;
239
- ctx.buf[ctx.len++] = id;
240
- ctx.buf[ctx.len++] = id >>>= 8;
241
- ctx.buf[ctx.len++] = id >>>= 8;
242
- ctx.buf[ctx.len++] = id >>>= 8;
243
- ctx.buf[ctx.len++] = index;
244
- ctx.buf[ctx.len++] = index >>>= 8;
245
- ctx.buf[ctx.len++] = index >>>= 8;
246
- ctx.buf[ctx.len++] = index >>>= 8;
247
- }
248
- else {
249
- return new ModifyError(def, refs);
250
- }
251
- ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], id);
252
- }
253
- let size = ctx.len - totalpos - 4;
254
- ctx.buf[totalpos++] = size;
255
- ctx.buf[totalpos++] = size >>>= 8;
256
- ctx.buf[totalpos++] = size >>>= 8;
257
- ctx.buf[totalpos] = size >>>= 8;
258
- }
259
- function putRefs(def, ctx, modifyOp, refs, op) {
260
- let size = refs.length * 4 + 1;
261
- if (refs.length === 0) {
262
- return 0;
263
- }
264
- ctx.buf[ctx.len++] = modifyOp;
265
- ctx.buf[ctx.len++] = size;
266
- ctx.buf[ctx.len++] = size >>>= 8;
267
- ctx.buf[ctx.len++] = size >>>= 8;
268
- ctx.buf[ctx.len++] = size >>>= 8;
269
- ctx.buf[ctx.len++] =
270
- op === REF_OP_OVERWRITE ? REF_OP_PUT_OVERWRITE : REF_OP_PUT_ADD;
271
- let i = 0;
272
- for (; i < refs.length; i++) {
273
- let ref = refs[i];
274
- if (typeof ref === 'number') {
275
- if (!def.validation(ref, def)) {
276
- return new ModifyError(def, ref);
277
- }
278
- else {
279
- ctx.buf[ctx.len++] = ref;
280
- ctx.buf[ctx.len++] = ref >>>= 8;
281
- ctx.buf[ctx.len++] = ref >>>= 8;
282
- ctx.buf[ctx.len++] = ref >>>= 8;
283
- }
284
- }
285
- else if (ref instanceof ModifyState) {
286
- if (ref.error) {
287
- return ref.error;
288
- }
289
- ref = ref.getId();
290
- if (!ref) {
291
- break;
292
- }
293
- if (!def.validation(ref, def)) {
294
- return new ModifyError(def, ref);
295
- }
296
- ctx.buf[ctx.len++] = ref;
297
- ctx.buf[ctx.len++] = ref >>>= 8;
298
- ctx.buf[ctx.len++] = ref >>>= 8;
299
- ctx.buf[ctx.len++] = ref >>>= 8;
300
- }
301
- else {
302
- break;
303
- }
304
- ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], ref);
305
- }
306
- return refs.length - i;
307
- }
308
- //# sourceMappingURL=references.js.map
@@ -1,5 +0,0 @@
1
- import { ModifyCtx } from '../../index.js';
2
- import { SchemaTypeDef } from '@based/schema/def';
3
- import { ModifyOp } from './types.js';
4
- export declare const setCursor: (ctx: ModifyCtx, schema: SchemaTypeDef, field: number, // TODO pass propdef better
5
- typeIndex: number, id: number, modifyOp: ModifyOp, ignoreField?: boolean) => void;
@@ -1,33 +0,0 @@
1
- import { CREATE, SWITCH_FIELD, SWITCH_ID_CREATE, SWITCH_ID_UPDATE, SWITCH_TYPE, } from './types.js';
2
- export const setCursor = (ctx, schema, field, // TODO pass propdef better
3
- typeIndex, id, modifyOp, ignoreField) => {
4
- const prefix0 = schema.idUint8[0];
5
- const prefix1 = schema.idUint8[1];
6
- if (ctx.prefix0 !== prefix0 || ctx.prefix1 !== prefix1) {
7
- ctx.buf[ctx.len++] = SWITCH_TYPE; // switch node type
8
- ctx.buf[ctx.len++] = prefix0; // type1
9
- ctx.buf[ctx.len++] = prefix1; // type2
10
- ctx.prefix0 = prefix0;
11
- ctx.prefix1 = prefix1;
12
- ctx.field = -1;
13
- ctx.id = -1;
14
- ctx.lastMain = -1;
15
- }
16
- if (!ignoreField && ctx.field !== field) {
17
- ctx.buf[ctx.len++] = SWITCH_FIELD; // switch field
18
- ctx.buf[ctx.len++] = field; // actual field
19
- ctx.buf[ctx.len++] = typeIndex;
20
- ctx.field = field;
21
- }
22
- if (ctx.id !== id) {
23
- ctx.id = id;
24
- ctx.lastMain = -1;
25
- ctx.buf[ctx.len++] =
26
- modifyOp === CREATE ? SWITCH_ID_CREATE : SWITCH_ID_UPDATE; // swtch id
27
- ctx.buf[ctx.len++] = id;
28
- ctx.buf[ctx.len++] = id >>>= 8;
29
- ctx.buf[ctx.len++] = id >>>= 8;
30
- ctx.buf[ctx.len++] = id >>>= 8;
31
- }
32
- };
33
- //# sourceMappingURL=setCursor.js.map
@@ -1,5 +0,0 @@
1
- import { LangCode } from '@based/schema';
2
- import { ModifyCtx } from '../../index.js';
3
- import { SchemaTypeDef, PropDef } from '@based/schema/def';
4
- import { ModifyOp, ModifyErr } from './types.js';
5
- export declare function writeString(lang: LangCode, value: string | null | Uint8Array, ctx: ModifyCtx, schema: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,71 +0,0 @@
1
- import { ENCODER } from '../../index.js';
2
- import { CREATE, UPDATE, RANGE_ERR, DELETE, DELETE_TEXT_FIELD, MOD_OPS_TO_STRING, } from './types.js';
3
- import { ModifyError } from './ModifyRes.js';
4
- import { setCursor } from './setCursor.js';
5
- import { write } from '../string.js';
6
- // allow setting buffer in modify create for strings
7
- // add compression handling for main buffer
8
- // add compression handling for edge fields
9
- export function writeString(lang, value, ctx, schema, t, parentId, modifyOp) {
10
- const isBuffer = value instanceof Uint8Array;
11
- if (value === null || value === '') {
12
- if (modifyOp === UPDATE) {
13
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 2 > ctx.max) {
14
- return RANGE_ERR;
15
- }
16
- setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
17
- if (lang === 0) {
18
- ctx.buf[ctx.len++] = DELETE;
19
- }
20
- else {
21
- ctx.buf[ctx.len++] = DELETE_TEXT_FIELD;
22
- ctx.buf[ctx.len++] = lang;
23
- }
24
- }
25
- }
26
- else {
27
- if (!t.validation(value, t)) {
28
- return new ModifyError(t, value);
29
- }
30
- if (t.transform) {
31
- value = t.transform(MOD_OPS_TO_STRING[modifyOp], value);
32
- }
33
- let size = isBuffer
34
- ? // @ts-ignore
35
- value.byteLength
36
- : // @ts-ignore
37
- ENCODER.encode(value).byteLength + 6;
38
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 11 + size > ctx.max) {
39
- return RANGE_ERR;
40
- }
41
- if (modifyOp === CREATE) {
42
- schema.seperateSort.bufferTmp[t.prop] = 2;
43
- ctx.hasSortField++;
44
- if (schema.hasSeperateDefaults) {
45
- schema.seperateDefaults.bufferTmp[t.prop] = 1;
46
- ctx.hasDefaults++;
47
- }
48
- }
49
- setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
50
- // TODO if buffer check if second byte is zero or one
51
- // modOp | size u32 | stringprotocol | string
52
- ctx.buf[ctx.len] = modifyOp;
53
- ctx.len += 5;
54
- if (isBuffer) {
55
- // @ts-ignore
56
- ctx.buf.set(value, ctx.len);
57
- }
58
- else {
59
- const isNoCompression = t.compression === 0;
60
- // @ts-ignore stupid str
61
- size = write(ctx.buf, value, ctx.len, isNoCompression, lang);
62
- }
63
- let sizepos = ctx.len + 1 - 5;
64
- ctx.len += size;
65
- ctx.buf[sizepos++] = size;
66
- ctx.buf[sizepos++] = size >>>= 8;
67
- ctx.buf[sizepos++] = size >>>= 8;
68
- ctx.buf[sizepos] = size >>>= 8;
69
- }
70
- }
71
- //# sourceMappingURL=string.js.map
@@ -1,8 +0,0 @@
1
- import { ModifyCtx } from '../../index.js';
2
- import { ModifyOp, ModifyErr } from './types.js';
3
- import { SchemaTypeDef, PropDef } from '@based/schema/def';
4
- import { writeString } from './string.js';
5
- import { ModifyState } from './ModifyRes.js';
6
- export declare function writeText(value: {
7
- [k: string]: Parameters<typeof writeString>[1];
8
- } | Parameters<typeof writeString>[1], ctx: ModifyCtx, schema: SchemaTypeDef, t: PropDef, res: ModifyState, id: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,69 +0,0 @@
1
- import { CREATE, UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
- import { writeString } from './string.js';
3
- import { ModifyError } from './ModifyRes.js';
4
- import { setCursor } from './setCursor.js';
5
- export function writeText(value, ctx, schema, t, res, id, modifyOp) {
6
- if (value === null && !res.locale) {
7
- if (modifyOp === UPDATE) {
8
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
9
- return RANGE_ERR;
10
- }
11
- setCursor(ctx, schema, t.prop, t.typeIndex, id, modifyOp);
12
- ctx.buf[ctx.len++] = DELETE;
13
- }
14
- return;
15
- }
16
- if ((value && typeof value !== 'object') || value === null || value === '') {
17
- let locale = res.locale;
18
- if (!locale) {
19
- // TODO: Add def lang option...
20
- for (const localeCode of schema.seperateTextSort.localeToIndex.keys()) {
21
- locale = localeCode;
22
- break;
23
- }
24
- }
25
- if (!schema.seperateTextSort.localeToIndex.has(locale)) {
26
- return new ModifyError(t, locale, 'Invalid locale');
27
- }
28
- const err = writeString(locale, value, ctx, schema, t, res.tmpId, modifyOp);
29
- if (modifyOp === CREATE) {
30
- const index = t.prop * (schema.localeSize + 1);
31
- const langIndex = schema.seperateTextSort.localeToIndex.get(locale);
32
- schema.seperateTextSort.bufferTmp[index] -= 1;
33
- schema.seperateTextSort.bufferTmp[index + langIndex] = 0;
34
- ctx.hasSortText += 1;
35
- if (schema.hasSeperateDefaults) {
36
- schema.seperateDefaults.bufferTmp[t.prop] = 1;
37
- ctx.hasDefaults++;
38
- }
39
- }
40
- return err;
41
- }
42
- else {
43
- if (modifyOp === CREATE && schema.hasSeperateDefaults) {
44
- ctx.hasDefaults++;
45
- }
46
- // @ts-ignore
47
- for (const lang in value) {
48
- const langC = schema.seperateTextSort.localeStringToIndex.get(lang);
49
- if (!langC) {
50
- return new ModifyError(t, lang, 'Invalid locale');
51
- }
52
- let s = value[lang];
53
- const err = writeString(langC[1], s, ctx, schema, t, res.tmpId, modifyOp);
54
- if (err) {
55
- return err;
56
- }
57
- if (modifyOp === CREATE) {
58
- const index = t.prop * (1 + schema.localeSize);
59
- schema.seperateTextSort.bufferTmp[index] -= 1;
60
- schema.seperateTextSort.bufferTmp[index + langC[0]] = 0;
61
- ctx.hasSortText += 1;
62
- if (schema.hasSeperateDefaults) {
63
- schema.seperateDefaults.bufferTmp[t.prop]++;
64
- }
65
- }
66
- }
67
- }
68
- }
69
- //# sourceMappingURL=text.js.map
@@ -1,6 +0,0 @@
1
- import { ModifyRes } from './ModifyRes.js';
2
- import { ModifyOpts } from './types.js';
3
- import { DbClient } from '../index.js';
4
- type Payload = Record<string, any>;
5
- export declare const update: (db: DbClient, type: string, id: number, obj: Payload, opts?: ModifyOpts) => ModifyRes;
6
- export {};
@@ -1,90 +0,0 @@
1
- import { MICRO_BUFFER, isValidId, } from '@based/schema/def';
2
- import { startDrain, flushBuffer } from '../flushModify.js';
3
- import { setCursor } from './setCursor.js';
4
- import { modify } from './modify.js';
5
- import { ModifyState } from './ModifyRes.js';
6
- import { RANGE_ERR, UPDATE } from './types.js';
7
- import { appendFixedValue } from './fixed.js';
8
- import { getByPath } from '@saulx/utils';
9
- const appendUpdate = (ctx, def, obj, res, overwrite) => {
10
- const err = modify(ctx, res, obj, def, UPDATE, def.tree, overwrite);
11
- if (err) {
12
- return err;
13
- }
14
- if (def.updateTs) {
15
- const updateTs = Date.now();
16
- for (const prop of def.updateTs) {
17
- if (getByPath(obj, prop.path) !== undefined) {
18
- continue;
19
- }
20
- if (ctx.mergeMain) {
21
- ctx.mergeMain.push(prop, updateTs);
22
- ctx.mergeMainSize += prop.len + 4;
23
- }
24
- else {
25
- ctx.mergeMain = [prop, updateTs];
26
- ctx.mergeMainSize = prop.len + 4;
27
- }
28
- }
29
- }
30
- if (ctx.mergeMain) {
31
- let { mergeMain, mergeMainSize } = ctx;
32
- ctx.mergeMainSize = 0;
33
- ctx.mergeMain = null;
34
- if (ctx.len + 15 + mergeMain.length * 4 > ctx.max) {
35
- return RANGE_ERR;
36
- }
37
- setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, UPDATE);
38
- ctx.buf[ctx.len++] = 5;
39
- ctx.buf[ctx.len++] = mergeMainSize;
40
- ctx.buf[ctx.len++] = mergeMainSize >>>= 8;
41
- ctx.buf[ctx.len++] = mergeMainSize >>>= 8;
42
- ctx.buf[ctx.len++] = mergeMainSize >>>= 8;
43
- for (let i = 0; i < mergeMain.length; i += 2) {
44
- const t = mergeMain[i];
45
- const v = mergeMain[i + 1];
46
- let { start, len } = t;
47
- ctx.buf[ctx.len++] = start;
48
- ctx.buf[ctx.len++] = start >>>= 8;
49
- ctx.buf[ctx.len++] = len;
50
- ctx.buf[ctx.len++] = len >>>= 8;
51
- const err = appendFixedValue(ctx, v, t, UPDATE);
52
- if (err) {
53
- return err;
54
- }
55
- }
56
- }
57
- };
58
- export const update = (db, type, id, obj, opts) => {
59
- const def = db.schemaTypesParsed[type];
60
- if (!def) {
61
- throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
62
- }
63
- if (!isValidId(id)) {
64
- throw new Error(`Update ${id} is not a valid id`);
65
- }
66
- const ctx = db.modifyCtx;
67
- const pos = ctx.len;
68
- const res = new ModifyState(def.id, id, db, opts, true);
69
- const err = appendUpdate(ctx, def, obj, res, opts?.overwrite);
70
- if (err) {
71
- ctx.prefix0 = -1; // force a new cursor
72
- ctx.len = pos;
73
- if (err === RANGE_ERR) {
74
- if (pos === 8) {
75
- throw new Error('out of range');
76
- }
77
- void flushBuffer(db);
78
- return update(db, type, id, obj, opts);
79
- }
80
- res.error = err;
81
- throw err;
82
- }
83
- ctx.markNodeDirty(def, id);
84
- if (!db.isDraining) {
85
- startDrain(db);
86
- }
87
- // @ts-ignore
88
- return res;
89
- };
90
- //# sourceMappingURL=update.js.map
@@ -1,3 +0,0 @@
1
- import { DbClient } from '../index.js';
2
- import { ModifyOpts } from './types.js';
3
- export declare function upsert(db: DbClient, type: string, obj: Record<string, any>, opts?: ModifyOpts): Promise<any>;
@@ -1,4 +0,0 @@
1
- import { ModifyCtx } from '../../index.js';
2
- import { PropDef, SchemaTypeDef } from '@based/schema/def';
3
- import { ModifyOp, ModifyErr } from './types.js';
4
- export declare function writeVector(value: any, ctx: ModifyCtx, schema: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,53 +0,0 @@
1
- import { UPDATE, RANGE_ERR, DELETE, } from './types.js';
2
- import { ModifyError } from './ModifyRes.js';
3
- import { setCursor } from './setCursor.js';
4
- function write(value, ctx, fieldSize) {
5
- let size = Math.min(value.byteLength, fieldSize);
6
- let padding = 0;
7
- if (ctx.len % 4 != 0) {
8
- padding = ctx.len % 4;
9
- }
10
- size -= padding;
11
- let tmp = size + 4;
12
- // 16-bits would be enough but the zig expects 32-bits
13
- ctx.buf[ctx.len++] = tmp;
14
- ctx.buf[ctx.len++] = tmp >>>= 8;
15
- ctx.buf[ctx.len++] = tmp >>>= 8;
16
- ctx.buf[ctx.len++] = tmp >>>= 8;
17
- ctx.buf[ctx.len++] = padding;
18
- ctx.buf[ctx.len++] = 0;
19
- ctx.buf[ctx.len++] = 0;
20
- ctx.buf[ctx.len++] = 0;
21
- ctx.buf.set(new Uint8Array(value.buffer).subarray(0, size), ctx.len - padding);
22
- ctx.len += size;
23
- }
24
- export function writeVector(value, ctx, schema, t, parentId, modifyOp) {
25
- let size;
26
- if (value === null) {
27
- size = 0;
28
- }
29
- else {
30
- size = value.byteLength + 4;
31
- }
32
- if (!t.validation(value, t)) {
33
- return new ModifyError(t, value);
34
- }
35
- if (size === 0) {
36
- if (modifyOp === UPDATE) {
37
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 1 > ctx.max) {
38
- return RANGE_ERR;
39
- }
40
- setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
41
- ctx.buf[ctx.len++] = DELETE;
42
- }
43
- }
44
- else {
45
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + size > ctx.max) {
46
- return RANGE_ERR;
47
- }
48
- setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
49
- ctx.buf[ctx.len++] = modifyOp;
50
- write(value, ctx, t.len);
51
- }
52
- }
53
- //# sourceMappingURL=vector.js.map