joydb 0.1.0

An in-memory embedded database with persistence and multiple adapters (JSON, CSV, etc). Acts like a minimalistic ORM with zero setup. Simple, lightweight, and perfect for prototypes, small apps, or experiments.
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
use crate::Model;
use crate::adapters::{Adapter, FromPath};
use crate::{
    JoydbError, Relation,
    state::{GetRelation, State},
};
use std::fmt::Debug;
use std::ops::Drop;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::time::Duration;

/// A struct that represents a database.
/// It's thread-safe and can be shared between multiple threads.
///
/// Essentially the database is a combination of a state that needs to be persisted and an
/// adapter that is used to persist/load the state.
///
/// # CRUD operations
///
/// | Operation | Methods                                                                                                  |
/// |-----------|----------------------------------------------------------------------------------------------------------|
/// | Create    | [`insert`](Self::insert), [`upsert`](Self::upsert)                                                       |
/// | Read      | [`get`](Self::get), [`get_all`](Self::get_all), [`get_all_by`](Self::get_all_by), [`count`](Self::count) |
/// | Update    | [`update`](Self::update), [`upsert`](Self::upsert)                                                       |
/// | Delete    | [`delete`](Self::delete), [`delete_all_by`](Self::delete_all_by)                                         |
///
#[derive(Debug)]
pub struct Joydb<S: State, A: Adapter> {
    inner: Arc<Mutex<InnerJoydb<S, A>>>,
}

// Implement `Clone` manually, otherwise the compile requires a `State: Clone` bound.
// But we deliberately don't want to be the inner state to implement `Clone`.
impl<S: State, A: Adapter> Clone for Joydb<S, A> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<S: State, A: Adapter + FromPath> Joydb<S, A> {
    /// Opens a database from the given file or directory.
    /// If the database does not exist, it will be created.
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, JoydbError> {
        let adapter = A::from_path(path);
        let config = JoydbConfig {
            mode: JoydbMode::Persistent {
                adapter,
                sync_policy: SyncPolicy::Instant,
            },
        };
        Self::open_with_config(config)
    }
}

impl<S: State, A: Adapter> Joydb<S, A> {
    /// Creates a new in-memory database.
    /// This database is not persisted to the file system.
    /// This is intended to be used mostly in tests.
    pub fn new_in_memory() -> Result<Self, JoydbError> {
        let config = JoydbConfig {
            mode: JoydbMode::InMemory,
        };
        Self::open_with_config(config)
    }

    pub fn open_with_config(config: JoydbConfig<A>) -> Result<Self, JoydbError> {
        let maybe_sync_policy = config.sync_policy();

        let inner: InnerJoydb<S, A> = InnerJoydb::open_with_config(config)?;
        let arc_inner = Arc::new(Mutex::new(inner));

        if let Some(SyncPolicy::Periodic(duration)) = maybe_sync_policy {
            let weak_inner_db = Arc::downgrade(&arc_inner);
            spawn_periodic_sync_thread(duration, weak_inner_db);
        }

        Ok(Self { inner: arc_inner })
    }

