acid_state/
wrapper_structs.rs1use std::fmt;
2use std::fs;
3use std::io;
4use std::io::prelude::*;
5use std::path::PathBuf;
6use std::ops::{Deref, DerefMut};
7use std::sync::{Arc, Mutex, MutexGuard};
8
9use rustc_serialize::{Encodable, Decodable};
10
11use bincode::SizeLimit;
12use bincode::rustc_serialize::{encode, decode, DecodingResult};
13
14fn to_binary<T: Encodable>(s: &T) -> Vec<u8> {
15 encode(s, SizeLimit::Infinite).unwrap()
16}
17
18fn from_binary<T: Decodable>(encoded: Vec<u8>) -> DecodingResult<T> {
19 decode(&encoded[..])
20}
21
22#[derive(Debug, Clone)]
23pub struct Persistent<T: Encodable + Decodable> {
24 pub inner: Arc<Mutex<T>>,
25 pub name: String,
26}
27
28impl<T: Encodable + Decodable> Persistent<T> {
29 pub fn handle<'a>(&'a self) -> Txn<'a, T> {
30 let mut inner = self.inner.lock().unwrap();
31 if let Some(read) = self.read() {
32 *inner = read;
33 }
34 Txn {
35 inner: inner,
36 name: self.name.clone(),
37 }
38 }
39
40 fn read(&self) -> Option<T> {
41 if let Ok(mut f) = fs::File::open(self.path()) {
42 let mut s = vec![];
43 f.read_to_end(&mut s).unwrap();
44 from_binary(s).ok().or_else(|| None)
45 } else {
46 None
47 }
48 }
49
50 fn path(&self) -> PathBuf {
51 self.name.clone().into()
52 }
53
54 fn clear(&self) -> io::Result<()> {
55 fs::remove_file(self.path())
56 }
57}
58
59
60impl<'a, T: 'a + Encodable + Decodable> Txn<'a, T> {
61 fn path(&self) -> PathBuf {
62 self.name.clone().into()
63 }
64
65 fn write(&self) -> io::Result<()> {
66 let bytes = to_binary(&*self.inner);
67 from_binary::<T>(bytes.clone()).unwrap();
68 let mut f = fs::File::create(self.path()).unwrap();
69 let res = f.write_all(&*bytes);
70 f.sync_all();
71 res
72 }
73}
74
75pub struct Txn<'a, T: 'a + Encodable + Decodable> {
76 pub inner: MutexGuard<'a, T>,
77 pub name: String,
78}
79
80impl<'a, T: Encodable + Decodable + fmt::Debug> fmt::Debug for Txn<'a, T> {
81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82 write!(f, "Txn {{ inner: {:?} }}", *self.inner)
83 }
84}
85
86impl<'a, T: Encodable + Decodable> Drop for Txn<'a, T> {
87 fn drop(&mut self) {
88 self.write();
89 }
90}
91
92impl<'a, T: 'a + Encodable + Decodable> Deref for Txn<'a, T> {
93 type Target = MutexGuard<'a, T>;
94
95 fn deref(&self) -> &MutexGuard<'a, T> {
96 &self.inner
97 }
98}
99
100impl<'a, T: 'a + Encodable + Decodable> DerefMut for Txn<'a, T> {
101 fn deref_mut(&mut self) -> &mut MutexGuard<'a, T> {
102 &mut self.inner
103 }
104}