@based/db 0.0.71 → 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 (259) 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/mblen.h +40 -0
  9. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +8 -5
  10. package/dist/lib/darwin_aarch64/include/selva/thread.h +37 -0
  11. package/dist/lib/darwin_aarch64/include/selva/types.h +18 -4
  12. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  13. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  14. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  15. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  16. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  17. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  18. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  19. package/dist/lib/linux_aarch64/include/libdeflate.h +23 -0
  20. package/dist/lib/linux_aarch64/include/selva/db.h +0 -41
  21. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +6 -2
  22. package/dist/lib/linux_aarch64/include/selva/fields.h +52 -37
  23. package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
  24. package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
  25. package/dist/lib/linux_aarch64/include/selva/selva_string.h +8 -5
  26. package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
  27. package/dist/lib/linux_aarch64/include/selva/types.h +18 -4
  28. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  29. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  30. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  31. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  32. package/dist/lib/linux_aarch64/libselva.so +0 -0
  33. package/dist/lib/linux_x86_64/include/libdeflate.h +23 -0
  34. package/dist/lib/linux_x86_64/include/selva/db.h +0 -41
  35. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +6 -2
  36. package/dist/lib/linux_x86_64/include/selva/fields.h +52 -37
  37. package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
  38. package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
  39. package/dist/lib/linux_x86_64/include/selva/selva_string.h +8 -5
  40. package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
  41. package/dist/lib/linux_x86_64/include/selva/types.h +18 -4
  42. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  43. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  44. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  45. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  46. package/dist/lib/linux_x86_64/libselva.so +0 -0
  47. package/dist/src/client/index.d.ts +16 -21
  48. package/dist/src/client/index.js +48 -59
  49. package/dist/src/client/modify/Ctx.d.ts +38 -0
  50. package/dist/src/client/modify/Ctx.js +30 -0
  51. package/dist/src/client/modify/Tmp.d.ts +19 -0
  52. package/dist/src/client/modify/Tmp.js +67 -0
  53. package/dist/src/client/modify/create/index.d.ts +6 -0
  54. package/dist/src/client/modify/create/index.js +191 -0
  55. package/dist/src/client/modify/create/mark.d.ts +7 -0
  56. package/dist/src/client/modify/create/mark.js +42 -0
  57. package/dist/src/client/modify/cursor.d.ts +10 -0
  58. package/dist/src/client/modify/cursor.js +48 -0
  59. package/dist/src/client/modify/delete/index.d.ts +2 -0
  60. package/dist/src/client/modify/delete/index.js +39 -0
  61. package/dist/src/client/modify/drain.d.ts +7 -0
  62. package/dist/src/client/modify/drain.js +77 -0
  63. package/dist/src/client/modify/edges/binary.d.ts +3 -0
  64. package/dist/src/client/modify/edges/binary.js +28 -0
  65. package/dist/src/client/modify/edges/cardinality.d.ts +3 -0
  66. package/dist/src/client/modify/edges/cardinality.js +15 -0
  67. package/dist/src/client/modify/edges/header.d.ts +5 -0
  68. package/dist/src/client/modify/edges/header.js +21 -0
  69. package/dist/src/client/modify/edges/index.d.ts +3 -0
  70. package/dist/src/client/modify/edges/index.js +128 -0
  71. package/dist/src/client/modify/edges/reference.d.ts +3 -0
  72. package/dist/src/client/modify/edges/reference.js +31 -0
  73. package/dist/src/client/modify/edges/references.d.ts +3 -0
  74. package/dist/src/client/modify/edges/references.js +38 -0
  75. package/dist/src/client/modify/edges/separate.d.ts +3 -0
  76. package/dist/src/client/modify/edges/separate.js +24 -0
  77. package/dist/src/client/modify/edges/string.d.ts +3 -0
  78. package/dist/src/client/modify/edges/string.js +30 -0
  79. package/dist/src/client/modify/error.d.ts +7 -0
  80. package/dist/src/client/modify/error.js +68 -0
  81. package/dist/src/client/modify/expire/index.d.ts +2 -0
  82. package/dist/src/client/modify/expire/index.js +31 -0
  83. package/dist/src/client/modify/props/alias.d.ts +3 -0
  84. package/dist/src/client/modify/props/alias.js +26 -0
  85. package/dist/src/client/modify/props/binary.d.ts +5 -0
  86. package/dist/src/client/modify/props/binary.js +52 -0
  87. package/dist/src/client/modify/props/cardinality.d.ts +4 -0
  88. package/dist/src/client/modify/props/cardinality.js +46 -0
  89. package/dist/src/client/modify/props/delete.d.ts +3 -0
  90. package/dist/src/client/modify/props/delete.js +13 -0
  91. package/dist/src/client/modify/props/fixed.d.ts +4 -0
  92. package/dist/src/client/modify/props/fixed.js +105 -0
  93. package/dist/src/client/modify/props/increment.d.ts +3 -0
  94. package/dist/src/client/modify/props/increment.js +28 -0
  95. package/dist/src/client/modify/props/json.d.ts +3 -0
  96. package/dist/src/client/modify/props/json.js +5 -0
  97. package/dist/src/client/modify/props/main.d.ts +4 -0
  98. package/dist/src/client/modify/props/main.js +22 -0
  99. package/dist/src/client/modify/props/object.d.ts +3 -0
  100. package/dist/src/client/modify/props/object.js +37 -0
  101. package/dist/src/client/modify/props/reference.d.ts +6 -0
  102. package/dist/src/client/modify/props/reference.js +92 -0
  103. package/dist/src/client/modify/props/references.d.ts +3 -0
  104. package/dist/src/client/modify/props/references.js +231 -0
  105. package/dist/src/client/modify/props/separate.d.ts +3 -0
  106. package/dist/src/client/modify/props/separate.js +43 -0
  107. package/dist/src/client/modify/props/string.d.ts +5 -0
  108. package/dist/src/client/modify/props/string.js +51 -0
  109. package/dist/src/client/modify/props/text.d.ts +3 -0
  110. package/dist/src/client/modify/props/text.js +35 -0
  111. package/dist/src/client/modify/props/vector.d.ts +3 -0
  112. package/dist/src/client/modify/props/vector.js +30 -0
  113. package/dist/src/client/modify/resize.d.ts +3 -0
  114. package/dist/src/client/modify/resize.js +20 -0
  115. package/dist/src/client/modify/types.d.ts +3 -4
  116. package/dist/src/client/modify/types.js +2 -1
  117. package/dist/src/client/modify/uint.d.ts +7 -0
  118. package/dist/src/client/modify/uint.js +27 -0
  119. package/dist/src/client/modify/update/index.d.ts +3 -0
  120. package/dist/src/client/modify/update/index.js +75 -0
  121. package/dist/src/client/modify/upsert/index.d.ts +3 -0
  122. package/dist/src/client/modify/{upsert.js → upsert/index.js} +4 -4
  123. package/dist/src/client/modify/validate.d.ts +6 -0
  124. package/dist/src/client/modify/validate.js +24 -0
  125. package/dist/src/client/query/BasedDbQuery.d.ts +17 -14
  126. package/dist/src/client/query/BasedDbQuery.js +90 -93
  127. package/dist/src/client/query/{BasedIterable.d.ts → BasedQueryResponse.d.ts} +5 -4
  128. package/dist/src/client/query/{BasedIterable.js → BasedQueryResponse.js} +15 -10
  129. package/dist/src/client/query/aggregates/aggregation.d.ts +5 -3
  130. package/dist/src/client/query/aggregates/aggregation.js +105 -48
  131. package/dist/src/client/query/aggregates/types.d.ts +33 -11
  132. package/dist/src/client/query/aggregates/types.js +29 -11
  133. package/dist/src/client/query/debug.js +1 -1
  134. package/dist/src/client/query/display.d.ts +1 -1
  135. package/dist/src/client/query/display.js +21 -4
  136. package/dist/src/client/query/filter/FilterBranch.js +2 -2
  137. package/dist/src/client/query/filter/convertFilter.d.ts +3 -2
  138. package/dist/src/client/query/filter/convertFilter.js +13 -1
  139. package/dist/src/client/query/filter/createFixedFilterBuffer.js +16 -30
  140. package/dist/src/client/query/filter/createReferenceFilter.js +6 -20
  141. package/dist/src/client/query/filter/createVariableFilterBuffer.js +15 -32
  142. package/dist/src/client/query/filter/parseFilterValue.js +3 -11
  143. package/dist/src/client/query/filter/primitiveFilter.js +1 -1
  144. package/dist/src/client/query/filter/toBuffer.js +9 -15
  145. package/dist/src/client/query/filter/types.d.ts +6 -6
  146. package/dist/src/client/query/filter/types.js +8 -8
  147. package/dist/src/client/query/include/include.d.ts +3 -0
  148. package/dist/src/client/query/include/include.js +58 -0
  149. package/dist/src/client/query/include/props.d.ts +7 -7
  150. package/dist/src/client/query/include/props.js +26 -21
  151. package/dist/src/client/query/include/toByteCode.js +188 -0
  152. package/dist/src/client/query/include/utils.d.ts +3 -1
  153. package/dist/src/client/query/include/utils.js +13 -0
  154. package/dist/src/client/query/include/walk.d.ts +2 -2
  155. package/dist/src/client/query/include/walk.js +38 -23
  156. package/dist/src/client/query/query.d.ts +2 -3
  157. package/dist/src/client/query/query.js +2 -3
  158. package/dist/src/client/query/queryDef.d.ts +4 -2
  159. package/dist/src/client/query/queryDef.js +3 -5
  160. package/dist/src/client/query/queryDefToReadSchema.d.ts +3 -0
  161. package/dist/src/client/query/queryDefToReadSchema.js +126 -0
  162. package/dist/src/client/query/registerQuery.js +15 -4
  163. package/dist/src/client/query/search/index.d.ts +2 -1
  164. package/dist/src/client/query/search/index.js +9 -2
  165. package/dist/src/client/query/sort.js +3 -0
  166. package/dist/src/client/query/subscription/index.d.ts +1 -1
  167. package/dist/src/client/query/subscription/index.js +2 -2
  168. package/dist/src/client/query/subscription/types.d.ts +1 -1
  169. package/dist/src/client/query/thresholds.d.ts +1 -1
  170. package/dist/src/client/query/thresholds.js +1 -1
  171. package/dist/src/client/query/toByteCode/{toBuffer.js → toByteCode.js} +8 -8
  172. package/dist/src/client/query/types.d.ts +40 -25
  173. package/dist/src/client/query/types.js +2 -6
  174. package/dist/src/client/query/validation.d.ts +6 -0
  175. package/dist/src/client/query/validation.js +31 -7
  176. package/dist/src/client/setLocalClientSchema.js +6 -10
  177. package/dist/src/client/string.d.ts +3 -7
  178. package/dist/src/client/string.js +18 -47
  179. package/dist/src/hooks.d.ts +2 -3
  180. package/dist/src/hooks.js +14 -9
  181. package/dist/src/index.d.ts +3 -5
  182. package/dist/src/index.js +4 -6
  183. package/dist/src/native.d.ts +3 -2
  184. package/dist/src/native.js +9 -8
  185. package/dist/src/server/IoWorker.js +2 -3
  186. package/dist/src/server/QueryWorker.js +1 -1
  187. package/dist/src/server/blocks.js +5 -4
  188. package/dist/src/server/dbHash.js +1 -1
  189. package/dist/src/server/index.d.ts +4 -6
  190. package/dist/src/server/index.js +63 -96
  191. package/dist/src/server/migrate/index.d.ts +2 -4
  192. package/dist/src/server/migrate/index.js +31 -16
  193. package/dist/src/server/migrate/worker.js +2 -27
  194. package/dist/src/server/save.js +3 -2
  195. package/dist/src/server/schema.js +9 -19
  196. package/dist/src/server/start.d.ts +1 -0
  197. package/dist/src/server/start.js +18 -10
  198. package/dist/src/server/tree.d.ts +20 -0
  199. package/dist/src/server/tree.js +17 -2
  200. package/dist/src/server/workers/DbWorker.d.ts +1 -0
  201. package/dist/src/server/workers/DbWorker.js +8 -1
  202. package/dist/src/server/workers/io_worker.js +1 -1
  203. package/dist/src/server/workers/worker.js +9 -4
  204. package/dist/src/table.d.ts +4 -0
  205. package/dist/src/table.js +97 -0
  206. package/package.json +18 -17
  207. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +0 -29
  208. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +0 -29
  209. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +0 -29
  210. package/dist/src/client/flushModify.d.ts +0 -36
  211. package/dist/src/client/flushModify.js +0 -197
  212. package/dist/src/client/modify/ModifyRes.d.ts +0 -23
  213. package/dist/src/client/modify/ModifyRes.js +0 -101
  214. package/dist/src/client/modify/alias.d.ts +0 -4
  215. package/dist/src/client/modify/alias.js +0 -57
  216. package/dist/src/client/modify/binary.d.ts +0 -6
  217. package/dist/src/client/modify/binary.js +0 -80
  218. package/dist/src/client/modify/cardinality.d.ts +0 -6
  219. package/dist/src/client/modify/cardinality.js +0 -63
  220. package/dist/src/client/modify/create.d.ts +0 -5
  221. package/dist/src/client/modify/create.js +0 -228
  222. package/dist/src/client/modify/delete.d.ts +0 -4
  223. package/dist/src/client/modify/delete.js +0 -47
  224. package/dist/src/client/modify/expire.d.ts +0 -3
  225. package/dist/src/client/modify/expire.js +0 -25
  226. package/dist/src/client/modify/fixed.d.ts +0 -5
  227. package/dist/src/client/modify/fixed.js +0 -235
  228. package/dist/src/client/modify/index.d.ts +0 -4
  229. package/dist/src/client/modify/index.js +0 -4
  230. package/dist/src/client/modify/json.d.ts +0 -4
  231. package/dist/src/client/modify/json.js +0 -26
  232. package/dist/src/client/modify/modify.d.ts +0 -5
  233. package/dist/src/client/modify/modify.js +0 -134
  234. package/dist/src/client/modify/references/appendEdgeRefs.d.ts +0 -4
  235. package/dist/src/client/modify/references/appendEdgeRefs.js +0 -33
  236. package/dist/src/client/modify/references/edge.d.ts +0 -5
  237. package/dist/src/client/modify/references/edge.js +0 -336
  238. package/dist/src/client/modify/references/getEdgeSize.d.ts +0 -3
  239. package/dist/src/client/modify/references/getEdgeSize.js +0 -27
  240. package/dist/src/client/modify/references/reference.d.ts +0 -11
  241. package/dist/src/client/modify/references/reference.js +0 -124
  242. package/dist/src/client/modify/references/references.d.ts +0 -16
  243. package/dist/src/client/modify/references/references.js +0 -316
  244. package/dist/src/client/modify/setCursor.d.ts +0 -5
  245. package/dist/src/client/modify/setCursor.js +0 -33
  246. package/dist/src/client/modify/string.d.ts +0 -5
  247. package/dist/src/client/modify/string.js +0 -71
  248. package/dist/src/client/modify/text.d.ts +0 -8
  249. package/dist/src/client/modify/text.js +0 -69
  250. package/dist/src/client/modify/update.d.ts +0 -6
  251. package/dist/src/client/modify/update.js +0 -90
  252. package/dist/src/client/modify/upsert.d.ts +0 -3
  253. package/dist/src/client/modify/vector.d.ts +0 -4
  254. package/dist/src/client/modify/vector.js +0 -53
  255. package/dist/src/client/query/include/toBuffer.js +0 -123
  256. package/dist/src/client/query/read/read.d.ts +0 -9
  257. package/dist/src/client/query/read/read.js +0 -483
  258. /package/dist/src/client/query/include/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
  259. /package/dist/src/client/query/toByteCode/{toBuffer.d.ts → toByteCode.d.ts} +0 -0
