Struct Database

Source
pub struct Database { /* private fields */ }
Expand description

Stores data in relation instances and implements incremental view maintenance over them.

Example:

use codd::{Database, expression::Select};

// create a new database:
let mut db = Database::new();

// add a new relation "numbers" with tuples of type `u32` to `db`:
let numbers = db.add_relation::<u32>("numbers").unwrap();

// create a view for odd numbers in `numbers`:
let odds = db.store_view(Select::new(numbers.clone(), |i| i % 2 == 1)).unwrap();

// insert some items in `numbers`:
db.insert(&numbers, vec![4, 8, 15, 16, 23, 42].into()).unwrap();

// query `db` for `numbers` and `odds`:
let numbers_data = db.evaluate(&numbers).unwrap();
let odds_data = db.evaluate(&odds).unwrap();

assert_eq!(vec![4, 8, 15, 16, 23, 42], numbers_data.into_tuples());
assert_eq!(vec![15, 23], odds_data.into_tuples());

// go nuts:
db.insert(&numbers, vec![8, 888, 23, 1001, 8008, 101].into()).unwrap();

// query `db` again:
let numbers_data = db.evaluate(&numbers).unwrap();
let odds_data = db.evaluate(&odds).unwrap();

assert_eq!(vec![4, 8, 15, 16, 23, 42, 101, 888, 1001, 8008], numbers_data.into_tuples());
assert_eq!(vec![15, 23, 101, 1001], odds_data.into_tuples());

Implementations§

Source§

impl Database

Source

pub fn new() -> Self

Creates a new empty database.

