@based/db 0.0.1

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 (296) hide show
  1. package/README.md +43 -0
  2. package/dist/lib/darwin_aarch64/include/cdefs.h +317 -0
  3. package/dist/lib/darwin_aarch64/include/libdeflate.h +322 -0
  4. package/dist/lib/darwin_aarch64/include/libdeflate_strings.h +35 -0
  5. package/dist/lib/darwin_aarch64/include/linker_set.h +109 -0
  6. package/dist/lib/darwin_aarch64/include/queue.h +627 -0
  7. package/dist/lib/darwin_aarch64/include/selva/_export.h +7 -0
  8. package/dist/lib/darwin_aarch64/include/selva/align.h +9 -0
  9. package/dist/lib/darwin_aarch64/include/selva/backoff_timeout.h +29 -0
  10. package/dist/lib/darwin_aarch64/include/selva/base64.h +59 -0
  11. package/dist/lib/darwin_aarch64/include/selva/base64url.h +59 -0
  12. package/dist/lib/darwin_aarch64/include/selva/bitmap.h +95 -0
  13. package/dist/lib/darwin_aarch64/include/selva/crc32c.h +17 -0
  14. package/dist/lib/darwin_aarch64/include/selva/ctime.h +135 -0
  15. package/dist/lib/darwin_aarch64/include/selva/db.h +284 -0
  16. package/dist/lib/darwin_aarch64/include/selva/endian.h +301 -0
  17. package/dist/lib/darwin_aarch64/include/selva/fast_linear_search.h +23 -0
  18. package/dist/lib/darwin_aarch64/include/selva/fast_memmem.h +11 -0
  19. package/dist/lib/darwin_aarch64/include/selva/fast_parsei.h +36 -0
  20. package/dist/lib/darwin_aarch64/include/selva/fields.h +338 -0
  21. package/dist/lib/darwin_aarch64/include/selva/find.h +47 -0
  22. package/dist/lib/darwin_aarch64/include/selva/lpf.h +28 -0
  23. package/dist/lib/darwin_aarch64/include/selva/node_id_set.h +43 -0
  24. package/dist/lib/darwin_aarch64/include/selva/poptop.h +114 -0
  25. package/dist/lib/darwin_aarch64/include/selva/queue_r.h +190 -0
  26. package/dist/lib/darwin_aarch64/include/selva/selva_hash128.h +39 -0
  27. package/dist/lib/darwin_aarch64/include/selva/selva_lang.h +105 -0
  28. package/dist/lib/darwin_aarch64/include/selva/selva_math.h +37 -0
  29. package/dist/lib/darwin_aarch64/include/selva/selva_string.h +672 -0
  30. package/dist/lib/darwin_aarch64/include/selva/sort.h +119 -0
  31. package/dist/lib/darwin_aarch64/include/selva/strsearch.h +43 -0
  32. package/dist/lib/darwin_aarch64/include/selva/timestamp.h +25 -0
  33. package/dist/lib/darwin_aarch64/include/selva/traverse.h +65 -0
  34. package/dist/lib/darwin_aarch64/include/selva/types.h +110 -0
  35. package/dist/lib/darwin_aarch64/include/selva/vector.h +14 -0
  36. package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +13 -0
  37. package/dist/lib/darwin_aarch64/include/selva_error.h +137 -0
  38. package/dist/lib/darwin_aarch64/include/selva_lang_code.h +157 -0
  39. package/dist/lib/darwin_aarch64/include/tree.h +852 -0
  40. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  41. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  42. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  43. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  44. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  45. package/dist/lib/darwin_aarch64/libnode-v22.8.0.node +0 -0
  46. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  47. package/dist/lib/darwin_aarch64/libxxhash.0.8.2.dylib +0 -0
  48. package/dist/lib/darwin_aarch64/libxxhash.0.dylib +0 -0
  49. package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
  50. package/dist/lib/linux_aarch64/include/cdefs.h +317 -0
  51. package/dist/lib/linux_aarch64/include/libdeflate.h +322 -0
  52. package/dist/lib/linux_aarch64/include/libdeflate_strings.h +35 -0
  53. package/dist/lib/linux_aarch64/include/linker_set.h +109 -0
  54. package/dist/lib/linux_aarch64/include/queue.h +627 -0
  55. package/dist/lib/linux_aarch64/include/selva/_export.h +7 -0
  56. package/dist/lib/linux_aarch64/include/selva/align.h +9 -0
  57. package/dist/lib/linux_aarch64/include/selva/backoff_timeout.h +29 -0
  58. package/dist/lib/linux_aarch64/include/selva/base64.h +59 -0
  59. package/dist/lib/linux_aarch64/include/selva/base64url.h +59 -0
  60. package/dist/lib/linux_aarch64/include/selva/bitmap.h +95 -0
  61. package/dist/lib/linux_aarch64/include/selva/crc32c.h +17 -0
  62. package/dist/lib/linux_aarch64/include/selva/ctime.h +135 -0
  63. package/dist/lib/linux_aarch64/include/selva/db.h +284 -0
  64. package/dist/lib/linux_aarch64/include/selva/endian.h +301 -0
  65. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +23 -0
  66. package/dist/lib/linux_aarch64/include/selva/fast_memmem.h +11 -0
  67. package/dist/lib/linux_aarch64/include/selva/fast_parsei.h +36 -0
  68. package/dist/lib/linux_aarch64/include/selva/fields.h +338 -0
  69. package/dist/lib/linux_aarch64/include/selva/find.h +47 -0
  70. package/dist/lib/linux_aarch64/include/selva/lpf.h +28 -0
  71. package/dist/lib/linux_aarch64/include/selva/node_id_set.h +43 -0
  72. package/dist/lib/linux_aarch64/include/selva/poptop.h +114 -0
  73. package/dist/lib/linux_aarch64/include/selva/queue_r.h +190 -0
  74. package/dist/lib/linux_aarch64/include/selva/selva_hash128.h +39 -0
  75. package/dist/lib/linux_aarch64/include/selva/selva_lang.h +105 -0
  76. package/dist/lib/linux_aarch64/include/selva/selva_math.h +37 -0
  77. package/dist/lib/linux_aarch64/include/selva/selva_string.h +672 -0
  78. package/dist/lib/linux_aarch64/include/selva/sort.h +119 -0
  79. package/dist/lib/linux_aarch64/include/selva/strsearch.h +43 -0
  80. package/dist/lib/linux_aarch64/include/selva/timestamp.h +25 -0
  81. package/dist/lib/linux_aarch64/include/selva/traverse.h +65 -0
  82. package/dist/lib/linux_aarch64/include/selva/types.h +110 -0
  83. package/dist/lib/linux_aarch64/include/selva/vector.h +14 -0
  84. package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +13 -0
  85. package/dist/lib/linux_aarch64/include/selva_error.h +137 -0
  86. package/dist/lib/linux_aarch64/include/selva_lang_code.h +157 -0
  87. package/dist/lib/linux_aarch64/include/tree.h +852 -0
  88. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  89. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  90. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  91. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  92. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  93. package/dist/lib/linux_aarch64/libselva.so +0 -0
  94. package/dist/lib/linux_aarch64/libxxhash.so +0 -0
  95. package/dist/lib/linux_aarch64/libxxhash.so.0 +0 -0
  96. package/dist/lib/linux_aarch64/libxxhash.so.0.8.2 +0 -0
  97. package/dist/lib/linux_x86_64/include/cdefs.h +317 -0
  98. package/dist/lib/linux_x86_64/include/libdeflate.h +322 -0
  99. package/dist/lib/linux_x86_64/include/libdeflate_strings.h +35 -0
  100. package/dist/lib/linux_x86_64/include/linker_set.h +109 -0
  101. package/dist/lib/linux_x86_64/include/queue.h +627 -0
  102. package/dist/lib/linux_x86_64/include/selva/_export.h +7 -0
  103. package/dist/lib/linux_x86_64/include/selva/align.h +9 -0
  104. package/dist/lib/linux_x86_64/include/selva/backoff_timeout.h +29 -0
  105. package/dist/lib/linux_x86_64/include/selva/base64.h +59 -0
  106. package/dist/lib/linux_x86_64/include/selva/base64url.h +59 -0
  107. package/dist/lib/linux_x86_64/include/selva/bitmap.h +95 -0
  108. package/dist/lib/linux_x86_64/include/selva/crc32c.h +17 -0
  109. package/dist/lib/linux_x86_64/include/selva/ctime.h +135 -0
  110. package/dist/lib/linux_x86_64/include/selva/db.h +284 -0
  111. package/dist/lib/linux_x86_64/include/selva/endian.h +301 -0
  112. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +23 -0
  113. package/dist/lib/linux_x86_64/include/selva/fast_memmem.h +11 -0
  114. package/dist/lib/linux_x86_64/include/selva/fast_parsei.h +36 -0
  115. package/dist/lib/linux_x86_64/include/selva/fields.h +338 -0
  116. package/dist/lib/linux_x86_64/include/selva/find.h +47 -0
  117. package/dist/lib/linux_x86_64/include/selva/lpf.h +28 -0
  118. package/dist/lib/linux_x86_64/include/selva/node_id_set.h +43 -0
  119. package/dist/lib/linux_x86_64/include/selva/poptop.h +114 -0
  120. package/dist/lib/linux_x86_64/include/selva/queue_r.h +190 -0
  121. package/dist/lib/linux_x86_64/include/selva/selva_hash128.h +39 -0
  122. package/dist/lib/linux_x86_64/include/selva/selva_lang.h +105 -0
  123. package/dist/lib/linux_x86_64/include/selva/selva_math.h +37 -0
  124. package/dist/lib/linux_x86_64/include/selva/selva_string.h +672 -0
  125. package/dist/lib/linux_x86_64/include/selva/sort.h +119 -0
  126. package/dist/lib/linux_x86_64/include/selva/strsearch.h +43 -0
  127. package/dist/lib/linux_x86_64/include/selva/timestamp.h +25 -0
  128. package/dist/lib/linux_x86_64/include/selva/traverse.h +65 -0
  129. package/dist/lib/linux_x86_64/include/selva/types.h +110 -0
  130. package/dist/lib/linux_x86_64/include/selva/vector.h +14 -0
  131. package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +13 -0
  132. package/dist/lib/linux_x86_64/include/selva_error.h +137 -0
  133. package/dist/lib/linux_x86_64/include/selva_lang_code.h +157 -0
  134. package/dist/lib/linux_x86_64/include/tree.h +852 -0
  135. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  136. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  137. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  138. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  139. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
  140. package/dist/lib/linux_x86_64/libselva.so +0 -0
  141. package/dist/lib/linux_x86_64/libxxhash.so +0 -0
  142. package/dist/lib/linux_x86_64/libxxhash.so.0 +0 -0
  143. package/dist/lib/linux_x86_64/libxxhash.so.0.8.2 +0 -0
  144. package/dist/src/client/crc32.d.ts +1 -0
  145. package/dist/src/client/crc32.js +281 -0
  146. package/dist/src/client/index.d.ts +58 -0
  147. package/dist/src/client/index.js +120 -0
  148. package/dist/src/client/modify/ModifyRes.d.ts +26 -0
  149. package/dist/src/client/modify/ModifyRes.js +98 -0
  150. package/dist/src/client/modify/alias.d.ts +4 -0
  151. package/dist/src/client/modify/alias.js +47 -0
  152. package/dist/src/client/modify/binary.d.ts +6 -0
  153. package/dist/src/client/modify/binary.js +61 -0
  154. package/dist/src/client/modify/create.d.ts +4 -0
  155. package/dist/src/client/modify/create.js +108 -0
  156. package/dist/src/client/modify/fixed.d.ts +5 -0
  157. package/dist/src/client/modify/fixed.js +118 -0
  158. package/dist/src/client/modify/hll.d.ts +4 -0
  159. package/dist/src/client/modify/hll.js +58 -0
  160. package/dist/src/client/modify/index.d.ts +4 -0
  161. package/dist/src/client/modify/index.js +4 -0
  162. package/dist/src/client/modify/modify.d.ts +5 -0
  163. package/dist/src/client/modify/modify.js +126 -0
  164. package/dist/src/client/modify/references/edge.d.ts +6 -0
  165. package/dist/src/client/modify/references/edge.js +158 -0
  166. package/dist/src/client/modify/references/reference.d.ts +11 -0
  167. package/dist/src/client/modify/references/reference.js +92 -0
  168. package/dist/src/client/modify/references/references.d.ts +17 -0
  169. package/dist/src/client/modify/references/references.js +271 -0
  170. package/dist/src/client/modify/remove.d.ts +2 -0
  171. package/dist/src/client/modify/remove.js +36 -0
  172. package/dist/src/client/modify/setCursor.d.ts +4 -0
  173. package/dist/src/client/modify/setCursor.js +36 -0
  174. package/dist/src/client/modify/string.d.ts +5 -0
  175. package/dist/src/client/modify/string.js +56 -0
  176. package/dist/src/client/modify/text.d.ts +5 -0
  177. package/dist/src/client/modify/text.js +14 -0
  178. package/dist/src/client/modify/types.d.ts +14 -0
  179. package/dist/src/client/modify/types.js +12 -0
  180. package/dist/src/client/modify/update.d.ts +5 -0
  181. package/dist/src/client/modify/update.js +80 -0
  182. package/dist/src/client/modify/upsert.d.ts +2 -0
  183. package/dist/src/client/modify/upsert.js +40 -0
  184. package/dist/src/client/operations.d.ts +31 -0
  185. package/dist/src/client/operations.js +110 -0
  186. package/dist/src/client/query/BasedDbQuery.d.ts +40 -0
  187. package/dist/src/client/query/BasedDbQuery.js +235 -0
  188. package/dist/src/client/query/BasedIterable.d.ts +26 -0
  189. package/dist/src/client/query/BasedIterable.js +113 -0
  190. package/dist/src/client/query/debug.d.ts +3 -0
  191. package/dist/src/client/query/debug.js +117 -0
  192. package/dist/src/client/query/display.d.ts +5 -0
  193. package/dist/src/client/query/display.js +233 -0
  194. package/dist/src/client/query/filter/FilterBranch.d.ts +13 -0
  195. package/dist/src/client/query/filter/FilterBranch.js +29 -0
  196. package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +3 -0
  197. package/dist/src/client/query/filter/createFixedFilterBuffer.js +101 -0
  198. package/dist/src/client/query/filter/createReferenceFilter.d.ts +2 -0
  199. package/dist/src/client/query/filter/createReferenceFilter.js +24 -0
  200. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -0
  201. package/dist/src/client/query/filter/createVariableFilterBuffer.js +81 -0
  202. package/dist/src/client/query/filter/filter.d.ts +10 -0
  203. package/dist/src/client/query/filter/filter.js +134 -0
  204. package/dist/src/client/query/filter/operators.d.ts +6 -0
  205. package/dist/src/client/query/filter/operators.js +98 -0
  206. package/dist/src/client/query/filter/parseFilterValue.d.ts +2 -0
  207. package/dist/src/client/query/filter/parseFilterValue.js +99 -0
  208. package/dist/src/client/query/filter/primitiveFilter.d.ts +4 -0
  209. package/dist/src/client/query/filter/primitiveFilter.js +47 -0
  210. package/dist/src/client/query/filter/toBuffer.d.ts +3 -0
  211. package/dist/src/client/query/filter/toBuffer.js +100 -0
  212. package/dist/src/client/query/filter/types.d.ts +6 -0
  213. package/dist/src/client/query/filter/types.js +7 -0
  214. package/dist/src/client/query/include/props.d.ts +6 -0
  215. package/dist/src/client/query/include/props.js +44 -0
  216. package/dist/src/client/query/include/toBuffer.d.ts +3 -0
  217. package/dist/src/client/query/include/toBuffer.js +82 -0
  218. package/dist/src/client/query/include/utils.d.ts +6 -0
  219. package/dist/src/client/query/include/utils.js +41 -0
  220. package/dist/src/client/query/include/walk.d.ts +3 -0
  221. package/dist/src/client/query/include/walk.js +70 -0
  222. package/dist/src/client/query/query.d.ts +10 -0
  223. package/dist/src/client/query/query.js +11 -0
  224. package/dist/src/client/query/queryDef.d.ts +3 -0
  225. package/dist/src/client/query/queryDef.js +54 -0
  226. package/dist/src/client/query/read/read.d.ts +8 -0
  227. package/dist/src/client/query/read/read.js +303 -0
  228. package/dist/src/client/query/registerQuery.d.ts +2 -0
  229. package/dist/src/client/query/registerQuery.js +31 -0
  230. package/dist/src/client/query/search/index.d.ts +6 -0
  231. package/dist/src/client/query/search/index.js +82 -0
  232. package/dist/src/client/query/sort.d.ts +3 -0
  233. package/dist/src/client/query/sort.js +18 -0
  234. package/dist/src/client/query/subscription/index.d.ts +5 -0
  235. package/dist/src/client/query/subscription/index.js +52 -0
  236. package/dist/src/client/query/subscription/markers.d.ts +10 -0
  237. package/dist/src/client/query/subscription/markers.js +213 -0
  238. package/dist/src/client/query/subscription/run.d.ts +5 -0
  239. package/dist/src/client/query/subscription/run.js +69 -0
  240. package/dist/src/client/query/subscription/types.d.ts +33 -0
  241. package/dist/src/client/query/subscription/types.js +3 -0
  242. package/dist/src/client/query/thresholds.d.ts +6 -0
  243. package/dist/src/client/query/thresholds.js +16 -0
  244. package/dist/src/client/query/toBuffer.d.ts +3 -0
  245. package/dist/src/client/query/toBuffer.js +229 -0
  246. package/dist/src/client/query/types.d.ts +88 -0
  247. package/dist/src/client/query/types.js +15 -0
  248. package/dist/src/client/query/validation.d.ts +15 -0
  249. package/dist/src/client/query/validation.js +59 -0
  250. package/dist/src/client/string.d.ts +5 -0
  251. package/dist/src/client/string.js +73 -0
  252. package/dist/src/client/tree.d.ts +1 -0
  253. package/dist/src/client/tree.js +5 -0
  254. package/dist/src/index.d.ts +31 -0
  255. package/dist/src/index.js +85 -0
  256. package/dist/src/native.d.ts +28 -0
  257. package/dist/src/native.js +96 -0
  258. package/dist/src/server/csmt/draw-dot.d.ts +2 -0
  259. package/dist/src/server/csmt/draw-dot.js +32 -0
  260. package/dist/src/server/csmt/index.d.ts +4 -0
  261. package/dist/src/server/csmt/index.js +5 -0
  262. package/dist/src/server/csmt/match.d.ts +7 -0
  263. package/dist/src/server/csmt/match.js +10 -0
  264. package/dist/src/server/csmt/memebership-proof.d.ts +7 -0
  265. package/dist/src/server/csmt/memebership-proof.js +122 -0
  266. package/dist/src/server/csmt/tree-utils.d.ts +6 -0
  267. package/dist/src/server/csmt/tree-utils.js +33 -0
  268. package/dist/src/server/csmt/tree.d.ts +2 -0
  269. package/dist/src/server/csmt/tree.js +209 -0
  270. package/dist/src/server/csmt/types.d.ts +40 -0
  271. package/dist/src/server/csmt/types.js +2 -0
  272. package/dist/src/server/index.d.ts +85 -0
  273. package/dist/src/server/index.js +407 -0
  274. package/dist/src/server/migrate/index.d.ts +7 -0
  275. package/dist/src/server/migrate/index.js +108 -0
  276. package/dist/src/server/migrate/worker.d.ts +1 -0
  277. package/dist/src/server/migrate/worker.js +81 -0
  278. package/dist/src/server/save.d.ts +2 -0
  279. package/dist/src/server/save.js +66 -0
  280. package/dist/src/server/schema/schema.d.ts +3 -0
  281. package/dist/src/server/schema/schema.js +4 -0
  282. package/dist/src/server/schema/selvaBuffer.d.ts +4 -0
  283. package/dist/src/server/schema/selvaBuffer.js +102 -0
  284. package/dist/src/server/schema/typeDef.d.ts +6 -0
  285. package/dist/src/server/schema/typeDef.js +315 -0
  286. package/dist/src/server/schema/types.d.ts +127 -0
  287. package/dist/src/server/schema/types.js +113 -0
  288. package/dist/src/server/schema/utils.d.ts +6 -0
  289. package/dist/src/server/schema/utils.js +21 -0
  290. package/dist/src/server/start.d.ts +6 -0
  291. package/dist/src/server/start.js +86 -0
  292. package/dist/src/server/tree.d.ts +13 -0
  293. package/dist/src/server/tree.js +33 -0
  294. package/dist/src/server/worker.d.ts +1 -0
  295. package/dist/src/server/worker.js +35 -0
  296. package/package.json +48 -0
