rollblock 0.4.1

A super-fast, block-oriented and rollbackable key-value store.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# Rollblock Architecture

## What is Rollblock?

Rollblock is a **block-oriented, rollbackable key-value store** optimized for high-throughput blockchain and event-sourcing workloads. It maintains the entire dataset in RAM for O(1) access while persisting an undo journal to disk for crash recovery and rollback.

```
┌─────────────────────────────────────────────────────────────────┐
│                         Your Application                        │
└─────────────────────────────┬───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│                       StoreFacade (API)                         │
│              set() · pop() · get() · rollback()                 │
└─────────────────────────────┬───────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│                    BlockOrchestrator (Runtime)                  │
│            Coordinates mutations, durability & rollback         │
└────────┬─────────────────────┬──────────────────────┬───────────┘
         │                     │                      │
         ▼                     ▼                      ▼
┌─────────────────┐  ┌──────────────────┐  ┌──────────────────────┐
│   StateEngine   │  │   BlockJournal   │  │    MetadataStore     │
│   (in-memory)   │  │  (undo on disk)  │  │   (LMDB offsets)     │
└────────┬────────┘  └──────────────────┘  └──────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│                         Shards (N×)                             │
│           RawTable hashmap per shard · O(1) operations          │
└─────────────────────────────────────────────────────────────────┘
```

---

## Core Concepts

### Data Model

| Type | Size | Description |
|------|------|-------------|
| `Key` | Compile-time width (≥ 8 bytes) | Fixed-size identifier, persisted in metadata. Client/server builds must agree on the width. |
| `Value` | 0–65,535 bytes | Arbitrary payload. Empty = deletion marker. |
| `BlockId` | u64 | Block height. Strictly increasing, caller-controlled. |
| `Operation` | Key + Value | A single mutation within a block. |

### Sharding Strategy

Keys are routed using a fast hash of their raw bytes:

```
shard_index = xxh3_64(key_bytes) % shard_count
```

No additional hashing—your key bytes directly control shard placement. Design keys for even distribution.

---

## Layers

Rollblock is organized in four layers, each depending only on the ones below:

```
┌──────────────────────────────────────────────┐
│  api/         Public interface & config      │  ← You call this
├──────────────────────────────────────────────┤
│  runtime/     Orchestration & metrics        │
├──────────────────────────────────────────────┤
│  state/       In-memory shards & execution   │
├──────────────────────────────────────────────┤
│  storage/     Journal, metadata, snapshots   │  ← Persisted to disk
└──────────────────────────────────────────────┘
```

### Layer 1: API (`src/api/`)

The public surface. Users interact exclusively with the `StoreFacade` trait:

```rust
pub trait StoreFacade {
    fn set(&self, block_height: BlockId, ops: Vec<Operation>) -> Result<()>;
    fn pop(&self, block_height: BlockId, key: Key) -> Result<Value>;
    fn get(&self, key: Key) -> Result<Value>;
    fn rollback(&self, target: BlockId) -> Result<()>;
    fn close(&self) -> Result<()>;
}
```

`pop` removes a single key under the same mutation lock that `set` uses and returns the previously committed value (or `Value::empty()` if the key was absent). This avoids coordinating an extra `get` + `set` round trip just to discover what was deleted.

**Implementation**: `SimpleStoreFacade` wraps everything and manages the embedded remote server.

### Layer 2: Runtime (`src/runtime/`)

**Orchestrator** — The central coordinator. Guarantees:

- **Atomicity**: All operations in a block succeed or fail together
- **Serialization**: A mutex prevents concurrent mutations
- **Auto-recovery**: Reverts in-memory state if persistence fails

**Metrics** — Real-time counters for ops/sec, latency, health status.

### Layer 3: State (`src/state/`)

**StateEngine** — Distributes operations across shards:

1. `prepare_journal()` — Plans mutations and captures undo info *before* applying
2. `commit()` — Applies the delta to shards, returns `BlockUndo`
3. `revert()` — Restores previous state using the undo

**StateShard** — Atomic storage unit (`RawTableShard`):

- Built on `hashbrown::RawTable` for minimal overhead
- `RwLock` for concurrent reads, exclusive writes
- O(1) get/set/delete

### Layer 4: Storage (`src/storage/`)

| Component | Purpose | Persistence |
|-----------|---------|-------------|
| **Journal** | Undo log for rollback | Chunked files + index |
| **Metadata** | Block heights, offsets | LMDB (ACID) |
| **Snapshot** | Full state backup | Single file per snapshot |

---

## Write Flow