Examples found in repository?
examples/music.rs (line 35)
34fn main() -> Result<(), Error> {
35    let mut music = Database::new();
36    let musician = music.add_relation("musician")?;
37    let band = music.add_relation("band")?;
38    let song = music.add_relation("song")?;
39    music.insert(
40        &musician,
41        vec![
42            Musician {
43                name: "John Petrucci".into(),
44                band: Some("Dream Theater".into()),
45                instruments: vec![Guitar],
46            },
47            Musician {
48                name: "Taylor Swift".into(),
49                band: None,
50                instruments: vec![Vocals],
51            },
52            Musician {
53                name: "Conor Mason".into(),
54                band: Some("Nothing But Thieves".into()),
55                instruments: vec![Vocals, Guitar],
56            },
57            Musician {
58                name: "Stevie Wonder".into(),
59                band: None,
60                instruments: vec![Vocals, Piano],
61            },
62            Musician {
63                name: "Jordan Rudess".into(),
64                band: Some("Dream Theater".into()),
65                instruments: vec![Keyboard],
66            },
67            Musician {
68                name: "Alex Turner".into(),
69                band: Some("Arctic Monkeys".into()),
70                instruments: vec![Vocals, Guitar, Piano],
71            },
72            Musician {
73                name: "Billie Eilish".into(),
74                band: None,
75                instruments: vec![Vocals, Piano],
76            },
77            Musician {
78                name: "Lars Ulrich".into(),
79                band: Some("Metallica".into()),
80                instruments: vec![Drums],
81            },
82        ]
83        .into(),
84    )?;
85
86    music.insert(
87        &band,
88        vec![
89            Band {
90                name: "Dream Theater".into(),
91                genre: "Progressive Metal".into(),
92            },
93            Band {
94                name: "Nothing But Thieves".into(),
95                genre: "Alternative Rock".into(),
96            },
97            Band {
98                name: "Metallica".into(),
99                genre: "Heavy Metal".into(),
100            },
101            Band {
102                name: "Arctic Monkeys".into(),
103                genre: "Indie Rock".into(),
104            },
105        ]
106        .into(),
107    )?;
108
109    music.insert(
110        &song,
111        vec![
112            Song {
113                title: "pull me under".into(),
114                artist: Either::Right("Dream Theater".into()),
115            },
116            Song {
117                title: "bad guy".into(),
118                artist: Either::Left("Billie Eilish".into()),
119            },
120            Song {
121                title: "excuse me".into(),
122                artist: Either::Left("Nothing But Thieves".into()),
123            },
124            Song {
125                title: "enter sandman".into(),
126                artist: Either::Right("Metallica".into()),
127            },
128            Song {
129                title: "panic attack".into(),
130                artist: Either::Right("Dream Theater".into()),
131            },
132            Song {
133                title: "shake it off".into(),
134                artist: Either::Left("Taylor Swift".into()),
135            },
136            Song {
137                title: "r u mine".into(),
138                artist: Either::Right("Artcic Monkeys".into()),
139            },
140            Song {
141                title: "as I am".into(),
142                artist: Either::Right("Dream Theater".into()),
143            },
144        ]
145        .into(),
146    )?;
147
148    let guitarist_name = musician
149        .builder()
150        .select(|m| m.instruments.contains(&Guitar))
151        .project(|g| g.name.to_string())
152        .build();
153
154    assert_eq!(
155        vec![
156            "Alex Turner".to_string(),
157            "Conor Mason".into(),
158            "John Petrucci".into(),
159        ],
160        music.evaluate(&guitarist_name)?.into_tuples()
161    );
162
163    let dt_member = musician
164        .builder()
165        .with_key(|m| m.band.clone())
166        .join(band.builder().with_key(|b| Some(b.name.clone())))
167        .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168        .select(|m| m.1 == "Dream Theater")
169        .project(|m| m.0.to_string())
170        .build();
171
172    assert_eq!(
173        vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174        music.evaluate(&dt_member)?.into_tuples()
175    );
176
177    let dt_member_view = music.store_view(dt_member)?;
178    let drummer_view = music.store_view(
179        musician
180            .builder()
181            .select(|m| m.instruments.contains(&Drums))
182            .build(),
183    )?;
184
185    music.insert(
186        &musician,
187        vec![
188            Musician {
189                name: "John Myung".into(),
190                band: Some("Dream Theater".into()),
191                instruments: vec![Guitar],
192            },
193            Musician {
194                name: "Mike Mangini".into(),
195                band: Some("Dream Theater".into()),
196                instruments: vec![Drums],
197            },
198        ]
199        .into(),
200    )?;
201
202    assert_eq!(
203        vec![
204            Musician {
205                name: "Lars Ulrich".into(),
206                band: Some("Metallica".into()),
207                instruments: vec![Drums]
208            },
209            Musician {
210                name: "Mike Mangini".into(),
211                band: Some("Dream Theater".into()),
212                instruments: vec![Drums]
213            }
214        ],
215        music.evaluate(&drummer_view)?.into_tuples()
216    );
217    assert_eq!(
218        vec![
219            "John Myung".to_string(),
220            "John Petrucci".into(),
221            "Jordan Rudess".into(),
222            "Mike Mangini".into()
223        ],
224        music.evaluate(&dt_member_view)?.into_tuples()
225    );
226
227    Ok(())
228}
Source

pub fn evaluate<T, E>(&self, expression: &E) -> Result<Tuples<T>, Error>
where T: Tuple, E: ExpressionExt<T>,

Evaluates expression in the database and returns the result in a Tuples object.

