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
use std::collections::HashSet;
use std::fmt;
use std::ops::{Deref, DerefMut};
use async_trait::async_trait;
use bytes::Bytes;
use destream::en;
use futures::{TryFutureExt, TryStreamExt};
use safecast::AsType;
use sha2::{Digest, Sha256};
use tc_error::*;
use tcgeneric::{Id, PathSegment, TCBoxTryStream};
use super::{Transaction, TxnId};
pub type BlockId = PathSegment;
pub trait BlockData: Clone + Send + Sync + 'static {
fn ext() -> &'static str;
}
#[cfg(feature = "tensor")]
impl BlockData for afarray::Array {
fn ext() -> &'static str {
"array"
}
}
impl BlockData for tc_value::Value {
fn ext() -> &'static str {
"value"
}
}
#[async_trait]
pub trait Store: Clone + Send + Sync {
async fn is_empty(&self, txn_id: TxnId) -> TCResult<bool>;
}
#[async_trait]
pub trait File<B: Clone>: Store + Sized + 'static {
type Read: Deref<Target = B> + Send;
type Write: DerefMut<Target = B> + Send;
async fn block_ids(&self, txn_id: TxnId) -> TCResult<HashSet<BlockId>>;
async fn contains_block(&self, txn_id: TxnId, name: &BlockId) -> TCResult<bool>;
async fn copy_from(&self, other: &Self, txn_id: TxnId) -> TCResult<()>;
async fn create_block(
&self,
txn_id: TxnId,
name: BlockId,
initial_value: B,
size_hint: usize,
) -> TCResult<Self::Write>;
async fn create_block_unique(
&self,
txn_id: TxnId,
initial_value: B,
size_hint: usize,
) -> TCResult<(BlockId, Self::Write)>;
async fn delete_block(&self, txn_id: TxnId, name: BlockId) -> TCResult<()>;
async fn read_block(&self, txn_id: TxnId, name: BlockId) -> TCResult<Self::Read>;
async fn read_block_owned(self, txn_id: TxnId, name: BlockId) -> TCResult<Self::Read>;
async fn write_block(&self, txn_id: TxnId, name: BlockId) -> TCResult<Self::Write>;
async fn truncate(&self, txn_id: TxnId) -> TCResult<()>;
}
#[async_trait]
pub trait Dir: Store + Send + Sized + 'static {
type File: Send;
type FileClass: Send;
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_dir_unique(&self, txn_id: TxnId) -> TCResult<Self>;
async fn create_file<C, F, B>(&self, txn_id: TxnId, name: Id, class: C) -> TCResult<F>
where
C: Copy + Send + fmt::Display,
F: Clone,
B: BlockData,
Self::FileClass: From<C>,
Self::File: AsType<F>,
F: File<B>;
async fn create_file_unique<C, F, B>(&self, txn_id: TxnId, class: C) -> TCResult<F>
where
C: Copy + Send + fmt::Display,
F: Clone,
B: BlockData,
Self::FileClass: From<C>,
Self::File: AsType<F>,
F: File<B>;
async fn get_dir(&self, txn_id: TxnId, name: &PathSegment) -> TCResult<Option<Self>>;
async fn get_file<F, B>(&self, txn_id: TxnId, name: &Id) -> TCResult<Option<F>>
where
F: Clone,
B: BlockData,
Self::File: AsType<F>,
F: File<B>;
}
#[async_trait]
pub trait Persist<D: Dir>: Sized {
type Schema;
type Store: Store;
type Txn: Transaction<D>;
fn schema(&self) -> &Self::Schema;
async fn load(txn: &Self::Txn, schema: Self::Schema, store: Self::Store) -> TCResult<Self>;
}
#[async_trait]
pub trait CopyFrom<D: Dir, I>: Persist<D> {
async fn copy_from(
instance: I,
store: <Self as Persist<D>>::Store,
txn: &<Self as Persist<D>>::Txn,
) -> TCResult<Self>;
}
#[async_trait]
pub trait Restore<D: Dir>: Persist<D> {
async fn restore(&self, backup: &Self, txn_id: TxnId) -> TCResult<()>;
}
#[async_trait]
pub trait Hash<'en, D: Dir> {
type Item: en::IntoStream<'en> + Send + 'en;
type Txn: Transaction<D>;
async fn hash_hex(&'en self, txn: &'en Self::Txn) -> TCResult<String> {
self.hash(txn).map_ok(|hash| hex::encode(hash)).await
}
async fn hash(&'en self, txn: &'en Self::Txn) -> TCResult<Bytes> {
let mut data = self.hashable(txn).await?;
let mut hasher = Sha256::default();
while let Some(item) = data.try_next().await? {
hash_chunks(&mut hasher, item).await?;
}
let digest = hasher.finalize();
Ok(Bytes::from(digest.to_vec()))
}
async fn hashable(&'en self, txn: &'en Self::Txn) -> TCResult<TCBoxTryStream<'en, Self::Item>>;
}
async fn hash_chunks<'en, T: en::IntoStream<'en> + 'en>(
hasher: &mut Sha256,
data: T,
) -> TCResult<()> {
let mut data = tbon::en::encode(data).map_err(TCError::internal)?;
while let Some(chunk) = data.try_next().map_err(TCError::internal).await? {
hasher.update(&chunk);
}
Ok(())
}