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
pub mod database;
mod database_log;
pub mod error;
mod manifest_manager;
pub mod mem_database;
mod merge;
mod sstable;

use agilulf_protocol::Slice;

use agilulf_protocol::DatabaseResult as Result;
use futures::Future;
use std::pin::Pin;

pub use database::{Database, DatabaseBuilder};

/// Abstraction layer for a SyncDatabase. Every method should return directly.
pub trait SyncDatabase: Send + Sync {
    fn get_sync(&self, key: Slice) -> Result<Slice>;

    fn put_sync(&self, key: Slice, value: Slice) -> Result<()>;

    fn scan_sync(&self, start: Slice, end: Slice) -> Vec<(Slice, Slice)>;

    fn delete_sync(&self, key: Slice) -> Result<()>;
}

/// Abstraction layer for a AsyncDatabase. Every method return a Future.
///
/// The return type of these function are fixed as `Pin<Box<dyn Future<Output = Result<_>> + Send + '_>>`
/// rather than a generic type for convenience.
pub trait AsyncDatabase: Send + Sync {
    fn get(&self, key: Slice) -> Pin<Box<dyn Future<Output = Result<Slice>> + Send + '_>>;

    fn put(
        &self,
        key: Slice,
        value: Slice,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>>;

    /// SCAN don't return a `Result` because if nothing is found, an empty vector will be returned.
    fn scan(
        &self,
        start: Slice,
        end: Slice,
    ) -> Pin<Box<dyn Future<Output = Vec<(Slice, Slice)>> + Send + '_>>;

    fn delete(&self, key: Slice) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>>;
}

/// Every sync database can be wrapped as an async database easily. With this wrapper, MemDatabase can
/// be used directly on Server.
impl<T: SyncDatabase> AsyncDatabase for T {
    fn get(&self, key: Slice) -> Pin<Box<dyn Future<Output = Result<Slice>> + Send + '_>> {
        Box::pin(async move { self.get_sync(key) })
    }

    fn put(
        &self,
        key: Slice,
        value: Slice,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> {
        Box::pin(async move { self.put_sync(key, value) })
    }

    fn scan(
        &self,
        start: Slice,
        end: Slice,
    ) -> Pin<Box<dyn Future<Output = Vec<(Slice, Slice)>> + Send + '_>> {
        Box::pin(async move { self.scan_sync(start, end) })
    }

    fn delete(&self, key: Slice) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> {
        Box::pin(async move { self.delete_sync(key) })
    }
}