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
use crate::block::Block;
use crate::cid::Cid;
use crate::codec::{Codec, Decode};
use crate::error::Result;
use crate::ipld::Ipld;
use crate::multihash::{MultihashDigest, U64};
use crate::path::DagPath;
use async_trait::async_trait;
pub trait StoreParams: std::fmt::Debug + Clone + Send + Sync + Unpin + 'static {
type Hashes: MultihashDigest<AllocSize = U64>;
type Codecs: Codec;
const MAX_BLOCK_SIZE: usize;
}
#[derive(Clone, Debug, Default)]
pub struct DefaultParams;
impl StoreParams for DefaultParams {
const MAX_BLOCK_SIZE: usize = 1_048_576;
type Codecs = crate::IpldCodec;
type Hashes = crate::multihash::Code;
}
#[async_trait]
pub trait Store: Clone + Send + Sync {
type Params: StoreParams;
async fn get(&self, cid: &Cid) -> Result<Block<Self::Params>>;
async fn insert(&self, block: &Block<Self::Params>) -> Result<()>;
async fn query(&self, path: &DagPath<'_>) -> Result<Ipld>
where
Ipld: Decode<<Self::Params as StoreParams>::Codecs>,
{
let mut ipld = self.get(path.root()).await?.ipld()?;
for segment in path.path().iter() {
ipld = ipld.take(segment)?;
if let Ipld::Link(cid) = ipld {
ipld = self.get(&cid).await?.ipld()?;
}
}
Ok(ipld)
}
async fn alias<T: AsRef<[u8]> + Send + Sync>(&self, alias: T, cid: Option<&Cid>) -> Result<()>;
async fn resolve<T: AsRef<[u8]> + Send + Sync>(&self, alias: T) -> Result<Option<Cid>>;
}
#[macro_export]
macro_rules! alias {
($name:ident) => {
concat!(module_path!(), "::", stringify!($name))
};
}
pub fn dyn_alias(alias: &'static str, id: u64) -> String {
let mut alias = alias.to_string();
alias.push_str("::");
alias.push_str(&id.to_string());
alias
}
#[cfg(test)]
mod tests {
use super::*;
use libipld_macro::ipld;
mod aliases {
pub const CHAIN_ALIAS: &str = alias!(CHAIN_ALIAS);
}
#[test]
fn test_alias() {
assert_eq!(alias!(test_alias), "libipld::store::tests::test_alias");
assert_eq!(
aliases::CHAIN_ALIAS,
"libipld::store::tests::aliases::CHAIN_ALIAS"
);
assert_eq!(
dyn_alias(aliases::CHAIN_ALIAS, 3).as_str(),
"libipld::store::tests::aliases::CHAIN_ALIAS::3"
);
}
#[async_std::test]
async fn test_query() -> Result<()> {
use crate::cbor::DagCborCodec;
use crate::mem::MemStore;
use crate::multihash::Code;
let store = MemStore::<DefaultParams>::default();
let leaf = ipld!({"name": "John Doe"});
let leaf_block = Block::encode(DagCborCodec, Code::Blake3_256, &leaf).unwrap();
let root = ipld!({ "list": [leaf_block.cid()] });
store.insert(&leaf_block).await.unwrap();
let root_block = Block::encode(DagCborCodec, Code::Blake3_256, &root).unwrap();
store.insert(&root_block).await.unwrap();
let path = DagPath::new(root_block.cid(), "list/0/name");
assert_eq!(
store.query(&path).await.unwrap(),
Ipld::String("John Doe".to_string())
);
Ok(())
}
}