@@ -1,134 +0,0 @@
1
- import { isPropDef, REFERENCE, REFERENCES, STRING, TEXT, ALIAS, BINARY, CARDINALITY, VECTOR, MICRO_BUFFER, JSON, COLVEC, } from '@based/schema/def';
2
- import { ModifyError } from './ModifyRes.js';
3
- import { writeReference } from './references/reference.js';
4
- import { writeReferences } from './references/references.js';
5
- import { writeString } from './string.js';
6
- import { writeText } from './text.js';
7
- import { DECREMENT, INCREMENT, RANGE_ERR, } from './types.js';
8
- import { writeBinary } from './binary.js';
9
- import { setCursor } from './setCursor.js';
10
- import { appendFixedValue, writeFixedValue } from './fixed.js';
11
- import { writeAlias } from './alias.js';
12
- import { writeHll } from './cardinality.js';
13
- import { writeVector } from './vector.js';
14
- import { writeJson } from './json.js';
15
- function _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe) {
16
- for (const key in obj) {
17
- const def = tree[key];
18
- if (def === undefined) {
19
- if (unsafe) {
20
- continue;
21
- }
22
- return new ModifyError(tree, key);
23
- }
24
- let err;
25
- if (isPropDef(def)) {
26
- const val = obj[key];
27
- if (val === undefined) {
28
- continue;
29
- }
30
- const type = def.typeIndex;
31
- if (def.separate) {
32
- if (type === STRING) {
33
- err = writeString(0, val, ctx, schema, def, res.tmpId, mod);
34
- }
35
- else if (type === TEXT) {
36
- err = writeText(val, ctx, schema, def, res, res.tmpId, mod);
37
- }
38
- else if (type === REFERENCE) {
39
- err = writeReference(val, ctx, schema, def, res, mod);
40
- }
41
- else if (type === REFERENCES) {
42
- err = writeReferences(val, ctx, schema, def, res, mod);
43
- }
44
- else if (type === BINARY) {
45
- err = writeBinary(val, ctx, schema, def, res.tmpId, mod);
46
- }
47
- else if (type === ALIAS) {
48
- err = writeAlias(val, ctx, schema, def, res.tmpId, mod);
49
- }
50
- else if (type === CARDINALITY) {
51
- err = writeHll(val, ctx, schema, def, res.tmpId, mod);
52
- }
53
- else if (type === VECTOR || type === COLVEC) {
54
- err = writeVector(val, ctx, schema, def, res.tmpId, mod);
55
- }
56
- else if (type === JSON) {
57
- err = writeJson(val, ctx, schema, def, res.tmpId, mod);
58
- }
59
- }
60
- else if (overwrite) {
61
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ + 5 + schema.mainLen > ctx.max) {
62
- return RANGE_ERR;
63
- }
64
- setCursor(ctx, schema, def.prop, MICRO_BUFFER, res.tmpId, mod, true);
65
- if (ctx.lastMain === -1) {
66
- let mainLenU32 = schema.mainLen;
67
- setCursor(ctx, schema, def.prop, MICRO_BUFFER, res.tmpId, mod);
68
- ctx.buf[ctx.len++] = mod;
69
- ctx.buf[ctx.len++] = mainLenU32;
70
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
71
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
72
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
73
- ctx.lastMain = ctx.len;
74
- ctx.buf.set(schema.mainEmpty, ctx.len);
75
- ctx.len += schema.mainLen;
76
- }
77
- if (typeof val === 'object' && val !== null && 'increment' in val) {
78
- err = writeFixedValue(ctx, val.increment, def, ctx.lastMain + def.start, mod);
79
- }
80
- else {
81
- err = writeFixedValue(ctx, val, def, ctx.lastMain + def.start, mod);
82
- }
83
- }
84
- else if (typeof val === 'object' && val !== null) {
85
- if ('increment' in val) {
86
- let increment = val.increment;
87
- if (increment === 0) {
88
- continue;
89
- }
90
- if (ctx.len + 11 /* SIZE.DEFAULT_CURSOR */ > ctx.max) {
91
- return RANGE_ERR;
92
- }
93
- setCursor(ctx, schema, def.prop, MICRO_BUFFER, res.tmpId, mod);
94
- let start = def.start;
95
- if (increment < 0) {
96
- ctx.buf[ctx.len++] = DECREMENT;
97
- increment = -increment;
98
- }
99
- else {
100
- ctx.buf[ctx.len++] = INCREMENT;
101
- }
102
- ctx.buf[ctx.len++] = def.typeIndex;
103
- ctx.buf[ctx.len++] = start;
104
- ctx.buf[ctx.len++] = start >>>= 8;
105
- appendFixedValue(ctx, increment, def, mod);
106
- }
107
- else {
108
- return new ModifyError(def, val);
109
- }
110
- }
111
- else if (ctx.mergeMain) {
112
- ctx.mergeMain.push(def, val);
113
- ctx.mergeMainSize += def.len + 4;
114
- }
115
- else {
116
- ctx.mergeMain = [def, val];
117
- ctx.mergeMainSize = def.len + 4;
118
- }
119
- }
120
- else {
121
- err = _modify(ctx, res, obj[key], schema, mod, def, overwrite, unsafe);
122
- }
123
- if (err) {
124
- if (unsafe && err !== RANGE_ERR) {
125
- continue;
126
- }
127
- return err;
128
- }
129
- }
130
- }
131
- export function modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe = false) {
132
- return _modify(ctx, res, obj, schema, mod, tree, overwrite, unsafe);
133
- }
134
- //# sourceMappingURL=modify.js.map
@@ -1,4 +0,0 @@
1
- import type { PropDefEdge } from '@based/schema/def';
2
- import type { ModifyCtx } from '../../flushModify.js';
3
- import type { ModifyErr } from '../types.js';
4
- export declare function appendEdgeRefs(t: PropDefEdge, ctx: ModifyCtx, value: any[]): ModifyErr;
@@ -1,33 +0,0 @@
1
- import { ModifyError, ModifyState } from '../ModifyRes.js';
2
- export function appendEdgeRefs(t, ctx, value) {
3
- for (let i = 0; i < value.length; i++) {
4
- let id = value[i];
5
- if (typeof id !== 'number') {
6
- if (id instanceof ModifyState) {
7
- if (id.error) {
8
- return id.error;
9
- }
10
- id = id.tmpId;
11
- }
12
- else if (typeof id === 'object' && id !== null && id.id) {
13
- id = id.id;
14
- }
15
- else {
16
- return new ModifyError(t, value);
17
- }
18
- }
19
- if (!t.validation(id, t)) {
20
- return new ModifyError(t, value);
21
- }
22
- if (id > 0) {
23
- ctx.buf[ctx.len++] = id;
24
- ctx.buf[ctx.len++] = id >>>= 8;
25
- ctx.buf[ctx.len++] = id >>>= 8;
26
- ctx.buf[ctx.len++] = id >>>= 8;
27
- }
28
- else {
29
- return new ModifyError(t, value);
30
- }
31
- }
32
- }
33
- //# sourceMappingURL=appendEdgeRefs.js.map
@@ -1,5 +0,0 @@
1
- import { ModifyCtx } from '../../../index.js';
2
- import { PropDef } from '@based/schema/def';
3
- import { ModifyErr } from '../types.js';
4
- import { RefModifyOpts } from './references.js';
5
- export declare function writeEdges(t: PropDef, ref: RefModifyOpts, ctx: ModifyCtx): ModifyErr;
@@ -1,336 +0,0 @@
1
- import { ENCODER } from '../../../index.js';
2
- import { BINARY, MICRO_BUFFER, CARDINALITY, REFERENCE, REFERENCES, STRING, } from '@based/schema/def';
3
- import { write } from '../../string.js';
4
- import { writeHllBuf } from '../cardinality.js';
5
- import { getBuffer, writeBinaryRaw } from '../binary.js';
6
- import { ModifyError, ModifyState } from '../ModifyRes.js';
7
- import { DECREMENT, INCREMENT, RANGE_ERR, UPDATE, UPDATE_PARTIAL, } from '../types.js';
8
- import { appendFixedValue } from '../fixed.js';
9
- import { appendEdgeRefs } from './appendEdgeRefs.js';
10
- import { writeUint32 } from '@saulx/utils';
11
- function valueOperation(value) {
12
- if (typeof value === 'object' && value !== null) {
13
- if (value.increment > 0) {
14
- return INCREMENT;
15
- }
16
- else if (value.increment < 0) {
17
- return DECREMENT;
18
- }
19
- return 0;
20
- }
21
- return UPDATE;
22
- }
23
- const EDGE_HEADER_SIZE = 5;
24
- export function writeEdges(t, ref, ctx) {
25
- let mainFields;
26
- let mainSize = 0;
27
- let hasIncr = false;
28
- if (t.hasDefaultEdges) {
29
- for (const key in t.edges) {
30
- if (t.edges[key].separate === true &&
31
- (!(key in ref) || ref[key] === undefined)) {
32
- const edge = t.edges[key];
33
- ref[key] = edge.default;
34
- }
35
- }
36
- }
37
- for (const key in ref) {
38
- if (key === 'id' || key === '$index') {
39
- continue;
40
- }
41
- const edge = t.edges?.[key];
42
- if (!edge) {
43
- return new ModifyError(t, key, `Does not exist`);
44
- }
45
- let value = ref[key];
46
- if (edge.separate === true) {
47
- if (ctx.len + 2 > ctx.max) {
48
- return RANGE_ERR;
49
- }
50
- ctx.buf[ctx.len++] = UPDATE;
51
- ctx.buf[ctx.len++] = edge.prop;
52
- /*
53
- Seperate fields:
54
-
55
- | Offset | Field | Size (bytes)| Description |
56
- |---------|-------------|-------------|---------------------------------------|
57
- | 0 | modify op | 1 | Modify operation identifier |
58
- | 1 | prop | 1 | Field identifier |
59
- | 2 | type | 1 | Indicates MICRO_BUFFER type |
60
- | 3 | size | 4 | Size of the data in bytes |
61
- | 7 | data | Variable | Content |
62
- */
63
- if (edge.typeIndex === BINARY) {
64
- let size = 0;
65
- // add null
66
- if (value === null) {
67
- size = 0;
68
- }
69
- else {
70
- const buf = getBuffer(value);
71
- if (!buf || !edge.validation(buf, edge)) {
72
- return new ModifyError(edge, value);
73
- }
74
- size = buf.byteLength;
75
- value = buf;
76
- }
77
- if (ctx.len + EDGE_HEADER_SIZE + size > ctx.max) {
78
- return RANGE_ERR;
79
- }
80
- ctx.buf[ctx.len++] = STRING;
81
- if (size) {
82
- writeBinaryRaw(value, ctx);
83
- }
84
- else {
85
- ctx.buf[ctx.len++] = 0;
86
- ctx.buf[ctx.len++] = 0;
87
- ctx.buf[ctx.len++] = 0;
88
- ctx.buf[ctx.len++] = 0;
89
- }
90
- }
91
- else if (edge.typeIndex === STRING) {
92
- let size = 0;
93
- // add null
94
- if (value !== null) {
95
- if (!edge.validation(value, edge)) {
96
- return new ModifyError(edge, value);
97
- }
98
- const isBuffer = value instanceof Uint8Array;
99
- size = isBuffer
100
- ? value.byteLength
101
- : ENCODER.encode(value).byteLength + 6;
102
- if (ctx.len + EDGE_HEADER_SIZE + size > ctx.max) {
103
- return RANGE_ERR;
104
- }
105
- ctx.buf[ctx.len++] = STRING;
106
- size = write(ctx.buf, value, ctx.len + 4, edge.compression === 0);
107
- }
108
- let sizeU32 = size;
109
- ctx.buf[ctx.len++] = sizeU32;
110
- ctx.buf[ctx.len++] = sizeU32 >>>= 8;
111
- ctx.buf[ctx.len++] = sizeU32 >>>= 8;
112
- ctx.buf[ctx.len++] = sizeU32 >>>= 8;
113
- ctx.len += size;
114
- }
115
- else if (edge.typeIndex === REFERENCE) {
116
- // add null
117
- if (typeof value !== 'number') {
118
- if (value instanceof ModifyState) {
119
- if (value.error) {
120
- return value.error;
121
- }
122
- value = value.tmpId;
123
- }
124
- else if (typeof value === 'object' && value !== null && value.id) {
125
- value = value.id;
126
- }
127
- else {
128
- return new ModifyError(edge, value);
129
- }
130
- }
131
- if (!edge.validation(value, edge)) {
132
- return new ModifyError(edge, value);
133
- }
134
- if (value > 0) {
135
- ctx.buf[ctx.len++] = REFERENCE;
136
- ctx.buf[ctx.len++] = value;
137
- ctx.buf[ctx.len++] = value >>>= 8;
138
- ctx.buf[ctx.len++] = value >>>= 8;
139
- ctx.buf[ctx.len++] = value >>>= 8;
140
- }
141
- else {
142
- return new ModifyError(edge, value);
143
- }
144
- }
145
- else if (edge.typeIndex === REFERENCES) {
146
- // add null
147
- if (!Array.isArray(value)) {
148
- return new ModifyError(edge, value);
149
- }
150
- let size = value.length * 4;
151
- if (ctx.len + EDGE_HEADER_SIZE + size > ctx.max) {
152
- return RANGE_ERR;
153
- }
154
- ctx.buf[ctx.len++] = REFERENCES;
155
- ctx.buf[ctx.len++] = size;
156
- ctx.buf[ctx.len++] = size >>>= 8;
157
- ctx.buf[ctx.len++] = size >>>= 8;
158
- ctx.buf[ctx.len++] = size >>>= 8;
159
- const err = appendEdgeRefs(edge, ctx, value);
160
- if (err) {
161
- return err;
162
- }
163
- }
164
- else if (edge.typeIndex === CARDINALITY) {
165
- // add null
166
- if (!Array.isArray(value)) {
167
- value = [value];
168
- }
169
- const len = value.length;
170
- let size = 4 + len * 8;
171
- if (ctx.len + size + EDGE_HEADER_SIZE > ctx.max) {
172
- return RANGE_ERR;
173
- }
174
- ctx.buf[ctx.len++] = CARDINALITY;
175
- writeHllBuf(value, ctx, edge, size);
176
- }
177
- }
178
- else {
179
- const op = valueOperation(value);
180
- if (op === 0) {
181
- return new ModifyError(edge, value);
182
- }
183
- if (op != UPDATE) {
184
- hasIncr = true;
185
- value = value.increment;
186
- }
187
- if (!hasIncr && t.edgeMainLen == edge.len) {
188
- /*
189
- Full main update:
190
-
191
- | Offset | Field | Size (bytes)| Description |
192
- |---------|-------------|-------------|---------------------------------------|
193
- | 0 | modify op | 1 | Modify operation identifier |
194
- | 1 | prop | 1 | Field identifier (0) |
195
- | 2 | type | 1 | Indicates MICRO_BUFFER type |
196
- | 3 | mainSize | 4 | Size of the main data in bytes |
197
- | 7 | main buffer | Variable | Main data content |
198
- */
199
- if (ctx.len + 7 + edge.len > ctx.max) {
200
- return RANGE_ERR;
201
- }
202
- ctx.buf[ctx.len++] = UPDATE;
203
- ctx.buf[ctx.len++] = 0;
204
- ctx.buf[ctx.len++] = MICRO_BUFFER;
205
- const size = edge.len;
206
- let sizeU32 = size;
207
- ctx.buf[ctx.len++] = sizeU32;
208
- ctx.buf[ctx.len++] = sizeU32 >>>= 8;
209
- ctx.buf[ctx.len++] = sizeU32 >>>= 8;
210
- ctx.buf[ctx.len++] = sizeU32 >>>= 8;
211
- const err = appendFixedValue(ctx, value, edge, UPDATE);
212
- if (err) {
213
- return err;
214
- }
215
- }
216
- else {
217
- mainSize += edge.len;
218
- if (!mainFields) {
219
- mainFields = [edge, value, op];
220
- }
221
- else {
222
- const len = mainFields.length;
223
- for (let i = 0; i < len; i += 3) {
224
- if (edge.start < mainFields[i].start) {
225
- mainFields.splice(i, 0, edge, value, op);
226
- break;
227
- }
228
- else if (mainFields[len - i - 3].start < edge.start) {
229
- mainFields.splice(len - i, 0, edge, value, op);
230
- break;
231
- }
232
- }
233
- }
234
- }
235
- }
236
- }
237
- // double check if has default edges has mainfields - add extra thing
238
- if (mainFields || t.hasDefaultEdges) {
239
- // add all
240
- // Single field in main buffer can immediately setup the main buffer
241
- if (!hasIncr && mainSize === t.edgeMainLen) {
242
- /*
243
- Full main update:
244
-
245
- | Offset | Field | Size (bytes)| Description |
246
- |---------|-------------|-------------|---------------------------------------|
247
- | 0 | modify op | 1 | Modify operation identifier |
248
- | 1 | prop | 1 | Field identifier (0) |
249
- | 2 | type | 1 | Indicates MICRO_BUFFER type |
250
- | 3 | mainSize | 4 | Size of the main data in bytes |
251
- | 7 | main buffer | Variable | Main data content |
252
- */
253
- if (ctx.len + 7 + mainSize > ctx.max) {
254
- return RANGE_ERR;
255
- }
256
- ctx.buf[ctx.len++] = UPDATE;
257
- ctx.buf[ctx.len++] = 0;
258
- ctx.buf[ctx.len++] = MICRO_BUFFER;
259
- let sizeU32 = mainSize;
260
- writeUint32(ctx.buf, sizeU32, ctx.len);
261
- ctx.len += 4;
262
- for (let i = 0; i < mainFields.length; i += 3) {
263
- const edge = mainFields[i];
264
- const err = appendFixedValue(ctx, mainFields[i + 1], edge, UPDATE);
265
- if (err) {
266
- return err;
267
- }
268
- }
269
- }
270
- else {
271
- if (!mainFields) {
272
- mainFields = [];
273
- }
274
- /*
275
- Partial main update:
276
-
277
- | Offset | Field | Size (bytes)| Description |
278
- |---------|-------------|-------------|--------------------------------------|
279
- | 0 | modify op | 1 | Modify operation identifier |
280
- | 1 | prop | 1 | Field identifier (0 in this case) |
281
- | 2 | type | 1 | Indicates MICRO_BUFFER type |
282
- | 3 | size | 4 | Total size of the payload |
283
- | 7 | mainSize | 2 | Length of the main data block |
284
- | 9 | start | 2 | Start position of first section |
285
- | 11 | len | 2 | Length of first section |
286
- | 12 | field op | 1 | Field operation type e.g. INCREMENT |
287
- | 13 | propType | 1 | Prop typeIndex |
288
- | ... | ... | ... | Additional (start, len) pairs |
289
- | X | main | len | Actual main content |
290
- */
291
- const mainFieldsStartSize = mainFields.length * 2;
292
- if (ctx.len + 7 + mainSize + mainFieldsStartSize > ctx.max) {
293
- return RANGE_ERR;
294
- }
295
- ctx.buf[ctx.len++] = UPDATE_PARTIAL;
296
- ctx.buf[ctx.len++] = 0;
297
- ctx.buf[ctx.len++] = MICRO_BUFFER;
298
- const size = mainFieldsStartSize + t.edgeMainLen;
299
- ctx.buf[ctx.len++] = size;
300
- ctx.buf[ctx.len++] = size >>> 8;
301
- ctx.buf[ctx.len++] = size >>> 16;
302
- ctx.buf[ctx.len++] = size >>> 24;
303
- ctx.buf[ctx.len++] = t.edgeMainLen;
304
- ctx.buf[ctx.len++] = t.edgeMainLen >>> 8;
305
- // Index of start of fields
306
- const sIndex = ctx.len;
307
- ctx.len += mainFieldsStartSize;
308
- // Add zeroes
309
- ctx.buf.set(t.edgeMainEmpty, ctx.len);
310
- // ctx.buf.fill(0, ctx.len, ctx.len + t.edgeMainLen)
311
- // Keep track of written bytes from append fixed
312
- let startMain = ctx.len;
313
- for (let i = 0; i < mainFields.length; i += 3) {
314
- const edge = mainFields[i];
315
- const value = mainFields[i + 1];
316
- const op = mainFields[i + 2];
317
- const sIndexI = i * 2 + sIndex;
318
- ctx.buf[sIndexI] = edge.start;
319
- ctx.buf[sIndexI + 1] = edge.start >>> 8;
320
- ctx.buf[sIndexI + 2] = edge.len;
321
- ctx.buf[sIndexI + 3] = edge.len >>> 8;
322
- ctx.buf[sIndexI + 4] = op;
323
- ctx.buf[sIndexI + 5] = edge.typeIndex;
324
- ctx.len = startMain + edge.start;
325
- // Add null support (defaults)
326
- const err = appendFixedValue(ctx, value, edge, op);
327
- if (err) {
328
- return err;
329
- }
330
- }
331
- // Correction append fixed value writes the len
332
- ctx.len = startMain + t.edgeMainLen;
333
- }
334
- }
335
- }
336
- //# sourceMappingURL=edge.js.map
@@ -1,3 +0,0 @@
1
- import { PropDef } from '@based/schema/def';
2
- import { RefModifyOpts } from './references.js';
3
- export declare function getEdgeSize(t: PropDef, ref: RefModifyOpts): number;
@@ -1,27 +0,0 @@
1
- import { STRING, REFERENCE, REFERENCES } from '@based/schema/def';
2
- import { ENCODER } from '@saulx/utils';
3
- export function getEdgeSize(t, ref) {
4
- let size = 0;
5
- for (const key in t.edges) {
6
- if (key in ref) {
7
- const edge = t.edges[key];
8
- const value = ref[key];
9
- if (edge.len === 0) {
10
- if (edge.typeIndex === STRING) {
11
- size += ENCODER.encode(value).byteLength + 4;
12
- }
13
- else if (edge.typeIndex === REFERENCE) {
14
- size += 4;
15
- }
16
- else if (edge.typeIndex === REFERENCES) {
17
- size += value.length * 4 + 4;
18
- }
19
- }
20
- else {
21
- size += edge.len;
22
- }
23
- }
24
- }
25
- return size;
26
- }
27
- //# sourceMappingURL=getEdgeSize.js.map
@@ -1,11 +0,0 @@
1
- import { ModifyCtx } from '../../../index.js';
2
- import { PropDef, SchemaTypeDef } from '@based/schema/def';
3
- import { ModifyState } from '../ModifyRes.js';
4
- import { ModifyErr, ModifyOp } from '../types.js';
5
- export declare function writeReference(value: number | ModifyState | {
6
- id: number;
7
- upsert?: Record<string, any>;
8
- } | {
9
- id?: number;
10
- upsert: Record<string, any>;
11
- }, ctx: ModifyCtx, schema: SchemaTypeDef, def: PropDef, res: ModifyState, modifyOp: ModifyOp): ModifyErr;
@@ -1,124 +0,0 @@
1
- import { ModifyError, ModifyState } from '../ModifyRes.js';
2
- import { setCursor } from '../setCursor.js';
3
- import { DELETE, RANGE_ERR, EDGE_NOINDEX_TMPID, NOEDGE_NOINDEX_TMPID, EDGE_NOINDEX_REALID, NOEDGE_INDEX_REALID, } from '../types.js';
4
- import { writeEdges } from './edge.js';
5
- import { dbUpdateFromUpsert } from './references.js';
6
- function writeRef(id, ctx, schema, def, parentId, modifyOp, hasEdges, isTmpId) {
7
- if (!def.validation(id, def)) {
8
- return new ModifyError(def, id);
9
- }
10
- if (ctx.len + 16 > ctx.max) {
11
- return RANGE_ERR;
12
- }
13
- ctx.markNodeDirty(ctx.db.schemaTypesParsed[def.inverseTypeName], id);
14
- setCursor(ctx, schema, def.prop, def.typeIndex, parentId, modifyOp);
15
- ctx.buf[ctx.len++] = modifyOp;
16
- if (isTmpId) {
17
- ctx.buf[ctx.len++] = hasEdges ? EDGE_NOINDEX_TMPID : NOEDGE_NOINDEX_TMPID;
18
- }
19
- else {
20
- ctx.buf[ctx.len++] = hasEdges ? EDGE_NOINDEX_REALID : NOEDGE_INDEX_REALID;
21
- }
22
- ctx.buf[ctx.len++] = id;
23
- ctx.buf[ctx.len++] = id >>>= 8;
24
- ctx.buf[ctx.len++] = id >>>= 8;
25
- ctx.buf[ctx.len++] = id >>>= 8;
26
- }
27
- function singleReferenceEdges(ref, ctx, schema, def, parentId, modifyOp, processDefaultEdges) {
28
- let id = ref.id;
29
- let isTmpId;
30
- if (typeof id !== 'number') {
31
- if (id instanceof ModifyState) {
32
- if (id.error) {
33
- return id.error;
34
- }
35
- const resolvedId = id.getId();
36
- if (resolvedId) {
37
- id = resolvedId;
38
- }
39
- else {
40
- isTmpId = !true;
41
- id = id.tmpId;
42
- }
43
- }
44
- }
45
- // TODO SINGLE REF
46
- if (id > 0) {
47
- if (def.edgesSeperateCnt === 0 && def.edgeMainLen === 0) {
48
- return writeRef(id, ctx, schema, def, parentId, modifyOp, false, isTmpId);
49
- }
50
- else {
51
- let err = writeRef(id, ctx, schema, def, parentId, modifyOp, true, isTmpId);
52
- if (err) {
53
- return err;
54
- }
55
- // TODO REMOVE - SEEMS REDUNDANT
56
- if (ctx.len + 4 > ctx.max) {
57
- return RANGE_ERR;
58
- }
59
- let sizepos = ctx.len;
60
- ctx.len += 4;
61
- err = writeEdges(def, ref, ctx);
62
- if (err) {
63
- return err;
64
- }
65
- let size = ctx.len - sizepos;
66
- ctx.buf[sizepos++] = size;
67
- ctx.buf[sizepos++] = size >>>= 8;
68
- ctx.buf[sizepos++] = size >>>= 8;
69
- ctx.buf[sizepos] = size >>>= 8;
70
- }
71
- }
72
- }
73
- export function writeReference(value, ctx, schema, def, res, modifyOp) {
74
- if (value === null) {
75
- if (ctx.len + 11 > ctx.max) {
76
- return RANGE_ERR;
77
- }
78
- setCursor(ctx, schema, def.prop, def.typeIndex, res.tmpId, modifyOp);
79
- ctx.buf[ctx.len++] = DELETE;
80
- return;
81
- }
82
- const processDefaultEdges = def.hasDefaultEdges;
83
- if (typeof value === 'number') {
84
- if (processDefaultEdges) {
85
- value = { id: value };
86
- }
87
- else {
88
- return writeRef(value, ctx, schema, def, res.tmpId, modifyOp, false, false);
89
- }
90
- }
91
- if (value instanceof ModifyState) {
92
- if (value.error) {
93
- return value.error;
94
- }
95
- const id = value.getId();
96
- if (processDefaultEdges) {
97
- value = { id: id || value.tmpId };
98
- }
99
- else {
100
- if (id) {
101
- return writeRef(id, ctx, schema, def, res.tmpId, modifyOp, false, false);
102
- }
103
- return writeRef(value.tmpId, ctx, schema, def, res.tmpId, modifyOp, false, true);
104
- }
105
- }
106
- if (typeof value === 'object') {
107
- if (def.edges) {
108
- return singleReferenceEdges(value, ctx, schema, def, res.tmpId, modifyOp, processDefaultEdges);
109
- }
110
- else if (typeof value.id === 'number') {
111
- return writeRef(value.id, ctx, schema, def, res.tmpId, modifyOp, false, false);
112
- }
113
- else if (typeof value.upsert === 'object' && value.upsert !== null) {
114
- dbUpdateFromUpsert(ctx, schema, def, res, ctx.db.upsert(def.inverseTypeName, value.upsert));
115
- }
116
- else {
117
- return new ModifyError(def, value);
118
- }
119
- }
120
- else {
121
- return new ModifyError(def, value);
122
- }
123
- }
124
- //# sourceMappingURL=reference.js.map