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
use std::path::Path;
use engula_journal::{grpc as grpc_journal, Error as JournalError, Journal};
use engula_storage::{grpc as grpc_storage, Error as StorageError, Storage};
use crate::{
file::Manifest as FileManifest,
local::{Kernel as LocalKernel, DEFAULT_NAME},
manifest::Manifest,
mem::Manifest as MemManifest,
Result,
};
pub type Kernel<M> = LocalKernel<grpc_journal::Journal, grpc_storage::Storage, M>;
async fn create_default_stream(journal: &impl Journal) -> Result<()> {
match journal.create_stream(DEFAULT_NAME).await {
Err(JournalError::AlreadyExists(_)) => Ok(()),
Ok(_) => Ok(()),
Err(e) => Err(e.into()),
}
}
async fn create_default_bucket(storage: &impl Storage) -> Result<()> {
match storage.create_bucket(DEFAULT_NAME).await {
Err(StorageError::AlreadyExists(_)) => Ok(()),
Ok(_) => Ok(()),
Err(e) => Err(e.into()),
}
}
async fn create_kernel<M: Manifest>(
journal_addr: &str,
storage_addr: &str,
manifest: M,
) -> Result<Kernel<M>> {
let journal = grpc_journal::Journal::connect(journal_addr).await?;
let storage = grpc_storage::Storage::connect(storage_addr).await?;
create_default_stream(&journal).await?;
create_default_bucket(&storage).await?;
Kernel::init(journal, storage, manifest).await
}
pub type MemKernel = Kernel<MemManifest>;
impl MemKernel {
pub async fn open(journal_addr: &str, storage_addr: &str) -> Result<Self> {
create_kernel(journal_addr, storage_addr, MemManifest::default()).await
}
}
pub type FileKernel = Kernel<FileManifest>;
impl FileKernel {
pub async fn open<P: AsRef<Path>>(
journal_addr: &str,
storage_addr: &str,
path: P,
) -> Result<Self> {
let manifest = FileManifest::open(path.as_ref()).await;
create_kernel(journal_addr, storage_addr, manifest).await
}
}