objstore 0.1.0-alpha.1

Core objstore crate: common traits, types, and APIs used by storage backend implementations.
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
use std::sync::Arc;

use anyhow::Context as _;
use bytes::Bytes;

use crate::{
    Conditions, Copy, DataSource, DownloadUrlArgs, KeyPage, KeyStream, ListArgs, MetaStream,
    ObjectMeta, ObjectMetaPage, Put, ValueStream,
};
use futures::{StreamExt as _, TryStreamExt as _, stream};

/// Abstraction for a generic key-value store.
#[async_trait::async_trait]
pub trait ObjStore: Send + Sync + std::fmt::Debug {
    /// Get a descriptive name for backend implementation.
    ///
    /// eg: "memory", "s3", ...
    fn kind(&self) -> &str;

    /// Get a "safe" URI for the store, which does not include any sensitive information
    /// like api keys.
    fn safe_uri(&self) -> &url::Url;

    /// Checks if the store is usable.
    ///
    /// May perform upstream service requests to validate connectivity and credentials.
    async fn healthcheck(&self) -> Result<(), anyhow::Error>;

    /// Get metadata for a given key.
    async fn meta(&self, key: &str) -> Result<Option<ObjectMeta>, anyhow::Error>;

    /// Get the value for a given key.
    async fn get(&self, key: &str) -> Result<Option<Bytes>, anyhow::Error>;

    async fn get_stream(&self, key: &str) -> Result<Option<ValueStream>, anyhow::Error>;

    /// Get both the value and metadata for a given key.
    async fn get_with_meta(&self, key: &str) -> Result<Option<(Bytes, ObjectMeta)>, anyhow::Error>;

    async fn get_stream_with_meta(
        &self,
        key: &str,
    ) -> Result<Option<(ObjectMeta, ValueStream)>, anyhow::Error>;

    /// Generate a download URL for a given key.
    ///
    /// NOTE: Must return `Ok(None)` if the store does not support download URLs!
    async fn generate_download_url(
        &self,
        args: DownloadUrlArgs,
    ) -> Result<Option<url::Url>, anyhow::Error>;

    /// Store a value under a given key.
    async fn send_put(&self, put: Put) -> Result<ObjectMeta, anyhow::Error>;

    /// Copy an existing object to a new key.
    ///
    /// May apply server-side copy optimizations and respects `Conditions`.
    async fn send_copy(&self, copy: Copy) -> Result<ObjectMeta, anyhow::Error>;

    /// Delete a key from the store.
    async fn delete(&self, key: &str) -> Result<(), anyhow::Error>;

    /// Delete all keys with a given prefix.
    async fn delete_prefix(&self, prefix: &str) -> Result<(), anyhow::Error>;

    /// List keys in the store.
    ///
    /// In contrast to [`Self::list`], this returns only the keys, not their metadata.
    async fn list_keys(&self, args: ListArgs) -> Result<KeyPage, anyhow::Error>;

    /// List all the keys, optionally filtered by a prefix.
    ///
    /// NOTE: this method will paginate through all keys, and accumulates
    /// the results in memory.
    ///
    /// Use with caution.
    async fn list_all_keys(&self, prefix: &str) -> Result<Vec<String>, anyhow::Error> {
        let args = ListArgs::new().with_prefix(prefix);
        self.list_keys_stream(args)
            .map_ok(|v| v.items)
            .try_concat()
            .await
    }

    fn list_keys_stream<'a>(&'a self, args: ListArgs) -> KeyStream<'a> {
        let init = Some(args.clone());
        let page_stream = stream::try_unfold(init, move |state| async move {
            if let Some(args) = state {
                let page = self.list_keys(args.clone()).await?;
                let next = page
                    .next_cursor
                    .as_ref()
                    .map(|c| args.clone().with_cursor(c.clone()));
                Ok(Some((page, next)))
            } else {
                Ok(None)
            }
        });
        Box::pin(page_stream)
    }

    /// List metadata for a given key.
    ///
    /// The arguments allow for prefix filtering, pagination, and limiting
    /// the number of results.
    async fn list(&self, args: ListArgs) -> Result<ObjectMetaPage, anyhow::Error>;

    /// Streaming variant of [`Self::list`]: pages through [`Self::list`] and yields each metadata page (`ObjectMetaPage`).
    ///
    /// This default method repeatedly calls `list` to page through all results lazily.
    fn list_stream(&self, args: ListArgs) -> MetaStream
    where
        Self: Sized + Clone + 'static,
    {
        let store = self.clone();
        let init = Some(args.clone());
        let page_stream = stream::try_unfold(init, move |state| {
            let store = store.clone();
            async move {
                if let Some(args) = state {
                    let page = store.list(args.clone()).await?;
                    let next = page
                        .next_cursor
                        .as_ref()
                        .map(|c| args.clone().with_cursor(c.clone()));
                    Ok(Some((page, next)))
                } else {
                    Ok(None)
                }
            }
        });
        Box::pin(page_stream)
    }

    /// Purge all keys in the store.
    async fn purge_all(&self) -> Result<(), anyhow::Error> {
        self.delete_prefix("").await
    }

    /// Get a JSON value from the store.
    async fn get_json<T: serde::de::DeserializeOwned>(
        &self,
        key: &str,
    ) -> Result<Option<T>, anyhow::Error>
    where
        Self: Sized,
    {
        match self.get(key).await {
            Ok(Some(data)) => {
                let jd = &mut serde_json::Deserializer::from_slice(&data);
                let out =
                    serde_path_to_error::deserialize(jd).context("could not deserialize JSON")?;

                Ok(Some(out))
            }
            Ok(None) => Ok(None),
            Err(e) => Err(e),
        }
    }
}

