epochfs 0.1.5

EpochFS is a versioned cloud file system with git-like branching, transaction support.
Documentation
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use std::mem;

use anyhow::Result;
use base64::Engine as _;
use futures::{Stream, StreamExt, TryStreamExt};
use opendal::{Buffer, Operator};
use prost::Message;
use sqlx::{QueryBuilder, SqlitePool};
use uuid::{NoContext, Timestamp};

use crate::{
    specs::{self, Checkpoint, FileChunks},
    File,
};

/// Fs is the main entry point for the epoch filesystem.
#[derive(Debug, Clone)]
pub struct Fs {
    db: SqlitePool,
    op: Operator,

    log_path: String,
    data_path: String,
}

impl Fs {
    /// Create a new filesystem instance.
    pub async fn new(op: Operator) -> Result<Self> {
        let db = SqlitePool::connect("sqlite://:memory:").await?;

        sqlx::query!(
            r#"
             CREATE TABLE IF NOT EXISTS files (
                path TEXT PRIMARY KEY NOT NULL,
                chunks BLOB NOT NULL
            )
            "#,
        )
        .execute(&db)
        .await?;

        Ok(Fs {
            db,
            op,
            data_path: "data/".to_string(),
            log_path: "logs/".to_string(),
        })
    }

    /// write a chunk to the storage and return the chunk id.
    pub(crate) async fn write_chunk(&self, bs: Buffer) -> Result<String> {
        let chunk_id = chunk_id(bs.clone());

        let chunk_path = format!("{}/{}", &self.data_path, chunk_id);
        if !self.op.exists(&chunk_path).await? {
            self.op.write(&chunk_path, bs).await?;
        }

        Ok(chunk_id)
    }

    /// Read a chunk from the storage.
    pub(crate) async fn read_chunk(&self, chunk_id: &str) -> Result<Buffer> {
        let chunk_path = format!("{}/{}", &self.data_path, chunk_id);
        Ok(self.op.read(&chunk_path).await?)
    }

    /// Save the checkpoint to the storage.
    pub(crate) async fn save_checkpoint(&self, chunk_ids: Vec<String>) -> Result<String> {
        let checkpoint = Checkpoint {
            chunks: Some(FileChunks { ids: chunk_ids }),
        };
        let bs = checkpoint.encode_to_vec();
        let checkpoint_name = uuid::Uuid::new_v7(Timestamp::now(NoContext)).to_string();
        let checkpoint_path = format!("{}/{}.checkpoint", &self.log_path, checkpoint_name);
        self.op.write(&checkpoint_path, bs).await?;
        Ok(checkpoint_name)
    }

    /// Read the checkpoint from the storage.
    pub(crate) async fn read_checkpoint(&self, checkpoint: &str) -> Result<Buffer> {
        let checkpoint_path = format!("{}/{}.checkpoint", &self.log_path, checkpoint);
        let bs = self.op.read(&checkpoint_path).await?;
        Ok(bs)
    }

    /// Create a new file.
    pub async fn create_file(&self, path: &str) -> Result<File> {
        let file = sqlx::query!(
            r"
                SELECT path FROM files WHERE path = ?
            ",
            path
        )
        .fetch_optional(&self.db)
        .await?;

        if file.is_some() {
            return Err(anyhow::anyhow!("file {path} already exists"));
        }

        let new_file = File::new(self.clone(), path.to_string());

        Ok(new_file)
    }

    /// Open a file for reading.
    pub async fn open_file(&self, path: &str) -> Result<Option<File>> {
        let Some(record) = sqlx::query!(
            r"
                SELECT * FROM files WHERE path = ?
            ",
            path
        )
        .fetch_optional(&self.db)
        .await?
        else {
            return Ok(None);
        };
        let file = File::with_chunks(
            self.clone(),
            path.to_string(),
            FileChunks::decode(record.chunks.as_slice())?.ids,
        );
        log::debug!(
            "open file {} with {} chunks loaded",
            path,
            file.chunks.len()
        );

        Ok(Some(file))
    }

    /// Check if the file exists in the filesystem.
    pub async fn check_file(&self, path: &str) -> Result<bool> {
        let file = sqlx::query!(
            r"
                SELECT path FROM files WHERE path = ?
            ",
            path
        )
        .fetch_optional(&self.db)
        .await?;

        Ok(file.is_some())
    }

