@optimystic/db-core 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 (345) hide show
  1. package/README.md +328 -0
  2. package/dist/index.min.js +18 -0
  3. package/dist/index.min.js.map +7 -0
  4. package/dist/src/blocks/block-store.d.ts +12 -0
  5. package/dist/src/blocks/block-store.d.ts.map +1 -0
  6. package/dist/src/blocks/block-store.js +2 -0
  7. package/dist/src/blocks/block-store.js.map +1 -0
  8. package/dist/src/blocks/block-types.d.ts +3 -0
  9. package/dist/src/blocks/block-types.d.ts.map +1 -0
  10. package/dist/src/blocks/block-types.js +9 -0
  11. package/dist/src/blocks/block-types.js.map +1 -0
  12. package/dist/src/blocks/helpers.d.ts +4 -0
  13. package/dist/src/blocks/helpers.d.ts.map +1 -0
  14. package/dist/src/blocks/helpers.js +12 -0
  15. package/dist/src/blocks/helpers.js.map +1 -0
  16. package/dist/src/blocks/index.d.ts +5 -0
  17. package/dist/src/blocks/index.d.ts.map +1 -0
  18. package/dist/src/blocks/index.js +5 -0
  19. package/dist/src/blocks/index.js.map +1 -0
  20. package/dist/src/blocks/structs.d.ts +14 -0
  21. package/dist/src/blocks/structs.d.ts.map +1 -0
  22. package/dist/src/blocks/structs.js +2 -0
  23. package/dist/src/blocks/structs.js.map +1 -0
  24. package/dist/src/btree/btree.d.ts +135 -0
  25. package/dist/src/btree/btree.d.ts.map +1 -0
  26. package/dist/src/btree/btree.js +727 -0
  27. package/dist/src/btree/btree.js.map +1 -0
  28. package/dist/src/btree/independent-trunk.d.ts +17 -0
  29. package/dist/src/btree/independent-trunk.d.ts.map +1 -0
  30. package/dist/src/btree/independent-trunk.js +41 -0
  31. package/dist/src/btree/independent-trunk.js.map +1 -0
  32. package/dist/src/btree/index.d.ts +6 -0
  33. package/dist/src/btree/index.d.ts.map +1 -0
  34. package/dist/src/btree/index.js +6 -0
  35. package/dist/src/btree/index.js.map +1 -0
  36. package/dist/src/btree/key-range.d.ts +13 -0
  37. package/dist/src/btree/key-range.d.ts.map +1 -0
  38. package/dist/src/btree/key-range.js +20 -0
  39. package/dist/src/btree/key-range.js.map +1 -0
  40. package/dist/src/btree/keyset.d.ts +4 -0
  41. package/dist/src/btree/keyset.d.ts.map +1 -0
  42. package/dist/src/btree/keyset.js +4 -0
  43. package/dist/src/btree/keyset.js.map +1 -0
  44. package/dist/src/btree/nodes.d.ts +16 -0
  45. package/dist/src/btree/nodes.d.ts.map +1 -0
  46. package/dist/src/btree/nodes.js +9 -0
  47. package/dist/src/btree/nodes.js.map +1 -0
  48. package/dist/src/btree/path.d.ts +22 -0
  49. package/dist/src/btree/path.d.ts.map +1 -0
  50. package/dist/src/btree/path.js +39 -0
  51. package/dist/src/btree/path.js.map +1 -0
  52. package/dist/src/btree/tree-block.d.ts +7 -0
  53. package/dist/src/btree/tree-block.d.ts.map +1 -0
  54. package/dist/src/btree/tree-block.js +5 -0
  55. package/dist/src/btree/tree-block.js.map +1 -0
  56. package/dist/src/btree/trunk.d.ts +13 -0
  57. package/dist/src/btree/trunk.d.ts.map +1 -0
  58. package/dist/src/btree/trunk.js +2 -0
  59. package/dist/src/btree/trunk.js.map +1 -0
  60. package/dist/src/chain/chain-nodes.d.ts +18 -0
  61. package/dist/src/chain/chain-nodes.d.ts.map +1 -0
  62. package/dist/src/chain/chain-nodes.js +10 -0
  63. package/dist/src/chain/chain-nodes.js.map +1 -0
  64. package/dist/src/chain/chain.d.ts +75 -0
  65. package/dist/src/chain/chain.d.ts.map +1 -0
  66. package/dist/src/chain/chain.js +268 -0
  67. package/dist/src/chain/chain.js.map +1 -0
  68. package/dist/src/chain/index.d.ts +2 -0
  69. package/dist/src/chain/index.d.ts.map +1 -0
  70. package/dist/src/chain/index.js +2 -0
  71. package/dist/src/chain/index.js.map +1 -0
  72. package/dist/src/cluster/i-cluster.d.ts +5 -0
  73. package/dist/src/cluster/i-cluster.d.ts.map +1 -0
  74. package/dist/src/cluster/i-cluster.js +2 -0
  75. package/dist/src/cluster/i-cluster.js.map +1 -0
  76. package/dist/src/cluster/index.d.ts +3 -0
  77. package/dist/src/cluster/index.d.ts.map +1 -0
  78. package/dist/src/cluster/index.js +3 -0
  79. package/dist/src/cluster/index.js.map +1 -0
  80. package/dist/src/cluster/structs.d.ts +47 -0
  81. package/dist/src/cluster/structs.d.ts.map +1 -0
  82. package/dist/src/cluster/structs.js +2 -0
  83. package/dist/src/cluster/structs.js.map +1 -0
  84. package/dist/src/collection/action.d.ts +26 -0
  85. package/dist/src/collection/action.d.ts.map +1 -0
  86. package/dist/src/collection/action.js +2 -0
  87. package/dist/src/collection/action.js.map +1 -0
  88. package/dist/src/collection/collection.d.ts +48 -0
  89. package/dist/src/collection/collection.d.ts.map +1 -0
  90. package/dist/src/collection/collection.js +175 -0
  91. package/dist/src/collection/collection.js.map +1 -0
  92. package/dist/src/collection/index.d.ts +4 -0
  93. package/dist/src/collection/index.d.ts.map +1 -0
  94. package/dist/src/collection/index.js +4 -0
  95. package/dist/src/collection/index.js.map +1 -0
  96. package/dist/src/collection/struct.d.ts +16 -0
  97. package/dist/src/collection/struct.d.ts.map +1 -0
  98. package/dist/src/collection/struct.js +2 -0
  99. package/dist/src/collection/struct.js.map +1 -0
  100. package/dist/src/collections/diary/diary.d.ts +9 -0
  101. package/dist/src/collections/diary/diary.d.ts.map +1 -0
  102. package/dist/src/collections/diary/diary.js +37 -0
  103. package/dist/src/collections/diary/diary.js.map +1 -0
  104. package/dist/src/collections/diary/index.d.ts +3 -0
  105. package/dist/src/collections/diary/index.d.ts.map +1 -0
  106. package/dist/src/collections/diary/index.js +3 -0
  107. package/dist/src/collections/diary/index.js.map +1 -0
  108. package/dist/src/collections/diary/struct.d.ts +2 -0
  109. package/dist/src/collections/diary/struct.d.ts.map +1 -0
  110. package/dist/src/collections/diary/struct.js +3 -0
  111. package/dist/src/collections/diary/struct.js.map +1 -0
  112. package/dist/src/collections/index.d.ts +3 -0
  113. package/dist/src/collections/index.d.ts.map +1 -0
  114. package/dist/src/collections/index.js +3 -0
  115. package/dist/src/collections/index.js.map +1 -0
  116. package/dist/src/collections/tree/collection-trunk.d.ts +11 -0
  117. package/dist/src/collections/tree/collection-trunk.d.ts.map +1 -0
  118. package/dist/src/collections/tree/collection-trunk.js +22 -0
  119. package/dist/src/collections/tree/collection-trunk.js.map +1 -0
  120. package/dist/src/collections/tree/index.d.ts +3 -0
  121. package/dist/src/collections/tree/index.d.ts.map +1 -0
  122. package/dist/src/collections/tree/index.js +3 -0
  123. package/dist/src/collections/tree/index.js.map +1 -0
  124. package/dist/src/collections/tree/struct.d.ts +12 -0
  125. package/dist/src/collections/tree/struct.d.ts.map +1 -0
  126. package/dist/src/collections/tree/struct.js +4 -0
  127. package/dist/src/collections/tree/struct.js.map +1 -0
  128. package/dist/src/collections/tree/tree.d.ts +34 -0
  129. package/dist/src/collections/tree/tree.d.ts.map +1 -0
  130. package/dist/src/collections/tree/tree.js +100 -0
  131. package/dist/src/collections/tree/tree.js.map +1 -0
  132. package/dist/src/index.d.ts +18 -0
  133. package/dist/src/index.d.ts.map +1 -0
  134. package/dist/src/index.js +18 -0
  135. package/dist/src/index.js.map +1 -0
  136. package/dist/src/log/index.d.ts +3 -0
  137. package/dist/src/log/index.d.ts.map +1 -0
  138. package/dist/src/log/index.js +3 -0
  139. package/dist/src/log/index.js.map +1 -0
  140. package/dist/src/log/log.d.ts +57 -0
  141. package/dist/src/log/log.d.ts.map +1 -0
  142. package/dist/src/log/log.js +131 -0
  143. package/dist/src/log/log.js.map +1 -0
  144. package/dist/src/log/struct.d.ts +36 -0
  145. package/dist/src/log/struct.d.ts.map +1 -0
  146. package/dist/src/log/struct.js +3 -0
  147. package/dist/src/log/struct.js.map +1 -0
  148. package/dist/src/network/i-key-network.d.ts +21 -0
  149. package/dist/src/network/i-key-network.d.ts.map +1 -0
  150. package/dist/src/network/i-key-network.js +2 -0
  151. package/dist/src/network/i-key-network.js.map +1 -0
  152. package/dist/src/network/i-peer-network.d.ts +8 -0
  153. package/dist/src/network/i-peer-network.d.ts.map +1 -0
  154. package/dist/src/network/i-peer-network.js +2 -0
  155. package/dist/src/network/i-peer-network.js.map +1 -0
  156. package/dist/src/network/i-repo.d.ts +17 -0
  157. package/dist/src/network/i-repo.d.ts.map +1 -0
  158. package/dist/src/network/i-repo.js +2 -0
  159. package/dist/src/network/i-repo.js.map +1 -0
  160. package/dist/src/network/index.d.ts +6 -0
  161. package/dist/src/network/index.d.ts.map +1 -0
  162. package/dist/src/network/index.js +6 -0
  163. package/dist/src/network/index.js.map +1 -0
  164. package/dist/src/network/repo-protocol.d.ts +19 -0
  165. package/dist/src/network/repo-protocol.d.ts.map +1 -0
  166. package/dist/src/network/repo-protocol.js +2 -0
  167. package/dist/src/network/repo-protocol.js.map +1 -0
  168. package/dist/src/network/struct.d.ts +115 -0
  169. package/dist/src/network/struct.d.ts.map +1 -0
  170. package/dist/src/network/struct.js +2 -0
  171. package/dist/src/network/struct.js.map +1 -0
  172. package/dist/src/transaction/actions-engine.d.ts +37 -0
  173. package/dist/src/transaction/actions-engine.d.ts.map +1 -0
  174. package/dist/src/transaction/actions-engine.js +67 -0
  175. package/dist/src/transaction/actions-engine.js.map +1 -0
  176. package/dist/src/transaction/context.d.ts +60 -0
  177. package/dist/src/transaction/context.d.ts.map +1 -0
  178. package/dist/src/transaction/context.js +91 -0
  179. package/dist/src/transaction/context.js.map +1 -0
  180. package/dist/src/transaction/coordinator.d.ts +118 -0
  181. package/dist/src/transaction/coordinator.d.ts.map +1 -0
  182. package/dist/src/transaction/coordinator.js +417 -0
  183. package/dist/src/transaction/coordinator.js.map +1 -0
  184. package/dist/src/transaction/index.d.ts +10 -0
  185. package/dist/src/transaction/index.d.ts.map +1 -0
  186. package/dist/src/transaction/index.js +7 -0
  187. package/dist/src/transaction/index.js.map +1 -0
  188. package/dist/src/transaction/session.d.ts +80 -0
  189. package/dist/src/transaction/session.d.ts.map +1 -0
  190. package/dist/src/transaction/session.js +161 -0
  191. package/dist/src/transaction/session.js.map +1 -0
  192. package/dist/src/transaction/transaction.d.ts +156 -0
  193. package/dist/src/transaction/transaction.d.ts.map +1 -0
  194. package/dist/src/transaction/transaction.js +31 -0
  195. package/dist/src/transaction/transaction.js.map +1 -0
  196. package/dist/src/transaction/validator.d.ts +46 -0
  197. package/dist/src/transaction/validator.d.ts.map +1 -0
  198. package/dist/src/transaction/validator.js +97 -0
  199. package/dist/src/transaction/validator.js.map +1 -0
  200. package/dist/src/transactor/index.d.ts +4 -0
  201. package/dist/src/transactor/index.d.ts.map +1 -0
  202. package/dist/src/transactor/index.js +4 -0
  203. package/dist/src/transactor/index.js.map +1 -0
  204. package/dist/src/transactor/network-transactor.d.ts +36 -0
  205. package/dist/src/transactor/network-transactor.d.ts.map +1 -0
  206. package/dist/src/transactor/network-transactor.js +297 -0
  207. package/dist/src/transactor/network-transactor.js.map +1 -0
  208. package/dist/src/transactor/transactor-source.d.ts +24 -0
  209. package/dist/src/transactor/transactor-source.d.ts.map +1 -0
  210. package/dist/src/transactor/transactor-source.js +62 -0
  211. package/dist/src/transactor/transactor-source.js.map +1 -0
  212. package/dist/src/transactor/transactor.d.ts +38 -0
  213. package/dist/src/transactor/transactor.d.ts.map +1 -0
  214. package/dist/src/transactor/transactor.js +2 -0
  215. package/dist/src/transactor/transactor.js.map +1 -0
  216. package/dist/src/transform/atomic.d.ts +8 -0
  217. package/dist/src/transform/atomic.d.ts.map +1 -0
  218. package/dist/src/transform/atomic.js +14 -0
  219. package/dist/src/transform/atomic.js.map +1 -0
  220. package/dist/src/transform/cache-source.d.ts +13 -0
  221. package/dist/src/transform/cache-source.d.ts.map +1 -0
  222. package/dist/src/transform/cache-source.js +52 -0
  223. package/dist/src/transform/cache-source.js.map +1 -0
  224. package/dist/src/transform/helpers.d.ts +25 -0
  225. package/dist/src/transform/helpers.d.ts.map +1 -0
  226. package/dist/src/transform/helpers.js +105 -0
  227. package/dist/src/transform/helpers.js.map +1 -0
  228. package/dist/src/transform/index.d.ts +6 -0
  229. package/dist/src/transform/index.d.ts.map +1 -0
  230. package/dist/src/transform/index.js +6 -0
  231. package/dist/src/transform/index.js.map +1 -0
  232. package/dist/src/transform/struct.d.ts +19 -0
  233. package/dist/src/transform/struct.d.ts.map +1 -0
  234. package/dist/src/transform/struct.js +2 -0
  235. package/dist/src/transform/struct.js.map +1 -0
  236. package/dist/src/transform/tracker.d.ts +22 -0
  237. package/dist/src/transform/tracker.d.ts.map +1 -0
  238. package/dist/src/transform/tracker.js +64 -0
  239. package/dist/src/transform/tracker.js.map +1 -0
  240. package/dist/src/utility/actor.d.ts +11 -0
  241. package/dist/src/utility/actor.d.ts.map +1 -0
  242. package/dist/src/utility/actor.js +39 -0
  243. package/dist/src/utility/actor.js.map +1 -0
  244. package/dist/src/utility/batch-coordinator.d.ts +56 -0
  245. package/dist/src/utility/batch-coordinator.d.ts.map +1 -0
  246. package/dist/src/utility/batch-coordinator.js +127 -0
  247. package/dist/src/utility/batch-coordinator.js.map +1 -0
  248. package/dist/src/utility/block-id-to-bytes.d.ts +3 -0
  249. package/dist/src/utility/block-id-to-bytes.d.ts.map +1 -0
  250. package/dist/src/utility/block-id-to-bytes.js +7 -0
  251. package/dist/src/utility/block-id-to-bytes.js.map +1 -0
  252. package/dist/src/utility/ensured.d.ts +3 -0
  253. package/dist/src/utility/ensured.d.ts.map +1 -0
  254. package/dist/src/utility/ensured.js +24 -0
  255. package/dist/src/utility/ensured.js.map +1 -0
  256. package/dist/src/utility/groupby.d.ts +8 -0
  257. package/dist/src/utility/groupby.d.ts.map +1 -0
  258. package/dist/src/utility/groupby.js +15 -0
  259. package/dist/src/utility/groupby.js.map +1 -0
  260. package/dist/src/utility/is-record-empty.d.ts +3 -0
  261. package/dist/src/utility/is-record-empty.d.ts.map +1 -0
  262. package/dist/src/utility/is-record-empty.js +7 -0
  263. package/dist/src/utility/is-record-empty.js.map +1 -0
  264. package/dist/src/utility/latches.d.ts +11 -0
  265. package/dist/src/utility/latches.d.ts.map +1 -0
  266. package/dist/src/utility/latches.js +36 -0
  267. package/dist/src/utility/latches.js.map +1 -0
  268. package/dist/src/utility/nameof.d.ts +3 -0
  269. package/dist/src/utility/nameof.d.ts.map +1 -0
  270. package/dist/src/utility/nameof.js +5 -0
  271. package/dist/src/utility/nameof.js.map +1 -0
  272. package/dist/src/utility/pending.d.ts +13 -0
  273. package/dist/src/utility/pending.d.ts.map +1 -0
  274. package/dist/src/utility/pending.js +37 -0
  275. package/dist/src/utility/pending.js.map +1 -0
  276. package/package.json +56 -0
  277. package/src/blocks/block-store.ts +13 -0
  278. package/src/blocks/block-types.ts +11 -0
  279. package/src/blocks/helpers.ts +13 -0
  280. package/src/blocks/index.ts +5 -0
  281. package/src/blocks/structs.ts +17 -0
  282. package/src/btree/btree.ts +804 -0
  283. package/src/btree/independent-trunk.ts +54 -0
  284. package/src/btree/index.ts +5 -0
  285. package/src/btree/key-range.ts +15 -0
  286. package/src/btree/keyset.ts +6 -0
  287. package/src/btree/nodes.ts +25 -0
  288. package/src/btree/path.ts +37 -0
  289. package/src/btree/tree-block.ts +11 -0
  290. package/src/btree/trunk.ts +14 -0
  291. package/src/chain/chain-nodes.ts +24 -0
  292. package/src/chain/chain.ts +324 -0
  293. package/src/chain/index.ts +2 -0
  294. package/src/cluster/i-cluster.ts +6 -0
  295. package/src/cluster/index.ts +2 -0
  296. package/src/cluster/structs.ts +46 -0
  297. package/src/collection/action.ts +31 -0
  298. package/src/collection/collection.ts +200 -0
  299. package/src/collection/index.ts +3 -0
  300. package/src/collection/struct.ts +20 -0
  301. package/src/collections/diary/diary.ts +43 -0
  302. package/src/collections/diary/index.ts +2 -0
  303. package/src/collections/diary/struct.ts +3 -0
  304. package/src/collections/index.ts +2 -0
  305. package/src/collections/tree/collection-trunk.ts +25 -0
  306. package/src/collections/tree/index.ts +2 -0
  307. package/src/collections/tree/readme.md +19 -0
  308. package/src/collections/tree/struct.ts +18 -0
  309. package/src/collections/tree/tree.ts +124 -0
  310. package/src/index.ts +17 -0
  311. package/src/log/index.ts +2 -0
  312. package/src/log/log.ts +155 -0
  313. package/src/log/struct.ts +40 -0
  314. package/src/network/i-key-network.ts +24 -0
  315. package/src/network/i-peer-network.ts +8 -0
  316. package/src/network/i-repo.ts +19 -0
  317. package/src/network/index.ts +5 -0
  318. package/src/network/repo-protocol.ts +12 -0
  319. package/src/network/struct.ts +137 -0
  320. package/src/transaction/actions-engine.ts +83 -0
  321. package/src/transaction/context.ts +103 -0
  322. package/src/transaction/coordinator.ts +583 -0
  323. package/src/transaction/index.ts +30 -0
  324. package/src/transaction/session.ts +182 -0
  325. package/src/transaction/transaction.ts +205 -0
  326. package/src/transaction/validator.ts +150 -0
  327. package/src/transactor/index.ts +4 -0
  328. package/src/transactor/network-transactor.ts +435 -0
  329. package/src/transactor/transactor-source.ts +65 -0
  330. package/src/transactor/transactor.ts +44 -0
  331. package/src/transform/atomic.ts +16 -0
  332. package/src/transform/cache-source.ts +57 -0
  333. package/src/transform/helpers.ts +117 -0
  334. package/src/transform/index.ts +5 -0
  335. package/src/transform/struct.ts +22 -0
  336. package/src/transform/tracker.ts +70 -0
  337. package/src/utility/actor.ts +62 -0
  338. package/src/utility/batch-coordinator.ts +174 -0
  339. package/src/utility/block-id-to-bytes.ts +8 -0
  340. package/src/utility/ensured.ts +32 -0
  341. package/src/utility/groupby.ts +18 -0
  342. package/src/utility/is-record-empty.ts +5 -0
  343. package/src/utility/latches.ts +42 -0
  344. package/src/utility/nameof.ts +7 -0
  345. package/src/utility/pending.ts +41 -0