When you call `store.set(block_height, operations)`:

```
┌──────────────────────────────────────────────────────────────────┐
│ 1. VALIDATE                                                      │
│    └─ block_height > current_block? ──no──▶ Error                │
└──────────────────────────────────────────────────────────────────┘
                              │ yes
┌──────────────────────────────────────────────────────────────────┐
│ 2. PREPARE                                                       │
│    └─ StateEngine.prepare_journal(ops)                           │
│       • Route each op to its shard                               │
│       • Capture current values for undo                          │
│       • Return BlockDelta (execution plan)                       │
└──────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────┐
│ 3. COMMIT                                                        │
│    └─ StateEngine.commit(delta)                                  │
│       • Apply operations to shards (parallel if configured)      │
│       • Return BlockUndo                                         │
└──────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────┐
│ 4. PERSIST                                                       │
│    ├─ Journal.append(block_height, undo, ops)                    │
│    ├─ Metadata.put_journal_offset(block_height, meta)            │
│    └─ Metadata.set_current_block(block_height)                   │
└──────────────────────────────────────────────────────────────────┘
                           SUCCESS

On persistence failure:
  └─ StateEngine.revert(block_height, undo)  ◀── automatic recovery
```

**Empty blocks**: If `ops` is empty, only `current_block` advances. This supports sparse block heights.

**Single-key deletes with return values**: `pop(block_height, key)` synthesizes a delete operation, runs through the same pipeline, and inspects the `BlockUndo` that `StateEngine` emits to return the previous value without issuing a separate `get`.

---

## Rollback Flow

When you call `store.rollback(target_block)`:

```
current = 105, target = 100

┌─────────────────────────────────────────────────────────────────┐
│ 1. Find journal entries between target+1 and current            │
│    └─ Blocks 101, 102, 103, 104, 105                            │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ 2. Iterate backwards, apply each BlockUndo                      │
│    └─ 105 → 104 → 103 → 102 → 101                               │
│       For each: StateEngine.revert(block, undo)                 │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│ 3. Truncate journal after target                                │
│ 4. Update metadata: current_block = 100                         │
│ 5. Prune snapshots newer than target                            │
└─────────────────────────────────────────────────────────────────┘
                     State restored to block 100
```

---

## Read Flow

Reads acquire the shared side of the global `reader_gate` `RwLock`, so multiple lookups can proceed concurrently while still yielding whenever a writer holds the exclusive lock. Once inside the gate, each shard only needs a read guard, which keeps lookups on different shards non-blocking.

```
store.get(key)
StateEngine.lookup(key)
    ├─ shard_index = xxh3_64(key_bytes) % shard_count
    └─ shard.get(key)  ◀── RwLock::read(), O(1) lookup
     Value or empty
```

---

## Durability Modes

| Mode | Sync Behavior | Throughput | Risk |
|------|---------------|------------|------|
| `Synchronous` | fsync every block | Lowest | Safest |
| `SynchronousRelaxed` | fsync every N blocks | Medium | N blocks at risk |
| `Async` | Background thread, fsync every block | High | In-flight blocks at risk |
| `AsyncRelaxed` | Background thread, fsync every N blocks | Highest | N + queue at risk |

**Default**: `Async { max_pending_blocks: 1024 }`

The `applied_block` may be ahead of `durable_block`. On crash, recovery replays from the last durable state.

---

## Storage Formats

### Journal Entry

Each journal entry stores operations and undo data for one block:

```
┌──────────────────────── HEADER (42 bytes) ────────────────────────┐
│ magic(4) │ version(2) │ flags(2) │ key_bytes(2) │ block_height(8) │
│ entry_count(4) │ compressed_len(8) │ uncompressed_len(8)          │
│ checksum(4)                                                       │
└───────────────────────────────────────────────────────────────────┘
┌──────────────────────── PAYLOAD (zstd compressed) ────────────────┐
│ bincode-encoded BlockUndo                                         │
│ + operation stream:                                               │
│   [key(key_bytes)][len(u16)][value bytes...] × entry_count        │
└───────────────────────────────────────────────────────────────────┘
```

- **Chunked files**: Journal rotates when a chunk exceeds `max_chunk_size_bytes` (default 128 MiB)
- **Index file**: Maps block heights to chunk + offset for O(1) lookup
- **Checksum**: Blake3 hash of compressed payload

### Snapshot

Full state export for fast recovery:

