@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.
- package/README.md +328 -0
- package/dist/index.min.js +18 -0
- package/dist/index.min.js.map +7 -0
- package/dist/src/blocks/block-store.d.ts +12 -0
- package/dist/src/blocks/block-store.d.ts.map +1 -0
- package/dist/src/blocks/block-store.js +2 -0
- package/dist/src/blocks/block-store.js.map +1 -0
- package/dist/src/blocks/block-types.d.ts +3 -0
- package/dist/src/blocks/block-types.d.ts.map +1 -0
- package/dist/src/blocks/block-types.js +9 -0
- package/dist/src/blocks/block-types.js.map +1 -0
- package/dist/src/blocks/helpers.d.ts +4 -0
- package/dist/src/blocks/helpers.d.ts.map +1 -0
- package/dist/src/blocks/helpers.js +12 -0
- package/dist/src/blocks/helpers.js.map +1 -0
- package/dist/src/blocks/index.d.ts +5 -0
- package/dist/src/blocks/index.d.ts.map +1 -0
- package/dist/src/blocks/index.js +5 -0
- package/dist/src/blocks/index.js.map +1 -0
- package/dist/src/blocks/structs.d.ts +14 -0
- package/dist/src/blocks/structs.d.ts.map +1 -0
- package/dist/src/blocks/structs.js +2 -0
- package/dist/src/blocks/structs.js.map +1 -0
- package/dist/src/btree/btree.d.ts +135 -0
- package/dist/src/btree/btree.d.ts.map +1 -0
- package/dist/src/btree/btree.js +727 -0
- package/dist/src/btree/btree.js.map +1 -0
- package/dist/src/btree/independent-trunk.d.ts +17 -0
- package/dist/src/btree/independent-trunk.d.ts.map +1 -0
- package/dist/src/btree/independent-trunk.js +41 -0
- package/dist/src/btree/independent-trunk.js.map +1 -0
- package/dist/src/btree/index.d.ts +6 -0
- package/dist/src/btree/index.d.ts.map +1 -0
- package/dist/src/btree/index.js +6 -0
- package/dist/src/btree/index.js.map +1 -0
- package/dist/src/btree/key-range.d.ts +13 -0
- package/dist/src/btree/key-range.d.ts.map +1 -0
- package/dist/src/btree/key-range.js +20 -0
- package/dist/src/btree/key-range.js.map +1 -0
- package/dist/src/btree/keyset.d.ts +4 -0
- package/dist/src/btree/keyset.d.ts.map +1 -0
- package/dist/src/btree/keyset.js +4 -0
- package/dist/src/btree/keyset.js.map +1 -0
- package/dist/src/btree/nodes.d.ts +16 -0
- package/dist/src/btree/nodes.d.ts.map +1 -0
- package/dist/src/btree/nodes.js +9 -0
- package/dist/src/btree/nodes.js.map +1 -0
- package/dist/src/btree/path.d.ts +22 -0
- package/dist/src/btree/path.d.ts.map +1 -0
- package/dist/src/btree/path.js +39 -0
- package/dist/src/btree/path.js.map +1 -0
- package/dist/src/btree/tree-block.d.ts +7 -0
- package/dist/src/btree/tree-block.d.ts.map +1 -0
- package/dist/src/btree/tree-block.js +5 -0
- package/dist/src/btree/tree-block.js.map +1 -0
- package/dist/src/btree/trunk.d.ts +13 -0
- package/dist/src/btree/trunk.d.ts.map +1 -0
- package/dist/src/btree/trunk.js +2 -0
- package/dist/src/btree/trunk.js.map +1 -0
- package/dist/src/chain/chain-nodes.d.ts +18 -0
- package/dist/src/chain/chain-nodes.d.ts.map +1 -0
- package/dist/src/chain/chain-nodes.js +10 -0
- package/dist/src/chain/chain-nodes.js.map +1 -0
- package/dist/src/chain/chain.d.ts +75 -0
- package/dist/src/chain/chain.d.ts.map +1 -0
- package/dist/src/chain/chain.js +268 -0
- package/dist/src/chain/chain.js.map +1 -0
- package/dist/src/chain/index.d.ts +2 -0
- package/dist/src/chain/index.d.ts.map +1 -0
- package/dist/src/chain/index.js +2 -0
- package/dist/src/chain/index.js.map +1 -0
- package/dist/src/cluster/i-cluster.d.ts +5 -0
- package/dist/src/cluster/i-cluster.d.ts.map +1 -0
- package/dist/src/cluster/i-cluster.js +2 -0
- package/dist/src/cluster/i-cluster.js.map +1 -0
- package/dist/src/cluster/index.d.ts +3 -0
- package/dist/src/cluster/index.d.ts.map +1 -0
- package/dist/src/cluster/index.js +3 -0
- package/dist/src/cluster/index.js.map +1 -0
- package/dist/src/cluster/structs.d.ts +47 -0
- package/dist/src/cluster/structs.d.ts.map +1 -0
- package/dist/src/cluster/structs.js +2 -0
- package/dist/src/cluster/structs.js.map +1 -0
- package/dist/src/collection/action.d.ts +26 -0
- package/dist/src/collection/action.d.ts.map +1 -0
- package/dist/src/collection/action.js +2 -0
- package/dist/src/collection/action.js.map +1 -0
- package/dist/src/collection/collection.d.ts +48 -0
- package/dist/src/collection/collection.d.ts.map +1 -0
- package/dist/src/collection/collection.js +175 -0
- package/dist/src/collection/collection.js.map +1 -0
- package/dist/src/collection/index.d.ts +4 -0
- package/dist/src/collection/index.d.ts.map +1 -0
- package/dist/src/collection/index.js +4 -0
- package/dist/src/collection/index.js.map +1 -0
- package/dist/src/collection/struct.d.ts +16 -0
- package/dist/src/collection/struct.d.ts.map +1 -0
- package/dist/src/collection/struct.js +2 -0
- package/dist/src/collection/struct.js.map +1 -0
- package/dist/src/collections/diary/diary.d.ts +9 -0
- package/dist/src/collections/diary/diary.d.ts.map +1 -0
- package/dist/src/collections/diary/diary.js +37 -0
- package/dist/src/collections/diary/diary.js.map +1 -0
- package/dist/src/collections/diary/index.d.ts +3 -0
- package/dist/src/collections/diary/index.d.ts.map +1 -0
- package/dist/src/collections/diary/index.js +3 -0
- package/dist/src/collections/diary/index.js.map +1 -0
- package/dist/src/collections/diary/struct.d.ts +2 -0
- package/dist/src/collections/diary/struct.d.ts.map +1 -0
- package/dist/src/collections/diary/struct.js +3 -0
- package/dist/src/collections/diary/struct.js.map +1 -0
- package/dist/src/collections/index.d.ts +3 -0
- package/dist/src/collections/index.d.ts.map +1 -0
- package/dist/src/collections/index.js +3 -0
- package/dist/src/collections/index.js.map +1 -0
- package/dist/src/collections/tree/collection-trunk.d.ts +11 -0
- package/dist/src/collections/tree/collection-trunk.d.ts.map +1 -0
- package/dist/src/collections/tree/collection-trunk.js +22 -0
- package/dist/src/collections/tree/collection-trunk.js.map +1 -0
- package/dist/src/collections/tree/index.d.ts +3 -0
- package/dist/src/collections/tree/index.d.ts.map +1 -0
- package/dist/src/collections/tree/index.js +3 -0
- package/dist/src/collections/tree/index.js.map +1 -0
- package/dist/src/collections/tree/struct.d.ts +12 -0
- package/dist/src/collections/tree/struct.d.ts.map +1 -0
- package/dist/src/collections/tree/struct.js +4 -0
- package/dist/src/collections/tree/struct.js.map +1 -0
- package/dist/src/collections/tree/tree.d.ts +34 -0
- package/dist/src/collections/tree/tree.d.ts.map +1 -0
- package/dist/src/collections/tree/tree.js +100 -0
- package/dist/src/collections/tree/tree.js.map +1 -0
- package/dist/src/index.d.ts +18 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +18 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/log/index.d.ts +3 -0
- package/dist/src/log/index.d.ts.map +1 -0
- package/dist/src/log/index.js +3 -0
- package/dist/src/log/index.js.map +1 -0
- package/dist/src/log/log.d.ts +57 -0
- package/dist/src/log/log.d.ts.map +1 -0
- package/dist/src/log/log.js +131 -0
- package/dist/src/log/log.js.map +1 -0
- package/dist/src/log/struct.d.ts +36 -0
- package/dist/src/log/struct.d.ts.map +1 -0
- package/dist/src/log/struct.js +3 -0
- package/dist/src/log/struct.js.map +1 -0
- package/dist/src/network/i-key-network.d.ts +21 -0
- package/dist/src/network/i-key-network.d.ts.map +1 -0
- package/dist/src/network/i-key-network.js +2 -0
- package/dist/src/network/i-key-network.js.map +1 -0
- package/dist/src/network/i-peer-network.d.ts +8 -0
- package/dist/src/network/i-peer-network.d.ts.map +1 -0
- package/dist/src/network/i-peer-network.js +2 -0
- package/dist/src/network/i-peer-network.js.map +1 -0
- package/dist/src/network/i-repo.d.ts +17 -0
- package/dist/src/network/i-repo.d.ts.map +1 -0
- package/dist/src/network/i-repo.js +2 -0
- package/dist/src/network/i-repo.js.map +1 -0
- package/dist/src/network/index.d.ts +6 -0
- package/dist/src/network/index.d.ts.map +1 -0
- package/dist/src/network/index.js +6 -0
- package/dist/src/network/index.js.map +1 -0
- package/dist/src/network/repo-protocol.d.ts +19 -0
- package/dist/src/network/repo-protocol.d.ts.map +1 -0
- package/dist/src/network/repo-protocol.js +2 -0
- package/dist/src/network/repo-protocol.js.map +1 -0
- package/dist/src/network/struct.d.ts +115 -0
- package/dist/src/network/struct.d.ts.map +1 -0
- package/dist/src/network/struct.js +2 -0
- package/dist/src/network/struct.js.map +1 -0
- package/dist/src/transaction/actions-engine.d.ts +37 -0
- package/dist/src/transaction/actions-engine.d.ts.map +1 -0
- package/dist/src/transaction/actions-engine.js +67 -0
- package/dist/src/transaction/actions-engine.js.map +1 -0
- package/dist/src/transaction/context.d.ts +60 -0
- package/dist/src/transaction/context.d.ts.map +1 -0
- package/dist/src/transaction/context.js +91 -0
- package/dist/src/transaction/context.js.map +1 -0
- package/dist/src/transaction/coordinator.d.ts +118 -0
- package/dist/src/transaction/coordinator.d.ts.map +1 -0
- package/dist/src/transaction/coordinator.js +417 -0
- package/dist/src/transaction/coordinator.js.map +1 -0
- package/dist/src/transaction/index.d.ts +10 -0
- package/dist/src/transaction/index.d.ts.map +1 -0
- package/dist/src/transaction/index.js +7 -0
- package/dist/src/transaction/index.js.map +1 -0
- package/dist/src/transaction/session.d.ts +80 -0
- package/dist/src/transaction/session.d.ts.map +1 -0
- package/dist/src/transaction/session.js +161 -0
- package/dist/src/transaction/session.js.map +1 -0
- package/dist/src/transaction/transaction.d.ts +156 -0
- package/dist/src/transaction/transaction.d.ts.map +1 -0
- package/dist/src/transaction/transaction.js +31 -0
- package/dist/src/transaction/transaction.js.map +1 -0
- package/dist/src/transaction/validator.d.ts +46 -0
- package/dist/src/transaction/validator.d.ts.map +1 -0
- package/dist/src/transaction/validator.js +97 -0
- package/dist/src/transaction/validator.js.map +1 -0
- package/dist/src/transactor/index.d.ts +4 -0
- package/dist/src/transactor/index.d.ts.map +1 -0
- package/dist/src/transactor/index.js +4 -0
- package/dist/src/transactor/index.js.map +1 -0
- package/dist/src/transactor/network-transactor.d.ts +36 -0
- package/dist/src/transactor/network-transactor.d.ts.map +1 -0
- package/dist/src/transactor/network-transactor.js +297 -0
- package/dist/src/transactor/network-transactor.js.map +1 -0
- package/dist/src/transactor/transactor-source.d.ts +24 -0
- package/dist/src/transactor/transactor-source.d.ts.map +1 -0
- package/dist/src/transactor/transactor-source.js +62 -0
- package/dist/src/transactor/transactor-source.js.map +1 -0
- package/dist/src/transactor/transactor.d.ts +38 -0
- package/dist/src/transactor/transactor.d.ts.map +1 -0
- package/dist/src/transactor/transactor.js +2 -0
- package/dist/src/transactor/transactor.js.map +1 -0
- package/dist/src/transform/atomic.d.ts +8 -0
- package/dist/src/transform/atomic.d.ts.map +1 -0
- package/dist/src/transform/atomic.js +14 -0
- package/dist/src/transform/atomic.js.map +1 -0
- package/dist/src/transform/cache-source.d.ts +13 -0
- package/dist/src/transform/cache-source.d.ts.map +1 -0
- package/dist/src/transform/cache-source.js +52 -0
- package/dist/src/transform/cache-source.js.map +1 -0
- package/dist/src/transform/helpers.d.ts +25 -0
- package/dist/src/transform/helpers.d.ts.map +1 -0
- package/dist/src/transform/helpers.js +105 -0
- package/dist/src/transform/helpers.js.map +1 -0
- package/dist/src/transform/index.d.ts +6 -0
- package/dist/src/transform/index.d.ts.map +1 -0
- package/dist/src/transform/index.js +6 -0
- package/dist/src/transform/index.js.map +1 -0
- package/dist/src/transform/struct.d.ts +19 -0
- package/dist/src/transform/struct.d.ts.map +1 -0
- package/dist/src/transform/struct.js +2 -0
- package/dist/src/transform/struct.js.map +1 -0
- package/dist/src/transform/tracker.d.ts +22 -0
- package/dist/src/transform/tracker.d.ts.map +1 -0
- package/dist/src/transform/tracker.js +64 -0
- package/dist/src/transform/tracker.js.map +1 -0
- package/dist/src/utility/actor.d.ts +11 -0
- package/dist/src/utility/actor.d.ts.map +1 -0
- package/dist/src/utility/actor.js +39 -0
- package/dist/src/utility/actor.js.map +1 -0
- package/dist/src/utility/batch-coordinator.d.ts +56 -0
- package/dist/src/utility/batch-coordinator.d.ts.map +1 -0
- package/dist/src/utility/batch-coordinator.js +127 -0
- package/dist/src/utility/batch-coordinator.js.map +1 -0
- package/dist/src/utility/block-id-to-bytes.d.ts +3 -0
- package/dist/src/utility/block-id-to-bytes.d.ts.map +1 -0
- package/dist/src/utility/block-id-to-bytes.js +7 -0
- package/dist/src/utility/block-id-to-bytes.js.map +1 -0
- package/dist/src/utility/ensured.d.ts +3 -0
- package/dist/src/utility/ensured.d.ts.map +1 -0
- package/dist/src/utility/ensured.js +24 -0
- package/dist/src/utility/ensured.js.map +1 -0
- package/dist/src/utility/groupby.d.ts +8 -0
- package/dist/src/utility/groupby.d.ts.map +1 -0
- package/dist/src/utility/groupby.js +15 -0
- package/dist/src/utility/groupby.js.map +1 -0
- package/dist/src/utility/is-record-empty.d.ts +3 -0
- package/dist/src/utility/is-record-empty.d.ts.map +1 -0
- package/dist/src/utility/is-record-empty.js +7 -0
- package/dist/src/utility/is-record-empty.js.map +1 -0
- package/dist/src/utility/latches.d.ts +11 -0
- package/dist/src/utility/latches.d.ts.map +1 -0
- package/dist/src/utility/latches.js +36 -0
- package/dist/src/utility/latches.js.map +1 -0
- package/dist/src/utility/nameof.d.ts +3 -0
- package/dist/src/utility/nameof.d.ts.map +1 -0
- package/dist/src/utility/nameof.js +5 -0
- package/dist/src/utility/nameof.js.map +1 -0
- package/dist/src/utility/pending.d.ts +13 -0
- package/dist/src/utility/pending.d.ts.map +1 -0
- package/dist/src/utility/pending.js +37 -0
- package/dist/src/utility/pending.js.map +1 -0
- package/package.json +56 -0
- package/src/blocks/block-store.ts +13 -0
- package/src/blocks/block-types.ts +11 -0
- package/src/blocks/helpers.ts +13 -0
- package/src/blocks/index.ts +5 -0
- package/src/blocks/structs.ts +17 -0
- package/src/btree/btree.ts +804 -0
- package/src/btree/independent-trunk.ts +54 -0
- package/src/btree/index.ts +5 -0
- package/src/btree/key-range.ts +15 -0
- package/src/btree/keyset.ts +6 -0
- package/src/btree/nodes.ts +25 -0
- package/src/btree/path.ts +37 -0
- package/src/btree/tree-block.ts +11 -0
- package/src/btree/trunk.ts +14 -0
- package/src/chain/chain-nodes.ts +24 -0
- package/src/chain/chain.ts +324 -0
- package/src/chain/index.ts +2 -0
- package/src/cluster/i-cluster.ts +6 -0
- package/src/cluster/index.ts +2 -0
- package/src/cluster/structs.ts +46 -0
- package/src/collection/action.ts +31 -0
- package/src/collection/collection.ts +200 -0
- package/src/collection/index.ts +3 -0
- package/src/collection/struct.ts +20 -0
- package/src/collections/diary/diary.ts +43 -0
- package/src/collections/diary/index.ts +2 -0
- package/src/collections/diary/struct.ts +3 -0
- package/src/collections/index.ts +2 -0
- package/src/collections/tree/collection-trunk.ts +25 -0
- package/src/collections/tree/index.ts +2 -0
- package/src/collections/tree/readme.md +19 -0
- package/src/collections/tree/struct.ts +18 -0
- package/src/collections/tree/tree.ts +124 -0
- package/src/index.ts +17 -0
- package/src/log/index.ts +2 -0
- package/src/log/log.ts +155 -0
- package/src/log/struct.ts +40 -0
- package/src/network/i-key-network.ts +24 -0
- package/src/network/i-peer-network.ts +8 -0
- package/src/network/i-repo.ts +19 -0
- package/src/network/index.ts +5 -0
- package/src/network/repo-protocol.ts +12 -0
- package/src/network/struct.ts +137 -0
- package/src/transaction/actions-engine.ts +83 -0
- package/src/transaction/context.ts +103 -0
- package/src/transaction/coordinator.ts +583 -0
- package/src/transaction/index.ts +30 -0
- package/src/transaction/session.ts +182 -0
- package/src/transaction/transaction.ts +205 -0
- package/src/transaction/validator.ts +150 -0
- package/src/transactor/index.ts +4 -0
- package/src/transactor/network-transactor.ts +435 -0
- package/src/transactor/transactor-source.ts +65 -0
- package/src/transactor/transactor.ts +44 -0
- package/src/transform/atomic.ts +16 -0
- package/src/transform/cache-source.ts +57 -0
- package/src/transform/helpers.ts +117 -0
- package/src/transform/index.ts +5 -0
- package/src/transform/struct.ts +22 -0
- package/src/transform/tracker.ts +70 -0
- package/src/utility/actor.ts +62 -0
- package/src/utility/batch-coordinator.ts +174 -0
- package/src/utility/block-id-to-bytes.ts +8 -0
- package/src/utility/ensured.ts +32 -0
- package/src/utility/groupby.ts +18 -0
- package/src/utility/is-record-empty.ts +5 -0
- package/src/utility/latches.ts +42 -0
- package/src/utility/nameof.ts +7 -0
- package/src/utility/pending.ts +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
# Optimystic DB Core
|
|
2
|
+
|
|
3
|
+
A distributed database system designed for peer-to-peer networks, providing ACID transactions across content-addressed storage with strong consistency guarantees and conflict resolution.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Optimystic DB Core solves the fundamental challenge of maintaining **atomic transactions across content-addressed distributed storage**. Unlike traditional databases where related data resides on a single server, Optimystic distributes blocks across multiple independent peers.
|
|
8
|
+
|
|
9
|
+
### The Challenge
|
|
10
|
+
|
|
11
|
+
In distributed content-addressed systems:
|
|
12
|
+
- **Logically related blocks** are scattered across different network peers
|
|
13
|
+
- **Atomic transactions** must coordinate across multiple independent clusters
|
|
14
|
+
- **Consensus ordering** must be established without centralized coordination
|
|
15
|
+
- **Network failures** can occur independently at any peer or cluster
|
|
16
|
+
- **Conflict resolution** must handle concurrent modifications gracefully
|
|
17
|
+
|
|
18
|
+
### The Solution
|
|
19
|
+
|
|
20
|
+
Optimystic provides a **layered architecture** that maintains ACID properties while achieving horizontal scalability and fault tolerance:
|
|
21
|
+
|
|
22
|
+
1. **Randomly distributed block storage** with immutable, versioned units
|
|
23
|
+
2. **Block-based data structures** (B-trees, chains) built on block primitives
|
|
24
|
+
3. **Transaction logging** with integrity guarantees and checkpointing
|
|
25
|
+
4. **Collection abstractions** that combine data structures with distributed transactions
|
|
26
|
+
5. **Distributed coordination** through log-first transaction ordering
|
|
27
|
+
6. **Peer-to-peer networking** with cluster-aware consensus and failure recovery
|
|
28
|
+
|
|
29
|
+
A key innovation of this system is that the transaction payload is transmitted prior to the commital, as part of the "pend" phase. This minimizes the overhead of commitment since the propagation and validation of the transaction are complete prior to commit.
|
|
30
|
+
|
|
31
|
+
## Architecture
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
35
|
+
│ Collections Layer │
|
|
36
|
+
│ High-level abstractions: Tree, Diary, Custom Collections │
|
|
37
|
+
│ • Action-based mutations with conflict resolution │
|
|
38
|
+
│ • Local snapshots with explicit synchronization │
|
|
39
|
+
│ • Integration with specialized data structures │
|
|
40
|
+
└─────────────────┬───────────────────────────────────────────┘
|
|
41
|
+
│
|
|
42
|
+
┌─────────────────▼───────────────────────────────────────────┐
|
|
43
|
+
│ Transaction Layer │
|
|
44
|
+
│ Logs (transaction ordering) + Transactors │
|
|
45
|
+
│ • Log-first transaction ordering across collections │
|
|
46
|
+
│ • Distributed consensus with conflict detection │
|
|
47
|
+
│ • Missing/pending transaction resolution │
|
|
48
|
+
└─────────────────┬───────────────────────────────────────────┘
|
|
49
|
+
│
|
|
50
|
+
┌─────────────────▼───────────────────────────────────────────┐
|
|
51
|
+
│ Data Structures Layer │
|
|
52
|
+
│ Chains (ordered) + BTrees (indexed) │
|
|
53
|
+
│ • Chains: stacks, queues, logs with linked blocks │
|
|
54
|
+
│ • BTrees: sorted access with range queries │
|
|
55
|
+
│ • Both built on immutable block operations │
|
|
56
|
+
└─────────────────┬───────────────────────────────────────────┘
|
|
57
|
+
│
|
|
58
|
+
┌─────────────────▼───────────────────────────────────────────┐
|
|
59
|
+
│ Block Layer │
|
|
60
|
+
│ Immutable, versioned storage units │
|
|
61
|
+
│ • Content-addressed with base32 IDs │
|
|
62
|
+
│ • Atomic operations with transform tracking │
|
|
63
|
+
│ • Network-ready serialization and distribution │
|
|
64
|
+
└─────────────────────────────────────────────────────────────┘
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Core Components
|
|
68
|
+
|
|
69
|
+
### [Blocks](docs/blocks.md) - Foundation Storage Units
|
|
70
|
+
|
|
71
|
+
Immutable, versioned data storage units that form the foundation of the system:
|
|
72
|
+
|
|
73
|
+
- **Content-addressed**: Uniquely identified by base32-encoded block IDs
|
|
74
|
+
- **Atomic operations**: Precise modifications through block operations
|
|
75
|
+
- **Transform tracking**: Collect changes without immediate application
|
|
76
|
+
- **Network-ready**: Designed for distributed storage and retrieval
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
// Basic block with header
|
|
80
|
+
type IBlock = {
|
|
81
|
+
header: {
|
|
82
|
+
id: BlockId;
|
|
83
|
+
type: BlockType;
|
|
84
|
+
collectionId: BlockId;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Atomic operations on block properties
|
|
89
|
+
apply(store, block, ['items', 0, 1, ['newValue']]);
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### [Chains](docs/chains.md) - Ordered Data Structures
|
|
93
|
+
|
|
94
|
+
Linked block primitives that can function as stacks, queues, or logs:
|
|
95
|
+
|
|
96
|
+
- **Flexible access patterns**: LIFO (stack), FIFO (queue), or sequential (log)
|
|
97
|
+
- **Efficient traversal**: Forward and backward iteration with precise navigation
|
|
98
|
+
- **Block efficiency**: 32 entries per block optimized for network transfer
|
|
99
|
+
- **Distributed ready**: Lazy loading and efficient network operations
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// Stack operations (LIFO)
|
|
103
|
+
await chain.add('item1', 'item2');
|
|
104
|
+
const items = await chain.pop(1); // ['item2']
|
|
105
|
+
|
|
106
|
+
// Queue operations (FIFO)
|
|
107
|
+
await chain.add('first', 'second');
|
|
108
|
+
const items = await chain.dequeue(1); // ['first']
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### [BTrees](docs/btree.md) - Indexed Data Structures
|
|
112
|
+
|
|
113
|
+
B+tree implementation providing efficient sorted access to data:
|
|
114
|
+
|
|
115
|
+
- **Sorted access**: Keys maintained in order for efficient range queries
|
|
116
|
+
- **Path-based navigation**: Cursor-like traversal through the tree
|
|
117
|
+
- **Automatic rebalancing**: Splits, merges, and borrowing maintain optimal structure
|
|
118
|
+
- **Type-safe**: Generic implementation supporting custom key/entry types
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// Range queries with efficient iteration
|
|
122
|
+
const range = new KeyRange(startKey, endKey, true);
|
|
123
|
+
for await (const path of tree.range(range)) {
|
|
124
|
+
const entry = tree.at(path);
|
|
125
|
+
console.log(entry);
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### [Logs](docs/logs.md) - Transaction Logging
|
|
130
|
+
|
|
131
|
+
Transaction logging built on chains with integrity guarantees:
|
|
132
|
+
|
|
133
|
+
- **SHA256 integrity**: Block hashing ensures tamper detection
|
|
134
|
+
- **Action tracking**: Transaction IDs, revision numbers, affected blocks
|
|
135
|
+
- **Checkpoint management**: Efficient transaction state queries
|
|
136
|
+
- **Multi-collection coordination**: Cross-collection transaction support
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
// Record transaction with affected blocks
|
|
140
|
+
await log.addActions(
|
|
141
|
+
[action1, action2],
|
|
142
|
+
transactionId,
|
|
143
|
+
revisionNumber,
|
|
144
|
+
() => affectedBlockIds,
|
|
145
|
+
involvedCollectionIds
|
|
146
|
+
);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### [Collections](docs/collections.md) - High-Level Abstractions
|
|
150
|
+
|
|
151
|
+
Logical groupings that combine data structures with distributed transactions:
|
|
152
|
+
|
|
153
|
+
- **Local snapshots**: Immediate local changes with explicit synchronization
|
|
154
|
+
- **Action abstraction**: Replay actions from local, remote, or conflict resolution
|
|
155
|
+
- **Conflict resolution**: Customizable strategies for concurrent modifications
|
|
156
|
+
- **Data structure integration**: Tree collections (indexed), Diary collections (append-only)
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
// Local changes applied immediately to snapshot
|
|
160
|
+
await collection.act(action1, action2, action3);
|
|
161
|
+
|
|
162
|
+
// Explicit synchronization with distributed state
|
|
163
|
+
await collection.updateAndSync();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### [Transactors](docs/transactor.md) - Distributed Coordination
|
|
167
|
+
|
|
168
|
+
Abstract coordination layer managing distributed transactions:
|
|
169
|
+
|
|
170
|
+
- **Log-first ordering**: Transaction order established by log tail commits
|
|
171
|
+
- **Two-phase coordination**: Pend → commit pattern for atomic operations
|
|
172
|
+
- **Conflict detection**: Missing vs pending transaction differentiation
|
|
173
|
+
- **Network abstraction**: Clean interface hiding consensus complexity
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
// Two-phase distributed transaction
|
|
177
|
+
const pendResult = await transactor.pend(request);
|
|
178
|
+
if (pendResult.success) {
|
|
179
|
+
const commitResult = await transactor.commit(request);
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### [Network Layer](docs/network.md) - Peer-to-Peer Implementation
|
|
184
|
+
|
|
185
|
+
Concrete implementation for peer-to-peer networks:
|
|
186
|
+
|
|
187
|
+
- **Content-to-cluster mapping**: Deterministic routing via consistent hashing
|
|
188
|
+
- **Parallel cluster coordination**: Execute operations across multiple clusters
|
|
189
|
+
- **Failure-aware retry**: Handle peer failures and network partitions
|
|
190
|
+
- **Cluster-aware batching**: Group operations by responsible peers
|
|
191
|
+
|
|
192
|
+
## Key Features
|
|
193
|
+
|
|
194
|
+
### Distributed ACID Transactions
|
|
195
|
+
|
|
196
|
+
- **Atomicity**: All changes in a transaction succeed or fail together
|
|
197
|
+
- **Consistency**: Strong consistency across distributed participants
|
|
198
|
+
- **Isolation**: Conflict detection and resolution for concurrent operations
|
|
199
|
+
- **Durability**: Committed transactions survive network partitions
|
|
200
|
+
|
|
201
|
+
### Content-Addressed Storage
|
|
202
|
+
|
|
203
|
+
- **Deterministic distribution**: Blocks distributed based on content hash
|
|
204
|
+
- **Location independence**: Access blocks without knowing physical location
|
|
205
|
+
- **Efficient routing**: Consistent hashing maps blocks to responsible peers
|
|
206
|
+
- **Fault tolerance**: Multiple peers can coordinate same keyspace regions
|
|
207
|
+
|
|
208
|
+
### Conflict Resolution
|
|
209
|
+
|
|
210
|
+
- **Action filtering**: Customizable strategies for resolving conflicts
|
|
211
|
+
- **State replay**: Re-apply local actions on updated remote state
|
|
212
|
+
- **Missing transactions**: Automatic rebasing on newer committed changes
|
|
213
|
+
- **Pending awareness**: Handle concurrent modifications gracefully
|
|
214
|
+
|
|
215
|
+
### Horizontal Scalability
|
|
216
|
+
|
|
217
|
+
- **Peer-to-peer architecture**: No single points of failure
|
|
218
|
+
- **Parallel execution**: Operations on different blocks execute concurrently
|
|
219
|
+
- **Load distribution**: Coordination responsibility spread across network
|
|
220
|
+
- **Dynamic membership**: Peers can join and leave without disruption
|
|
221
|
+
|
|
222
|
+
## Usage Patterns
|
|
223
|
+
|
|
224
|
+
### Simple Append-Only Storage
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
// Create diary for event logging
|
|
228
|
+
const eventLog = await Diary.create<Event>(transactor, 'events');
|
|
229
|
+
|
|
230
|
+
// Add events
|
|
231
|
+
await eventLog.append({ type: 'user_login', userId: '123' });
|
|
232
|
+
await eventLog.append({ type: 'user_logout', userId: '123' });
|
|
233
|
+
|
|
234
|
+
// Read events in order
|
|
235
|
+
for await (const event of eventLog.select()) {
|
|
236
|
+
processEvent(event);
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Indexed Data with Range Queries
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
// Create tree collection for user data
|
|
244
|
+
const userTree = await Tree.createOrOpen<string, User>(
|
|
245
|
+
transactor,
|
|
246
|
+
'users',
|
|
247
|
+
user => user.id, // Key extractor
|
|
248
|
+
(a, b) => a.localeCompare(b) // Comparator
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
// Batch updates
|
|
252
|
+
await userTree.replace([
|
|
253
|
+
['user1', { id: 'user1', name: 'Alice' }],
|
|
254
|
+
['user2', { id: 'user2', name: 'Bob' }]
|
|
255
|
+
]);
|
|
256
|
+
|
|
257
|
+
// Range queries
|
|
258
|
+
for await (const path of userTree.range({ from: 'user1', to: 'user9' })) {
|
|
259
|
+
console.log(userTree.at(path));
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Custom Collections with Conflict Resolution
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
class CounterCollection {
|
|
267
|
+
static async create(transactor: ITransactor, id: CollectionId) {
|
|
268
|
+
const init: CollectionInitOptions<IncrementAction> = {
|
|
269
|
+
modules: {
|
|
270
|
+
"increment": async (action, store) => {
|
|
271
|
+
// Custom increment logic
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
filterConflict: (local, remote) => {
|
|
275
|
+
// Merge conflicting increments
|
|
276
|
+
const remoteSum = remote.reduce((sum, r) => sum + r.data.value, 0);
|
|
277
|
+
return { ...local, data: { ...local.data, value: local.data.value + remoteSum }};
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
return await Collection.createOrOpen(transactor, id, init);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
## Getting Started
|
|
287
|
+
|
|
288
|
+
### Installation
|
|
289
|
+
|
|
290
|
+
```bash
|
|
291
|
+
npm install @optimystic/db-core
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Basic Setup
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
import {
|
|
298
|
+
NetworkTransactor,
|
|
299
|
+
Tree,
|
|
300
|
+
Diary,
|
|
301
|
+
Collection
|
|
302
|
+
} from '@optimystic/db-core';
|
|
303
|
+
|
|
304
|
+
// Set up distributed transactor
|
|
305
|
+
const transactor = new NetworkTransactor({
|
|
306
|
+
keyNetwork, // Peer discovery implementation
|
|
307
|
+
peerNetwork, // Communication layer
|
|
308
|
+
getRepo // Repository factory
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
// Create collections
|
|
312
|
+
const userTree = await Tree.createOrOpen(transactor, 'users', /* ... */);
|
|
313
|
+
const eventLog = await Diary.create(transactor, 'events');
|
|
314
|
+
|
|
315
|
+
// Use collections
|
|
316
|
+
await userTree.replace([['user1', userData]]);
|
|
317
|
+
await eventLog.append(eventData);
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Documentation
|
|
321
|
+
|
|
322
|
+
- **[Blocks](docs/blocks.md)** - Immutable storage units and operations
|
|
323
|
+
- **[BTrees](docs/btree.md)** - Sorted data access and range queries
|
|
324
|
+
- **[Chains](docs/chains.md)** - Ordered data structures (stacks, queues, logs)
|
|
325
|
+
- **[Logs](docs/logs.md)** - Transaction logging with integrity guarantees
|
|
326
|
+
- **[Collections](docs/collections.md)** - High-level abstractions with conflict resolution
|
|
327
|
+
- **[Transactors](docs/transactor.md)** - Distributed transaction coordination
|
|
328
|
+
- **[Network](docs/network.md)** - Peer-to-peer implementation architecture
|