1
mod helpers;
2
use helpers::{create_and_drop, create_and_drop_with_config};
3
use persy::Config;
4
use persy::PersyError;
5
use persy::TxStrategy;
6

7
#[test]
8 1
fn test_insert_read_commit_record() {
9 1
    create_and_drop("irc", |persy| {
10 1
        let mut tx = persy.begin().unwrap();
11 1
        tx.create_segment("test").unwrap();
12 1
        let bytes = String::from("something").into_bytes();
13 1
        let id = tx.insert("test", &bytes).unwrap();
14 1
        let read_opt = tx.read("test", &id).unwrap();
15 1
        assert_eq!(read_opt, Some(bytes));
16 1
        let finalizer = tx.prepare().unwrap();
17 1
        finalizer.commit().unwrap();
18

19 1
        let bytes = String::from("something").into_bytes();
20 1
        let read_after = persy.read("test", &id).unwrap();
21 1
        assert_eq!(read_after, Some(bytes));
22 1
    });
23 1
}
24

25
#[test]
26 1
fn test_insert_update_record() {
27 1
    create_and_drop("iru", |persy| {
28 1
        let mut tx = persy.begin().unwrap();
29 1
        tx.create_segment("test").unwrap();
30 1
        let rec_data: String = "something".into();
31 1
        let bytes = rec_data.into_bytes();
32 1
        let id = tx.insert("test", &bytes).unwrap();
33 1
        let read_opt = tx.read("test", &id).unwrap();
34 1
        assert_eq!(read_opt, Some(bytes));
35 1
        let finalizer = tx.prepare().unwrap();
36 1
        finalizer.commit().unwrap();
37

38 1
        let rec_1: String = "newthing".into();
39 1
        let bytes_1 = rec_1.into_bytes();
40 1
        let mut tx1 = persy.begin().unwrap();
41 1
        tx1.update("test", &id, &bytes_1).unwrap();
42 1
        let read_after = tx1.read("test", &id).unwrap();
43 1
        assert_eq!(read_after, Some(bytes_1));
44 1
        let finalizer = tx1.prepare().unwrap();
45 1
        finalizer.commit().unwrap();
46 1
    });
47 1
}
48

49
#[test]
50 1
fn test_insert_delete_record() {
51 1
    create_and_drop("ird", |persy| {
52 1
        let mut tx = persy.begin().unwrap();
53 1
        tx.create_segment("test").unwrap();
54 1
        let rec_data: String = "something".into();
55 1
        let bytes = rec_data.into_bytes();
56 1
        let id = tx.insert("test", &bytes).unwrap();
57 1
        let read_opt = tx.read("test", &id).unwrap();
58 1
        assert_eq!(read_opt, Some(bytes));
59 1
        let finalizer = tx.prepare().unwrap();
60 1
        finalizer.commit().unwrap();
61

62 1
        let mut tx1 = persy.begin().unwrap();
63 1
        tx1.delete("test", &id).unwrap();
64 1
        let read_after = tx1.read("test", &id).unwrap();
65 1
        assert_eq!(read_after, None);
66 1
        let finalizer = tx1.prepare().unwrap();
67 1
        finalizer.commit().unwrap();
68 1
    });
69 1
}
70

71
#[test]
72 1
fn test_insert_scan() {
73 1
    create_and_drop("irs", |persy| {
74 1
        let mut tx = persy.begin().unwrap();
75 1
        tx.create_segment("test").unwrap();
76 1
        let rec_data: String = "something".into();
77 1
        let bytes = rec_data.into_bytes();
78 1
        tx.insert("test", &bytes).unwrap();
79 1
        let finalizer = tx.prepare().unwrap();
80 1
        finalizer.commit().unwrap();
81 1
        let iter = persy.scan("test").unwrap().into_iter();
82 1
        assert_eq!(iter.count(), 1);
83 1
        let (_, content) = persy.scan("test").unwrap().into_iter().next().unwrap();
84 1
        assert_eq!(bytes, content);
85 1
    });
86 1
}
87