Examples found in repository?
examples/music.rs (line 160)
34fn main() -> Result<(), Error> {
35    let mut music = Database::new();
36    let musician = music.add_relation("musician")?;
37    let band = music.add_relation("band")?;
38    let song = music.add_relation("song")?;
39    music.insert(
40        &musician,
41        vec![
42            Musician {
43                name: "John Petrucci".into(),
44                band: Some("Dream Theater".into()),
45                instruments: vec![Guitar],
46            },
47            Musician {
48                name: "Taylor Swift".into(),
49                band: None,
50                instruments: vec![Vocals],
51            },
52            Musician {
53                name: "Conor Mason".into(),
54                band: Some("Nothing But Thieves".into()),
55                instruments: vec![Vocals, Guitar],
56            },
57            Musician {
58                name: "Stevie Wonder".into(),
59                band: None,
60                instruments: vec![Vocals, Piano],
61            },
62            Musician {
63                name: "Jordan Rudess".into(),
64                band: Some("Dream Theater".into()),
65                instruments: vec![Keyboard],
66            },
67            Musician {
68                name: "Alex Turner".into(),
69                band: Some("Arctic Monkeys".into()),
70                instruments: vec![Vocals, Guitar, Piano],
71            },
72            Musician {
73                name: "Billie Eilish".into(),
74                band: None,
75                instruments: vec![Vocals, Piano],
76            },
77            Musician {
78                name: "Lars Ulrich".into(),
79                band: Some("Metallica".into()),
80                instruments: vec![Drums],
81            },
82        ]
83        .into(),
84    )?;
85
86    music.insert(
87        &band,
88        vec![
89            Band {
90                name: "Dream Theater".into(),
91                genre: "Progressive Metal".into(),
92            },
93            Band {
94                name: "Nothing But Thieves".into(),
95                genre: "Alternative Rock".into(),
96            },
97            Band {
98                name: "Metallica".into(),
99                genre: "Heavy Metal".into(),
100            },
101            Band {
102                name: "Arctic Monkeys".into(),
103                genre: "Indie Rock".into(),
104            },
105        ]
106        .into(),
107    )?;
108
109    music.insert(
110        &song,
111        vec![
112            Song {
113                title: "pull me under".into(),
114                artist: Either::Right("Dream Theater".into()),
115            },
116            Song {
117                title: "bad guy".into(),
118                artist: Either::Left("Billie Eilish".into()),
119            },
120            Song {
121                title: "excuse me".into(),
122                artist: Either::Left("Nothing But Thieves".into()),
123            },
124            Song {
125                title: "enter sandman".into(),
126                artist: Either::Right("Metallica".into()),
127            },
128            Song {
129                title: "panic attack".into(),
130                artist: Either::Right("Dream Theater".into()),
131            },
132            Song {
133                title: "shake it off".into(),
134                artist: Either::Left("Taylor Swift".into()),
135            },
136            Song {
137                title: "r u mine".into(),
138                artist: Either::Right("Artcic Monkeys".into()),
139            },
140            Song {
141                title: "as I am".into(),
142                artist: Either::Right("Dream Theater".into()),
143            },
144        ]
145        .into(),
146    )?;
147
148    let guitarist_name = musician
149        .builder()
150        .select(|m| m.instruments.contains(&Guitar))
151        .project(|g| g.name.to_string())
152        .build();
153
154    assert_eq!(
155        vec![
156            "Alex Turner".to_string(),
157            "Conor Mason".into(),
158            "John Petrucci".into(),
159        ],
160        music.evaluate(&guitarist_name)?.into_tuples()
161    );
162
163    let dt_member = musician
164        .builder()
165        .with_key(|m| m.band.clone())
166        .join(band.builder().with_key(|b| Some(b.name.clone())))
167        .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168        .select(|m| m.1 == "Dream Theater")
169        .project(|m| m.0.to_string())
170        .build();
171
172    assert_eq!(
173        vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174        music.evaluate(&dt_member)?.into_tuples()
175    );
176
177    let dt_member_view = music.store_view(dt_member)?;
178    let drummer_view = music.store_view(
179        musician
180            .builder()
181            .select(|m| m.instruments.contains(&Drums))
182            .build(),
183    )?;
184
185    music.insert(
186        &musician,
187        vec![
188            Musician {
189                name: "John Myung".into(),
190                band: Some("Dream Theater".into()),
191                instruments: vec![Guitar],
192            },
193            Musician {
194                name: "Mike Mangini".into(),
195                band: Some("Dream Theater".into()),
196                instruments: vec![Drums],
197            },
198        ]
199        .into(),
200    )?;
201
202    assert_eq!(
203        vec![
204            Musician {
205                name: "Lars Ulrich".into(),
206                band: Some("Metallica".into()),
207                instruments: vec![Drums]
208            },
209            Musician {
210                name: "Mike Mangini".into(),
211                band: Some("Dream Theater".into()),
212                instruments: vec![Drums]
213            }
214        ],
215        music.evaluate(&drummer_view)?.into_tuples()
216    );
217    assert_eq!(
218        vec![
219            "John Myung".to_string(),
220            "John Petrucci".into(),
221            "Jordan Rudess".into(),
222            "Mike Mangini".into()
223        ],
224        music.evaluate(&dt_member_view)?.into_tuples()
225    );
226
227    Ok(())
228}
Source

pub fn add_relation<T>(&mut self, name: &str) -> Result<Relation<T>, Error>
where T: Tuple + 'static,

