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
//! A simple JSON file store written in Rust.
//! This is a port of the Node.js library
//! [json-file-store](https://github.com/flosse/json-file-store/).
//!
//! **WARNING**:
//! Don't use it if you want to persist a large amount of objects.
//! Use a real DB instead.
//!
//! # Example
//!
//! ```rust,no_run
//! use serde::{Serialize,Deserialize};
//! use jfs::Store;
//!
//! #[derive(Serialize,Deserialize)]
//! struct Foo {
//!     foo: String
//! }
//!
//! let db = Store::new("data").unwrap();
//! let f = Foo { foo: "bar".to_owned() };
//! let id = db.save(&f).unwrap();
//! let obj = db.get::<Foo>(&id).unwrap();
//! db.delete(&id).unwrap();
//! ```
//!
//! You can also store all data in one single JSON-File:
//!
//! ```rust,no_run
//! let mut cfg = jfs::Config::default();
//! cfg.single = true; // false is default
//! let db = jfs::Store::new_with_cfg("data",cfg);
//! ```
//!
//! If you like to pretty print the file content, set `pretty` to `true`
//! and choose a number of whitespaces for the indention:
//!
//! ```rust,no_run
//! let mut cfg = jfs::Config::default();
//! cfg.pretty = true;  // false is default
//! cfg.indent = 4;     // 2 is default
//! ```
//!
//! Creating a store instance that is living in the memory can be done like this:
//!
//! ```rust,no_run
//! let db = jfs::Store::new(jfs::IN_MEMORY).unwrap();
//! ```

use log::error;
use serde::{Deserialize, Serialize};
use std::{
    collections::BTreeMap,
    io::Result,
    path::{Path, PathBuf},
    sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard},
};

mod file_store;
mod json_store;
mod memory_store;

use file_store::FileStore;
use json_store::JsonStore;
use memory_store::MemoryStore;

pub use file_store::Config;

#[derive(Debug, Clone)]
pub struct Store(StoreType);

#[derive(Debug, Clone)]
enum StoreType {
    File(Arc<RwLock<FileStore>>, PathBuf),
    Memory(MemoryStore),
}

pub const IN_MEMORY: &str = "::memory::";

impl Store {
    /// Opens a `Store` against the specified path.
    ///
    /// See `new_with_cfg(..)` for more details
    ///
    /// # Arguments
    ///
    /// * `path` - path to the db directory of JSON documents
    pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
        Store::new_with_cfg(path, Config::default())
    }

    /// Opens a `Store` against the specified path with the given configuration
    ///
    /// If the `Store` already exists, it will be opened, otherwise this has the side-effect of creating the new `Store`
    ///  and the backing directories and files.
    ///
    /// # Arguments
    ///
    /// * `path` - path to the db directory of JSON documents, if configured for single db mode then `.json` will be used as the extension (replacing any existing extension)
    /// * `cfg` - configuration for the DB instance
    pub fn new_with_cfg<P: AsRef<Path>>(path: P, cfg: Config) -> Result<Self> {
        if path.as_ref() == Path::new(IN_MEMORY) {
            Ok(Self(StoreType::Memory(MemoryStore::default())))
        } else {
            let s = FileStore::new_with_cfg(path, cfg)?;
            let p = s.path().to_path_buf();
            Ok(Self(StoreType::File(Arc::new(RwLock::new(s)), p)))
        }
    }

    /// Returns the storage path for the backing JSON store.
    ///
    /// In single-file-mode this will be the JSON file location, otherwise it's
    ///  the directory in which all JSON objects are stored.
    pub fn path(&self) -> &Path {
        match &self.0 {
            StoreType::File(_, p) => p,
            StoreType::Memory(_) => Path::new(IN_MEMORY),
        }
    }

    pub fn save<T>(&self, obj: &T) -> Result<String>
    where
        for<'de> T: Serialize + Deserialize<'de>,
    {
        match &self.0 {
            StoreType::File(f, _) => f.write().unwrap_or_else(handle_write_err).save(obj),
            StoreType::Memory(m) => m.save(obj),
        }
    }

    pub fn save_with_id<T>(&self, obj: &T, id: &str) -> Result<String>
    where
        for<'de> T: Serialize + Deserialize<'de>,
    {
        match &self.0 {
            StoreType::File(f, _) => f
                .write()
                .unwrap_or_else(handle_write_err)
                .save_with_id(obj, id),
            StoreType::Memory(m) => m.save_with_id(obj, id),
        }
    }

    pub fn get<T>(&self, id: &str) -> Result<T>
    where
        for<'de> T: Deserialize<'de>,
    {
        match &self.0 {
            StoreType::File(f, _) => f.read().unwrap_or_else(handle_read_err).get(id),
            StoreType::Memory(m) => m.get(id),
        }
    }

    pub fn all<T>(&self) -> Result<BTreeMap<String, T>>
    where
        for<'de> T: Deserialize<'de>,
    {
        match &self.0 {
            StoreType::File(f, _) => f.read().unwrap_or_else(handle_read_err).all(),
            StoreType::Memory(m) => m.all(),
        }
    }

    pub fn delete(&self, id: &str) -> Result<()> {
        match &self.0 {
            StoreType::File(f, _) => f.write().unwrap_or_else(handle_write_err).delete(id),
            StoreType::Memory(m) => m.delete(id),
        }
    }
}

fn handle_write_err<T>(err: PoisonError<RwLockWriteGuard<T>>) -> RwLockWriteGuard<T> {
    error!("Write lock poisoned");
    err.into_inner()
}

fn handle_read_err<T>(err: PoisonError<RwLockReadGuard<T>>) -> RwLockReadGuard<T> {
    error!("Read lock poisoned");
    err.into_inner()
}

#[cfg(test)]
mod tests {

    use super::*;
    use serde_derive::{Deserialize, Serialize};
    use std::thread;
    use tempdir::TempDir;

    #[derive(Serialize, Deserialize)]
    struct Data {
        x: i32,
    }

    fn multi_threaded_write(store: Store) {
        let mut threads: Vec<thread::JoinHandle<()>> = vec![];
        for i in 0..20 {
            let db = store.clone();
            let x = Data { x: i };
            threads.push(thread::spawn(move || {
                db.save_with_id(&x, &i.to_string()).unwrap();
            }));
        }
        for t in threads {
            t.join().unwrap();
        }
        let all = store.all::<Data>().unwrap();
        assert_eq!(all.len(), 20);
        for (id, data) in all {
            assert_eq!(data.x.to_string(), id);
        }
    }

    #[test]
    fn multi_threaded_write_with_single_file() {
        let dir = TempDir::new("test").expect("Could not create temporary directory");
        let file = dir.path().join("db.json");
        let mut cfg = Config::default();
        cfg.single = true;
        let store = Store::new_with_cfg(file, cfg).unwrap();
        multi_threaded_write(store);
    }

    #[test]
    fn multi_threaded_write_with_dir() {
        #[derive(Serialize, Deserialize)]
        struct Data {
            x: i32,
        }
        let dir = TempDir::new("test").expect("Could not create temporary directory");
        let mut cfg = Config::default();
        cfg.single = false;
        let store = Store::new_with_cfg(dir.path(), cfg).unwrap();
        multi_threaded_write(store);
    }

    #[test]
    fn multi_threaded_write_in_memory() {
        #[derive(Serialize, Deserialize)]
        struct Data {
            x: i32,
        }
        let store = Store::new(IN_MEMORY).unwrap();
        multi_threaded_write(store);
    }
}