Skip to main content

cloudillo_types/
blob_adapter.rs

1// SPDX-FileCopyrightText: Szilárd Hajba
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4//! Adapter that manages and stores blobs (immutable file data)
5use async_trait::async_trait;
6use axum::body::Bytes;
7use futures_core::Stream;
8use std::{fmt::Debug, pin::Pin};
9use tokio::io::AsyncRead;
10
11use crate::prelude::*;
12
13#[derive(Clone, Default)]
14pub struct CreateBlobOptions {}
15
16pub struct BlobStat {
17	pub size: u64,
18}
19
20#[async_trait]
21pub trait BlobAdapter: Debug + Send + Sync {
22	/// Creates a new blob from a buffer
23	async fn create_blob_buf(
24		&self,
25		tn_id: TnId,
26		file_id: &str,
27		data: &[u8],
28		opts: &CreateBlobOptions,
29	) -> ClResult<()>;
30
31	/// Creates a new blob using a stream
32	async fn create_blob_stream(
33		&self,
34		tn_id: TnId,
35		file_id: &str,
36		stream: &mut (dyn AsyncRead + Send + Unpin),
37	) -> ClResult<()>;
38
39	/// Stats a blob
40	async fn stat_blob(&self, tn_id: TnId, blob_id: &str) -> Option<u64>;
41
42	/// Reads a blob
43	async fn read_blob_buf(&self, tn_id: TnId, blob_id: &str) -> ClResult<Box<[u8]>>;
44
45	/// Reads a byte range from a blob
46	async fn read_blob_range(
47		&self,
48		tn_id: TnId,
49		blob_id: &str,
50		offset: u64,
51		length: u64,
52	) -> ClResult<Box<[u8]>>;
53
54	/// Reads a blob
55	async fn read_blob_stream(
56		&self,
57		tn_id: TnId,
58		blob_id: &str,
59	) -> ClResult<Pin<Box<dyn Stream<Item = Result<Bytes, std::io::Error>> + Send>>>;
60}
61
62// vim: ts=4