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
use std::path::PathBuf;

use crate::storage::Storage;
use crate::store::Store;

use anyhow::Result;
use async_trait::async_trait;
use sled::{Db, Tree};

pub enum NativeStorageInit {
    Path(PathBuf),
    Db(Db),
}

#[derive(Clone)]
pub struct NativeStorage {
    db: Db,
}

impl NativeStorage {
    pub fn new(init: NativeStorageInit) -> Result<Self> {
        let db: Db = match init {
            NativeStorageInit::Path(path) => {
                std::fs::create_dir_all(&path)?;
                sled::open(path)?
            }
            NativeStorageInit::Db(db) => db,
        };

        Ok(NativeStorage { db })
    }

    async fn get_store(&self, name: &str) -> Result<NativeStore> {
        Ok(NativeStore::new(&self.db.open_tree(name)?))
    }
}

#[async_trait]
impl Storage for NativeStorage {
    type BlockStore = NativeStore;

    type KeyValueStore = NativeStore;

    async fn get_block_store(&self, name: &str) -> Result<Self::BlockStore> {
        self.get_store(name).await
    }

    async fn get_key_value_store(&self, name: &str) -> Result<Self::KeyValueStore> {
        self.get_store(name).await
    }
}

#[derive(Clone)]
pub struct NativeStore {
    db: Tree,
}

impl NativeStore {
    pub fn new(db: &Tree) -> Self {
        NativeStore { db: db.clone() }
    }
}

#[async_trait]
impl Store for NativeStore {
    async fn read(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        Ok(self.db.get(key)?.map(|entry| entry.to_vec()))
    }

    async fn write(&mut self, key: &[u8], bytes: &[u8]) -> Result<Option<Vec<u8>>> {
        let old_bytes = self
            .db
            .insert(key, bytes)?
            .map(|old_entry| old_entry.to_vec());
        Ok(old_bytes)
    }

    /// Remove a value given a CID
    async fn remove(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>> {
        Ok(self
            .db
            .remove(key)
            .map(|maybe_entry| maybe_entry.map(|entry| entry.to_vec()))?)
    }

    /// Flushes pending writes if there are any
    async fn flush(&self) -> Result<()> {
        self.db.flush_async().await?;
        Ok(())
    }
}