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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Transactional filesystem traits and data structures.

use std::borrow::Borrow;
use std::collections::HashSet;
use std::fmt;
use std::ops::{Deref, DerefMut};

use async_trait::async_trait;
use safecast::AsType;

use tc_error::*;
use tcgeneric::{Id, PathSegment};

use super::{Transaction, TxnId};

/// An alias for [`Id`] used for code clarity
pub type BlockId = PathSegment;

/// The data contained by a single block on the filesystem
pub trait BlockData: Clone + Send + Sync + 'static {
    fn ext() -> &'static str;
}

#[cfg(feature = "tensor")]
impl BlockData for afarray::Array {
    fn ext() -> &'static str {
        "array"
    }
}

/// A read lock on a block
pub trait BlockRead<B: BlockData>: Deref<Target = B> + Send {}

/// An exclusive read lock on a block
pub trait BlockReadExclusive<B: BlockData>: Deref<Target = B> + Send {
    /// The type of [`File`] that this block is part of
    type File: File<B>;

    fn upgrade(self) -> <Self::File as File<B>>::BlockWrite;
}

/// A write lock on a block
pub trait BlockWrite<B: BlockData>: DerefMut<Target = B> + Send {
    /// The type of [`File`] that this block is part of
    type File: File<B>;

    fn downgrade(self) -> <Self::File as File<B>>::BlockReadExclusive;
}

/// A read lock on a [`File`]
#[async_trait]
pub trait FileRead<B: BlockData>: Sized + Send + Sync {
    /// The type of this [`File`]
    type File: File<B>;

    /// Get the set of all [`BlockId`]s in this [`File`].
    fn block_ids(&self) -> HashSet<&BlockId>;

    /// Return `true` if this [`File`] contains the given `block_id`.
    fn contains(&self, block_id: &BlockId) -> bool;

    /// Return `true` if there are no blocks in this [`File`].
    fn is_empty(&self) -> bool;

    /// Lock the block at `name` for reading.
    async fn read_block<I>(&self, name: I) -> TCResult<<Self::File as File<B>>::BlockRead>
    where
        I: Borrow<BlockId> + Send + Sync;

    /// Lock the block at `name` for reading exclusively,
    /// i.e. prevent any more read locks being aquired while this one is active.
    async fn read_block_exclusive<I>(
        &self,
        name: I,
    ) -> TCResult<<Self::File as File<B>>::BlockReadExclusive>
    where
        I: Borrow<BlockId> + Send + Sync;

    /// Lock the block at `name` for reading, without borrowing.
    async fn read_block_owned<I>(self, name: I) -> TCResult<<Self::File as File<B>>::BlockRead>
    where
        I: Borrow<BlockId> + Send + Sync,
    {
        self.read_block(name).await
    }

    /// Convenience method to lock the block at `name` for writing.
    async fn write_block<I>(&self, name: I) -> TCResult<<Self::File as File<B>>::BlockWrite>
    where
        I: Borrow<BlockId> + Send + Sync;
}

/// An exclusive read lock on a [`File`]
pub trait FileReadExclusive<B: BlockData>: FileRead<B> {
    /// Upgrade this read lock to a write lock
    fn upgrade(self) -> <Self::File as File<B>>::Write;
}

/// A write lock on a [`File`]
#[async_trait]
pub trait FileWrite<B: BlockData>: FileRead<B> {
    /// Downgrade this write lock to an exclusive read lock.
    fn downgrade(self) -> <Self::File as File<B>>::ReadExclusive;

    /// Create a new block.
    async fn create_block(
        &mut self,
        name: BlockId,
        initial_value: B,
        size_hint: usize,
    ) -> TCResult<<Self::File as File<B>>::BlockWrite>;

    /// Create a new block.
    async fn create_block_unique(
        &mut self,
        initial_value: B,
        size_hint: usize,
    ) -> TCResult<(BlockId, <Self::File as File<B>>::BlockWrite)>;

