-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontacts.rs
More file actions
190 lines (164 loc) · 5.02 KB
/
contacts.rs
File metadata and controls
190 lines (164 loc) · 5.02 KB
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
//! Persistent contacts book — file-backed with WAL.
//!
//! Demonstrates CRUD with crash-safe persistence.
//! Data survives process restarts — run it twice to see.
//!
//! Run with: `cargo run --example contacts`
use etchdb::{Op, Overlay, Replayable, Store, Transactable, WalBackend, apply_overlay_btree};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::PathBuf;
// Schema
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct ContactBook {
people: BTreeMap<String, Person>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Person {
name: String,
email: String,
phone: String,
}
// Collection tag — identifies which BTreeMap an op belongs to.
const PEOPLE: u8 = 0;
// Replayable — tells etch how to reconstruct state from WAL ops on startup.
// One line per collection: route ops to the right BTreeMap.
impl Replayable for ContactBook {
fn apply_with_format(
&mut self,
ops: &[Op],
_format: etchdb::ReplayFormat,
) -> etchdb::Result<()> {
for op in ops {
etchdb::apply_op(&mut self.people, op)?;
}
Ok(())
}
}
// Transactable — defines the write API for your state.
// Each method updates the in-memory overlay AND emits an Op for persistence.
struct ContactTx<'a> {
committed: &'a ContactBook,
people: Overlay<String, Person>,
ops: Vec<Op>,
}
struct ContactOverlay {
people: Overlay<String, Person>,
}
impl<'a> ContactTx<'a> {
fn insert(&mut self, id: &str, person: Person) {
self.ops.push(Op::Put {
collection: PEOPLE,
key: id.as_bytes().to_vec(),
value: postcard::to_allocvec(&person).expect("serialize"),
});
self.people.put(id.to_string(), person);
}
fn update(&mut self, id: &str, f: impl FnOnce(&mut Person)) {
if let Some(p) = self.people.get(&self.committed.people, &id.to_string()) {
let mut p = p.clone();
f(&mut p);
self.ops.push(Op::Put {
collection: PEOPLE,
key: id.as_bytes().to_vec(),
value: postcard::to_allocvec(&p).expect("serialize"),
});
self.people.put(id.to_string(), p);
}
}
fn delete(&mut self, id: &str) {
self.ops.push(Op::Delete {
collection: PEOPLE,
key: id.as_bytes().to_vec(),
});
self.people.delete(&id.to_string(), &self.committed.people);
}
}
impl Transactable for ContactBook {
type Tx<'a> = ContactTx<'a>;
type Overlay = ContactOverlay;
fn begin_tx(&self) -> ContactTx<'_> {
ContactTx {
committed: self,
people: Overlay::new(),
ops: Vec::new(),
}
}
fn finish_tx(tx: ContactTx<'_>) -> (Vec<Op>, ContactOverlay) {
(tx.ops, ContactOverlay { people: tx.people })
}
fn apply_overlay(&mut self, overlay: ContactOverlay) {
apply_overlay_btree(&mut self.people, overlay.people);
}
}
fn main() -> etchdb::Result<()> {
let dir = PathBuf::from("data/contacts");
std::fs::create_dir_all(&dir)?;
let store = Store::<ContactBook, WalBackend<ContactBook>>::open_wal(dir.clone())?;
println!(
"Opened contact book ({} existing contacts)\n",
store.read().people.len()
);
// Create
store.write(|tx| {
tx.insert(
"alice",
Person {
name: "Alice Park".into(),
email: "alice@example.com".into(),
phone: "555-0101".into(),
},
);
tx.insert(
"bob",
Person {
name: "Bob Chen".into(),
email: "bob@example.com".into(),
phone: "555-0102".into(),
},
);
tx.insert(
"carol",
Person {
name: "Carol Diaz".into(),
email: "carol@example.com".into(),
phone: "555-0103".into(),
},
);
Ok(())
})?;
println!("After adding 3 contacts:");
print_contacts(&store);
// Read
let state = store.read();
let alice = &state.people["alice"];
println!("Lookup alice: {} <{}>\n", alice.name, alice.email);
drop(state);
// Update
store.write(|tx| {
tx.update("bob", |p| p.phone = "555-9999".into());
Ok(())
})?;
println!("After updating Bob's phone:");
print_contacts(&store);
// Delete
store.write(|tx| {
tx.delete("carol");
Ok(())
})?;
println!("After removing Carol:");
print_contacts(&store);
// Persistence — drop and reopen from disk.
drop(store);
let store = Store::<ContactBook, WalBackend<ContactBook>>::open_wal(dir)?;
println!("Reopened from disk:");
print_contacts(&store);
Ok(())
}
fn print_contacts(store: &Store<ContactBook, WalBackend<ContactBook>>) {
let state = store.read();
for (id, p) in state.people.iter() {
println!(" {id}: {} <{}> {}", p.name, p.email, p.phone);
}
println!();
}