    /// Commit the file to the database.
    pub(crate) async fn commit_file(&self, path: &str, chunk_ids: Vec<String>) -> Result<()> {
        log::debug!("commit file {} with {} chunks saved", path, chunk_ids.len());

        let chunk_ids = super::specs::FileChunks { ids: chunk_ids };
        let chunk_id_content = chunk_ids.encode_to_vec();

        sqlx::query!(
            r#"
                INSERT INTO files (path, chunks) VALUES (?, ?)
            "#,
            path,
            chunk_id_content
        )
        .execute(&self.db)
        .await?;

        Ok(())
    }

    /// List all files in the filesystem.
    pub fn list_files(&self) -> impl Stream<Item = Result<File>> + use<'_> {
        let fs = self.clone();

        sqlx::query!("SELECT * FROM files")
            .fetch(&self.db)
            .map(move |record| {
                let record = record?;
                let file = File::with_chunks(
                    fs.clone(),
                    record.path,
                    FileChunks::decode(record.chunks.as_slice())?.ids,
                );
                Ok(file)
            })
    }

    /// Load the filesystem from storage.
    pub async fn load(&self, checkpoint: &str) -> Result<()> {
        let bs = self.read_checkpoint(checkpoint).await?;
        let checkpoint = Checkpoint::decode(bs)?;
        let chunk_ids = checkpoint.chunks.unwrap_or_default().ids;

        for chunk_id in chunk_ids {
            let chunk = self.read_chunk(&chunk_id).await?;
            let files = specs::Files::decode(chunk)?;

            let files = files
                .files
                .into_iter()
                .map(|file| (file.path, file.chunks.unwrap_or_default().encode_to_vec()))
                .collect::<Vec<_>>();

            // Insert the files in batch.
            //
            // sqlite allows up to 1000 parameters in a single query, we use 400 here.
            const BATCH_SIZE: usize = 400;

            for sql_chunk in files.chunks(BATCH_SIZE) {
                let mut query_builder = QueryBuilder::new("INSERT INTO files (path, chunks) ");

                query_builder.push_values(sql_chunk, |mut b, (path, chunks)| {
                    b.push_bind(path).push_bind(chunks);
                });

                let query = query_builder.build();
                query.execute(&self.db).await?;
            }
        }

        Ok(())
    }

    /// Commit the filesystem to storage.
    ///
    /// The commit will be saved as a checkpoint in storage.
    /// The checkpoint consists of multiple files, each containing a
    /// batch of files.
    pub async fn commit(&self) -> Result<String> {
        let mut file_stream = self.list_files();

        let mut chunk_ids = Vec::with_capacity(16);
        let mut size = 0;
        let mut files = Vec::with_capacity(10000);

        while let Some(record) = file_stream.try_next().await? {
            let (path, chunks) = record.into_parts();
            let file = specs::File {
                path,
                chunks: Some(FileChunks { ids: chunks }),
            };
            size += file.encoded_len();
            files.push(file);

            // If the size is less than 8MiB, we can add more files in this chunk.
            if size < 8 * 1024 * 1024 {
                continue;
            }
            let files = specs::Files {
                files: mem::replace(&mut files, Vec::with_capacity(10000)),
            };
            let bs = files.encode_to_vec();
            let chunk_id = self.write_chunk(Buffer::from(bs)).await?;
            chunk_ids.push(chunk_id);
        }

        if !files.is_empty() {
            let files = specs::Files { files };
            let bs = files.encode_to_vec();
            let chunk_id = self.write_chunk(Buffer::from(bs)).await?;
            chunk_ids.push(chunk_id);
        }

        self.save_checkpoint(chunk_ids).await
    }
}

/// Calculate the chunk id from the buffer.
///
/// The chunk id is the URL_SAFE_NO_PAD base64 of blake3 hash of the buffer content.
///
/// chund id is used to identify the chunk in the storage. If the chunk
/// id already exists, we can reuse the existing chunk instead of creating
/// a new one.
fn chunk_id(bs: Buffer) -> String {
    let mut hasher = blake3::Hasher::new();
    for b in bs {
        hasher.update(&b);
    }
    let result = hasher.finalize();
    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(result.as_bytes())
}

#[cfg(test)]
mod tests {
    use anyhow::Result;
    use futures::stream;
    use opendal::{services::MemoryConfig, Buffer};
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_chunk_id() {
        let bs = Buffer::from("hello world");
        let id = chunk_id(bs);
        assert_eq!(id, "10mB76cKDIgLjYwZhdB128v2ebmaX5kU5ar5a4ManiQ");
    }

