1use crate::HandlerResult;
2use wapc_guest::host_call;
3use wascc_codec::blobstore::Blob;
4use wascc_codec::blobstore::Container;
5use wascc_codec::blobstore::{BlobList, FileChunk, StreamRequest, Transfer};
6use wascc_codec::blobstore::{
7 OP_CREATE_CONTAINER, OP_GET_OBJECT_INFO, OP_LIST_OBJECTS, OP_REMOVE_CONTAINER,
8 OP_REMOVE_OBJECT, OP_START_DOWNLOAD, OP_START_UPLOAD, OP_UPLOAD_CHUNK,
9};
10use wascc_codec::{deserialize, serialize};
11
12const CAPID_BLOBSTORE: &str = "wascc:blobstore";
13
14pub struct ObjectStoreHostBinding {
16 binding: String,
17}
18
19impl Default for ObjectStoreHostBinding {
20 fn default() -> Self {
21 ObjectStoreHostBinding {
22 binding: "default".to_string(),
23 }
24 }
25}
26
27pub fn host(binding: &str) -> ObjectStoreHostBinding {
29 ObjectStoreHostBinding {
30 binding: binding.to_string(),
31 }
32}
33
34pub fn default() -> ObjectStoreHostBinding {
36 ObjectStoreHostBinding::default()
37}
38
39impl ObjectStoreHostBinding {
40 pub fn create_container(&self, name: &str) -> HandlerResult<Container> {
42 let cmd = Container {
43 id: name.to_string(),
44 };
45 host_call(
46 &self.binding,
47 CAPID_BLOBSTORE,
48 OP_CREATE_CONTAINER,
49 &serialize(cmd)?,
50 )
51 .map(|v| deserialize::<Container>(v.as_ref()).unwrap())
52 .map_err(|e| e.into())
53 }
54
55 pub fn remove_container(&self, name: &str) -> HandlerResult<()> {
58 let cmd = Container {
59 id: name.to_string(),
60 };
61 host_call(
62 &self.binding,
63 CAPID_BLOBSTORE,
64 OP_REMOVE_CONTAINER,
65 &serialize(cmd)?,
66 )
67 .map(|_v| ())
68 .map_err(|e| e.into())
69 }
70
71 pub fn remove_object(&self, name: &str, container: &str) -> crate::HandlerResult<()> {
73 let cmd = Blob {
74 id: name.to_string(),
75 container: container.to_string(),
76 byte_size: 0,
77 };
78 host_call(
79 &self.binding,
80 CAPID_BLOBSTORE,
81 OP_REMOVE_OBJECT,
82 &serialize(cmd)?,
83 )
84 .map(|_v| ())
85 .map_err(|e| e.into())
86 }
87
88 pub fn list_objects(&self, container: &str) -> HandlerResult<BlobList> {
90 let cmd = Container {
91 id: container.to_string(),
92 };
93 host_call(
94 &self.binding,
95 CAPID_BLOBSTORE,
96 OP_LIST_OBJECTS,
97 &serialize(cmd)?,
98 )
99 .map(|v| deserialize::<BlobList>(v.as_ref()).unwrap())
100 .map_err(|e| e.into())
101 }
102
103 pub fn get_blob_info(&self, container: &str, id: &str) -> HandlerResult<Option<Blob>> {
105 let cmd = Blob {
106 id: id.to_string(),
107 container: container.to_string(),
108 byte_size: 0,
109 };
110 host_call(
111 &self.binding,
112 CAPID_BLOBSTORE,
113 OP_GET_OBJECT_INFO,
114 &serialize(cmd)?,
115 )
116 .map(|v| {
117 let b = deserialize::<Blob>(v.as_ref()).unwrap();
118 if b.id.is_empty() {
119 None
120 } else {
121 Some(b)
122 }
123 })
124 .map_err(|e| e.into())
125 }
126
127 pub fn start_upload(
132 &self,
133 blob: &Blob,
134 chunk_size: u64,
135 total_bytes: u64,
136 ) -> HandlerResult<Transfer> {
137 let transfer = Transfer {
138 blob_id: blob.id.to_string(),
139 container: blob.container.to_string(),
140 chunk_size,
141 total_size: total_bytes,
142 total_chunks: total_bytes / chunk_size,
143 context: None,
144 };
145 let cmd = FileChunk {
146 sequence_no: 0,
147 container: blob.container.to_string(),
148 id: blob.id.to_string(),
149 chunk_size,
150 total_bytes,
151 chunk_bytes: vec![],
152 context: None,
153 };
154 host_call(
155 &self.binding,
156 CAPID_BLOBSTORE,
157 OP_START_UPLOAD,
158 &serialize(cmd)?,
159 )
160 .map(|_v| transfer)
161 .map_err(|e| e.into())
162 }
163
164 pub fn upload_chunk(
167 &self,
168 transfer: &Transfer,
169 offset: u64,
170 bytes: &[u8],
171 ) -> crate::HandlerResult<()> {
172 let cmd = FileChunk {
173 id: transfer.blob_id.to_string(),
174 container: transfer.container.to_string(),
175 sequence_no: offset,
176 chunk_size: transfer.chunk_size,
177 total_bytes: transfer.total_size,
178 chunk_bytes: bytes.to_vec(),
179 context: None,
180 };
181 host_call(
182 &self.binding,
183 CAPID_BLOBSTORE,
184 OP_UPLOAD_CHUNK,
185 &serialize(cmd)?,
186 )
187 .map(|_v| ())
188 .map_err(|e| e.into())
189 }
190
191 pub fn start_download(
195 &self,
196 blob: &Blob,
197 chunk_size: u64,
198 context: Option<String>,
199 ) -> crate::HandlerResult<Transfer> {
200 let transfer = Transfer {
201 blob_id: blob.id.to_string(),
202 container: blob.container.to_string(),
203 chunk_size,
204 total_size: blob.byte_size,
205 total_chunks: blob.byte_size / chunk_size,
206 context: context.clone(),
207 };
208 let cmd = StreamRequest {
209 container: blob.container.to_string(),
210 id: blob.id.to_string(),
211 chunk_size,
212 context,
213 };
214 host_call(
215 &self.binding,
216 CAPID_BLOBSTORE,
217 OP_START_DOWNLOAD,
218 &serialize(cmd)?,
219 )
220 .map(|_v| transfer)
221 .map_err(|e| e.into())
222 }
223}