Adds a new relation instance identified by name to the database and returns a Relation object that can be used to access the instance.

Examples found in repository?
examples/music.rs (line 36)
34fn main() -> Result<(), Error> {
35    let mut music = Database::new();
36    let musician = music.add_relation("musician")?;
37    let band = music.add_relation("band")?;
38    let song = music.add_relation("song")?;
39    music.insert(
40        &musician,
41        vec![
42            Musician {
43                name: "John Petrucci".into(),
44                band: Some("Dream Theater".into()),
45                instruments: vec![Guitar],
46            },
47            Musician {
48                name: "Taylor Swift".into(),
49                band: None,
50                instruments: vec![Vocals],
51            },
52            Musician {
53                name: "Conor Mason".into(),
54                band: Some("Nothing But Thieves".into()),
55                instruments: vec![Vocals, Guitar],
56            },
57            Musician {
58                name: "Stevie Wonder".into(),
59                band: None,
60                instruments: vec![Vocals, Piano],
61            },
62            Musician {
63                name: "Jordan Rudess".into(),
64                band: Some("Dream Theater".into()),
65                instruments: vec![Keyboard],
66            },
67            Musician {
68                name: "Alex Turner".into(),
69                band: Some("Arctic Monkeys".into()),
70                instruments: vec![Vocals, Guitar, Piano],
71            },
72            Musician {
73                name: "Billie Eilish".into(),
74                band: None,
75                instruments: vec![Vocals, Piano],
76            },
77            Musician {
78                name: "Lars Ulrich".into(),
79                band: Some("Metallica".into()),
80                instruments: vec![Drums],
81            },
82        ]
83        .into(),
84    )?;
85
86    music.insert(
87        &band,
88        vec![
89            Band {
90                name: "Dream Theater".into(),
91                genre: "Progressive Metal".into(),
92            },
93            Band {
94                name: "Nothing But Thieves".into(),
95                genre: "Alternative Rock".into(),
96            },
97            Band {
98                name: "Metallica".into(),
99                genre: "Heavy Metal".into(),
100            },
101            Band {
102                name: "Arctic Monkeys".into(),
103                genre: "Indie Rock".into(),
104            },
105        ]
106        .into(),
107    )?;
108
109    music.insert(
110        &song,
111        vec![
112            Song {
113                title: "pull me under".into(),
114                artist: Either::Right("Dream Theater".into()),
115            },
116            Song {
117                title: "bad guy".into(),
118                artist: Either::Left("Billie Eilish".into()),
119            },
120            Song {
121                title: "excuse me".into(),
122                artist: Either::Left("Nothing But Thieves".into()),
123            },
124            Song {
125                title: "enter sandman".into(),
126                artist: Either::Right("Metallica".into()),
127            },
128            Song {
129                title: "panic attack".into(),
130                artist: Either::Right("Dream Theater".into()),
131            },
132            Song {
133                title: "shake it off".into(),
134                artist: Either::Left("Taylor Swift".into()),
135            },
136            Song {
137                title: "r u mine".into(),
138                artist: Either::Right("Artcic Monkeys".into()),
139            },
140            Song {
141                title: "as I am".into(),
142                artist: Either::Right("Dream Theater".into()),
143            },
144        ]
145        .into(),
146    )?;
147
148    let guitarist_name = musician
149        .builder()
150        .select(|m| m.instruments.contains(&Guitar))
151        .project(|g| g.name.to_string())
152        .build();
153
154    assert_eq!(
155        vec![
156            "Alex Turner".to_string(),
157            "Conor Mason".into(),
158            "John Petrucci".into(),
159        ],
160        music.evaluate(&guitarist_name)?.into_tuples()
161    );
162
163    let dt_member = musician
164        .builder()
165        .with_key(|m| m.band.clone())
166        .join(band.builder().with_key(|b| Some(b.name.clone())))
167        .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168        .select(|m| m.1 == "Dream Theater")
169        .project(|m| m.0.to_string())
170        .build();
171
172    assert_eq!(
173        vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174        music.evaluate(&dt_member)?.into_tuples()
175    );
176
177    let dt_member_view = music.store_view(dt_member)?;
178    let drummer_view = music.store_view(
179        musician
180            .builder()
181            .select(|m| m.instruments.contains(&Drums))
182            .build(),
183    )?;
184
185    music.insert(
186        &musician,
187        vec![
188            Musician {
189                name: "John Myung".into(),
190                band: Some("Dream Theater".into()),
191                instruments: vec![Guitar],
192            },
193            Musician {
194                name: "Mike Mangini".into(),
195                band: Some("Dream Theater".into()),
196                instruments: vec![Drums],
197            },
198        ]
199        .into(),
200    )?;
201
202    assert_eq!(
203        vec![
204            Musician {
205                name: "Lars Ulrich".into(),
206                band: Some("Metallica".into()),
207                instruments: vec![Drums]
208            },
209            Musician {
210                name: "Mike Mangini".into(),
211                band: Some("Dream Theater".into()),
212                instruments: vec![Drums]
213            }
214        ],
215        music.evaluate(&drummer_view)?.into_tuples()
216    );
217    assert_eq!(
218        vec![
219            "John Myung".to_string(),
220            "John Petrucci".into(),
221            "Jordan Rudess".into(),
222            "Mike Mangini".into()
223        ],
224        music.evaluate(&dt_member_view)?.into_tuples()
225    );
226
227    Ok(())
228}
Source

