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
use std::{marker::PhantomData, ops::Range};
use async_trait::async_trait;
use serde::Serialize;
use crate::{
document::{Document, Header},
schema::{self, map},
transaction::{self, OperationResult, Transaction},
Error,
};
#[async_trait]
pub trait Connection<'a>: Send + Sync {
fn collection<C: schema::Collection + 'static>(
&'a self,
) -> Result<Collection<'a, Self, C>, Error>
where
Self: Sized;
async fn insert<C: schema::Collection>(&self, contents: Vec<u8>) -> Result<Header, Error>;
async fn update(&self, doc: &mut Document<'_>) -> Result<(), Error>;
async fn get<C: schema::Collection>(&self, id: u64)
-> Result<Option<Document<'static>>, Error>;
#[must_use]
fn view<V: schema::View>(&'a self) -> View<'a, Self, V>
where
Self: Sized,
{
View::new(self)
}
#[must_use]
async fn query<'k, V: schema::View>(
&self,
query: View<'a, Self, V>,
) -> Result<Vec<map::Serialized>, Error>
where
Self: Sized;
async fn apply_transaction(
&self,
transaction: Transaction<'static>,
) -> Result<Vec<OperationResult>, Error>;
async fn list_executed_transactions(
&self,
starting_id: Option<u64>,
result_limit: Option<usize>,
) -> Result<Vec<transaction::Executed<'static>>, Error>;
}
pub struct Collection<'a, Cn, Cl> {
connection: &'a Cn,
_phantom: PhantomData<Cl>,
}
impl<'a, Cn, Cl> Collection<'a, Cn, Cl>
where
Cn: Connection<'a>,
Cl: schema::Collection,
{
pub fn new(connection: &'a Cn) -> Self {
Self {
connection,
_phantom: PhantomData::default(),
}
}
pub async fn push<S: Serialize + Sync>(&self, item: &S) -> Result<Header, crate::Error> {
let contents = serde_cbor::to_vec(item)?;
Ok(self.connection.insert::<Cl>(contents).await?)
}
pub async fn get(&self, id: u64) -> Result<Option<Document<'static>>, Error> {
self.connection.get::<Cl>(id).await
}
}
pub struct View<'a, Cn, V: schema::View> {
connection: &'a Cn,
pub key: Option<QueryKey<V::MapKey>>,
}
impl<'a, Cn, V> View<'a, Cn, V>
where
V: schema::View,
Cn: Connection<'a>,
{
fn new(connection: &'a Cn) -> Self {
Self {
connection,
key: None,
}
}
#[must_use]
pub fn with_key(mut self, key: V::MapKey) -> Self {
self.key = Some(QueryKey::Matches(key));
self
}
#[must_use]
pub fn with_keys(mut self, keys: Vec<V::MapKey>) -> Self {
self.key = Some(QueryKey::Multiple(keys));
self
}
#[must_use]
pub fn with_key_range(mut self, range: Range<V::MapKey>) -> Self {
self.key = Some(QueryKey::Range(range));
self
}
pub async fn query(self) -> Result<Vec<map::Serialized>, Error> {
self.connection.query(self).await
}
}
pub enum QueryKey<K> {
Matches(K),
Range(Range<K>),
Multiple(Vec<K>),
}