Repository navigation
Expand file tree
/
Copy pathcursor_test.rs
More file actions
364 lines (331 loc) · 11.9 KB
/
Copy pathcursor_test.rs
File metadata and controls
364 lines (331 loc) · 11.9 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! `Minigraf::query` and `PreparedQuery::query` return a `Cursor` that delivers
//! the answer in batches (#432). These tests pin the public contract the
//! streaming engine must keep: batches partition the `execute()` answer, the
//! answer is fixed when the cursor opens, and non-queries are rejected.
// File-backed (Minigraf::open, tempfile): native only, like the other file tests.
#![cfg(not(target_arch = "wasm32"))]
use minigraf::{BindValue, Cursor, Minigraf, OpenOptions, QueryResult, Value};
fn seeded(n: i64) -> Minigraf {
let db = Minigraf::in_memory().unwrap();
let facts: String = (0..n)
.map(|i| format!("[:p{i} :person/age {i}] [:p{i} :person/name \"n{i}\"]"))
.collect::<Vec<_>>()
.join(" ");
db.execute(&format!("(transact [{facts}])")).unwrap();
db
}
fn execute_rows(db: &Minigraf, q: &str) -> (Vec<String>, Vec<Vec<Value>>) {
match db.execute(q).unwrap() {
QueryResult::QueryResults { vars, results } => (vars, results),
_ => panic!("expected query results"),
}
}
/// Rows in a stable order, so answers can be compared as multisets.
fn sorted(mut rows: Vec<Vec<Value>>) -> Vec<Vec<Value>> {
rows.sort_by_key(|r| format!("{r:?}"));
rows
}
fn drain(cursor: &mut Cursor, max_rows: usize) -> Vec<Vec<Value>> {
let mut rows = Vec::new();
while let Some(batch) = cursor.next_batch(max_rows).unwrap() {
assert!(!batch.is_empty(), "a batch is never empty");
assert!(batch.len() <= max_rows.max(1), "batch exceeds max_rows");
assert_eq!(batch.len(), batch.rows().len());
rows.extend(batch.into_rows());
}
rows
}
const AGES: &str = "(query [:find ?e ?age :where [?e :person/age ?age]])";
#[test]
fn batches_partition_the_execute_answer() {
let db = seeded(25);
let (vars, expected) = execute_rows(&db, AGES);
for max_rows in [1, 7, 25, 1000] {
let mut cursor = db.query(AGES).unwrap();
assert_eq!(cursor.vars(), vars.as_slice());
let rows = drain(&mut cursor, max_rows);
assert_eq!(rows.len(), 25, "every row delivered once");
assert_eq!(sorted(rows), sorted(expected.clone()));
// The end stays the end.
assert!(cursor.next_batch(max_rows).unwrap().is_none());
}
}
#[test]
fn batch_sizes_are_full_until_the_last() {
let db = seeded(25);
let mut cursor = db.query(AGES).unwrap();
let mut sizes = Vec::new();
while let Some(batch) = cursor.next_batch(10).unwrap() {
sizes.push(batch.len());
}
assert_eq!(sizes, vec![10, 10, 5]);
}
#[test]
fn zero_max_rows_still_makes_progress() {
let db = seeded(3);
let mut cursor = db.query(AGES).unwrap();
let rows = drain(&mut cursor, 0);
assert_eq!(rows.len(), 3);
}
#[test]
fn iterator_yields_the_execute_rows() {
let db = seeded(12);
let (_, expected) = execute_rows(&db, AGES);
let rows: Vec<Vec<Value>> = db.query(AGES).unwrap().collect::<Result<_, _>>().unwrap();
assert_eq!(sorted(rows), sorted(expected));
}
#[test]
fn batch_into_iterator_yields_rows() {
let db = seeded(4);
let batch = db.query(AGES).unwrap().next_batch(10).unwrap().unwrap();
let n = batch.into_iter().filter(|row| row.len() == 2).count();
assert_eq!(n, 4);
}
#[test]
fn empty_answer_has_vars_and_no_batch() {
let db = seeded(3);
let mut cursor = db
.query("(query [:find ?e :where [?e :person/missing ?x]])")
.unwrap();
assert_eq!(cursor.vars(), ["?e".to_string()].as_slice());
assert!(cursor.next_batch(10).unwrap().is_none());
assert!(cursor.next().is_none());
}
#[test]
fn aggregates_rules_and_as_of_match_execute() {
let db = seeded(10);
db.execute("(transact [[:p1 :person/friend :p2] [:p2 :person/friend :p3]])")
.unwrap();
db.execute("(rule [(reach ?a ?b) [?a :person/friend ?b]])")
.unwrap();
db.execute("(rule [(reach ?a ?b) [?a :person/friend ?m] (reach ?m ?b)])")
.unwrap();
let queries = [
"(query [:find (count ?e) (max ?age) :where [?e :person/age ?age]])",
"(query [:find ?a ?b :where (reach ?a ?b)])",
"(query [:find ?e :as-of 1 :where [?e :person/friend ?f]])",
"(query [:find ?e :any-valid-time :where [?e :person/age ?age]])",
];
for q in queries {
let (vars, expected) = execute_rows(&db, q);
let mut cursor = db.query(q).unwrap();
assert_eq!(cursor.vars(), vars.as_slice());
assert_eq!(sorted(drain(&mut cursor, 2)), sorted(expected));
}
}
#[test]
fn cursor_answer_is_fixed_at_open() {
let db = seeded(5);
let mut cursor = db.query(AGES).unwrap();
let first = cursor.next_batch(2).unwrap().unwrap();
assert_eq!(first.len(), 2);
db.execute("(transact [[:late :person/age 99]])").unwrap();
let mut tx = db.begin_write().unwrap();
tx.execute("(transact [[:later :person/age 100]])").unwrap();
tx.commit().unwrap();
db.execute("(retract [[:p0 :person/age 0]])").unwrap();
let rest = drain(&mut cursor, 2);
assert_eq!(
first.len() + rest.len(),
5,
"pre-transaction answer to the end"
);
let ages: Vec<Value> = first
.into_rows()
.into_iter()
.chain(rest)
.map(|r| r[1].clone())
.collect();
assert!(!ages.contains(&Value::Integer(99)), "later write not seen");
assert!(
!ages.contains(&Value::Integer(100)),
"later commit not seen"
);
assert!(ages.contains(&Value::Integer(0)), "later retract not seen");
}
#[test]
fn file_backed_cursor_answer_is_fixed_at_open() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("cursor.graph");
let db = Minigraf::open(&path).unwrap();
db.execute("(transact [[:a :n 1] [:b :n 2] [:c :n 3]])")
.unwrap();
db.checkpoint().unwrap();
db.execute("(transact [[:d :n 4]])").unwrap();
let mut cursor = db.query("(query [:find ?v :where [?e :n ?v]])").unwrap();
db.execute("(transact [[:e :n 5]])").unwrap();
db.checkpoint().unwrap();
let rows = drain(&mut cursor, 1);
assert_eq!(
rows.len(),
4,
"committed and WAL facts at open, nothing later"
);
}
#[test]
fn non_queries_are_api_012() {
let db = seeded(1);
for (cmd, kind) in [
("(transact [[:x :person/age 1]])", "transact"),
("(retract [[:p0 :person/age 0]])", "retract"),
("(rule [(adult ?e) [?e :person/age ?a]])", "rule"),
] {
let err = db.query(cmd).unwrap_err();
assert_eq!(err.code(), "API-012");
assert!(err.to_string().contains(kind), "message names the command");
}
// Nothing was written or registered.
let (_, rows) = execute_rows(&db, AGES);
assert_eq!(rows.len(), 1);
}
#[test]
fn bind_slots_are_api_010() {
let db = seeded(1);
let err = db
.query("(query [:find ?e :where [?e :person/age $age]])")
.unwrap_err();
assert_eq!(err.code(), "API-010");
}
#[test]
fn parse_errors_keep_their_code() {
let db = seeded(1);
let err = db.query("(query [:find ?e :where").unwrap_err();
assert!(err.code().starts_with("PRS-"), "parse error code");
}
#[test]
fn query_on_a_write_transaction_thread_is_int_001() {
let db = seeded(1);
let tx = db.begin_write().unwrap();
let err = db.query(AGES).unwrap_err();
assert_eq!(err.code(), "INT-001");
tx.rollback();
}
#[test]
fn rule_limits_apply_while_the_engine_materialises() {
let db = OpenOptions::new()
.max_derived_facts(3)
.open_memory()
.unwrap();
db.execute("(transact [[:a :next :b] [:b :next :c] [:c :next :d] [:d :next :e]])")
.unwrap();
db.execute("(rule [(reach ?x ?y) [?x :next ?y]])").unwrap();
db.execute("(rule [(reach ?x ?y) [?x :next ?m] (reach ?m ?y)])")
.unwrap();
let q = "(query [:find ?x ?y :where (reach ?x ?y)])";
assert_eq!(db.execute(q).unwrap_err().code(), "INT-020");
assert_eq!(db.query(q).unwrap_err().code(), "INT-020");
}
#[test]
fn prepared_query_cursor_matches_prepared_execute() {
let db = seeded(10);
let pq = db
.prepare("(query [:find ?name :where [?e :person/age $age] [?e :person/name ?name]])")
.unwrap();
for age in [0, 4, 9, 42] {
let binds = [("age", BindValue::Val(Value::Integer(age)))];
let expected = match pq.execute(&binds).unwrap() {
QueryResult::QueryResults { results, .. } => results,
_ => panic!("expected query results"),
};
let mut cursor = pq.query(&binds).unwrap();
assert_eq!(cursor.vars(), ["?name".to_string()].as_slice());
assert_eq!(drain(&mut cursor, 1), expected);
}
let err = pq.query(&[]).unwrap_err();
assert_eq!(err.code(), "API-022", "missing bind value");
}
#[test]
fn cursor_is_send_and_outlives_the_database_handle() {
fn assert_send<T: Send + 'static>() {}
assert_send::<Cursor>();
let db = seeded(6);
let cursor = db.query(AGES).unwrap();
drop(db);
let n = std::thread::spawn(move || cursor.map(Result::unwrap).count())
.join()
.unwrap();
assert_eq!(n, 6);
}
#[test]
fn close_after_first_batch() {
let db = seeded(10);
let mut cursor = db.query(AGES).unwrap();
assert!(cursor.next_batch(1).unwrap().is_some());
cursor.close();
// The database is unaffected.
assert_eq!(execute_rows(&db, AGES).1.len(), 10);
}
fn file_len(path: &std::path::Path) -> u64 {
std::fs::metadata(path).unwrap().len()
}
/// Checkpoints that rewrite the leaves of one entity and free the old ones.
fn churn(db: &Minigraf, rounds: usize) {
for i in 0..rounds {
let facts: String = (0..50)
.map(|j| format!("[:churn :churn/n {}]", i * 100 + j))
.collect();
db.execute(&format!("(transact [{facts}])")).unwrap();
db.checkpoint().unwrap();
}
}
#[test]
fn cursor_open_across_checkpoints_keeps_its_answer_and_pages() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("pin.graph");
let db = Minigraf::open(&path).unwrap();
let facts: String = (0..200)
.map(|i| format!("[:p{i} :person/age {i}]"))
.collect();
db.execute(&format!("(transact [{facts}])")).unwrap();
churn(&db, 4);
// Growth over the same rounds of checkpoints: with no cursor, with one
// open, and after it closes.
let grow = |db: &Minigraf| {
let before = file_len(&path);
churn(db, 4);
file_len(&path) - before
};
let free = grow(&db);
let expected = sorted(execute_rows(&db, AGES).1);
let mut cursor = db.query(AGES).unwrap();
let mut rows = cursor.next_batch(7).unwrap().expect("a batch").into_rows();
let held = grow(&db);
db.execute("(transact [[:late :person/age 999]])").unwrap();
db.rebuild_indexes().unwrap();
assert!(
db.verify().unwrap().is_ok(),
"verify clean with the cursor open"
);
rows.extend(drain(&mut cursor, 7));
assert_eq!(sorted(rows), expected, "the answer at open");
cursor.close();
let after = grow(&db);
eprintln!("growth: free {free}, held {held}, after {after}");
assert!(
held > 4 * free.max(4096),
"pages the cursor pins are not reused"
);
assert!(after < held, "after close they are reused");
assert!(db.verify().unwrap().is_ok());
}
#[test]
fn prepared_cursor_pins_like_a_query_cursor() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("prepared.graph");
let db = Minigraf::open(&path).unwrap();
let facts: String = (0..50)
.map(|i| format!("[:p{i} :person/age {i}]"))
.collect();
db.execute(&format!("(transact [{facts}])")).unwrap();
db.checkpoint().unwrap();
let pq = db
.prepare("(query [:find ?a :where [?e :person/age ?a] [(< ?a $max)]])")
.unwrap();
let mut cursor = pq
.query(&[("max", BindValue::Val(Value::Integer(10)))])
.unwrap();
churn(&db, 3);
db.execute("(transact [[:p0 :person/age 5]])").unwrap();
assert_eq!(drain(&mut cursor, 3).len(), 10, "the answer at open");
assert!(db.verify().unwrap().is_ok());
}