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
use std::io;
use std::ops::{Deref, DerefMut};
use async_trait::async_trait;
use bytes::Bytes;
use futures::{future, TryFutureExt, TryStreamExt};
use tokio::io::{AsyncReadExt, AsyncWrite};
use tokio_util::io::StreamReader;
use tc_error::*;
use tc_value::Value;
use tcgeneric::{Id, PathSegment};
use super::TxnId;
pub type BlockId = PathSegment;
#[async_trait]
pub trait BlockData: Clone + Send + Sync {
    async fn load<S: AsyncReadExt + Send + Unpin>(source: S) -> TCResult<Self>;
    async fn persist<W: AsyncWrite + Send + Unpin>(&self, sink: &mut W) -> TCResult<u64>;
    async fn size(&self) -> TCResult<u64>;
}
#[async_trait]
impl BlockData for Value {
    async fn load<S: AsyncReadExt + Send + Unpin>(source: S) -> TCResult<Self> {
        destream_json::read_from((), source)
            .map_err(|e| TCError::internal(format!("unable to parse Value: {}", e)))
            .await
    }
    async fn persist<W: AsyncWrite + Send + Unpin>(&self, sink: &mut W) -> TCResult<u64> {
        let encoded = destream_json::encode(self.clone())
            .map_err(|e| TCError::internal(format!("unable to serialize Value: {}", e)))?;
        let mut reader = StreamReader::new(
            encoded
                .map_ok(Bytes::from)
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)),
        );
        tokio::io::copy(&mut reader, sink)
            .map_err(|e| TCError::bad_gateway(e))
            .await
    }
    async fn size(&self) -> TCResult<u64> {
        let encoded = destream_json::encode(self)
            .map_err(|e| TCError::bad_request("serialization error", e))?;
        encoded
            .map_err(|e| TCError::bad_request("serialization error", e))
            .try_fold(0, |size, chunk| {
                future::ready(Ok(size + chunk.len() as u64))
            })
            .await
    }
}
#[async_trait]
pub trait Block<B: BlockData>: Send + Sync {
    type ReadLock: Deref<Target = B>;
    type WriteLock: DerefMut<Target = B>;
    
    async fn read(&self, txn_id: &TxnId) -> TCResult<Self::ReadLock>;
    
    async fn write(&self, txn_id: &TxnId) -> TCResult<Self::WriteLock>;
}
#[async_trait]
pub trait Store: Send + Sync {
    
    async fn is_empty(&self, txn_id: &TxnId) -> TCResult<bool>;
}
#[async_trait]
pub trait File<B: BlockData>: Store + Sized {
    
    type Block: Block<B>;
    
    async fn contains_block(&self, txn_id: &TxnId, name: &BlockId) -> TCResult<bool>;
    
    async fn create_block(
        &self,
        txn_id: TxnId,
        name: BlockId,
        initial_value: B,
    ) -> TCResult<<Self::Block as Block<B>>::ReadLock>;
    
    async fn delete_block(&self, txn_id: &TxnId, name: BlockId) -> TCResult<()>;
    
    async fn get_block(
        &self,
        txn_id: &TxnId,
        name: BlockId,
    ) -> TCResult<<Self::Block as Block<B>>::ReadLock>;
    
    async fn get_block_mut(
        &self,
        txn_id: &TxnId,
        name: BlockId,
    ) -> TCResult<<Self::Block as Block<B>>::WriteLock>;
}
#[async_trait]
pub trait Dir: Store + Sized {
    
    type File;
    
    type FileClass;
    
    async fn contains(&self, txn_id: &TxnId, name: &PathSegment) -> TCResult<bool>;
    
    async fn create_dir(&self, txn_id: TxnId, name: PathSegment) -> TCResult<Self>;
    
    async fn create_file(
        &self,
        txn_id: TxnId,
        name: Id,
        class: Self::FileClass,
    ) -> TCResult<Self::File>;
    
    async fn get_dir(&self, txn_id: &TxnId, name: &PathSegment) -> TCResult<Option<Self>>;
    
    async fn get_file(&self, txn_id: &TxnId, name: &Id) -> TCResult<Option<Self::File>>;
}
#[async_trait]
pub trait Persist: Sized {
    type Schema;
    type Store: Store;
    
    fn schema(&'_ self) -> &'_ Self::Schema;
    
    async fn load(schema: Self::Schema, store: Self::Store, txn_id: TxnId) -> TCResult<Self>;
}