-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
view-examples.rs
164 lines (140 loc) · 4.58 KB
/
view-examples.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
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
use bonsaidb::{
core::{
connection::Connection,
document::CollectionDocument,
schema::{
view::CollectionViewSchema, Collection, CollectionName, DefaultSerialization,
DefaultViewSerialization, Name, ReduceResult, Schematic, SerializedCollection, View,
ViewMapResult, ViewMappedValue,
},
Error,
},
local::{
config::{Builder, StorageConfiguration},
Database,
},
};
use serde::{Deserialize, Serialize};
// begin rustme snippet: snippet-a
#[derive(Debug, Serialize, Deserialize)]
struct Shape {
pub sides: u32,
}
impl Collection for Shape {
fn collection_name() -> CollectionName {
CollectionName::private("shapes")
}
fn define_views(schema: &mut Schematic) -> Result<(), Error> {
schema.define_view(ShapesByNumberOfSides)
}
}
impl DefaultSerialization for Shape {}
#[derive(Debug, Clone)]
struct ShapesByNumberOfSides;
impl View for ShapesByNumberOfSides {
type Collection = Shape;
type Key = u32;
type Value = usize;
fn name(&self) -> Name {
Name::new("by-number-of-sides")
}
}
impl CollectionViewSchema for ShapesByNumberOfSides {
type View = Self;
fn map(&self, document: CollectionDocument<Shape>) -> ViewMapResult<Self::View> {
Ok(document.emit_key_and_value(document.contents.sides, 1))
}
fn reduce(
&self,
mappings: &[ViewMappedValue<Self>],
_rereduce: bool,
) -> ReduceResult<Self::View> {
Ok(mappings.iter().map(|m| m.value).sum())
}
}
impl DefaultViewSerialization for ShapesByNumberOfSides {}
// end rustme snippet
impl Shape {
fn new(sides: u32) -> Self {
Self { sides }
}
}
#[tokio::main]
async fn main() -> Result<(), bonsaidb::core::Error> {
// begin rustme snippet: snippet-b
let db = Database::open::<Shape>(StorageConfiguration::new("view-examples.bonsaidb")).await?;
// Insert a new document into the Shape collection.
Shape::new(3).push_into(&db).await?;
// end rustme snippet
// Views in `BonsaiDb` are written using a Map/Reduce approach. In this
// example, we take a look at how document mapping can be used to filter and
// retrieve data
//
// Let's start by seeding the database with some shapes of various sizes:
for sides in 3..=20 {
Shape::new(sides).push_into(&db).await?;
}
// And, let's add a few shapes with the same number of sides
Shape::new(3).push_into(&db).await?;
Shape::new(4).push_into(&db).await?;
// At this point, our database should have 3 triangles:
// begin rustme snippet: snippet-c
let triangles = db
.view::<ShapesByNumberOfSides>()
.with_key(3)
.query()
.await?;
println!("Number of triangles: {}", triangles.len());
// end rustme snippet
// What is returned is a list of entries containing the document id
// (source), the key of the entry, and the value of the entry:
println!("Triangles: {:#?}", triangles);
// If you want the associated documents, use query_with_collection_docs:
for entry in &db
.view::<ShapesByNumberOfSides>()
.with_key(3)
.query_with_collection_docs()
.await?
{
println!(
"Shape ID {} has {} sides",
entry.document.header.id, entry.document.contents.sides
);
}
// The reduce() function takes the "values" emitted during the map()
// function, and reduces a list down to a single value. In this example, the
// reduce function is acting as a count. So, if you want to query for the
// number of shapes, we don't need to fetch all the records, we can just
// retrieve the result of the calculation directly.
//
// So, here we're using reduce() to count the number of shapes with 4 sides.
println!(
"Number of quads: {} (expected 2)",
db.view::<ShapesByNumberOfSides>()
.with_key(4)
.reduce()
.await?
);
// Or, 5 shapes that are triangles or quads
println!(
"Number of quads and triangles: {} (expected 5)",
db.view::<ShapesByNumberOfSides>()
.with_keys([3, 4])
.reduce()
.await?
);
// And, 10 shapes that have more than 10 sides
println!(
"Number of shapes with more than 10 sides: {} (expected 10)",
db.view::<ShapesByNumberOfSides>()
.with_key_range(11..u32::MAX)
.reduce()
.await?
);
Ok(())
}
#[test]
fn runs() {
drop(std::fs::remove_dir_all("view-examples.bonsaidb"));
main().unwrap()
}