2022-08-06 20:16:08 -04:00
|
|
|
// Copyright 2022 Jeremy Wall (Jeremy@marzhilsltudios.com)
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2022-08-24 13:10:59 -04:00
|
|
|
//! Module implementing a store interface using LevelDB for a MerkleDag.
|
|
|
|
//! Requires the `rusty-leveldb` interface.
|
|
|
|
|
2022-08-06 20:16:08 -04:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use crate::{
|
2022-08-24 15:43:00 -04:00
|
|
|
hash::HashWriter,
|
2022-08-06 20:16:08 -04:00
|
|
|
node::Node,
|
|
|
|
store::{Result, Store, StoreError},
|
|
|
|
};
|
|
|
|
|
|
|
|
use ciborium;
|
|
|
|
use rusty_leveldb;
|
|
|
|
|
2022-08-24 13:10:59 -04:00
|
|
|
/// A `Store` implementation using the rusty-leveldb port of leveldb.
|
|
|
|
/// The Default implementation of this `Default::default()` is an in-memory
|
|
|
|
/// implementation of the store.
|
2022-08-06 20:16:08 -04:00
|
|
|
pub struct LevelStore {
|
|
|
|
store: RefCell<rusty_leveldb::DB>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LevelStore {
|
|
|
|
pub fn open<P: AsRef<Path>>(path: P) -> std::result::Result<Self, rusty_leveldb::Status> {
|
|
|
|
let opts = Default::default();
|
|
|
|
Ok(Self {
|
|
|
|
store: RefCell::new(rusty_leveldb::DB::open(path, opts)?),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 13:10:59 -04:00
|
|
|
|
2022-08-24 15:43:00 -04:00
|
|
|
impl<HW> Store<HW> for LevelStore
|
|
|
|
where
|
|
|
|
HW: HashWriter,
|
|
|
|
{
|
2022-08-24 10:00:08 -04:00
|
|
|
fn contains(&self, id: &[u8]) -> Result<bool> {
|
2022-08-06 20:16:08 -04:00
|
|
|
Ok(self.store.borrow_mut().get(id).is_some())
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:43:00 -04:00
|
|
|
fn get(&self, id: &[u8]) -> Result<Option<Node<HW>>> {
|
2022-08-06 20:16:08 -04:00
|
|
|
Ok(match self.store.borrow_mut().get(id) {
|
|
|
|
Some(bs) => ciborium::de::from_reader(bs.as_slice())
|
|
|
|
.map_err(|e| StoreError::StoreFailure(format!("Invalid serialization {:?}", e)))?,
|
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:43:00 -04:00
|
|
|
fn store(&mut self, node: Node<HW>) -> Result<()> {
|
2022-08-06 20:16:08 -04:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
ciborium::ser::into_writer(&node, &mut buf).unwrap();
|
|
|
|
self.store.borrow_mut().put(node.id(), &buf)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<rusty_leveldb::Status> for StoreError {
|
|
|
|
fn from(status: rusty_leveldb::Status) -> Self {
|
|
|
|
StoreError::StoreFailure(format!("{}", status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LevelStore {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
store: RefCell::new(
|
|
|
|
rusty_leveldb::DB::open("memory", rusty_leveldb::in_memory()).unwrap(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|