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
use crate::{protobytes, ObjectStore, Result};
use prost::Message;
use wapc_guest::host_call;
use wascc_codec::blobstore::Blob;
use wascc_codec::blobstore::Container;
use wascc_codec::blobstore::{BlobList, FileChunk, StreamRequest, Transfer};
use wascc_codec::blobstore::{
OP_CREATE_CONTAINER, OP_GET_OBJECT_INFO, OP_LIST_OBJECTS, OP_REMOVE_CONTAINER,
OP_REMOVE_OBJECT, OP_START_DOWNLOAD, OP_START_UPLOAD, OP_UPLOAD_CHUNK,
};
pub const CAPID_BLOBSTORE: &str = "wascc:blobstore";
#[derive(Default)]
pub struct DefaultObjectStore {}
impl DefaultObjectStore {
pub fn new() -> Self {
Self::default()
}
}
impl ObjectStore for DefaultObjectStore {
fn create_container(&self, name: &str) -> Result<Container> {
let cmd = Container {
id: name.to_string(),
};
host_call(CAPID_BLOBSTORE, OP_CREATE_CONTAINER, &protobytes(cmd)?)
.map(|v| Container::decode(v.as_ref()).unwrap())
.map_err(|e| e.into())
}
fn remove_container(&self, name: &str) -> Result<()> {
let cmd = Container {
id: name.to_string(),
};
host_call(CAPID_BLOBSTORE, OP_REMOVE_CONTAINER, &protobytes(cmd)?)
.map(|_v| ())
.map_err(|e| e.into())
}
fn remove_object(&self, name: &str, container: &str) -> crate::Result<()> {
let cmd = Blob {
id: name.to_string(),
container: container.to_string(),
byte_size: 0,
};
host_call(CAPID_BLOBSTORE, OP_REMOVE_OBJECT, &protobytes(cmd)?)
.map(|_v| ())
.map_err(|e| e.into())
}
fn list_objects(&self, container: &str) -> Result<BlobList> {
let cmd = Container {
id: container.to_string(),
};
host_call(CAPID_BLOBSTORE, OP_LIST_OBJECTS, &protobytes(cmd)?)
.map(|v| BlobList::decode(v.as_ref()).unwrap())
.map_err(|e| e.into())
}
fn get_blob_info(&self, container: &str, id: &str) -> Result<Option<Blob>> {
let cmd = Blob {
id: id.to_string(),
container: container.to_string(),
byte_size: 0,
};
host_call(CAPID_BLOBSTORE, OP_GET_OBJECT_INFO, &protobytes(cmd)?)
.map(|v| {
let b = Blob::decode(v.as_ref()).unwrap();
if b.id.is_empty() {
None
} else {
Some(b)
}
})
.map_err(|e| e.into())
}
fn start_upload(&self, blob: &Blob, chunk_size: u64, total_bytes: u64) -> Result<Transfer> {
let transfer = Transfer {
blob_id: blob.id.to_string(),
container: blob.container.to_string(),
chunk_size,
total_size: total_bytes,
total_chunks: total_bytes / chunk_size,
};
let cmd = FileChunk {
sequence_no: 0,
container: blob.container.to_string(),
id: blob.id.to_string(),
chunk_size,
total_bytes,
chunk_bytes: vec![],
};
host_call(CAPID_BLOBSTORE, OP_START_UPLOAD, &protobytes(cmd)?)
.map(|_v| transfer)
.map_err(|e| e.into())
}
fn upload_chunk(&self, transfer: &Transfer, offset: u64, bytes: &[u8]) -> crate::Result<()> {
let cmd = FileChunk {
id: transfer.blob_id.to_string(),
container: transfer.container.to_string(),
sequence_no: offset,
chunk_size: transfer.chunk_size,
total_bytes: transfer.total_size,
chunk_bytes: bytes.to_vec(),
};
host_call(CAPID_BLOBSTORE, OP_UPLOAD_CHUNK, &protobytes(cmd)?)
.map(|_v| ())
.map_err(|e| e.into())
}
fn start_download(&self, blob: &Blob, chunk_size: u64) -> crate::Result<Transfer> {
let transfer = Transfer {
blob_id: blob.id.to_string(),
container: blob.container.to_string(),
chunk_size,
total_size: blob.byte_size,
total_chunks: blob.byte_size / chunk_size,
};
let cmd = StreamRequest {
container: blob.container.to_string(),
id: blob.id.to_string(),
chunk_size,
};
host_call(CAPID_BLOBSTORE, OP_START_DOWNLOAD, &protobytes(cmd)?)
.map(|_v| transfer)
.map_err(|e| e.into())
}
}