```
┌──────────────────────── HEADER (32 bytes) ────────────────────────┐
│ magic(4) "MHIS" │ version(2) │ key_bytes(2) │ block_height(8)     │
│ shard_count(8) │ checksum(8)                                      │
└───────────────────────────────────────────────────────────────────┘
┌──────────────────────── SHARD DATA (per shard) ───────────────────┐
│ entry_count(u64)                                                  │
│ repeat entry_count times:                                         │
│   [key(key_bytes)][len(u16)][value bytes...]                      │
└───────────────────────────────────────────────────────────────────┘
```

---

## LMDB Metadata

The `MetadataStore` tracks persistent state using LMDB:

| Key | Value | Purpose |
|-----|-------|---------|
| `current_block` | BlockId | Latest committed block height |
| `journal_offsets/{block}` | JournalMeta | Chunk ID + offset for each block |
| `gc_watermark` | GcWatermark | Pending prune state for crash recovery |
| `snapshot_watermark` | BlockId | Latest snapshot block for pruning |

LMDB provides ACID guarantees, but metadata is recorded *after* the journal append succeeds. If the process crashes after writing the journal entry but before LMDB catches up, recovery replays the durable journal blocks and re-derives the missing metadata.

---

## Pruning

Old journal chunks are pruned to bound disk usage:

```
min_rollback_window = 100
current_block = 1000

┌──────────────────────────────────────────────────────────────────┐
│ Blocks 0–900: eligible for pruning                               │
│ Blocks 901–1000: protected (within rollback window)              │
└──────────────────────────────────────────────────────────────────┘
```

Pruning runs in the background at `prune_interval`. The pruner:

1. Scans sealed chunks (not the active write chunk)
2. Deletes chunks whose highest block ≤ `current_block - min_rollback_window`
3. Updates the journal index atomically

---

## Concurrency Model

```
┌─────────────────────────────────────────────────────────────────┐
│                        update_mutex                             │
│  • Serializes all mutations (set, rollback)                     │
│  • Held for the entire write path                               │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│                        reader_gate (RwLock)                     │
│  • Writes acquire exclusive lock                                │
│  • Reads acquire shared lock                                    │
│  • Ensures consistent view during mutations                     │
└─────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────┐
│              Shard RwLocks (one per shard)                      │
│  • Fine-grained: reads to different shards don't block          │
│  • Writes acquire exclusive per-shard lock                      │
└─────────────────────────────────────────────────────────────────┘
```

**Thread-safety**: All components are `Send + Sync`.

---

## Parallelism

When `thread_count > 1`, Rollblock uses Rayon for:

- Preparing deltas across shards
- Committing operations to shards
- Reverting undo entries

The speedup is most noticeable with many shards and large batches.

---

## Traits & Extensibility

Every major component is a trait. Swap implementations without touching the rest:

| Trait | Default Implementation | Role |
|-------|------------------------|------|
| `StoreFacade` | `SimpleStoreFacade` | Public API |
| `BlockOrchestrator` | `DefaultBlockOrchestrator` | Coordination |
| `StateEngine` | `ShardedStateEngine` | Shard management |
| `StateShard` | `RawTableShard` | In-memory storage |
| `BlockJournal` | `FileBlockJournal` | Undo persistence |
| `MetadataStore` | `LmdbMetadataStore` | Block tracking |
| `Snapshotter` | `MmapSnapshotter` | State backup |

---

## Directory Layout

```
data_dir/
├── metadata/           # LMDB environment
│   ├── data.mdb
│   └── lock.mdb
├── journal/            # Undo log
│   ├── journal.00000001.bin
│   ├── journal.00000002.bin
│   └── journal.idx
├── snapshots/          # Full state exports
│   └── snapshot_00000000000003e8.bin
└── rollblock.lock      # Single-writer lock
```

---

## Guarantees

| Category | Guarantee |
|----------|-----------|
| **Atomicity** | All ops in a block succeed or fail together |
| **Durability** | Committed blocks survive crash (per mode) |
| **Integrity** | Blake3 checksums on journal and snapshots |
| **Isolation** | Reads see consistent state; writes are serialized |
| **Performance** | O(1) get/set/delete via hashmap shards |

---

## Quick Reference

```rust
use rollblock::types::StoreKey as Key;

// Create store
let config = StoreConfig::new("./data", 4, 10_000, 1, false)?;
let store = SimpleStoreFacade::new(config)?;

// Write a block
store.set(
    1,
    vec![Operation {
        key: [1u8; Key::BYTES].into(),
        value: 42.into(),
    }],
)?;

// Read
let value = store.get([1u8; Key::BYTES].into())?;

// Rollback
store.rollback(0)?;

// Graceful shutdown
store.close()?;
```