wascc_actor/
objectstore.rs

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
14/// An abstraction around a host runtime capability for a key-value store
15pub 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
27/// Creates a named host binding for the `wascc:objectstore` capability
28pub fn host(binding: &str) -> ObjectStoreHostBinding {
29    ObjectStoreHostBinding {
30        binding: binding.to_string(),
31    }
32}
33
34/// Creates the default host binding for the `wascc:objectstore` capability
35pub fn default() -> ObjectStoreHostBinding {
36    ObjectStoreHostBinding::default()
37}
38
39impl ObjectStoreHostBinding {
40    /// Creates a new container within the store
41    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    /// Removes a container from the store. Whether or not this will fail if the container
56    /// has items may be specific to a given provider implementation.
57    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    /// Removes an object from a container
72    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    /// Lists all objects within a container
89    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    /// Obtains binary object metadata, does not include the object bytes
104    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    /// Indicates that an upload is about to begin for an item. You should follow this
128    /// call up with a for loop/iteration that sends successive chunks to the store. The chunk
129    /// size specified in this call is a request or suggestion. It is up to the provider to determine
130    /// the actual chunk size, which is returned in the resulting `Transfer` instance
131    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    /// Uploads an individual chunk of a file to the blob store. This call must only ever
165    /// come after signaling the start of a new upload with the `start_upload` function.
166    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    /// Sends a request to the provider to begin a chunked download of a file. If this
192    /// succeeds, your actor will begin receiving `OP_RECEIVE_CHUNK` messages from the
193    /// provider.
194    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}