    /// Delete the block with the given ID.
    async fn delete_block(&mut self, name: BlockId) -> TCResult<()>;

    /// Delete all of this `File`'s blocks.
    async fn copy_from<O: FileRead<B>>(&mut self, other: &O, truncate: bool) -> TCResult<()>;

    /// Delete all of this `File`'s blocks.
    async fn truncate(&mut self) -> TCResult<()>;
}

/// A transactional file
#[async_trait]
pub trait File<B: BlockData>: Store + 'static {
    /// The type of read guard used by this `File`
    type Read: FileRead<B, File = Self> + Clone;

    /// The type of exclusive read guard used by this `File`
    type ReadExclusive: FileReadExclusive<B, File = Self>;

    /// The type of write guard used by this `File`
    type Write: FileWrite<B, File = Self>;

    /// A read lock on a block in this file.
    type BlockRead: BlockRead<B>;

    /// An exclusive read lock on a block in this file.
    type BlockReadExclusive: BlockReadExclusive<B, File = Self>;

    /// A write lock on a block in this file.
    type BlockWrite: BlockWrite<B, File = Self>;

    /// Lock the contents of this file for reading at the given `txn_id`.
    async fn read(&self, txn_id: TxnId) -> TCResult<Self::Read>;

    /// Lock the contents of this file for reading at the given `txn_id`, exclusively,
    /// i.e. don't allow any more read locks while this one is active.
    async fn read_exclusive(&self, txn_id: TxnId) -> TCResult<Self::ReadExclusive>;

    /// Lock the contents of this file for writing.
    async fn write(&self, txn_id: TxnId) -> TCResult<Self::Write>;

    /// Convenience method to lock the block at `name` for reading.
    async fn read_block<I>(&self, txn_id: TxnId, name: I) -> TCResult<Self::BlockRead>
    where
        I: Borrow<BlockId> + Send + Sync,
    {
        let file = self.read(txn_id).await?;
        file.read_block(name).await
    }

    /// Convenience method to lock the block at `name` for writing.
    async fn write_block<I>(&self, txn_id: TxnId, name: I) -> TCResult<Self::BlockWrite>
    where
        I: Borrow<BlockId> + Send + Sync,
    {
        let file = self.read(txn_id).await?;
        file.write_block(name).await
    }
}

/// A read lock on a [`Dir`]
pub trait DirRead: Send + Sync {
    /// The type of a file entry in this [`Dir`]
    type FileEntry;

    /// The type of lock used to guard subdirectories in this [`Dir`]
    type Lock: Dir;

    /// Return `true` if this directory has an entry at the given [`PathSegment`].
    fn contains(&self, name: &PathSegment) -> bool;

    /// Look up a subdirectory of this `Dir`.
    fn get_dir(&self, name: &PathSegment) -> TCResult<Option<Self::Lock>>;

    /// Get a [`File`] in this `Dir`.
    fn get_file<F, B>(&self, name: &Id) -> TCResult<Option<F>>
    where
        Self::FileEntry: AsType<F>,
        B: BlockData,
        F: File<B>;

    /// Return `true` if there are no files or subdirectories in this [`Dir`].
    fn is_empty(&self) -> bool;
}

/// A write lock on a [`Dir`]
pub trait DirWrite: DirRead {
    /// The `Class` of a file stored in this [`Dir`]
    type FileClass: Copy + Send + fmt::Display;

    /// Create a new `Dir`.
    fn create_dir(&mut self, name: PathSegment) -> TCResult<Self::Lock>;

    /// Create a new `Dir` with a new unique ID.
    fn create_dir_unique(&mut self) -> TCResult<Self::Lock>;

    /// Create a new [`File`].
    fn create_file<C, F, B>(&mut self, name: Id, class: C) -> TCResult<F>
    where
        Self::FileClass: From<C>,
        Self::FileEntry: AsType<F>,
        C: Copy + Send + fmt::Display,
        B: BlockData,
        F: File<B>;