pub fn insert<T>( &self, relation: &Relation<T>, tuples: Tuples<T>, ) -> Result<(), Error>
where T: Tuple + 'static,

Inserts tuples in the Instance corresponding to relation.

Examples found in repository?
examples/music.rs (lines 39-84)
34fn main() -> Result<(), Error> {
35    let mut music = Database::new();
36    let musician = music.add_relation("musician")?;
37    let band = music.add_relation("band")?;
38    let song = music.add_relation("song")?;
39    music.insert(
40        &musician,
41        vec![
42            Musician {
43                name: "John Petrucci".into(),
44                band: Some("Dream Theater".into()),
45                instruments: vec![Guitar],
46            },
47            Musician {
48                name: "Taylor Swift".into(),
49                band: None,
50                instruments: vec![Vocals],
51            },
52            Musician {
53                name: "Conor Mason".into(),
54                band: Some("Nothing But Thieves".into()),
55                instruments: vec![Vocals, Guitar],
56            },
57            Musician {
58                name: "Stevie Wonder".into(),
59                band: None,
60                instruments: vec![Vocals, Piano],
61            },
62            Musician {
63                name: "Jordan Rudess".into(),
64                band: Some("Dream Theater".into()),
65                instruments: vec![Keyboard],
66            },
67            Musician {
68                name: "Alex Turner".into(),
69                band: Some("Arctic Monkeys".into()),
70                instruments: vec![Vocals, Guitar, Piano],
71            },
72            Musician {
73                name: "Billie Eilish".into(),
74                band: None,
75                instruments: vec![Vocals, Piano],
76            },
77            Musician {
78                name: "Lars Ulrich".into(),
79                band: Some("Metallica".into()),
80                instruments: vec![Drums],
81            },
82        ]
83        .into(),
84    )?;
85
86    music.insert(
87        &band,
88        vec![
89            Band {
90                name: "Dream Theater".into(),
91                genre: "Progressive Metal".into(),
92            },
93            Band {
94                name: "Nothing But Thieves".into(),
95                genre: "Alternative Rock".into(),
96            },
97            Band {
98                name: "Metallica".into(),
99                genre: "Heavy Metal".into(),
100            },
101            Band {
102                name: "Arctic Monkeys".into(),
103                genre: "Indie Rock".into(),
104            },
105        ]
106        .into(),
107    )?;
108
109    music.insert(
110        &song,
111        vec![
112            Song {
113                title: "pull me under".into(),
114                artist: Either::Right("Dream Theater".into()),
115            },
116            Song {
117                title: "bad guy".into(),
118                artist: Either::Left("Billie Eilish".into()),
119            },
120            Song {
121                title: "excuse me".into(),
122                artist: Either::Left("Nothing But Thieves".into()),
123            },
124            Song {
125                title: "enter sandman".into(),
126                artist: Either::Right("Metallica".into()),
127            },
128            Song {
129                title: "panic attack".into(),
130                artist: Either::Right("Dream Theater".into()),
131            },
132            Song {
133                title: "shake it off".into(),
134                artist: Either::Left("Taylor Swift".into()),
135            },
136            Song {
137                title: "r u mine".into(),
138                artist: Either::Right("Artcic Monkeys".into()),
139            },
140            Song {
141                title: "as I am".into(),
142                artist: Either::Right("Dream Theater".into()),
143            },
144        ]
145        .into(),
146    )?;
147
148    let guitarist_name = musician
149        .builder()
150        .select(|m| m.instruments.contains(&Guitar))
151        .project(|g| g.name.to_string())
152        .build();
153
154    assert_eq!(
155        vec![
156            "Alex Turner".to_string(),
157            "Conor Mason".into(),
158            "John Petrucci".into(),
159        ],
160        music.evaluate(&guitarist_name)?.into_tuples()
161    );
162
163    let dt_member = musician
164        .builder()
165        .with_key(|m| m.band.clone())
166        .join(band.builder().with_key(|b| Some(b.name.clone())))
167        .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168        .select(|m| m.1 == "Dream Theater")
169        .project(|m| m.0.to_string())
170        .build();
171
172    assert_eq!(
173        vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174        music.evaluate(&dt_member)?.into_tuples()
175    );
176
177    let dt_member_view = music.store_view(dt_member)?;
178    let drummer_view = music.store_view(
179        musician
180            .builder()
181            .select(|m| m.instruments.contains(&Drums))
182            .build(),
183    )?;
184
185    music.insert(
186        &musician,
187        vec![
188            Musician {
189                name: "John Myung".into(),
190                band: Some("Dream Theater".into()),
191                instruments: vec![Guitar],
192            },
193            Musician {
194                name: "Mike Mangini".into(),
195                band: Some("Dream Theater".into()),
196                instruments: vec![Drums],
197            },
198        ]
199        .into(),
200    )?;
201
202    assert_eq!(
203        vec![
204            Musician {
205                name: "Lars Ulrich".into(),
206                band: Some("Metallica".into()),
207                instruments: vec![Drums]
208            },
209            Musician {
210                name: "Mike Mangini".into(),
211                band: Some("Dream Theater".into()),
212                instruments: vec![Drums]
213            }
214        ],
215        music.evaluate(&drummer_view)?.into_tuples()
216    );
217    assert_eq!(
218        vec![
219            "John Myung".to_string(),
220            "John Petrucci".into(),
221            "Jordan Rudess".into(),
222            "Mike Mangini".into()
223        ],
224        music.evaluate(&dt_member_view)?.into_tuples()
225    );
226
227    Ok(())
228}
Source