88
#[test]
89 1
fn test_insert_update_same_tx() {
90 1
    create_and_drop("ius", |persy| {
91 1
        let mut tx = persy.begin().unwrap();
92 1
        tx.create_segment("test").unwrap();
93 1
        let bytes = String::from("something").into_bytes();
94 1
        let id = tx.insert("test", &bytes).unwrap();
95 1
        let read_opt = tx.read("test", &id).unwrap();
96 1
        assert_eq!(read_opt, Some(bytes));
97 1
        let bytes = String::from("data1").into_bytes();
98 1
        tx.update("test", &id, &bytes).unwrap();
99 1
        let read_opt = tx.read("test", &id).unwrap();
100 1
        assert_eq!(read_opt, Some(bytes));
101 1
        let finalizer = tx.prepare().unwrap();
102 1
        finalizer.commit().unwrap();
103

104 1
        let read_after = persy.read("test", &id).unwrap();
105 1
        let bytes = String::from("data1").into_bytes();
106 1
        assert_eq!(read_after, Some(bytes));
107 1
    });
108 1
}
109

110
#[test]
111 1
fn test_double_update_same_tx() {
112 1
    create_and_drop("iuus", |persy| {
113 1
        let mut tx = persy.begin().unwrap();
114 1
        tx.create_segment("test").unwrap();
115 1
        let bytes = String::from("something").into_bytes();
116 1
        let id = tx.insert("test", &bytes).unwrap();
117 1
        let finalizer = tx.prepare().unwrap();
118 1
        finalizer.commit().unwrap();
119 1
        let mut tx = persy.begin().unwrap();
120 1
        let bytes = String::from("first").into_bytes();
121 1
        tx.update("test", &id, &bytes).unwrap();
122 1
        let bytes = String::from("second").into_bytes();
123 1
        tx.update("test", &id, &bytes).unwrap();
124 1
        let read_opt = tx.read("test", &id).unwrap();
125 1
        assert_eq!(read_opt, Some(bytes));
126 1
        let finalizer = tx.prepare().unwrap();
127 1
        finalizer.commit().unwrap();
128

129 1
        let read_after = persy.read("test", &id).unwrap();
130 1
        let bytes = String::from("second").into_bytes();
131 1
        assert_eq!(read_after, Some(bytes));
132 1
    });
133 1
}
134

135
#[test]
136 1
fn test_insert_update_delete_same_tx() {
137 1
    create_and_drop("iuds", |persy| {
138 1
        let mut tx = persy.begin().unwrap();
139 1
        tx.create_segment("test").unwrap();
140 1
        let bytes = String::from("something").into_bytes();
141 1
        let id = tx.insert("test", &bytes).unwrap();
142 1
        let bytes = String::from("first").into_bytes();
143 1
        tx.update("test", &id, &bytes).unwrap();
144 1
        tx.delete("test", &id).unwrap();
145 1
        let read_opt = tx.read("test", &id).unwrap();
146 1
        assert_eq!(read_opt, None);
147 1
        let finalizer = tx.prepare().unwrap();
148 1
        finalizer.commit().unwrap();
149

150 1
        let read_after = persy.read("test", &id).unwrap();
151 1
        assert_eq!(read_after, None);
152 1
    });
153 1
}
154

155
#[test]
156 1
fn test_update_delete_same_tx() {
157 1
    create_and_drop("uds", |persy| {
158 1
        let mut tx = persy.begin().unwrap();
159 1
        tx.create_segment("test").unwrap();
160 1
        let bytes = String::from("something").into_bytes();
161 1
        let id = tx.insert("test", &bytes).unwrap();
162 1
        let finalizer = tx.prepare().unwrap();
163 1
        finalizer.commit().unwrap();
164

165 1
        let mut tx = persy.begin().unwrap();
166 1
        let bytes = String::from("first").into_bytes();
167 1
        tx.update("test", &id, &bytes).unwrap();
168 1
        tx.delete("test", &id).unwrap();
169 1
        let read_opt = tx.read("test", &id).unwrap();
170 1
        assert_eq!(read_opt, None);
171 1
        let finalizer = tx.prepare().unwrap();
172 1
        finalizer.commit().unwrap();
173

174 1
        let read_after = persy.read("test", &id).unwrap();
175 1
        assert_eq!(read_after, None);
176 1
    });
177 1
}
178