    /// Inserts a new record.
    /// Returns the inserted record.
    ///
    /// # Errors
    /// Returns an error if the record with the same id already exists.
    pub fn insert<M: Model>(&self, model: &M) -> Result<(), JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().insert(model)
    }

    /// Finds a record by its id.
    /// Returns `None` if the record is not found.
    pub fn get<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().get(id)
    }

    /// Returns all records that corresponds to the model type.
    /// The order of the records is not guaranteed and is a subject to change in the future versions.
    pub fn get_all<M: Model>(&self) -> Result<Vec<M>, JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().get_all()
    }

    /// Return all records that match the predicate.
    pub fn get_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
    where
        M: Model,
        S: GetRelation<M>,
        F: Fn(&M) -> bool,
    {
        self.inner.lock().unwrap().get_all_by(predicate)
    }

    /// Returns the number of records that corresponds to the model type.
    ///
    /// # Errors
    ///
    /// No real errors are expected to happen.
    /// However, `Result<usize, JoydbError>` is used to keep the API consistent with other methods
    /// and to make the user treat interaction with the database as fallible operations.
    pub fn count<M: Model>(&self) -> Result<usize, JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().count()
    }

    pub fn update<M: Model>(&self, new_record: &M) -> Result<(), JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().update(new_record)
    }

    /// Upserts a record.
    /// If the record with the same id already exists, it will be updated.
    /// Otherwise, it will be inserted.
    pub fn upsert<M: Model>(&self, record: &M) -> Result<(), JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().upsert(record)
    }

    /// Deletes a record by its id and returns the deleted record.
    /// If the record is not found, it returns `None`.
    pub fn delete<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>
    where
        S: GetRelation<M>,
    {
        self.inner.lock().unwrap().delete(id)
    }

    /// Deletes all records that match the predicate.
    /// Returns the deleted records.
    pub fn delete_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
    where
        M: Model,
        S: GetRelation<M>,
        F: Fn(&M) -> bool,
    {
        self.inner.lock().unwrap().delete_all_by(predicate)
    }

    /// Flushes the state to the file system.
    /// If there are any unsaved changes the corresponding file(s) will be rewritten from scratch.
    /// This method is also always called automatically on drop.
    pub fn flush(&self) -> Result<(), JoydbError> {
        self.inner.lock().unwrap().flush()
    }
}

#[derive(Debug)]
struct InnerJoydb<S: State, A: Adapter> {
    state: S,
    mode: JoydbMode<A>,
}

impl<S: State, A: Adapter> InnerJoydb<S, A> {
    fn open_with_config(config: JoydbConfig<A>) -> Result<Self, JoydbError> {
        let JoydbConfig { mode } = config;

        // Get the initial state
        let state = match &mode {
            JoydbMode::Persistent {
                adapter,
                sync_policy: _,
            } => adapter.load_state::<S>()?,
            JoydbMode::InMemory => S::default(),
        };

        Ok(Self { state, mode })
    }

    /// Write data to the file system if there are unsaved changes.
    fn flush(&mut self) -> Result<(), JoydbError> {
        if self.is_dirty() {
            self.write_state()?;
            self.state.reset_dirty();
        }
        Ok(())
    }

    fn write_state(&mut self) -> Result<(), JoydbError> {
        match &self.mode {
            JoydbMode::Persistent {
                adapter,
                sync_policy: _,
            } => adapter.write_state(&self.state),
            JoydbMode::InMemory => {
                // Do nothing
                Ok(())
            }
        }
    }

    fn is_dirty(&self) -> bool {
        self.state.is_dirty()
    }

    fn get_relation_mut<M: Model>(&mut self) -> &mut Relation<M>
    where
        S: GetRelation<M>,
    {
        let state = &mut self.state;
        <S as GetRelation<M>>::get_relation_mut(state)
    }

    fn get_relation<M: Model>(&self) -> &Relation<M>
    where
        S: GetRelation<M>,
    {
        let state = &self.state;
        <S as GetRelation<M>>::get_relation(state)
    }