pub fn store_view<T, E, I>( &mut self, expression: I, ) -> Result<View<T, E>, Error>
where T: Tuple + 'static, E: ExpressionExt<T> + 'static, I: IntoExpression<T, E>,

Stores a new view over expression and returns a View objeect that can be evaluated as a view.

Examples found in repository?
examples/music.rs (line 177)
34fn main() -> Result<(), Error> {
35    let mut music = Database::new();
36    let musician = music.add_relation("musician")?;
37    let band = music.add_relation("band")?;
38    let song = music.add_relation("song")?;
39    music.insert(
40        &musician,
41        vec![
42            Musician {
43                name: "John Petrucci".into(),
44                band: Some("Dream Theater".into()),
45                instruments: vec![Guitar],
46            },
47            Musician {
48                name: "Taylor Swift".into(),
49                band: None,
50                instruments: vec![Vocals],
51            },
52            Musician {
53                name: "Conor Mason".into(),
54                band: Some("Nothing But Thieves".into()),
55                instruments: vec![Vocals, Guitar],
56            },
57            Musician {
58                name: "Stevie Wonder".into(),
59                band: None,
60                instruments: vec![Vocals, Piano],
61            },
62            Musician {
63                name: "Jordan Rudess".into(),
64                band: Some("Dream Theater".into()),
65                instruments: vec![Keyboard],
66            },
67            Musician {
68                name: "Alex Turner".into(),
69                band: Some("Arctic Monkeys".into()),
70                instruments: vec![Vocals, Guitar, Piano],
71            },
72            Musician {
73                name: "Billie Eilish".into(),
74                band: None,
75                instruments: vec![Vocals, Piano],
76            },
77            Musician {
78                name: "Lars Ulrich".into(),
79                band: Some("Metallica".into()),
80                instruments: vec![Drums],
81            },
82        ]
83        .into(),
84    )?;
85
86    music.insert(
87        &band,
88        vec![
89            Band {
90                name: "Dream Theater".into(),
91                genre: "Progressive Metal".into(),
92            },
93            Band {
94                name: "Nothing But Thieves".into(),
95                genre: "Alternative Rock".into(),
96            },
97            Band {
98                name: "Metallica".into(),
99                genre: "Heavy Metal".into(),
100            },
101            Band {
102                name: "Arctic Monkeys".into(),
103                genre: "Indie Rock".into(),
104            },
105        ]
106        .into(),
107    )?;
108
109    music.insert(
110        &song,
111        vec![
112            Song {
113                title: "pull me under".into(),
114                artist: Either::Right("Dream Theater".into()),
115            },
116            Song {
117                title: "bad guy".into(),
118                artist: Either::Left("Billie Eilish".into()),
119            },
120            Song {
121                title: "excuse me".into(),
122                artist: Either::Left("Nothing But Thieves".into()),
123            },
124            Song {
125                title: "enter sandman".into(),
126                artist: Either::Right("Metallica".into()),
127            },
128            Song {
129                title: "panic attack".into(),
130                artist: Either::Right("Dream Theater".into()),
131            },
132            Song {
133                title: "shake it off".into(),
134                artist: Either::Left("Taylor Swift".into()),
135            },
136            Song {
137                title: "r u mine".into(),
138                artist: Either::Right("Artcic Monkeys".into()),
139            },
140            Song {
141                title: "as I am".into(),
142                artist: Either::Right("Dream Theater".into()),
143            },
144        ]
145        .into(),
146    )?;
147
148    let guitarist_name = musician
149        .builder()
150        .select(|m| m.instruments.contains(&Guitar))
151        .project(|g| g.name.to_string())
152        .build();
153
154    assert_eq!(
155        vec![
156            "Alex Turner".to_string(),
157            "Conor Mason".into(),
158            "John Petrucci".into(),
159        ],
160        music.evaluate(&guitarist_name)?.into_tuples()
161    );
162
163    let dt_member = musician
164        .builder()
165        .with_key(|m| m.band.clone())
166        .join(band.builder().with_key(|b| Some(b.name.clone())))
167        .on(|_, m, b| (m.name.to_string(), b.name.to_string()))
168        .select(|m| m.1 == "Dream Theater")
169        .project(|m| m.0.to_string())
170        .build();
171
172    assert_eq!(
173        vec!["John Petrucci".to_string(), "Jordan Rudess".into()],
174        music.evaluate(&dt_member)?.into_tuples()
175    );
176
177    let dt_member_view = music.store_view(dt_member)?;
178    let drummer_view = music.store_view(
179        musician
180            .builder()
181            .select(|m| m.instruments.contains(&Drums))
182            .build(),
183    )?;
184
185    music.insert(
186        &musician,
187        vec![
188            Musician {
189                name: "John Myung".into(),
190                band: Some("Dream Theater".into()),
191                instruments: vec![Guitar],
192            },
193            Musician {
194                name: "Mike Mangini".into(),
195                band: Some("Dream Theater".into()),
196                instruments: vec![Drums],
197            },
198        ]
199        .into(),
200    )?;
201
202    assert_eq!(
203        vec![
204            Musician {
205                name: "Lars Ulrich".into(),
206                band: Some("Metallica".into()),
207                instruments: vec![Drums]
208            },
209            Musician {
210                name: "Mike Mangini".into(),
211                band: Some("Dream Theater".into()),
212                instruments: vec![Drums]
213            }
214        ],
215        music.evaluate(&drummer_view)?.into_tuples()
216    );
217    assert_eq!(
218        vec![
219            "John Myung".to_string(),
220            "John Petrucci".into(),
221            "Jordan Rudess".into(),
222            "Mike Mangini".into()
223        ],
224        music.evaluate(&dt_member_view)?.into_tuples()
225    );
226
227    Ok(())
228}

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for Database

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.