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
#![allow(warnings)]

use sled::IVec;
use std::{
    ops::RangeBounds,
    path::{Path, PathBuf},
};

pub mod escaping;
use escaping::*;

pub mod keys;
pub use keys::*;

pub mod list;

pub mod table;

mod segment;
pub use segment::*;

mod error;
pub use error::*;

mod lock_table;

pub struct Conn {
    db: sled::Db,
    items: sled::Tree,
    ttl: sled::Tree,
    locks: lock_table::Table,
}

impl Conn {
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, sled::Error> {
        let db = sled::open(path)?;
        let items = db.open_tree("items")?;
        let ttl = db.open_tree("ttl")?;
        let locks = lock_table::Table::default();
        Ok(Conn {
            db,
            items,
            ttl,
            locks,
        })
    }

    pub fn clear(&self) -> Result<(), sled::Error> {
        self.items.clear()?;
        self.ttl.clear()?;
        Ok(())
    }

    pub fn blob_get(&self, name: &[u8]) -> Result<Option<IVec>, sled::Error> {
        self.items.get(&keys::blob(name))
    }

    pub fn blob_set(&self, name: &[u8], val: IVec) -> Result<Option<IVec>, sled::Error> {
        self.items.insert(&keys::blob(name), val)
    }
}