    /// Create a new [`File`] with a new unique ID.
    fn create_file_unique<C, F, B>(&mut self, class: C) -> TCResult<F>
    where
        Self::FileClass: From<C>,
        Self::FileEntry: AsType<F>,
        C: Copy + Send + fmt::Display,
        B: BlockData,
        F: File<B>;

    /// Get the [`Dir`] with the given `name` and create a new one if none exists.
    fn get_or_create_dir(&mut self, name: PathSegment) -> TCResult<Self::Lock> {
        if let Some(dir) = self.get_dir(&name)? {
            Ok(dir)
        } else {
            self.create_dir(name)
        }
    }

    /// Get the [`File`] with the given `name` and create a new one if none exists.
    fn get_or_create_file<C, F, B>(&mut self, name: PathSegment, class: C) -> TCResult<F>
    where
        Self::FileClass: From<C>,
        Self::FileEntry: AsType<F>,
        C: Copy + Send + fmt::Display,
        B: BlockData,
        F: File<B>,
    {
        if let Some(file) = self.get_file(&name)? {
            Ok(file)
        } else {
            self.create_file(name, class)
        }
    }
}

/// A transactional directory
#[async_trait]
pub trait Dir: Store + Send + Sized + 'static {
    /// The type of read guard used by this `Dir`
    type Read: DirRead<Lock = Self>;

    /// The type of write guard used by this `Dir`
    type Write: DirWrite<FileEntry = <Self::Read as DirRead>::FileEntry, Lock = Self>;

    /// Lock this [`Dir`] for reading.
    async fn read(&self, txn_id: TxnId) -> TCResult<Self::Read>;

    /// Lock this [`Dir`] for writing.
    async fn write(&self, txn_id: TxnId) -> TCResult<Self::Write>;

    /// Convenience method to create a temporary working directory
    async fn create_dir_unique(&self, txn_id: TxnId) -> TCResult<Self> {
        let mut dir = self.write(txn_id).await?;
        dir.create_dir_unique()
    }

    /// Convenience method to create a temporary file
    async fn create_file_unique<C, F, B>(&self, txn_id: TxnId, class: C) -> TCResult<F>
    where
        <Self::Write as DirWrite>::FileClass: From<C>,
        <Self::Read as DirRead>::FileEntry: AsType<F>,
        C: Copy + Send + fmt::Display,
        B: BlockData,
        F: File<B>,
    {
        let mut dir = self.write(txn_id).await?;
        dir.create_file_unique(class)
    }
}

/// A transactional persistent data store, i.e. a [`File`] or [`Dir`].
pub trait Store: Clone + Send + Sync {}

/// Defines how to load a persistent data structure from the filesystem.
#[async_trait]
pub trait Persist<D: Dir>: Sized {
    type Schema;
    type Store: Store;
    type Txn: Transaction<D>;

    /// Return the schema of this persistent state.
    fn schema(&self) -> &Self::Schema;

    /// Load a saved state from persistent storage.
    async fn load(txn: &Self::Txn, schema: Self::Schema, store: Self::Store) -> TCResult<Self>;
}

/// Defines how to copy a base state from another instance, possibly a view.
#[async_trait]
pub trait CopyFrom<D: Dir, I>: Persist<D> {
    /// Copy a new instance of `Self` from an existing instance.
    async fn copy_from(
        instance: I,
        store: <Self as Persist<D>>::Store,
        txn: &<Self as Persist<D>>::Txn,
    ) -> TCResult<Self>;
}

/// Defines how to restore persistent state from backup.
#[async_trait]
pub trait Restore<D: Dir>: Persist<D> {
    /// Restore this persistent state from a backup.
    async fn restore(&self, backup: &Self, txn_id: TxnId) -> TCResult<()>;
}