Skip to content

Commit 43338bd

Browse files
Add example with Reduction
1 parent fedad66 commit 43338bd

File tree

2 files changed

+158
-14
lines changed

2 files changed

+158
-14
lines changed

README.md

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,21 @@ Quick links to key API documentation:
5252

5353
### Basic Example
5454

55-
In this example, we create a Storage of puppies from old American comic strips.
55+
In this example, perform some basic operations on puppies from old American comic strips.
5656

5757
```rust
5858
use retriever::prelude::*;
5959
use std::borrow::Cow;
6060

61+
// Each Puppy has a name and age.
6162
struct Puppy {
6263
name: String,
6364
age: u64,
6465
}
6566

67+
// Use the Puppy's name as it's key.
68+
// Using () as the ChunkKey effectively disables chunking;
69+
// this is recommended if you aren't sure what chunk key to use.
6670
impl Record<(),str> for Puppy {
6771
fn chunk_key(&self) -> Cow<()> {
6872
Cow::Owned(())
@@ -90,19 +94,87 @@ storage.add(Puppy {
9094
age: 66
9195
});
9296

97+
// Look up the age of a Puppy
9398
assert_eq!(
94-
Some(52),
95-
storage.get(&ID.item("Odie")).map(|puppy| puppy.age)
99+
52,
100+
storage.get(&ID.item("Odie")).unwrap().age
96101
);
97102

103+
// Count the number of puppies older than 60 years.
98104
assert_eq!(
99-
3,
100-
storage.query(Everything).count()
105+
2,
106+
storage.query(Everything.filter(|puppy: &Puppy| puppy.age > 60)).count()
101107
);
102108

103-
assert_eq!(
109+
```
110+
111+
### Summarizing a storage with a Reduction
112+
113+
In this example, each puppy has some number of bones, tennis balls, and squeaks.
114+
Use a `Reduction` to maintain a total count of these items. A `Reduction` can efficiently
115+
recalculate these totals whenever the `Storage` changes.
116+
117+
```rust
118+
use retriever::prelude::*;
119+
use std::borrow::Cow;
120+
121+
struct Puppy {
122+
name: String,
123+
toys: Toys,
124+
}
125+
126+
#[derive(Clone,Copy,Debug,Default,Eq,PartialEq)]
127+
struct Toys {
128+
bones: u64,
129+
tennis_balls: u64,
130+
squeaks: u64,
131+
}
132+
133+
impl Record<(),str> for Puppy {
134+
fn chunk_key(&self) -> Cow<()> {
135+
Cow::Owned(())
136+
}
137+
138+
fn item_key(&self) -> Cow<str> {
139+
Cow::Borrowed(&self.name)
140+
}
141+
}
142+
143+
let mut storage : Storage<(),str,Puppy> = Storage::new();
144+
let mut reduction : Reduction<(),Puppy,Toys> = Reduction::new(
145+
&storage,
104146
2,
105-
storage.query(Everything.filter(|puppy: &Puppy| puppy.age > 60)).count()
147+
|puppy: &Puppy, _| Some(puppy.toys),
148+
|toys: &[Toys], _| Some(Toys {
149+
bones: toys.iter().map(|toys| toys.bones).sum::<u64>(),
150+
tennis_balls: toys.iter().map(|toys| toys.tennis_balls).sum::<u64>(),
151+
squeaks: toys.iter().map(|toys| toys.squeaks).sum::<u64>(),
152+
})
153+
);
154+
155+
storage.add(Puppy {
156+
name: "Lazy".to_string(),
157+
toys: Toys { bones: 3, tennis_balls: 0, squeaks: 1 }
158+
});
159+
160+
storage.add(Puppy {
161+
name: "Toby".to_string(),
162+
toys: Toys { bones: 0, tennis_balls: 9, squeaks: 0 }
163+
});
164+
165+
storage.add(Puppy {
166+
name: "Ralph".to_string(),
167+
toys: Toys { bones: 0, tennis_balls: 0, squeaks: 3 }
168+
});
169+
170+
storage.add(Puppy {
171+
name: "Larry".to_string(),
172+
toys: Toys { bones: 1, tennis_balls: 0, squeaks: 2 }
173+
});
174+
175+
assert_eq!(
176+
&Toys { bones: 4, tennis_balls: 9, squeaks: 6 },
177+
reduction.reduce(&storage).unwrap()
106178
);
107179

108180
```