#[async_trait::async_trait]
impl<K: ObjStore> ObjStore for Arc<K> {
    fn kind(&self) -> &str {
        self.as_ref().kind()
    }

    fn safe_uri(&self) -> &url::Url {
        self.as_ref().safe_uri()
    }

    async fn healthcheck(&self) -> Result<(), anyhow::Error> {
        self.as_ref().healthcheck().await
    }

    async fn meta(&self, key: &str) -> Result<Option<ObjectMeta>, anyhow::Error> {
        self.as_ref().meta(key).await
    }

    async fn get(&self, key: &str) -> Result<Option<Bytes>, anyhow::Error> {
        self.as_ref().get(key).await
    }

    async fn get_stream(&self, key: &str) -> Result<Option<ValueStream>, anyhow::Error> {
        self.as_ref().get_stream(key).await
    }

    async fn get_with_meta(&self, key: &str) -> Result<Option<(Bytes, ObjectMeta)>, anyhow::Error> {
        self.as_ref().get_with_meta(key).await
    }

    async fn get_stream_with_meta(
        &self,
        key: &str,
    ) -> Result<Option<(ObjectMeta, ValueStream)>, anyhow::Error> {
        self.as_ref().get_stream_with_meta(key).await
    }

    async fn generate_download_url(
        &self,
        args: DownloadUrlArgs,
    ) -> Result<Option<url::Url>, anyhow::Error> {
        self.as_ref().generate_download_url(args).await
    }

    async fn send_put(&self, put: Put) -> Result<ObjectMeta, anyhow::Error> {
        self.as_ref().send_put(put).await
    }
    async fn send_copy(&self, copy: Copy) -> Result<ObjectMeta, anyhow::Error> {
        self.as_ref().send_copy(copy).await
    }

    async fn delete(&self, key: &str) -> Result<(), anyhow::Error> {
        self.as_ref().delete(key).await
    }

    async fn delete_prefix(&self, prefix: &str) -> Result<(), anyhow::Error> {
        self.as_ref().delete_prefix(prefix).await
    }

    async fn list(&self, args: ListArgs) -> Result<ObjectMetaPage, anyhow::Error> {
        self.as_ref().list(args).await
    }

    async fn list_keys(&self, args: ListArgs) -> Result<KeyPage, anyhow::Error> {
        self.as_ref().list_keys(args).await
    }
}

pub type DynObjStore = Arc<dyn ObjStore>;

#[async_trait::async_trait]
impl ObjStore for DynObjStore {
    fn kind(&self) -> &str {
        self.as_ref().kind()
    }

    fn safe_uri(&self) -> &url::Url {
        self.as_ref().safe_uri()
    }

    async fn healthcheck(&self) -> Result<(), anyhow::Error> {
        self.as_ref().healthcheck().await
    }

    async fn meta(&self, key: &str) -> Result<Option<ObjectMeta>, anyhow::Error> {
        self.as_ref().meta(key).await
    }

    async fn get(&self, key: &str) -> Result<Option<Bytes>, anyhow::Error> {
        self.as_ref().get(key).await
    }

    async fn get_stream(&self, key: &str) -> Result<Option<ValueStream>, anyhow::Error> {
        self.as_ref().get_stream(key).await
    }

    async fn get_with_meta(&self, key: &str) -> Result<Option<(Bytes, ObjectMeta)>, anyhow::Error> {
        self.as_ref().get_with_meta(key).await
    }

    async fn get_stream_with_meta(
        &self,
        key: &str,
    ) -> Result<Option<(ObjectMeta, ValueStream)>, anyhow::Error> {
        self.as_ref().get_stream_with_meta(key).await
    }

    async fn generate_download_url(
        &self,
        args: DownloadUrlArgs,
    ) -> Result<Option<url::Url>, anyhow::Error> {
        self.as_ref().generate_download_url(args).await
    }

