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
use std::collections::HashMap;
use std::marker::PhantomData;

use lmdb;

use crate::config::{Config, DatabaseFlags};
use crate::error::Error;
use crate::txn::Txn;
use crate::types::{Integer, Key, Value};

/// A Store is used to keep data on disk using LMDB
pub struct Store {
    env: lmdb::Environment,
    buckets: HashMap<Option<String>, DatabaseFlags>,

    /// The `config` field stores the initial configuration values for the given store
    pub cfg: Config,
}

/// A Bucket represents a single database, or section of the Store
pub struct Bucket<'a, K: Key, V: 'a + Value<'a>>(
    lmdb::Database,
    PhantomData<K>,
    PhantomData<&'a V>,
);

impl<'a, K: Key, V: Value<'a>> Bucket<'a, K, V> {
    /// Provides access to the underlying LMDB dbi handle
    pub fn db(&self) -> lmdb::Database {
        self.0
    }
}

impl Store {
    pub(crate) fn wrap(env: lmdb::Environment, config: Config) -> Store {
        let mut store = Store {
            env,
            buckets: HashMap::new(),
            cfg: config,
        };

        let mut initialized_default = false;

        for (bucket_name, flag) in &store.cfg.buckets {
            let name = if bucket_name == "default" {
                initialized_default = true;
                None
            } else {
                Some(bucket_name.clone())
            };

            store.buckets.insert(name, flag.database_flags());
        }

        if !initialized_default {
            store.buckets.insert(None, lmdb::DatabaseFlags::empty());
        }

        store
    }

    /// Create a new store with the given configuration
    pub fn new(mut config: Config) -> Result<Store, Error> {
        let env = config.env()?;
        Ok(Self::wrap(env, config))
    }

    /// Get a named bucket
    pub fn bucket<'a, K: Key, V: Value<'a>>(
        &self,
        name: Option<&str>,
    ) -> Result<Bucket<'a, K, V>, Error> {
        let n = name.map(String::from);
        match self.buckets.get(&n) {
            Some(flags) => Ok(Bucket(
                self.env.create_db(name, *flags)?,
                PhantomData,
                PhantomData,
            )),
            None => Err(Error::InvalidBucket),
        }
    }

    /// Get a named bucket
    pub fn int_bucket<'a, V: Value<'a>>(
        &self,
        name: Option<&str>,
    ) -> Result<Bucket<'a, Integer, V>, Error> {
        let n = name.map(String::from);
        match self.buckets.get(&n) {
            Some(flags) => {
                let mut f = *flags;
                f.insert(lmdb::DatabaseFlags::INTEGER_KEY);
                Ok(Bucket(
                    self.env.create_db(name, f)?,
                    PhantomData,
                    PhantomData,
                ))
            }
            None => Err(Error::InvalidBucket),
        }
    }

    /// Create a readonly transaction and pass it to the provided function
    pub fn with_read_txn<'a, Res, F: FnOnce(&Txn<'a>) -> Result<Res, Error>>(
        &'a self,
        f: F,
    ) -> Result<Res, Error> {
        let txn = self.read_txn()?;
        f(&txn)
    }

    /// Create a writable transaction and pass it to the provided function
    pub fn with_write_txn<'a, Res, F: FnOnce(&mut Txn<'a>) -> Result<Res, Error>>(
        &'a mut self,
        f: F,
    ) -> Result<Res, Error> {
        let mut txn = self.write_txn()?;
        let res = f(&mut txn)?;
        txn.commit()?;
        Ok(res)
    }

    #[inline]
    /// Open a readonly transaction
    pub fn read_txn(&self) -> Result<Txn, Error> {
        let txn = self.env.begin_ro_txn()?;
        Ok(Txn::read_only(txn))
    }

    #[inline]
    /// Open a writable transaction
    pub fn write_txn(&self) -> Result<Txn, Error> {
        if self.cfg.readonly {
            return Err(Error::ReadOnly);
        }

        let txn = self.env.begin_rw_txn()?;
        Ok(Txn::read_write(txn))
    }

    #[inline]
    /// Sync data to disk
    pub fn sync(&self, force: bool) -> Result<(), Error> {
        self.env.sync(force)?;
        Ok(())
    }

    #[inline]
    /// Get database statistics
    pub fn stat(&self) -> Result<lmdb::Stat, Error> {
        Ok(self.env.stat()?)
    }
}