src/lib.rs

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,21 @@
5454
//!
5555
//! ## Basic Example
5656
//!
57-
//! In this example, we create a Storage of puppies from old American comic strips.
57+
//! In this example, perform some basic operations on puppies from old American comic strips.
5858
//!
5959
//! ```
6060
//! use retriever::prelude::*;
6161
//! use std::borrow::Cow;
6262
//!
63+
//! // Each Puppy has a name and age.
6364
//! struct Puppy {
6465
//! name: String,
6566
//! age: u64,
6667
//! }
6768
//!
69+
//! // Use the Puppy's name as it's key.
70+
//! // Using () as the ChunkKey effectively disables chunking;
71+
//! // this is recommended if you aren't sure what chunk key to use.
6872
//! impl Record<(),str> for Puppy {
6973
//! fn chunk_key(&self) -> Cow<()> {
7074
//! Cow::Owned(())
@@ -92,19 +96,87 @@
9296
//! age: 66
9397
//! });
9498
//!
99+
//! // Look up the age of a Puppy
95100
//! assert_eq!(
96-
//! Some(52),
97-
//! storage.get(&ID.item("Odie")).map(|puppy| puppy.age)
101+
//! 52,
102+
//! storage.get(&ID.item("Odie")).unwrap().age
98103
//! );
99104
//!
105+
//! // Count the number of puppies older than 60 years.
100106
//! assert_eq!(
101-
//! 3,
102-
//! storage.query(Everything).count()
107+
//! 2,
108+
//! storage.query(Everything.filter(|puppy: &Puppy| puppy.age > 60)).count()
103109
//! );
104110
//!
105-
//! assert_eq!(
111+
//! ```
112+
//!
113+
//! ## Summarizing a storage with a Reduction
114+
//!
115+
//! In this example, each puppy has some number of bones, tennis balls, and squeaks.
116+
//! Use a `Reduction` to maintain a total count of these items. A `Reduction` can efficiently
117+
//! recalculate these totals whenever the `Storage` changes.
118+
//!
119+
//! ```
120+
//! use retriever::prelude::*;
121+
//! use std::borrow::Cow;
122+
//!
123+
//! struct Puppy {
124+
//! name: String,
125+
//! toys: Toys,
126+
//! }
127+
//!
128+
//! #[derive(Clone,Copy,Debug,Default,Eq,PartialEq)]
129+
//! struct Toys {
130+
//! bones: u64,
131+
//! tennis_balls: u64,
132+
//! squeaks: u64,
133+
//! }
134+
//!
135+
//! impl Record<(),str> for Puppy {
136+
//! fn chunk_key(&self) -> Cow<()> {
137+
//! Cow::Owned(())
138+
//! }
139+
//!
140+
//! fn item_key(&self) -> Cow<str> {
141+
//! Cow::Borrowed(&self.name)
142+
//! }
143+
//! }
144+
//!
145+
//! let mut storage : Storage<(),str,Puppy> = Storage::new();
146+
//! let mut reduction : Reduction<(),Puppy,Toys> = Reduction::new(
147+
//! &storage,
106148
//! 2,
107-
//! storage.query(Everything.filter(|puppy: &Puppy| puppy.age > 60)).count()
149+
//! |puppy: &Puppy, _| Some(puppy.toys),
150+
//! |toys: &[Toys], _| Some(Toys {
151+
//! bones: toys.iter().map(|toys| toys.bones).sum::<u64>(),
152+
//! tennis_balls: toys.iter().map(|toys| toys.tennis_balls).sum::<u64>(),
153+
//! squeaks: toys.iter().map(|toys| toys.squeaks).sum::<u64>(),
154+
//! })
155+
//! );
156+
//!
157+
//! storage.add(Puppy {
158+
//! name: "Lazy".to_string(),
159+
//! toys: Toys { bones: 3, tennis_balls: 0, squeaks: 1 }
160+
//! });
161+
//!
162+
//! storage.add(Puppy {
163+
//! name: "Toby".to_string(),
164+
//! toys: Toys { bones: 0, tennis_balls: 9, squeaks: 0 }
165+
//! });
166+
//!
167+
//! storage.add(Puppy {
168+
//! name: "Ralph".to_string(),
169+
//! toys: Toys { bones: 0, tennis_balls: 0, squeaks: 3 }
170+
//! });
171+
//!
172+
//! storage.add(Puppy {
173+
//! name: "Larry".to_string(),
174+
//! toys: Toys { bones: 1, tennis_balls: 0, squeaks: 2 }
175+
//! });
176+
//!
177+
//! assert_eq!(
178+
//! &Toys { bones: 4, tennis_balls: 9, squeaks: 6 },
179+
//! reduction.reduce(&storage).unwrap()
108180
//! );
109181
//!
110182
//! ```

0 commit comments

Comments
 (0)