forked from ucan-wg/rs-ucan
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.rs
134 lines (118 loc) · 3.71 KB
/
store.rs
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
//! Storage for [`Invocation`]s.
use super::Invocation;
use crate::ability;
use crate::{crypto::varsig, did::Did};
use libipld_core::{cid::Cid, codec::Codec};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{collections::BTreeMap, convert::Infallible};
pub trait Store<T, DID: Did, V: varsig::Header<C>, C: Codec + Into<u64> + TryFrom<u64>> {
type InvocationStoreError;
fn get(
&self,
cid: Cid,
) -> Result<Option<Arc<Invocation<T, DID, V, C>>>, Self::InvocationStoreError>;
fn put(
&self,
cid: Cid,
invocation: Invocation<T, DID, V, C>,
) -> Result<(), Self::InvocationStoreError>;
fn has(&self, cid: Cid) -> Result<bool, Self::InvocationStoreError> {
Ok(self.get(cid).is_ok())
}
}
impl<
S: Store<T, DID, V, C>,
T,
DID: Did,
V: varsig::Header<C>,
C: Codec + Into<u64> + TryFrom<u64>,
> Store<T, DID, V, C> for &S
{
type InvocationStoreError = <S as Store<T, DID, V, C>>::InvocationStoreError;
fn get(
&self,
cid: Cid,
) -> Result<
Option<Arc<Invocation<T, DID, V, C>>>,
<S as Store<T, DID, V, C>>::InvocationStoreError,
> {
(**self).get(cid)
}
fn put(
&self,
cid: Cid,
invocation: Invocation<T, DID, V, C>,
) -> Result<(), <S as Store<T, DID, V, C>>::InvocationStoreError> {
(**self).put(cid, invocation)
}
}
#[derive(Debug, Clone)]
pub struct MemoryStore<
T = crate::ability::preset::Preset,
DID: crate::did::Did = crate::did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec + TryFrom<u64> + Into<u64> = varsig::encoding::Preset,
> {
inner: Arc<RwLock<MemoryStoreInner<T, DID, V, C>>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct MemoryStoreInner<
T = crate::ability::preset::Preset,
DID: crate::did::Did = crate::did::preset::Verifier,
V: varsig::Header<C> = varsig::header::Preset,
C: Codec + TryFrom<u64> + Into<u64> = varsig::encoding::Preset,
> {
store: BTreeMap<Cid, Arc<Invocation<T, DID, V, C>>>,
}
impl<T, DID: Did, V: varsig::Header<Enc>, Enc: Codec + Into<u64> + TryFrom<u64>>
MemoryStore<T, DID, V, Enc>
{
fn read(&self) -> RwLockReadGuard<'_, MemoryStoreInner<T, DID, V, Enc>> {
match self.inner.read() {
Ok(guard) => guard,
Err(poison) => {
// There's no logic errors through lock poisoning in our case
poison.into_inner()
}
}
}
fn write(&self) -> RwLockWriteGuard<'_, MemoryStoreInner<T, DID, V, Enc>> {
match self.inner.write() {
Ok(guard) => guard,
Err(poison) => {
// There's no logic errors through lock poisoning in our case
poison.into_inner()
}
}
}
}
impl<T, DID: Did, V: varsig::Header<Enc>, Enc: Codec + Into<u64> + TryFrom<u64>> Default
for MemoryStore<T, DID, V, Enc>
{
fn default() -> Self {
Self {
inner: Arc::new(RwLock::new(MemoryStoreInner {
store: BTreeMap::new(),
})),
}
}
}
impl<T, DID: Did, V: varsig::Header<Enc>, Enc: Codec + Into<u64> + TryFrom<u64>>
Store<T, DID, V, Enc> for MemoryStore<T, DID, V, Enc>
{
type InvocationStoreError = Infallible;
fn get(
&self,
cid: Cid,
) -> Result<Option<Arc<Invocation<T, DID, V, Enc>>>, Self::InvocationStoreError> {
Ok(self.read().store.get(&cid).cloned())
}
fn put(
&self,
cid: Cid,
invocation: Invocation<T, DID, V, Enc>,
) -> Result<(), Self::InvocationStoreError> {
self.write().store.insert(cid, Arc::new(invocation));
Ok(())
}
}