    #[tokio::test]
    async fn test_upload_chunk() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from("hello world");
        let id = fs.write_chunk(source.clone()).await.unwrap();

        let actual = fs.read_chunk(&id).await?;
        assert_eq!(source.to_vec(), actual.to_vec());
        Ok(())
    }

    #[tokio::test]
    async fn test_create_file() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from("hello world");
        let id = chunk_id(source.clone());

        let mut file = fs.create_file("hello.txt").await?;
        file.write(source.clone()).await?;
        file.commit().await?;

        let actual = fs.read_chunk(&id).await?;
        assert_eq!(source.to_vec(), actual.to_vec());
        Ok(())
    }

    #[tokio::test]
    async fn test_create_file_with_sink() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from("hello world");
        let id = chunk_id(source.clone());

        let mut file = fs.create_file("hello.txt").await?;
        file.sink(stream::iter([Ok(source.clone())])).await?;
        file.commit().await?;

        let actual = fs.read_chunk(&id).await?;
        assert_eq!(source.to_vec(), actual.to_vec());
        Ok(())
    }

    #[tokio::test]
    async fn test_create_file_with_large_sink() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from(vec![6u8; 3 * 1024 * 1024]);
        let id_8mib = chunk_id(Buffer::from(vec![6; 8 * 1024 * 1024]));
        let id_1mib = chunk_id(Buffer::from(vec![6; 1024 * 1024]));

        let mut file = fs.create_file("hello.txt").await?;
        // We are writing 3 * 3MiB of data, so we should have 2 chunks:
        // the first is 8MiB, the second is 1MiB.
        file.sink(stream::iter([
            Ok(source.clone()),
            Ok(source.clone()),
            Ok(source.clone()),
        ]))
        .await?;
        file.commit().await?;

        let actual_8mib = fs.read_chunk(&id_8mib).await?;
        assert_eq!(vec![6; 8 * 1024 * 1024].to_vec(), actual_8mib.to_vec());

        let actual_1mib = fs.read_chunk(&id_1mib).await?;
        assert_eq!(vec![6; 1024 * 1024].to_vec(), actual_1mib.to_vec());
        Ok(())
    }

    #[tokio::test]
    async fn test_open_file() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from("hello world");

        let mut file = fs.create_file("hello.txt").await?;
        file.write(source.clone()).await?;
        file.commit().await?;

        let file = fs.open_file("hello.txt").await?.expect("file must exist");

        let actual = file.read().await?;
        assert_eq!(source.to_vec(), actual.to_vec());
        Ok(())
    }

    #[tokio::test]
    async fn test_list_files() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from("hello world");
        let id = chunk_id(source.clone());

        let mut file = fs.create_file("hello.txt").await?;
        file.write(source.clone()).await?;
        file.commit().await?;

        let files: Vec<File> = fs.list_files().try_collect::<Vec<_>>().await?;
        assert_eq!(files.len(), 1);
        let (path, chunks) = files[0].clone().into_parts();
        assert_eq!(path, "hello.txt");
        assert_eq!(chunks.len(), 1);
        assert_eq!(chunks[0], id);

        let actual = fs.read_chunk(&id).await?;
        assert_eq!(source.to_vec(), actual.to_vec());

        let actual = files[0].read().await?;
        assert_eq!(source.to_vec(), actual.to_vec());
        Ok(())
    }

    #[tokio::test]
    async fn test_save_checkpoint() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op).await?;

        let source = Buffer::from("hello world");

        let mut file = fs.create_file("hello.txt").await?;
        file.write(source.clone()).await?;
        file.commit().await?;

        let checkpoint_name = fs.commit().await?;

        let bs = fs.read_checkpoint(&checkpoint_name).await?;
        let checkpoint = Checkpoint::decode(bs)?;
        assert_eq!(checkpoint.chunks.unwrap().ids.len(), 1);
        Ok(())
    }

    #[tokio::test]
    async fn test_load_checkpoint() -> Result<()> {
        let op = Operator::from_config(MemoryConfig::default())?.finish();
        let fs = Fs::new(op.clone()).await?;

        let source = Buffer::from("hello world");

        let mut file = fs.create_file("hello.txt").await?;
        file.write(source.clone()).await?;
        file.commit().await?;

        let checkpoint_name = fs.commit().await?;

        // Create a fs for loading test.
        let fs = Fs::new(op).await?;
        fs.load(&checkpoint_name).await?;

        assert!(fs.check_file("hello.txt").await?);
        Ok(())
    }
}