    async fn send_put(&self, put: Put) -> Result<ObjectMeta, anyhow::Error> {
        self.as_ref().send_put(put).await
    }
    async fn send_copy(&self, copy: Copy) -> Result<ObjectMeta, anyhow::Error> {
        self.as_ref().send_copy(copy).await
    }

    async fn delete(&self, key: &str) -> Result<(), anyhow::Error> {
        self.as_ref().delete(key).await
    }

    async fn list(&self, args: ListArgs) -> Result<ObjectMetaPage, anyhow::Error> {
        self.as_ref().list(args).await
    }

    async fn list_keys(&self, args: ListArgs) -> Result<KeyPage, anyhow::Error> {
        self.as_ref().list_keys(args).await
    }

    async fn delete_prefix(&self, prefix: &str) -> Result<(), anyhow::Error> {
        self.as_ref().delete_prefix(prefix).await
    }

    async fn get_json<T: serde::de::DeserializeOwned>(
        &self,
        key: &str,
    ) -> Result<Option<T>, anyhow::Error> {
        match self.get(key).await {
            Ok(Some(data)) => {
                let jd = &mut serde_json::Deserializer::from_slice(&data);
                let out =
                    serde_path_to_error::deserialize(jd).context("could not deserialize JSON")?;

                Ok(Some(out))
            }
            Ok(None) => Ok(None),
            Err(e) => Err(e),
        }
    }
}

pub struct PutBuilder<'a, S> {
    store: &'a S,
    key: String,
    conditions: Conditions,
    /// Specifies the MIME type of the data.
    mime_type: Option<String>,
}

impl<'a, S: ObjStore> PutBuilder<'a, S>
where
    S: ObjStore,
{
    pub fn build(self, data: impl Into<DataSource>) -> Put {
        let mut put = Put::new(self.key, data.into());
        put.conditions = self.conditions;
        put.mime_type = self.mime_type;
        put
    }

    pub async fn json<T: serde::Serialize>(self, data: &T) -> Result<ObjectMeta, anyhow::Error> {
        let data = serde_json::to_vec(data).context("could not serialize JSON data for put")?;
        let store = self.store;
        let put = self.build(DataSource::Data(Bytes::from(data)));
        store.send_put(put).await
    }

    pub async fn send(self, data: impl Into<DataSource>) -> Result<ObjectMeta, anyhow::Error> {
        let store = self.store;
        let put = self.build(data);
        store.send_put(put).await
    }

    pub async fn text(self, text: impl Into<String>) -> Result<ObjectMeta, anyhow::Error> {
        let data = Bytes::from(text.into());
        self.send(DataSource::Data(data)).await
    }

    pub async fn bytes(self, data: impl Into<Bytes>) -> Result<ObjectMeta, anyhow::Error> {
        self.send(DataSource::Data(data.into())).await
    }

    pub async fn stream<D, E>(
        self,
        stream: impl futures::Stream<Item = Result<D, E>> + Send + 'static,
    ) -> Result<ObjectMeta, anyhow::Error>
    where
        Bytes: From<D>,
        anyhow::Error: From<E>,
        E: Send + 'static,
    {
        let stream: ValueStream = stream
            .map_ok(|item: D| Bytes::from(item))
            .map_err(anyhow::Error::from)
            .boxed();

        self.send(DataSource::Stream(stream)).await
    }
}

/// Builder for a copy request from one key to another, respecting conditions.
pub struct CopyBuilder<'a, S> {
    store: &'a S,
    src: String,
    dest: String,
    conditions: Conditions,
}

impl<'a, S: ObjStore> CopyBuilder<'a, S>
where
    S: ObjStore,
{
    /// Construct the underlying `Copy` request.
    pub fn build(&self) -> Copy {
        let mut copy = Copy::new(self.src.clone(), self.dest.clone());
        copy.conditions = self.conditions.clone();
        copy
    }

    /// Execute the copy request.
    pub async fn send(self) -> Result<ObjectMeta, anyhow::Error> {
        let mut copy = Copy::new(self.src.clone(), self.dest.clone());
        copy.conditions = self.conditions.clone();
        self.store.send_copy(copy).await
    }
}

pub trait ObjStoreExt: ObjStore
where
    Self: Sized,
{
    fn put(&self, key: &str) -> PutBuilder<'_, Self> {
        PutBuilder {
            store: self,
            key: key.to_string(),
            conditions: Conditions::default(),
            mime_type: None,
        }
    }

    /// Begin a copy operation from `src` to `dest`, allows setting conditions.
    fn copy(&self, src: &str, dest: &str) -> CopyBuilder<'_, Self> {
        CopyBuilder {
            store: self,
            src: src.to_string(),
            dest: dest.to_string(),
            conditions: Conditions::default(),
        }
    }
}

impl<S: ObjStore> ObjStoreExt for S {}