1pub mod blocks;
18pub mod errors;
19pub mod pins;
20pub mod routing;
21
22use std::collections::HashMap;
23use std::future::Future;
24use std::pin::Pin;
25use std::sync::Arc;
26
27use async_trait::async_trait;
28use bytes::Bytes;
29use cid::Cid;
30use futures::Stream;
31use libp2p::Swarm;
32use serde::{Deserialize, Serialize};
33use tokio::sync::{broadcast, Mutex};
34use trust_dns_resolver::TokioAsyncResolver;
35
36pub use blocks::*;
37pub use errors::*;
38pub use pins::*;
39pub use routing::*;
40
41pub type AwaitIterable<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
43
44pub type Await<T> = Pin<Box<dyn Future<Output = T> + Send>>;
46
47#[derive(Debug, Default)]
49pub struct AbortOptions {
50 }
53
54impl Clone for AbortOptions {
55 fn clone(&self) -> Self {
56 Self::default()
58 }
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct ProgressEvent<T> {
64 pub event_type: String,
65 pub detail: T,
66}
67
68pub struct ProgressOptions<T> {
70 pub on_progress: Option<Box<dyn Fn(ProgressEvent<T>) + Send + Sync>>,
72}
73
74impl<T> std::fmt::Debug for ProgressOptions<T> {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 f.debug_struct("ProgressOptions")
77 .field(
78 "on_progress",
79 &self.on_progress.as_ref().map(|_| "Some(closure)"),
80 )
81 .finish()
82 }
83}
84
85impl<T> Default for ProgressOptions<T> {
86 fn default() -> Self {
87 Self { on_progress: None }
88 }
89}
90
91impl<T> Clone for ProgressOptions<T> {
92 fn clone(&self) -> Self {
93 Self::default()
95 }
96}
97
98#[async_trait]
100pub trait CodecLoader: Send + Sync {
101 async fn load_codec(&self, code: u64) -> Result<Box<dyn Codec>, HeliaError>;
103}
104
105#[async_trait]
107pub trait HasherLoader: Send + Sync {
108 async fn load_hasher(&self, code: u64) -> Result<Box<dyn Hasher>, HeliaError>;
110}
111
112#[async_trait]
114pub trait Codec: Send + Sync {
115 async fn encode(&self, data: &[u8]) -> Result<Bytes, HeliaError>;
117
118 async fn decode(&self, data: &[u8]) -> Result<Bytes, HeliaError>;
120
121 fn code(&self) -> u64;
123}
124
125#[async_trait]
127pub trait Hasher: Send + Sync {
128 async fn hash(&self, data: &[u8]) -> Result<multihash::Multihash<64>, HeliaError>;
130
131 fn code(&self) -> u64;
133}
134
135#[derive(Debug, Clone)]
137pub enum HeliaEvent {
138 Start,
140 Stop,
142 GcStarted,
144 GcCompleted,
146}
147
148pub type HeliaEventReceiver = broadcast::Receiver<HeliaEvent>;
150
151#[derive(Debug)]
153pub struct GcOptions {
154 pub abort: AbortOptions,
156 pub progress: ProgressOptions<GcEvent>,
158}
159
160impl Default for GcOptions {
161 fn default() -> Self {
162 Self {
163 abort: AbortOptions::default(),
164 progress: ProgressOptions::default(),
165 }
166 }
167}
168
169impl Clone for GcOptions {
170 fn clone(&self) -> Self {
171 Self {
172 abort: self.abort.clone(),
173 progress: self.progress.clone(),
174 }
175 }
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize)]
180pub enum GcEvent {
181 Deleted(Cid),
183 Error(String),
185}
186
187pub trait ComponentLogger: Send + Sync {
189 fn debug(&self, message: &str);
191 fn info(&self, message: &str);
193 fn warn(&self, message: &str);
195 fn error(&self, message: &str);
197}
198
199#[async_trait]
201pub trait Metrics: Send + Sync {
202 async fn record_counter(&self, name: &str, value: u64, labels: HashMap<String, String>);
204
205 async fn record_gauge(&self, name: &str, value: f64, labels: HashMap<String, String>);
207
208 async fn record_histogram(&self, name: &str, value: f64, labels: HashMap<String, String>);
210}
211
212#[async_trait]
214pub trait Helia: Send + Sync {
215 fn blockstore(&self) -> &dyn Blocks;
217
218 fn datastore(&self) -> &dyn Datastore;
220
221 fn pins(&self) -> &dyn Pins;
223
224 fn logger(&self) -> &dyn ComponentLogger;
226
227 fn routing(&self) -> &dyn Routing;
229
230 fn dns(&self) -> &TokioAsyncResolver;
232
233 fn metrics(&self) -> Option<&dyn Metrics>;
235
236 fn subscribe_events(&self) -> HeliaEventReceiver;
260
261 async fn start(&self) -> Result<(), HeliaError>;
263
264 async fn stop(&self) -> Result<(), HeliaError>;
266
267 async fn gc(&self, options: Option<GcOptions>) -> Result<(), HeliaError>;
269
270 async fn get_codec(&self, code: u64) -> Result<Box<dyn Codec>, HeliaError>;
272
273 async fn get_hasher(&self, code: u64) -> Result<Box<dyn Hasher>, HeliaError>;
275}
276
277#[async_trait]
279pub trait HeliaWithLibp2p<T>: Helia
280where
281 T: libp2p::swarm::NetworkBehaviour + Send + 'static,
282{
283 fn libp2p(&self) -> Arc<Mutex<Swarm<T>>>;
285}
286
287#[async_trait]
289pub trait Datastore: Send + Sync {
290 async fn get(&self, key: &[u8]) -> Result<Option<Bytes>, HeliaError>;
292
293 async fn put(&self, key: &[u8], value: Bytes) -> Result<(), HeliaError>;
295
296 async fn delete(&self, key: &[u8]) -> Result<(), HeliaError>;
298
299 async fn has(&self, key: &[u8]) -> Result<bool, HeliaError>;
301
302 async fn query(&self, prefix: Option<&[u8]>) -> Result<AwaitIterable<Bytes>, HeliaError>;
304}