179
#[test]
180 1
fn test_insert_100_same_tx() {
181 1
    create_and_drop("i100", |persy| {
182 1
        let mut tx = persy.begin().unwrap();
183 1
        tx.create_segment("test").unwrap();
184 1
        let bytes = String::from("something").into_bytes();
185 1
        for _ in [0; 100].iter() {
186 1
            tx.insert("test", &bytes).unwrap();
187
        }
188 1
        let finalizer = tx.prepare().unwrap();
189 1
        finalizer.commit().unwrap();
190

191 1
        let mut count = 0;
192 1
        for _ in persy.scan("test").unwrap() {
193 1
            count += 1;
194 1
        }
195 1
        assert_eq!(count, 100);
196 1
    });
197 1
}
198

199
#[test]
200 1
fn test_update_100_same_tx() {
201 1
    create_and_drop("i100", |persy| {
202 1
        let mut tx = persy.begin().unwrap();
203 1
        tx.create_segment("test").unwrap();
204 1
        let bytes = String::from("something").into_bytes();
205 1
        let mut ids = Vec::new();
206 1
        for _ in [0; 100].iter() {
207 1
            ids.push(tx.insert("test", &bytes).unwrap());
208
        }
209 1
        let finalizer = tx.prepare().unwrap();
210 1
        finalizer.commit().unwrap();
211

212 1
        let mut tx = persy.begin().unwrap();
213 1
        for id in ids {
214 1
            tx.update("test", &id, &bytes).unwrap();
215 1
        }
216

217 1
        let finalizer = tx.prepare().unwrap();
218 1
        finalizer.commit().unwrap();
219

220 1
        let mut count = 0;
221 1
        for _ in persy.scan("test").unwrap() {
222 1
            count += 1;
223 1
        }
224 1
        assert_eq!(count, 100);
225 1
    });
226 1
}
227

228
#[test]
229 1
fn test_create_insert_scan_same_tx() {
230 1
    create_and_drop("cistx", |persy| {
231 1
        let mut tx = persy.begin().unwrap();
232 1
        tx.create_segment("test").unwrap();
233 1
        let bytes = String::from("something").into_bytes();
234 1
        for _ in [0; 50].iter() {
235 1
            tx.insert("test", &bytes).unwrap();
236
        }
237 1
        let mut count = 0;
238 1
        for (_, content) in tx.scan("test").unwrap() {
239 1
            assert_eq!(content, bytes);
240 1
            count += 1;
241 1
        }
242 1
        assert_eq!(count, 50);
243 1
        let finalizer = tx.prepare().unwrap();
244 1
        finalizer.commit().unwrap();
245 1
    });
246 1
}
247

248
#[test]
249 1
fn test_create_insert_scan_same_tx_access() {
250 1
    create_and_drop("cistx", |persy| {
251 1
        let mut tx = persy.begin().unwrap();
252 1
        tx.create_segment("test").unwrap();
253 1
        tx.create_segment("test1").unwrap();
254 1
        let bytes = String::from("something").into_bytes();
255 1
        for _ in [0; 50].iter() {
256 1
            tx.insert("test", &bytes).unwrap();
257
        }
258 1
        let mut count = 0;
259 1
        let mut iter = tx.scan("test").unwrap();
260 1
        iter.tx().insert("test1", "bytes".as_bytes()).unwrap();
261 1
        let new_bytes = String::from("other").into_bytes();
262 1
        while let Some((id, content, tx)) = iter.next_tx() {
263 1
            assert_eq!(content, bytes);
264 1
            count += 1;
265 1
            tx.update("test", &id, &new_bytes).unwrap();
266 1
        }
267 1
        assert_eq!(count, 50);
268 1
        let mut count = 0;
269 1
        for (_, content) in tx.scan("test").unwrap() {
270 1
            assert_eq!(content, new_bytes);
271 1
            count += 1;
272 1
        }
273 1
        assert_eq!(count, 50);
274 1
        let finalizer = tx.prepare().unwrap();
275 1
        finalizer.commit().unwrap();
276 1
    });
277 1
}
278