    fn insert<M: Model>(&mut self, model: &M) -> Result<(), JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation_mut::<M>();
        relation.insert(model)?;
        self.after_change()?;
        Ok(())
    }

    fn get<M: Model>(&self, id: &M::Id) -> Result<Option<M>, JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation::<M>();
        relation.get(id)
    }

    fn get_all<M: Model>(&self) -> Result<Vec<M>, JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation::<M>();
        relation.get_all()
    }

    /// Return all records that match the predicate.
    pub(crate) fn get_all_by<M, F>(&self, predicate: F) -> Result<Vec<M>, JoydbError>
    where
        M: Model,
        S: GetRelation<M>,
        F: Fn(&M) -> bool,
    {
        let relation = self.get_relation::<M>();
        relation.get_all_by(predicate)
    }

    pub fn count<M: Model>(&self) -> Result<usize, JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation::<M>();
        relation.count()
    }

    fn update<M: Model>(&mut self, new_record: &M) -> Result<(), JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation_mut::<M>();
        relation.update(new_record)?;
        self.after_change()?;
        Ok(())
    }

    fn upsert<M: Model>(&mut self, record: &M) -> Result<(), JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation_mut::<M>();
        relation.upsert(record)?;
        self.after_change()?;
        Ok(())
    }

    fn delete<M: Model>(&mut self, id: &M::Id) -> Result<Option<M>, JoydbError>
    where
        S: GetRelation<M>,
    {
        let relation = self.get_relation_mut::<M>();
        let maybe_deleted_record = relation.delete(id)?;
        if maybe_deleted_record.is_some() {
            self.after_change()?;
        }
        Ok(maybe_deleted_record)
    }

    pub fn delete_all_by<M, F>(&mut self, predicate: F) -> Result<Vec<M>, JoydbError>
    where
        M: Model,
        S: GetRelation<M>,
        F: Fn(&M) -> bool,
    {
        let relation = self.get_relation_mut::<M>();
        let deleted_records = relation.delete_all_by(predicate)?;
        if !deleted_records.is_empty() {
            self.after_change()?;
        }
        Ok(deleted_records)
    }

    /// Hook which is called every time after database state has changed.
    fn after_change(&mut self) -> Result<(), JoydbError> {
        if self.mode.is_instant_sync_policy() {
            self.flush()?;
        }
        Ok(())
    }
}

impl<S: State, A: Adapter> Drop for InnerJoydb<S, A> {
    fn drop(&mut self) {
        if let Err(err) = self.flush() {
            eprintln!("Failed to flush the database: {}", err);
        }
    }
}

/// Specifies how and when the database should be synchronized with the file system.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SyncPolicy {
    /// The data are flushed to the file system instantly with every mutable operation.
    /// This is the default mode.
    /// This mode is the slowest, but the safest.
    Instant,

    /// The data are flushed to the file system periodically by a thread
    /// that runs in the background.
    Periodic(Duration),

    /// The data are flushed to the file system manually when the [Joydb::flush] method is called.
    /// The only exception is on drop, which always flushes the data.
    Manual,
}

#[derive(Debug)]
pub struct JoydbConfig<A: Adapter> {
    pub mode: JoydbMode<A>,
}

impl<A: Adapter> JoydbConfig<A> {
    fn sync_policy(&self) -> Option<SyncPolicy> {
        match &self.mode {
            JoydbMode::Persistent { sync_policy, .. } => Some(*sync_policy),
            JoydbMode::InMemory => None,
        }
    }
}

/// The mode of the database.
/// This is used to specify how the database should be opened.
#[derive(Debug)]
pub enum JoydbMode<A: Adapter> {
    /// The data are flushed to the file system.
    Persistent {
        /// The adapter used to read/write the data (e.g. JSON, CSV, etc.)
        adapter: A,

        /// Specifies when data must be flushed to the file system.
        sync_policy: SyncPolicy,
    },
    /// The data are never flushed to the file system. Even when [Joydb::flush] is explicitly
    /// called.
    /// With this mode, Joydb acts like in-memory-only database and this mode is mostly intended
    /// for unit tests.
    InMemory,
}

impl<A: Adapter> JoydbMode<A> {
    fn is_instant_sync_policy(&self) -> bool {
        match self {
            JoydbMode::Persistent { sync_policy, .. } => *sync_policy == SyncPolicy::Instant,
            JoydbMode::InMemory => false,
        }
    }
}

/// Spawns a thread that periodically flushes the database.
/// The thread owns a weak reference to the database, and runs until the database is dropped.
/// This is used only when the [SyncPolicy] is set to [`Periodic`](SyncPolicy::Periodic).
fn spawn_periodic_sync_thread<S: State, A: Adapter>(
    interval: Duration,
    weak_inner_db: std::sync::Weak<Mutex<InnerJoydb<S, A>>>,
) {
    std::thread::spawn(move || {
        loop {
            std::thread::sleep(interval);
            if let Some(inner) = weak_inner_db.upgrade() {
                inner
                    .lock()
                    .expect("Failed to lock the Joydb database from the background thread")
                    .flush()
                    .expect("Failed to flush the Joydb database from the background thread");
            } else {
                break;
            }
        }
    });
}