@@ -0,0 +1,67 @@
1
+ export const ACTIONS_ENGINE_ID = "actions@1.0.0";
2
+ /**
3
+ * Built-in action-based transaction engine for testing.
4
+ *
5
+ * This engine treats each statement as a JSON-encoded CollectionActions object.
6
+ * It's useful for testing the transaction infrastructure without needing SQL.
7
+ *
8
+ * Each statement format:
9
+ * ```json
10
+ * {
11
+ * "collectionId": "users",
12
+ * "actions": [
13
+ * { "type": "insert", "data": { "id": 1, "name": "Alice" } }
14
+ * ]
15
+ * }
16
+ * ```
17
+ */
18
+ export class ActionsEngine {
19
+ coordinator;
20
+ constructor(coordinator) {
21
+ this.coordinator = coordinator;
22
+ }
23
+ async execute(transaction) {
24
+ try {
25
+ // Parse each statement as a CollectionActions object
26
+ const allActions = [];
27
+ for (const statement of transaction.statements) {
28
+ const collectionActions = JSON.parse(statement);
29
+ // Validate structure
30
+ if (!collectionActions.collectionId || typeof collectionActions.collectionId !== 'string') {
31
+ return {
32
+ success: false,
33
+ error: 'Invalid statement: missing collectionId'
34
+ };
35
+ }
36
+ if (!collectionActions.actions || !Array.isArray(collectionActions.actions)) {
37
+ return {
38
+ success: false,
39
+ error: `Invalid statement: collection ${collectionActions.collectionId} missing or invalid actions array`
40
+ };
41
+ }
42
+ allActions.push(collectionActions);
43
+ // Apply actions through coordinator (for validation/replay)
44
+ await this.coordinator.applyActions([collectionActions], transaction.stamp.id);
45
+ }
46
+ // Return success (actions already applied)
47
+ return {
48
+ success: true,
49
+ actions: allActions
50
+ };
51
+ }
52
+ catch (error) {
53
+ return {
54
+ success: false,
55
+ error: `Failed to execute transaction: ${error instanceof Error ? error.message : String(error)}`
56
+ };
57
+ }
58
+ }
59
+ }
60
+ /**
61
+ * Helper to create an actions-based transaction statements array.
62
+ * Each CollectionActions becomes a separate statement.
63
+ */
64
+ export function createActionsStatements(collections) {
65
+ return collections.map(c => JSON.stringify(c));
66
+ }
67
+ //# sourceMappingURL=actions-engine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actions-engine.js","sourceRoot":"","sources":["../../../src/transaction/actions-engine.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,iBAAiB,GAAG,eAAe,CAAC;AAEjD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,aAAa;IACL;IAApB,YAAoB,WAAmC;QAAnC,gBAAW,GAAX,WAAW,CAAwB;IAAG,CAAC;IAE3D,KAAK,CAAC,OAAO,CAAC,WAAwB;QACrC,IAAI,CAAC;YACJ,qDAAqD;YACrD,MAAM,UAAU,GAAwB,EAAE,CAAC;YAE3C,KAAK,MAAM,SAAS,IAAI,WAAW,CAAC,UAAU,EAAE,CAAC;gBAChD,MAAM,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,CAAsB,CAAC;gBAErE,qBAAqB;gBACrB,IAAI,CAAC,iBAAiB,CAAC,YAAY,IAAI,OAAO,iBAAiB,CAAC,YAAY,KAAK,QAAQ,EAAE,CAAC;oBAC3F,OAAO;wBACN,OAAO,EAAE,KAAK;wBACd,KAAK,EAAE,yCAAyC;qBAChD,CAAC;gBACH,CAAC;gBAED,IAAI,CAAC,iBAAiB,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,iBAAiB,CAAC,OAAO,CAAC,EAAE,CAAC;oBAC7E,OAAO;wBACN,OAAO,EAAE,KAAK;wBACd,KAAK,EAAE,iCAAiC,iBAAiB,CAAC,YAAY,mCAAmC;qBACzG,CAAC;gBACH,CAAC;gBAED,UAAU,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;gBAEnC,4DAA4D;gBAC5D,MAAM,IAAI,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC,iBAAiB,CAAC,EAAE,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YAChF,CAAC;YAED,2CAA2C;YAC3C,OAAO;gBACN,OAAO,EAAE,IAAI;gBACb,OAAO,EAAE,UAAU;aACnB,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO;gBACN,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,kCAAkC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;aACjG,CAAC;QACH,CAAC;IACF,CAAC;CACD;AAUD;;;GAGG;AACH,MAAM,UAAU,uBAAuB,CAAC,WAAgC;IACvE,OAAO,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;AAChD,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { CollectionId } from "../index.js";
2
+ import type { TransactionCoordinator } from "./coordinator.js";
3
+ import type { ReadDependency, ExecutionResult } from "./transaction.js";
4
+ import type { Action } from "../collection/action.js";
5
+ /**
6
+ * Transaction context for accumulating actions and reads.
7
+ *
8
+ * Usage:
9
+ * const txn = coordinator.begin();
10
+ * txn.addAction('users', { type: 'insert', data: {...} });
11
+ * txn.addAction('users', { type: 'get', data: { key: 1 } });
12
+ * const result = await txn.commit();
13
+ */
14
+ export declare class TransactionContext {
15
+ private readonly coordinator;
16
+ readonly transactionId: string;
17
+ readonly engine: string;
18
+ private readonly collectionActions;
19
+ private readonly reads;
20
+ constructor(coordinator: TransactionCoordinator, transactionId: string, engine: string);
21
+ /**
22
+ * Add an action to a collection.
23
+ *
24
+ * Actions are collection-specific:
25
+ * - Tree: 'insert', 'delete', 'get', 'scan'
26
+ * - Diary: 'append', 'read'
27
+ * - etc.
28
+ */
29
+ addAction(collectionId: CollectionId, action: Action<any>): Promise<void>;
30
+ /**
31
+ * Add a read dependency for optimistic concurrency control.
32
+ */
33
+ addRead(read: ReadDependency): void;
34
+ /**
35
+ * Commit the transaction.
36
+ *
37
+ * This executes all accumulated actions across all affected collections,
38
+ * coordinating with the network as needed.
39
+ */
40
+ commit(): Promise<ExecutionResult>;
41
+ /**
42
+ * Rollback the transaction (just discard accumulated state).
43
+ */
44
+ rollback(): void;
45
+ /**
46
+ * Get all accumulated actions by collection.
47
+ * Used by coordinator during commit.
48
+ */
49
+ getCollectionActions(): Map<CollectionId, Action<any>[]>;
50
+ /**
51
+ * Get all accumulated read dependencies.
52
+ * Used by coordinator during commit.
53
+ */
54
+ getReads(): ReadDependency[];
55
+ /**
56
+ * Get the set of affected collection IDs.
57
+ */
58
+ getAffectedCollections(): Set<CollectionId>;
59
+ }
60
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../../src/transaction/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAY,MAAM,aAAa,CAAC;AAC1D,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD;;;;;;;;GAQG;AACH,qBAAa,kBAAkB;IAK7B,OAAO,CAAC,QAAQ,CAAC,WAAW;aACZ,aAAa,EAAE,MAAM;aACrB,MAAM,EAAE,MAAM;IAN/B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAA+C;IACjF,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAwB;gBAG5B,WAAW,EAAE,sBAAsB,EACpC,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,MAAM;IAG/B;;;;;;;OAOG;IACG,SAAS,CAAC,YAAY,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAsB/E;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI;IAInC;;;;;OAKG;IACG,MAAM,IAAI,OAAO,CAAC,eAAe,CAAC;IAIxC;;OAEG;IACH,QAAQ,IAAI,IAAI;IAKhB;;;OAGG;IACH,oBAAoB,IAAI,GAAG,CAAC,YAAY,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;IAIxD;;;OAGG;IACH,QAAQ,IAAI,cAAc,EAAE;IAI5B;;OAEG;IACH,sBAAsB,IAAI,GAAG,CAAC,YAAY,CAAC;CAG3C"}
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Transaction context for accumulating actions and reads.
3
+ *
4
+ * Usage:
5
+ * const txn = coordinator.begin();
6
+ * txn.addAction('users', { type: 'insert', data: {...} });
7
+ * txn.addAction('users', { type: 'get', data: { key: 1 } });
8
+ * const result = await txn.commit();
9
+ */
10
+ export class TransactionContext {
11
+ coordinator;
12
+ transactionId;
13
+ engine;
14
+ collectionActions = new Map();
15
+ reads = [];
16
+ constructor(coordinator, transactionId, engine) {
17
+ this.coordinator = coordinator;
18
+ this.transactionId = transactionId;
19
+ this.engine = engine;
20
+ }
21
+ /**
22
+ * Add an action to a collection.
23
+ *
24
+ * Actions are collection-specific:
25
+ * - Tree: 'insert', 'delete', 'get', 'scan'
26
+ * - Diary: 'append', 'read'
27
+ * - etc.
28
+ */
29
+ async addAction(collectionId, action) {
30
+ // Tag action with transaction reference
31
+ const taggedAction = {
32
+ ...action,
33
+ transaction: this.transactionId
34
+ };
35
+ // Get the collection and immediately execute the action to update local snapshot
36
+ const collection = this.coordinator['collections'].get(collectionId);
37
+ if (collection) {
38
+ // Execute through collection to update tracker and pending buffer
39
+ await collection.act(taggedAction);
40
+ }
41
+ // If no collection registered, just buffer the action (for testing or deferred collection creation)
42
+ // Record in transaction context
43
+ if (!this.collectionActions.has(collectionId)) {
44
+ this.collectionActions.set(collectionId, []);
45
+ }
46
+ this.collectionActions.get(collectionId).push(taggedAction);
47
+ }
48
+ /**
49
+ * Add a read dependency for optimistic concurrency control.
50
+ */
51
+ addRead(read) {
52
+ this.reads.push(read);
53
+ }
54
+ /**
55
+ * Commit the transaction.
56
+ *
57
+ * This executes all accumulated actions across all affected collections,
58
+ * coordinating with the network as needed.
59
+ */
60
+ async commit() {
61
+ return await this.coordinator.commitTransaction(this);
62
+ }
63
+ /**
64
+ * Rollback the transaction (just discard accumulated state).
65
+ */
66
+ rollback() {
67
+ this.collectionActions.clear();
68
+ this.reads.length = 0;
69
+ }
70
+ /**
71
+ * Get all accumulated actions by collection.
72
+ * Used by coordinator during commit.
73
+ */
74
+ getCollectionActions() {
75
+ return this.collectionActions;
76
+ }
77
+ /**
78
+ * Get all accumulated read dependencies.
79
+ * Used by coordinator during commit.
80
+ */
81
+ getReads() {
82
+ return this.reads;
83
+ }
84
+ /**
85
+ * Get the set of affected collection IDs.
86
+ */
87
+ getAffectedCollections() {
88
+ return new Set(this.collectionActions.keys());
89
+ }
90
+ }
91
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../../src/transaction/context.ts"],"names":[],"mappings":"AAKA;;;;;;;;GAQG;AACH,MAAM,OAAO,kBAAkB;IAKZ;IACD;IACA;IANA,iBAAiB,GAAqC,IAAI,GAAG,EAAE,CAAC;IAChE,KAAK,GAAqB,EAAE,CAAC;IAE9C,YACkB,WAAmC,EACpC,aAAqB,EACrB,MAAc;QAFb,gBAAW,GAAX,WAAW,CAAwB;QACpC,kBAAa,GAAb,aAAa,CAAQ;QACrB,WAAM,GAAN,MAAM,CAAQ;IAC5B,CAAC;IAEJ;;;;;;;OAOG;IACH,KAAK,CAAC,SAAS,CAAC,YAA0B,EAAE,MAAmB;QAC9D,wCAAwC;QACxC,MAAM,YAAY,GAAG;YACpB,GAAG,MAAM;YACT,WAAW,EAAE,IAAI,CAAC,aAAa;SAC/B,CAAC;QAEF,iFAAiF;QACjF,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QACrE,IAAI,UAAU,EAAE,CAAC;YAChB,kEAAkE;YAClE,MAAM,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QACpC,CAAC;QACD,oGAAoG;QAEpG,gCAAgC;QAChC,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;YAC/C,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;QAC9C,CAAC;QACD,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,YAAY,CAAE,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC9D,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,IAAoB;QAC3B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,MAAM;QACX,OAAO,MAAM,IAAI,CAAC,WAAW,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACvD,CAAC;IAED;;OAEG;IACH,QAAQ;QACP,IAAI,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC;QAC/B,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;IACvB,CAAC;IAED;;;OAGG;IACH,oBAAoB;QACnB,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACH,QAAQ;QACP,OAAO,IAAI,CAAC,KAAK,CAAC;IACnB,CAAC;IAED;;OAEG;IACH,sBAAsB;QACrB,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;CACD"}
@@ -0,0 +1,118 @@
1
+ import type { ITransactor, CollectionId } from "../index.js";
2
+ import type { Transaction, ExecutionResult, ITransactionEngine, CollectionActions } from "./transaction.js";
3
+ import type { Collection } from "../collection/collection.js";
4
+ import { TransactionContext } from "./context.js";
5
+ /**
6
+ * Coordinates multi-collection transactions.
7
+ *
8
+ * This is the ONLY interface for all mutations (single or multi-collection).
9
+ *
10
+ * Responsibilities:
11
+ * - Manage collections (create as needed)
12
+ * - Apply actions to collections (run handlers, write to logs)
13
+ * - Commit transactions by running consensus phases (GATHER, PEND, COMMIT)
14
+ */
15
+ export declare class TransactionCoordinator {
16
+ private readonly transactor;
17
+ private readonly collections;
18
+ constructor(transactor: ITransactor, collections: Map<CollectionId, Collection<any>>);
19
+ /**
20
+ * Apply actions to collections (called by engines during statement execution).
21
+ *
22
+ * This is the core method that engines call to apply actions to collections.
23
+ * Actions are tagged with the stamp ID and executed immediately through collections
24
+ * to update the local snapshot.
25
+ *
26
+ * @param actions - The actions to apply (per collection)
27
+ * @param stampId - The transaction stamp ID to tag actions with
28
+ */
29
+ applyActions(actions: CollectionActions[], stampId: string): Promise<void>;
30
+ /**
31
+ * Commit a transaction (actions already applied, orchestrate PEND/COMMIT).
32
+ *
33
+ * This is called by TransactionSession.commit() after all statements have been executed.
34
+ * Actions have already been applied to collections via applyActions(), so this method
35
+ * just orchestrates the distributed consensus.
36
+ *
37
+ * @param transaction - The transaction to commit
38
+ */
39
+ commit(transaction: Transaction): Promise<void>;
40
+ /**
41
+ * Rollback a transaction (undo applied actions).
42
+ *
43
+ * This is called by TransactionSession.rollback() to undo all actions
44
+ * that were applied via applyActions().
45
+ *
46
+ * @param _stampId - The transaction stamp ID to rollback (currently unused - we clear all trackers)
47
+ */
48
+ rollback(_stampId: string): Promise<void>;
49
+ /**
50
+ * Compute hash of all operations in a transaction.
51
+ * This hash is used for validation - validators re-execute the transaction
52
+ * and compare their computed operations hash with this one.
53
+ */
54
+ private hashOperations;
55
+ /**
56
+ * Commit a transaction context.
57
+ *
58
+ * @deprecated Use TransactionSession instead of TransactionContext
59
+ * This is called by TransactionContext.commit().
60
+ *
61
+ * @param context - The transaction context to commit
62
+ * @returns Execution result with actions and results
63
+ */
64
+ commitTransaction(context: TransactionContext): Promise<ExecutionResult>;
65
+ /**
66
+ * Execute a fully-formed transaction.
67
+ *
68
+ * This can be called directly with a complete transaction (e.g., from Quereus),
69
+ * or indirectly via commitTransaction().
70
+ *
71
+ * @param transaction - The transaction to execute
72
+ * @param engine - The engine to use for executing the transaction
73
+ * @returns Execution result with actions and results
74
+ */
75
+ execute(transaction: Transaction, engine: ITransactionEngine): Promise<ExecutionResult>;
76
+ /**
77
+ * Apply actions to a collection.
78
+ *
79
+ * This runs the action handlers, writes to the log, and collects transforms.
80
+ */
81
+ private applyActionsToCollection;
82
+ /**
83
+ * Coordinate a transaction across multiple collections.
84
+ *
85
+ * @param transaction - The transaction to coordinate
86
+ * @param operationsHash - Hash of all operations for validation
87
+ * @param collectionTransforms - Map of collectionId to its transforms
88
+ * @param criticalBlocks - Map of collectionId to its log tail blockId
89
+ */
90
+ private coordinateTransaction;
91
+ /**
92
+ * GATHER phase: Collect nominees from critical clusters.
93
+ *
94
+ * Skip if only one collection affected (single-collection consensus).
95
+ *
96
+ * @param criticalBlockIds - Block IDs of all log tails
97
+ * @returns Set of peer IDs to use for consensus, or null for single-collection
98
+ */
99
+ private gatherPhase;
100
+ /**
101
+ * PEND phase: Distribute transaction to all affected block clusters.
102
+ *
103
+ * @param transaction - The full transaction for replay/validation
104
+ * @param operationsHash - Hash of all operations for validation
105
+ * @param collectionTransforms - Map of collectionId to its transforms
106
+ * @param superclusterNominees - Nominees for multi-collection consensus (null for single-collection)
107
+ */
108
+ private pendPhase;
109
+ /**
110
+ * COMMIT phase: Commit to all critical blocks.
111
+ */
112
+ private commitPhase;
113
+ /**
114
+ * CANCEL phase: Cancel pending actions on all affected blocks.
115
+ */
116
+ private cancelPhase;
117
+ }
118
+ //# sourceMappingURL=coordinator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"coordinator.d.ts","sourceRoot":"","sources":["../../../src/transaction/coordinator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAW,YAAY,EAA6E,MAAM,aAAa,CAAC;AACjJ,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAE5G,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAalD;;;;;;;;;GASG;AACH,qBAAa,sBAAsB;IAEjC,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,WAAW;gBADX,UAAU,EAAE,WAAW,EACvB,WAAW,EAAE,GAAG,CAAC,YAAY,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC;IAGjE;;;;;;;;;OASG;IACG,YAAY,CACjB,OAAO,EAAE,iBAAiB,EAAE,EAC5B,OAAO,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,CAAC;IAgBhB;;;;;;;;OAQG;IACG,MAAM,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAoErD;;;;;;;OAOG;IACG,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAW/C;;;;OAIG;IACH,OAAO,CAAC,cAAc;IAStB;;;;;;;;OAQG;IACG,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,CAAC;IAoC9E;;;;;;;;;OASG;IACG,OAAO,CAAC,WAAW,EAAE,WAAW,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,CAAC;IAsE7F;;;;OAIG;YACW,wBAAwB;IAyDtC;;;;;;;OAOG;YACW,qBAAqB;IAuCnC;;;;;;;OAOG;YACW,WAAW;IAgCzB;;;;;;;OAOG;YACW,SAAS;IAmDvB;;OAEG;YACW,WAAW;IAgDzB;;OAEG;YACW,WAAW;CAsBzB"}