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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use std::fmt::Debug;
use serde::{Deserialize, Serialize};
use crate::{
document::Document,
schema::{Collection, CollectionName, InvalidNameError, Name, ViewName},
};
pub mod map;
use map::{Key, Map, MappedValue};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("error deserializing document {0}")]
Serialization(#[from] serde_cbor::Error),
#[error("error serializing view keys {0}")]
KeySerialization(anyhow::Error),
#[error("reduce is unimplemented")]
ReduceUnimplemented,
}
pub type MapResult<K = (), V = ()> = Result<Option<Map<K, V>>, Error>;
pub trait View: Send + Sync + Debug + 'static {
type Collection: Collection;
type Key: Key + 'static;
type Value: Serialize + for<'de> Deserialize<'de> + Send + Sync;
fn unique(&self) -> bool {
false
}
fn version(&self) -> u64;
fn name(&self) -> Result<Name, InvalidNameError>;
fn view_name(&self) -> Result<ViewName, InvalidNameError> {
Ok(ViewName {
collection: Self::Collection::collection_name()?,
name: self.name()?,
})
}
fn map(&self, document: &Document<'_>) -> MapResult<Self::Key, Self::Value>;
#[allow(unused_variables)]
fn reduce(
&self,
mappings: &[MappedValue<Self::Key, Self::Value>],
rereduce: bool,
) -> Result<Self::Value, Error> {
Err(Error::ReduceUnimplemented)
}
}
pub enum SerializableValue<'a, T: Serialize> {
Owned(T),
Borrowed(&'a T),
}
impl<'a, T> From<&'a T> for SerializableValue<'a, T>
where
T: Serialize,
{
fn from(other: &'a T) -> SerializableValue<'a, T> {
SerializableValue::Borrowed(other)
}
}
impl<'a, T> AsRef<T> for SerializableValue<'a, T>
where
T: Serialize,
{
fn as_ref(&self) -> &T {
match self {
Self::Owned(value) => value,
Self::Borrowed(value) => value,
}
}
}
pub trait Serialized: Send + Sync + Debug {
fn collection(&self) -> Result<CollectionName, InvalidNameError>;
fn unique(&self) -> bool;
fn version(&self) -> u64;
fn view_name(&self) -> Result<ViewName, InvalidNameError>;
fn map(&self, document: &Document<'_>) -> Result<Option<map::Serialized>, Error>;
fn reduce(&self, mappings: &[(&[u8], &[u8])], rereduce: bool) -> Result<Vec<u8>, Error>;
}
#[allow(clippy::use_self)]
impl<T> Serialized for T
where
T: View,
<T as View>::Key: 'static,
{
fn collection(&self) -> Result<CollectionName, InvalidNameError> {
<<Self as View>::Collection as Collection>::collection_name()
}
fn unique(&self) -> bool {
self.unique()
}
fn version(&self) -> u64 {
self.version()
}
fn view_name(&self) -> Result<ViewName, InvalidNameError> {
self.view_name()
}
fn map(&self, document: &Document<'_>) -> Result<Option<map::Serialized>, Error> {
let map = self.map(document)?;
map.map(|map| map.serialized()).transpose()
}
fn reduce(&self, mappings: &[(&[u8], &[u8])], rereduce: bool) -> Result<Vec<u8>, Error> {
let mappings = mappings
.iter()
.map(
|(key, value)| match <T::Key as Key>::from_big_endian_bytes(key) {
Ok(key) => match serde_cbor::from_slice::<T::Value>(value) {
Ok(value) => Ok(MappedValue { key, value }),
Err(err) => Err(Error::from(err)),
},
Err(err) => Err(Error::KeySerialization(err)),
},
)
.collect::<Result<Vec<_>, Error>>()?;
let reduced_value = match self.reduce(&mappings, rereduce) {
Ok(value) => value,
Err(Error::ReduceUnimplemented) => return Ok(Vec::new()),
Err(other) => return Err(other),
};
serde_cbor::to_vec(&reduced_value).map_err(Error::from)
}
}