279
#[test]
280 1
fn test_insert_100_and_scan_same_tx() {
281 1
    create_and_drop("i100tx", |persy| {
282 1
        let mut tx = persy.begin().unwrap();
283 1
        tx.create_segment("test").unwrap();
284 1
        let bytes = String::from("something").into_bytes();
285 1
        for _ in [0; 50].iter() {
286 1
            tx.insert("test", &bytes).unwrap();
287
        }
288 1
        let finalizer = tx.prepare().unwrap();
289 1
        finalizer.commit().unwrap();
290

291 1
        let mut tx = persy.begin().unwrap();
292 1
        for _ in [0; 50].iter() {
293 1
            tx.insert("test", &bytes).unwrap();
294
        }
295 1
        let mut count = 0;
296 1
        for (_, content) in tx.scan("test").unwrap() {
297 1
            assert_eq!(content, bytes);
298 1
            count += 1;
299 1
        }
300 1
        assert_eq!(count, 100);
301 1
        let finalizer = tx.prepare().unwrap();
302 1
        finalizer.commit().unwrap();
303 1
    });
304 1
}
305

306
#[test]
307 1
fn test_insert_100_update_and_scan_same_tx() {
308 1
    create_and_drop("i100u_tx", |persy| {
309 1
        let mut tx = persy.begin().unwrap();
310 1
        tx.create_segment("test").unwrap();
311 1
        let bytes = String::from("something").into_bytes();
312 1
        let mut inserted = Vec::new();
313 1
        for _ in [0; 50].iter() {
314 1
            inserted.push(tx.insert("test", &bytes).unwrap());
315
        }
316 1
        let finalizer = tx.prepare().unwrap();
317 1
        finalizer.commit().unwrap();
318

319 1
        let bytes_other = String::from("other").into_bytes();
320 1
        let mut tx = persy.begin().unwrap();
321 1
        for _ in [0; 50].iter() {
322 1
            tx.insert("test", &bytes).unwrap();
323
        }
324 1
        for id in inserted.iter() {
325 1
            tx.update("test", id, &bytes_other).unwrap();
326
        }
327

328 1
        let mut count = 0;
329 1
        for (id, content) in tx.scan("test").unwrap() {
330 1
            if inserted.contains(&id) {
331 1
                assert_eq!(content, bytes_other);
332
            } else {
333 1
                assert_eq!(content, bytes);
334
            }
335 1
            count += 1;
336 1
        }
337 1
        assert_eq!(count, 100);
338 1
        let finalizer = tx.prepare().unwrap();
339 1
        finalizer.commit().unwrap();
340 1
    });
341 1
}
342

343
#[test]
344 1
fn test_scan_tx_segment_not_exist() {
345 1
    create_and_drop("sne_tx", |persy| {
346 1
        let mut tx = persy.begin().unwrap();
347 1
        let res = tx.scan("test");
348 1
        if let Err(x) = res {
349
            match x {
350 1
                PersyError::SegmentNotFound => {}
351 0
                _ => assert!(false),
352
            }
353 1
        } else {
354 0
            assert!(false);
355
        }
356 1
    });
357 1
}
358

359
#[test]
360 1
fn test_concurrency_version_on_write() {
361 1
    let mut config = Config::new();
362 1
    config.change_tx_strategy(TxStrategy::VersionOnWrite);
363 1
    create_and_drop_with_config("cvow", config, |persy| {
364 1
        let mut tx = persy.begin().unwrap();
365 1
        tx.create_segment("seg").unwrap();
366 1
        let bytes = String::from("aaa").into_bytes();
367 1
        let id = tx.insert("seg", &bytes).unwrap();
368 1
        let prep = tx.prepare().unwrap();
369 1
        prep.commit().unwrap();
370

371 1
        let bytes2 = String::from("bbb").into_bytes();
372 1
        let mut tx = persy.begin().unwrap();
373 1
        tx.update("seg", &id, &bytes2).unwrap();
374

375 1
        let bytes3 = String::from("cccc").into_bytes();
376 1
        let mut tx1 = persy.begin().unwrap();
377 1
        tx1.update("seg", &id, &bytes3).unwrap();
378

379 1
        let prep = tx1.prepare().unwrap();
380 1
        prep.commit().unwrap();
381 1
        let to_fail = tx.prepare();
382 1
        assert!(to_fail.is_err());
383 1
        let val = persy.read("seg", &id).unwrap().unwrap();
384 1
        assert_eq!(val, bytes3);
385 1
    });
386 1
}
387