@@ -0,0 +1,122 @@
1
+ import { minInSubtree, maxInSubtree } from './tree-utils.js';
2
+ import match from './match.js';
3
+ export var Direction;
4
+ (function (Direction) {
5
+ Direction["Left"] = "L";
6
+ Direction["Right"] = "R";
7
+ })(Direction || (Direction = {}));
8
+ function reverse(direction) {
9
+ switch (direction) {
10
+ case Direction.Left:
11
+ return Direction.Right;
12
+ case Direction.Right:
13
+ return Direction.Left;
14
+ default:
15
+ throw new TypeError('"direction" is not type of Direction');
16
+ }
17
+ }
18
+ function aeq(arr) {
19
+ return (x) => x.every((y, i) => y === arr[i]);
20
+ }
21
+ function isList(x) {
22
+ return Array.isArray(x) && x.length > 0 && Array.isArray(x[0]);
23
+ }
24
+ function nonMembershipProof(k, key, direction, sibling) {
25
+ return match([k > key, direction])
26
+ .on(aeq([true, Direction.Left]), () => [key, minInSubtree(sibling)])
27
+ .on(aeq([true, Direction.Right]), () => [key, null])
28
+ .on(aeq([false, Direction.Left]), () => [null, key])
29
+ .on(aeq([false, Direction.Right]), () => [maxInSubtree(sibling), key])
30
+ .otherwise(() => {
31
+ throw new TypeError('"direction" is not type of Direction');
32
+ });
33
+ }
34
+ function membershipProofR(sibling, direction, node, k) {
35
+ const left = node.left;
36
+ const right = node.right;
37
+ if (k === undefined || k === null) {
38
+ throw new TypeError('k is not a TreeKey');
39
+ }
40
+ // && would be more accurate here but there is never a case where only one
41
+ // would be set and thus this way we save on error handling later on.
42
+ if (!left || !right) {
43
+ if (!direction) {
44
+ throw new Error('"direction" must be set');
45
+ }
46
+ if (!sibling) {
47
+ throw new Error('"sibling" must be set');
48
+ }
49
+ // This is a leaf node
50
+ if (node.key === k) {
51
+ return [
52
+ [sibling.hash, reverse(direction)],
53
+ [node.hash, node.key],
54
+ ];
55
+ }
56
+ else {
57
+ // Find the non-membership proof otherwise
58
+ return nonMembershipProof(k, node.key, direction, sibling);
59
+ }
60
+ }
61
+ let result;
62
+ if (k <= left.key) {
63
+ // Going towards left child
64
+ result = membershipProofR(right, Direction.Left, left, k);
65
+ }
66
+ else if (k <= right.key) {
67
+ // Going towards right child
68
+ result = membershipProofR(left, Direction.Right, right, k);
69
+ }
70
+ else {
71
+ if (k > right.key) {
72
+ // The given key `k` is greater than any key in this tree.
73
+ // Trigger a proof for the largest key
74
+ return [right.key, null];
75
+ }
76
+ if (!direction) {
77
+ // TODO How should we set direction?
78
+ throw new TypeError('"Direction" must be set');
79
+ }
80
+ if (!sibling) {
81
+ throw new Error('"sibling" must be set');
82
+ }
83
+ // Find the non-membership proof otherwise
84
+ return nonMembershipProof(k, node.key, direction, sibling);
85
+ }
86
+ if (sibling) {
87
+ if (isList(result) && direction) {
88
+ // @ts-ignore the array thing is confusing for TS
89
+ return [[sibling.hash, reverse(direction)], ...result];
90
+ }
91
+ else if (result[1] === null && direction === Direction.Left) {
92
+ return [node.key, minInSubtree(sibling)];
93
+ }
94
+ else if (result[0] === null && direction === Direction.Right) {
95
+ return [maxInSubtree(sibling), node.key];
96
+ }
97
+ }
98
+ return result;
99
+ }
100
+ export default function membershipProof(root, k) {
101
+ if (!root) {
102
+ return [];
103
+ }
104
+ // Root has no sibling or direction so null is used
105
+ return (match(membershipProofR(null, null, root, k))
106
+ // The key is present in the tree
107
+ // Provide the proof in reverse order
108
+ .on((x) => isList(x), (r) => r.reverse())
109
+ // The key is greater than the largest element in the tree
110
+ // Provide a proof for the largest key
111
+ .on(([_, y]) => y === null, ([x, _]) => [...membershipProof(root, x), null])
112
+ // The key is smaller than the smallest element in the tree
113
+ // Provide a proof for the smallest key
114
+ .on(([x, _]) => x === null, ([_, y]) => [null, ...membershipProof(root, y)])
115
+ // The key is bounded by by two keys in the case of non-membership proff
116
+ // provide a proof for the bounding keys to exist
117
+ .otherwise(([x, y]) => [
118
+ membershipProof(root, x),
119
+ membershipProof(root, y),
120
+ ]));
121
+ }
122
+ //# sourceMappingURL=memebership-proof.js.map
@@ -0,0 +1,6 @@
1
+ import { TreeKey, TreeNode } from './types.js';
2
+ export declare function distance(x: TreeKey, y: TreeKey): TreeKey;
3
+ export declare function min(x: TreeNode | null, y: TreeNode | null): number;
4
+ export declare function max(x: TreeNode | null, y: TreeNode | null): number;
5
+ export declare function minInSubtree(node: TreeNode): TreeKey;
6
+ export declare function maxInSubtree(node: TreeNode): TreeKey;
@@ -0,0 +1,33 @@
1
+ import { TreeKeyNil } from './types.js';
2
+ export function distance(x, y) {
3
+ let v = x ^ y;
4
+ let r = TreeKeyNil;
5
+ while ((v >>= 1)) {
6
+ r++;
7
+ }
8
+ return r;
9
+ }
10
+ export function min(x, y) {
11
+ const a = (x && x.key) || TreeKeyNil;
12
+ const b = (y && y.key) || TreeKeyNil;
13
+ return a < b ? a : b;
14
+ }
15
+ export function max(x, y) {
16
+ const a = (x && x.key) || TreeKeyNil;
17
+ const b = (y && y.key) || TreeKeyNil;
18
+ return a > b ? a : b;
19
+ }
20
+ // Find min key in a subtree.
21
+ export function minInSubtree(node) {
22
+ if (!node.left) {
23
+ // We assume that the tree is always full and the last left node we can
24
+ // find is the min.
25
+ return node.key;
26
+ }
27
+ return minInSubtree(node.left);
28
+ }
29
+ // Find max key in a subtree.
30
+ export function maxInSubtree(node) {
31
+ return node.key;
32
+ }
33
+ //# sourceMappingURL=tree-utils.js.map
@@ -0,0 +1,2 @@
1
+ import { Csmt } from './types.js';
2
+ export declare function createTree(createHash: () => any): Csmt;
@@ -0,0 +1,209 @@
1
+ import { TreeKeyNil } from './types.js';
2
+ import { distance, min, max } from './tree-utils.js';
3
+ import membershipProof from './memebership-proof.js';
4
+ export function createTree(createHash) {
5
+ let root = null;
6
+ function genHash(s) {
7
+ return createHash().update(s).digest();
8
+ }
9
+ function genNodeHash(lHash, rHash) {
10
+ return createHash().update(lHash).update(rHash).digest();
11
+ }
12
+ function createNode(left, right) {
13
+ const hash = left && right
14
+ ? genNodeHash(left.hash, right.hash)
15
+ : genHash(Buffer.from(''));
16
+ return {
17
+ hash,
18
+ key: max(left, right),
19
+ left,
20
+ right,
21
+ };
22
+ }
23
+ function createLeaf(k, h, data) {
24
+ return {
25
+ hash: h,
26
+ key: k,
27
+ data,
28
+ left: null,
29
+ right: null,
30
+ };
31
+ }
32
+ /**
33
+ * Update node properties.
34
+ */
35
+ function updateNode(node) {
36
+ if (node.left || node.right) {
37
+ node.key = max(node.left, node.right);
38
+ if (node.left && node.right) {
39
+ node.hash = genNodeHash(node.left.hash, node.right.hash);
40
+ }
41
+ }
42
+ }
43
+ function insert(node, newLeaf) {
44
+ const { key: k } = newLeaf;
45
+ let left = node.left;
46
+ let right = node.right;
47
+ // Check if this is a leaf
48
+ if (!left && !right) {
49
+ const nodeKey = node.key;
50
+ if (nodeKey < k) {
51
+ return createNode(node, newLeaf);
52
+ }
53
+ else if (nodeKey > k) {
54
+ return createNode(newLeaf, node);
55
+ }
56
+ else {
57
+ throw new Error('Key exist');
58
+ }
59
+ }
60
+ const lDist = distance(k, (left && left.key) || TreeKeyNil);
61
+ const rDist = distance(k, (right && right.key) || TreeKeyNil);
62
+ if (lDist < rDist) {
63
+ node.left = left ? insert(left, newLeaf) : newLeaf;
64
+ }
65
+ else if (lDist > rDist) {
66
+ node.right = right ? insert(right, newLeaf) : newLeaf;
67
+ }
68
+ else {
69
+ return k < min(left, right)
70
+ ? createNode(newLeaf, node)
71
+ : createNode(node, newLeaf);
72
+ }
73
+ updateNode(node);
74
+ return node;
75
+ }
76
+ function checkForLeaf(node, k) {
77
+ return !node.left && !node.right && node.key === k;
78
+ }
79
+ function deleteNode(node, k) {
80
+ const left = node.left;
81
+ const right = node.right;
82
+ if (!left || !right) {
83
+ throw new Error('The tree is broken');
84
+ }
85
+ if (checkForLeaf(left, k) || checkForLeaf(right, k)) {
86
+ if (left.key === k) {
87
+ // The `left` node is discarded
88
+ return right;
89
+ }
90
+ else {
91
+ // The `right` node is discarded
92
+ return left;
93
+ }
94
+ }
95
+ else {
96
+ const lDist = distance(k, left.key);
97
+ const rDist = distance(k, right.key);
98
+ if (lDist < rDist) {
99
+ node.left = deleteNode(left, k);
100
+ updateNode(node);
101
+ return node;
102
+ }
103
+ else if (lDist > rDist) {
104
+ node.right = deleteNode(right, k);
105
+ updateNode(node);
106
+ return node;
107
+ }
108
+ else {
109
+ throw new Error(`k=${k} does not exist`);
110
+ }
111
+ }
112
+ }
113
+ function diffAB(diffA, diffB, nodeA, nodeB) {
114
+ if (nodeA &&
115
+ nodeB &&
116
+ nodeA.key === nodeB.key &&
117
+ nodeA.hash.compare(nodeB.hash) === 0) {
118
+ return;
119
+ }
120
+ // No hash match
121
+ const leftA = nodeA && nodeA.left;
122
+ const leftB = nodeB && nodeB.left;
123
+ const rightA = nodeA && nodeA.right;
124
+ const rightB = nodeB && nodeB.right;
125
+ // Check if this is a leaf that is missing from the right tree
126
+ if (nodeA && !leftA && !rightA) {
127
+ const bHash = diffB.get(nodeA.key);
128
+ if (bHash && bHash.compare(nodeA.hash) === 0) {
129
+ // The same leaf appears to exist in both trees
130
+ diffB.delete(nodeA.key);
131
+ }
132
+ else {
133
+ // The leaf doesn't exist or differs from the right tree
134
+ diffA.set(nodeA.key, nodeA.hash);
135
+ }
136
+ }
137
+ // Check if this is a leaf that is missing from the left tree
138
+ if (nodeB && !leftB && !rightB) {
139
+ const aHash = diffA.get(nodeB.key);
140
+ if (aHash && aHash.compare(nodeB.hash) === 0) {
141
+ diffA.delete(nodeB.key);
142
+ }
143
+ else {
144
+ diffB.set(nodeB.key, nodeB.hash);
145
+ }
146
+ }
147
+ if (leftA || leftB) {
148
+ // Recurse to the left branch
149
+ diffAB(diffA, diffB, leftA, leftB);
150
+ }
151
+ if (rightA || rightB) {
152
+ // Recurse to the right branch
153
+ diffAB(diffA, diffB, rightA, rightB);
154
+ }
155
+ }
156
+ function diff(tree) {
157
+ const leftMap = new Map();
158
+ const rightMap = new Map();
159
+ const nodeA = root;
160
+ const nodeB = tree.getRoot();
161
+ diffAB(leftMap, rightMap, nodeA, nodeB);
162
+ return {
163
+ left: [...leftMap],
164
+ right: [...rightMap],
165
+ };
166
+ }
167
+ function _visitLeafNodes(node, cb) {
168
+ if (!node)
169
+ return;
170
+ if (!node.left && !node.right)
171
+ cb(node);
172
+ else {
173
+ if (node.left)
174
+ _visitLeafNodes(node.left, cb);
175
+ if (node.right)
176
+ _visitLeafNodes(node.right, cb);
177
+ }
178
+ }
179
+ function search(node, k) {
180
+ if (!node || (k === node.key && !node.left && !node.right))
181
+ return node;
182
+ const { left, right } = node;
183
+ if (left && k <= left.key)
184
+ return search(left, k);
185
+ if (right && k <= right.key)
186
+ return search(right, k);
187
+ return null;
188
+ }
189
+ return {
190
+ getRoot: () => root,
191
+ insert: (k, h, data = null) => {
192
+ if (!(h instanceof Buffer)) {
193
+ throw new TypeError('`h` must be a Buffer');
194
+ }
195
+ const newLeaf = createLeaf(k, h, data);
196
+ root = root ? insert(root, newLeaf) : newLeaf;
197
+ },
198
+ delete: (k) => {
199
+ if (root) {
200
+ root = deleteNode(root, k);
201
+ }
202
+ },
203
+ diff,
204
+ membershipProof: (k) => membershipProof(root, k),
205
+ visitLeafNodes: (cb) => _visitLeafNodes(root, cb),
206
+ search: (k) => search(root, k),
207
+ };
208
+ }
209
+ //# sourceMappingURL=tree.js.map
@@ -0,0 +1,40 @@
1
+ import { Proof } from './memebership-proof.js';
2
+ export type TreeKey = number;
3
+ export declare const TreeKeyNil = 0;
4
+ export interface TreeNode {
5
+ hash: Buffer;
6
+ key: TreeKey;
7
+ data?: any;
8
+ left: TreeNode | null;
9
+ right: TreeNode | null;
10
+ }
11
+ export type KeyHashPair = [TreeKey, Buffer];
12
+ export interface TreeDiff {
13
+ left: KeyHashPair[];
14
+ right: KeyHashPair[];
15
+ }
16
+ export interface Csmt {
17
+ /**
18
+ * Get the root node.
19
+ */
20
+ getRoot: () => TreeNode | null;
21
+ /**
22
+ * Insert a new key-hash pair.
23
+ */
24
+ insert: (k: TreeKey, h: Buffer, data?: any) => void;
25
+ /**
26
+ * Delete a key-hash pair from the tree.
27
+ */
28
+ delete: (k: TreeKey) => void;
29
+ /**
30
+ * Compute the diff between this and a given tree.
31
+ */
32
+ diff: (tree: Csmt) => TreeDiff;
33
+ /**
34
+ * Provide a proof of membership if a key exist in the three;
35
+ * Otherwise a proof of non-membership is returned.
36
+ */
37
+ membershipProof: (k: TreeKey) => Proof;
38
+ visitLeafNodes: (cb: (leaf: TreeNode) => void) => void;
39
+ search: (k: TreeKey) => TreeNode;
40
+ }
@@ -0,0 +1,2 @@
1
+ export const TreeKeyNil = 0;
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,85 @@
1
+ import { StrictSchema } from '@based/schema';
2
+ import { SchemaTypeDef } from './schema/types.js';
3
+ import { createTree } from './csmt/index.js';
4
+ import { Worker, MessagePort } from 'node:worker_threads';
5
+ import { TransformFns } from './migrate/index.js';
6
+ export declare class DbWorker {
7
+ constructor(address: BigInt, db: DbServer);
8
+ db: DbServer;
9
+ channel: MessagePort;
10
+ worker: Worker;
11
+ resolvers: any[];
12
+ callback: (resolve: any) => void;
13
+ updateCtx(address: BigInt): Promise<void>;
14
+ getQueryBuf(buf: Buffer): Promise<Buffer>;
15
+ }
16
+ type OnSchemaChange = (schema: StrictSchema) => void;
17
+ export declare class DbServer {
18
+ #private;
19
+ modifyBuf: SharedArrayBuffer;
20
+ dbCtxExternal: any;
21
+ schema: StrictSchema & {
22
+ lastId: number;
23
+ };
24
+ migrating: number;
25
+ schemaTypesParsed: {
26
+ [key: string]: SchemaTypeDef;
27
+ };
28
+ schemaTypesParsedById: Record<number, SchemaTypeDef>;
29
+ fileSystemPath: string;
30
+ maxModifySize: number;
31
+ merkleTree: ReturnType<typeof createTree>;
32
+ dirtyRanges: Set<number>;
33
+ csmtHashFun: {
34
+ update: (buf: Buffer) => any;
35
+ digest: (encoding?: BufferEncoding) => Buffer | string;
36
+ reset: () => void;
37
+ };
38
+ workers: DbWorker[];
39
+ availableWorkerIndex: number;
40
+ processingQueries: number;
41
+ modifyQueue: Buffer[];
42
+ queryQueue: Map<Function, Buffer>;
43
+ stopped: boolean;
44
+ onSchemaChange: OnSchemaChange;
45
+ constructor({ path, maxModifySize, onSchemaChange, }: {
46
+ path: string;
47
+ maxModifySize?: number;
48
+ onSchemaChange?: OnSchemaChange;
49
+ });
50
+ start(opts?: {
51
+ clean?: boolean;
52
+ }): Promise<void>;
53
+ save(): Promise<void>;
54
+ createCsmtHashFun: () => {
55
+ update: (buf: Buffer) => any;
56
+ digest: (encoding?: BufferEncoding) => Buffer | string;
57
+ reset: () => void;
58
+ };
59
+ sortIndexes: {
60
+ [type: number]: {
61
+ [field: number]: {
62
+ [start: number]: any;
63
+ };
64
+ };
65
+ };
66
+ createSortIndex(type: string, field: string): any;
67
+ destroySortIndex(type: string, field: string): any;
68
+ hasSortIndex(typeId: number, field: number, start: number): boolean;
69
+ migrateSchema(schema: StrictSchema, transform?: Record<string, (node: Record<string, any>) => Record<string, any> | [string, Record<string, any>]>): Promise<StrictSchema & {
70
+ lastId: number;
71
+ }>;
72
+ createSortIndexBuffer(typeId: number, field: number, start: number): any;
73
+ updateMerkleTree(): void;
74
+ putSchema(strictSchema: StrictSchema, fromStart?: boolean, transformFns?: TransformFns): (StrictSchema & {
75
+ lastId: number;
76
+ }) | Promise<StrictSchema & {
77
+ lastId: number;
78
+ }>;
79
+ modify(buf: Buffer): Record<number, number>;
80
+ getQueryBuf(buf: Buffer): Promise<Uint8Array>;
81
+ onQueryEnd(): void;
82
+ stop(noSave?: boolean): Promise<void>;
83
+ destroy(): Promise<void>;
84
+ }
85
+ export {};