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
//! prost-sled: An integration layer between [prost] and [sled].
//!
//! prost-sled makes it easy to store protobufs in a sled database because
//! it abstracts away the boilerplate of encoding and decoding the protobufs.
//! If for any reason you wish to interact with the raw bytes instead or
//! [sled::Db] implements a method that [ProtoDb] doesn't yet, you can simply
//! use the `from` and `into` methods of the corresponding types as [From] and
//! [Into] are implemented as a go between, between the two types.

use prost::{bytes::BytesMut, Message};
use thiserror::Error;

/// Errors that can be returned by this library. It's really a simple
/// integration layer to encompass the possible errors returned by [sled] and
/// [prost].
#[derive(Debug, Error, PartialEq)]
pub enum Error {
    /// An error was returned by [sled].
    #[error(transparent)]
    SledError(#[from] sled::Error),
    /// A decoding error ([prost::DecodeError]) occurred in [prost].
    #[error(transparent)]
    ProstDecodeError(#[from] prost::DecodeError),
    /// An encoding error ([prost::EncodeError]) occurred in [prost].
    #[error(transparent)]
    ProstEncodeError(#[from] prost::EncodeError),
}

/// Result of a database action. That is, either some type `T` or an
/// [enum@Error].
pub type Result<T> = std::result::Result<T, Error>;

/// Wrapper around [sled::Db] that allows you to use types implementing
/// [prost::Message] instead of raw bytes.
#[derive(Clone)]
pub struct ProtoDb(sled::Db);

/// Convenience implementation to convert an existing [sled::Db] to a [ProtoDb].
impl From<ProtoDb> for sled::Db {
    fn from(db: ProtoDb) -> Self {
        db.0
    }
}

/// Escape hatch to get from a [ProtoDb] to a [sled::Db] in-case you need
/// something more low level.
impl From<sled::Db> for ProtoDb {
    fn from(db: sled::Db) -> Self {
        Self(db)
    }
}

/// Create a [ProtoDb] and return it.
pub fn open(path: &str) -> Result<ProtoDb> {
    let db = sled::open(path)?;
    Ok(db.into())
}

/// ProtoDb is a trait intended to be used to provide extension methods to
/// [sled::Db].
///
/// [ProtoDb] provides methods to make it easier store and retrieve protobuf
/// encoded data in a Sled database.
impl ProtoDb {
    pub fn contains_key<K>(&self, key: K) -> Result<bool>
    where
        K: AsRef<[u8]>,
    {
        let exists = self.0.contains_key(key)?;
        Ok(exists)
    }

    /// Get a value by its key.
    pub fn get<K, T>(&self, key: K) -> Result<Option<T>>
    where
        K: AsRef<[u8]>,
        T: Message + Default,
    {
        let maybe_data = self.0.get(key)?;
        if let Some(data) = maybe_data {
            let msg = T::decode(&*data)?;
            Ok(Some(msg))
        } else {
            Ok(None)
        }
    }

    /// Atomically retrieve then update a value.
    pub fn update_and_fetch<K, V, F, T>(
        &self,
        key: K,
        mut f: F,
    ) -> Result<Option<T>>
    where
        K: AsRef<[u8]>,
        F: FnMut(Option<T>) -> Option<V>,
        V: Into<T>,
        T: Message + Default,
    {
        // Escape the scoping of the closure so we can report the error.
        let mut err: Option<Error> = None;
        let maybe_data = self.0.update_and_fetch(key, |maybe_data| {
            let maybe_msg = if let Some(data) = maybe_data {
                match T::decode(data) {
                    Ok(value) => Some(value),
                    Err(e) => {
                        err = Some(e.into());
                        None
                    },
                }
            } else {
                None
            };
            if let Some(inserted) = f(maybe_msg) {
                let mut buf = BytesMut::default();
                let inserted_msg: T = inserted.into();
                if let Err(e) = inserted_msg.encode(&mut buf) {
                    err = Some(e.into());
                    None
                } else {
                    Some(buf.as_bytes())
                }
            } else {
                None
            }
        })?;

        if let Some(e) = err {
            return Err(e);
        }

        if let Some(data) = maybe_data {
            let msg = T::decode(&*data)?;
            Ok(Some(msg))
        } else {
            Ok(None)
        }
    }

    /// Insert a value into the database.
    pub fn insert<K, V, T>(&self, key: K, value: V) -> Result<Option<T>>
    where
        K: AsRef<[u8]>,
        V: Into<T>,
        T: Message + Default,
    {
        let mut buf = BytesMut::default();
        let msg: T = value.into();
        msg.encode(&mut buf)?;
        let maybe_inserted = self.0.insert(key, buf.as_bytes())?;
        if let Some(inserted) = maybe_inserted {
            let msg = T::decode(&*inserted)?;
            Ok(Some(msg))
        } else {
            Ok(None)
        }
    }
}

/// Convenience trait to convert to stdlib bytes. This is intended only for
/// internal use (hence not `pub`) and is only implemented for `BytesMut`.
trait BytesMutAsBytes {
    fn as_bytes(&self) -> Vec<u8>;
}

/// Conversion between `BytesMut` and `Vec<u8>`. This is just a convenience as
/// `sled` works with `Vec<u8>` and `prost` uses `BytesMut`.
impl BytesMutAsBytes for BytesMut {
    fn as_bytes(&self) -> Vec<u8> {
        let bytes: &[u8] = &self;
        bytes.to_owned()
    }
}

#[cfg(test)]
mod test {
    use once_cell::sync::OnceCell;
    use rand::{distributions::Alphanumeric, thread_rng, Rng};

    use super::ProtoDb;

    mod messages {
        include!(concat!(env!("OUT_DIR"), "/messages.rs"));
    }

    fn random_key() -> String {
        thread_rng()
            .sample_iter(&Alphanumeric)
            .take(5)
            .map(char::from)
            .collect()
    }

    fn proto_db() -> ProtoDb {
        static DB: OnceCell<sled::Db> = OnceCell::new();
        let db = DB.get_or_init(|| {
            sled::open("/tmp/prost-sled-test")
                .expect("failed to open sled DB for test")
        });
        ProtoDb(db.to_owned())
    }

    #[test]
    fn get_exists() {
        let db = proto_db();
        let thing = messages::Thing::default();
        let key = random_key();
        let _: Option<messages::Thing> =
            db.insert(&key, thing.clone()).unwrap();
        let retrieved = db.get(&key).unwrap().unwrap();
        assert_eq!(thing, retrieved);
    }

    #[test]
    fn get_no_exist() {
        let db = proto_db();
        let key = random_key();
        let retrieved: Option<messages::Thing> = db.get(&key).unwrap();
        assert!(retrieved.is_none());
    }

    #[test]
    fn update_and_fetch_existing() {
        let db = proto_db();
        let thing = messages::Thing::default();
        let key = random_key();
        let _: Option<messages::Thing> = db.insert(&key, thing).unwrap();
        let updated = db
            .update_and_fetch(&key, |maybe_msg| {
                let mut msg: messages::Thing = maybe_msg.unwrap();
                msg.i = "test".into();
                Some(msg)
            })
            .unwrap();
        let expected = messages::Thing {
            i: "test".into(),
            ..Default::default()
        };
        assert_eq!(updated, Some(expected));
    }

    #[test]
    fn update_and_fetch_no_existing() {
        let db = proto_db();
        let key = random_key();
        let updated = db
            .update_and_fetch(&key, |maybe_msg| {
                assert!(maybe_msg.is_none());
                let mut msg = messages::Thing::default();
                msg.i = "test".into();
                Some(msg)
            })
            .unwrap();
        let expected = messages::Thing {
            i: "test".into(),
            ..Default::default()
        };
        assert_eq!(updated, Some(expected));
    }

    #[test]
    fn insert_not_existing() {
        let db = proto_db();
        let key = random_key();
        let thing = messages::Thing::default();
        let previous: Option<messages::Thing> =
            db.insert(&key, thing.clone()).unwrap();
        assert!(previous.is_none());
        let inserted: messages::Thing = db.get(&key).unwrap().unwrap();
        assert_eq!(inserted, thing);
    }

    #[test]
    fn insert_existing() {
        let db = proto_db();
        let key = random_key();
        let first_thing = messages::Thing::default();
        let previous: Option<messages::Thing> =
            db.insert(&key, first_thing.clone()).unwrap();
        assert!(previous.is_none());
        let inserted: messages::Thing = db.get(&key).unwrap().unwrap();
        assert_eq!(inserted, first_thing);

        let second_thing = messages::Thing {
            i: random_key(),
            ..Default::default()
        };
        let second_inserted: messages::Thing =
            db.insert(&key, second_thing.clone()).unwrap().unwrap();
        assert_eq!(first_thing, second_inserted);
        let retieved: messages::Thing = db.get(&key).unwrap().unwrap();
        assert_eq!(retieved, second_thing);
    }
}