388
#[test]
389 1
fn test_concurrency_last_win() {
390 1
    let mut config = Config::new();
391 1
    config.change_tx_strategy(TxStrategy::LastWin);
392 1
    create_and_drop_with_config("cvol", config, |persy| {
393 1
        let mut tx = persy.begin().unwrap();
394 1
        tx.create_segment("seg").unwrap();
395 1
        let bytes = String::from("aaa").into_bytes();
396 1
        let id = tx.insert("seg", &bytes).unwrap();
397 1
        let prep = tx.prepare().unwrap();
398 1
        prep.commit().unwrap();
399

400 1
        let bytes2 = String::from("bbb").into_bytes();
401 1
        let mut tx = persy.begin().unwrap();
402 1
        tx.update("seg", &id, &bytes2).unwrap();
403

404 1
        let bytes3 = String::from("cccc").into_bytes();
405 1
        let mut tx1 = persy.begin().unwrap();
406 1
        tx1.update("seg", &id, &bytes3).unwrap();
407

408 1
        let prep = tx1.prepare().unwrap();
409 1
        prep.commit().unwrap();
410

411 1
        let prep = tx.prepare().unwrap();
412 1
        prep.commit().unwrap();
413

414 1
        let val = persy.read("seg", &id).unwrap().unwrap();
415 1
        assert_eq!(val, bytes2);
416 1
    });
417 1
}
418

419
#[test]
420 1
fn test_concurrency_version_on_read() {
421 1
    let mut config = Config::new();
422 1
    config.change_tx_strategy(TxStrategy::VersionOnRead);
423 1
    create_and_drop_with_config("cvor", config, |persy| {
424 1
        let mut tx = persy.begin().unwrap();
425 1
        tx.create_segment("seg").unwrap();
426 1
        let bytes = String::from("aaa").into_bytes();
427 1
        let id = tx.insert("seg", &bytes).unwrap();
428 1
        let prep = tx.prepare().unwrap();
429 1
        prep.commit().unwrap();
430

431 1
        let bytes2 = String::from("bbb").into_bytes();
432 1
        let mut tx = persy.begin().unwrap();
433 1
        let _ = tx.read("seg", &id).unwrap();
434

435 1
        let bytes3 = String::from("cccc").into_bytes();
436

437 1
        let mut tx1 = persy.begin().unwrap();
438 1
        tx1.update("seg", &id, &bytes3).unwrap();
439 1
        let prep = tx1.prepare().unwrap();
440 1
        prep.commit().unwrap();
441

442 1
        tx.update("seg", &id, &bytes2).unwrap();
443 1
        let to_fail = tx.prepare();
444 1
        assert!(to_fail.is_err());
445

446 1
        let val = persy.read("seg", &id).unwrap().unwrap();
447 1
        assert_eq!(val, bytes3);
448 1
    });
449 1
}
450

451
#[test]
452 1
fn test_scan_segment_not_exist() {
453 1
    create_and_drop("sne", |persy| {
454 1
        let res = persy.scan("test");
455 1
        if let Err(x) = res {
456
            match x {
457 1
                PersyError::SegmentNotFound => {}
458 0
                _ => assert!(false),
459
            }
460 1
        } else {
461 0
            assert!(false);
462
        }
463 1
    });
464 1
}
465

466
#[test]
467 1
fn test_cleanup_after_delete() {
468 1
    create_and_drop("cleanup_after_delete", |persy| {
469 1
        let mut tx = persy.begin().unwrap();
470 1
        tx.create_segment("one").unwrap();
471 1
        tx.prepare().unwrap().commit().unwrap();
472

473 1
        let mut tx = persy.begin().unwrap();
474 1
        for _i in 0..5000 {
475 1
            tx.insert("one", "two".as_bytes()).unwrap();
476
        }
477 1
        tx.prepare().unwrap().commit().unwrap();
478 1
        let mut tx = persy.begin().unwrap();
479 1
        for (id, _rec) in persy.scan("one").unwrap() {
480 1
            tx.delete("one", &id).unwrap();
481 1
        }
482 1
        tx.prepare().unwrap().commit().unwrap();
483 1
        assert!(persy.scan("one").unwrap().next().is_none());
484 1
    });
485 1
}

Read our documentation on viewing source code .

Loading