1
|
|
mod helpers;
|
2
|
|
use helpers::create_and_drop;
|
3
|
|
use persy::Persy;
|
4
|
|
use persy::{ByteVec, IndexIter, IndexType, PRes, TxIndexIter, Value, ValueMode};
|
5
|
|
|
6
|
1
|
fn create_and_drop_index<F>(test: &str, f: F)
|
7
|
|
where
|
8
|
|
F: FnOnce(&Persy, &str),
|
9
|
|
{
|
10
|
1
|
create_and_drop_index_mode(test, ValueMode::CLUSTER, f);
|
11
|
1
|
}
|
12
|
|
|
13
|
1
|
fn create_and_drop_index_mode<F>(test: &str, mode: ValueMode, f: F)
|
14
|
|
where
|
15
|
|
F: FnOnce(&Persy, &str),
|
16
|
|
{
|
17
|
1
|
create_and_drop(test, |persy| {
|
18
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
19
|
1
|
tx.create_index::<u8, u8>("index1", mode)
|
20
|
|
.expect("index created correctly");
|
21
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
22
|
1
|
prep.commit().expect("commit with index works");
|
23
|
|
|
24
|
1
|
f(persy, "index1");
|
25
|
|
|
26
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
27
|
1
|
tx.drop_index("index1").expect("index created correctly");
|
28
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
29
|
1
|
prep.commit().expect("commit with index works");
|
30
|
1
|
});
|
31
|
1
|
}
|
32
|
|
|
33
|
|
#[test]
|
34
|
1
|
fn test_create_drop_index() {
|
35
|
1
|
create_and_drop("create_drop_index", |persy| {
|
36
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
37
|
1
|
tx.create_index::<u8, u8>("index1", ValueMode::CLUSTER)
|
38
|
|
.expect("index created correctly");
|
39
|
1
|
assert!(tx.exists_index("index1").expect("exists wokrs"));
|
40
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
41
|
1
|
prep.commit().expect("commit with index works");
|
42
|
1
|
assert!(persy.exists_index("index1").expect("exists wokrs"));
|
43
|
|
|
44
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
45
|
1
|
tx.drop_index("index1").expect("index created correctly");
|
46
|
1
|
assert!(!tx.exists_index("index1").expect("exists works"));
|
47
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
48
|
1
|
prep.commit().expect("commit with index works");
|
49
|
1
|
assert!(!persy.exists_index("index1").expect("exists wokrs"));
|
50
|
1
|
});
|
51
|
1
|
}
|
52
|
|
|
53
|
|
#[test]
|
54
|
1
|
fn test_create_put_remove_get_drop_index_same_tx() {
|
55
|
1
|
create_and_drop("create_drop_index", |persy| {
|
56
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
57
|
1
|
tx.create_index::<u8, u8>("index1", ValueMode::CLUSTER)
|
58
|
|
.expect("index created correctly");
|
59
|
1
|
tx.put::<u8, u8>("index1", 10, 12).expect("put works correctly");
|
60
|
1
|
let res = tx.get::<u8, u8>("index1", &10).expect("get works correctly");
|
61
|
1
|
assert_eq!(Some(Value::SINGLE(12)), res);
|
62
|
1
|
tx.remove::<u8, u8>("index1", 10, None).expect("put works correctly");
|
63
|
1
|
let res = tx.get::<u8, u8>("index1", &10).expect("get works correctly");
|
64
|
1
|
assert_eq!(None, res);
|
65
|
|
|
66
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
67
|
1
|
prep.commit().expect("commit with index works");
|
68
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
69
|
1
|
tx.drop_index("index1").expect("index created correctly");
|
70
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
71
|
1
|
prep.commit().expect("commit with index works");
|
72
|
1
|
});
|
73
|
1
|
}
|
74
|
|
|
75
|
1
|
fn test_for_type<K: IndexType, V>(persy: &Persy, name: &str, k: K, v: V) -> PRes<()>
|
76
|
|
where
|
77
|
|
V: IndexType + std::fmt::Debug + PartialEq,
|
78
|
|
{
|
79
|
1
|
let mut tx = persy.begin()?;
|
80
|
1
|
tx.create_index::<K, V>(name, ValueMode::CLUSTER)?;
|
81
|
1
|
tx.put::<K, V>(name, k.clone(), v.clone())?;
|
82
|
1
|
let res = tx.get::<K, V>(name, &k)?;
|
83
|
1
|
assert_eq!(Some(Value::SINGLE(v.clone())), res);
|
84
|
1
|
let prep = tx.prepare()?;
|
85
|
1
|
prep.commit()?;
|
86
|
|
|
87
|
1
|
let res = persy.get::<K, V>(name, &k)?;
|
88
|
1
|
assert_eq!(Some(Value::SINGLE(v)), res);
|
89
|
1
|
let mut tx = persy.begin()?;
|
90
|
1
|
tx.remove::<K, V>(name, k.clone(), None)?;
|
91
|
1
|
let res = tx.get::<K, V>(name, &k)?;
|
92
|
1
|
assert!(res.is_none());
|
93
|
1
|
let prep = tx.prepare()?;
|
94
|
1
|
prep.commit()?;
|
95
|
|
|
96
|
1
|
let res = persy.get::<K, V>(name, &k)?;
|
97
|
1
|
assert!(res.is_none());
|
98
|
1
|
let mut tx = persy.begin()?;
|
99
|
1
|
tx.drop_index(name)?;
|
100
|
1
|
let prep = tx.prepare()?;
|
101
|
1
|
prep.commit()?;
|
102
|
1
|
Ok(())
|
103
|
1
|
}
|
104
|
|
|
105
|
|
#[test]
|
106
|
1
|
fn test_all_indexable_types() {
|
107
|
1
|
create_and_drop("all_indexable_types", |persy| {
|
108
|
1
|
test_for_type::<u8, u8>(persy, "idx_u8", 10, 10).expect("test pass");
|
109
|
1
|
test_for_type::<u16, u16>(persy, "idx_u16", 10, 10).expect("test pass");
|
110
|
1
|
test_for_type::<u32, u32>(persy, "idx_u32", 10, 10).expect("test pass");
|
111
|
1
|
test_for_type::<u64, u64>(persy, "idx_u64", 10, 10).expect("test pass");
|
112
|
1
|
test_for_type::<u128, u128>(persy, "idx_u128", 10, 10).expect("test pass");
|
113
|
1
|
test_for_type::<i8, i8>(persy, "idx_i8", 10, 10).expect("test pass");
|
114
|
1
|
test_for_type::<i16, i16>(persy, "idx_i16", 10, 10).expect("test pass");
|
115
|
1
|
test_for_type::<i32, i32>(persy, "idx_i32", 10, 10).expect("test pass");
|
116
|
1
|
test_for_type::<i64, i64>(persy, "idx_i64", 10, 10).expect("test pass");
|
117
|
1
|
test_for_type::<i128, i128>(persy, "idx_i128", 10, 10).expect("test pass");
|
118
|
1
|
test_for_type::<f32, f32>(persy, "idx_f32", 10.0, 10.0).expect("test pass");
|
119
|
1
|
test_for_type::<f64, f64>(persy, "idx_f64", 10.0, 10.0).expect("test pass");
|
120
|
1
|
test_for_type::<String, String>(persy, "idx_string", "key".to_string(), "value".to_string())
|
121
|
|
.expect("test pass");
|
122
|
1
|
test_for_type::<ByteVec, ByteVec>(persy, "idx_bytevec", vec![10; 10].into(), vec![10; 10].into())
|
123
|
|
.expect("test pass");
|
124
|
1
|
});
|
125
|
1
|
}
|
126
|
|
|
127
|
|
#[test]
|
128
|
1
|
fn test_put_get_index() {
|
129
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
130
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
131
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
132
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
133
|
1
|
prep.commit().expect("commit with index works");
|
134
|
|
|
135
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
136
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
137
|
1
|
});
|
138
|
1
|
}
|
139
|
|
|
140
|
|
#[test]
|
141
|
1
|
fn test_put_get_tx_index() {
|
142
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
143
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
144
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
145
|
1
|
let res = tx.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
146
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
147
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
148
|
1
|
prep.commit().expect("commit with index works");
|
149
|
|
|
150
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
151
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
152
|
1
|
});
|
153
|
1
|
}
|
154
|
|
|
155
|
|
#[test]
|
156
|
1
|
fn test_put_remove_index() {
|
157
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
158
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
159
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
160
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
161
|
1
|
prep.commit().expect("commit with index works");
|
162
|
|
|
163
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
164
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
165
|
|
|
166
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
167
|
1
|
tx.remove::<u8, u8>(index_name, 10, None).expect("put works correctly");
|
168
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
169
|
1
|
prep.commit().expect("commit with index works");
|
170
|
|
|
171
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
172
|
1
|
assert_eq!(res, None);
|
173
|
1
|
});
|
174
|
1
|
}
|
175
|
|
|
176
|
|
#[test]
|
177
|
1
|
fn test_duplicate_put() {
|
178
|
1
|
create_and_drop_index_mode("create_drop_index", ValueMode::EXCLUSIVE, |persy, index_name| {
|
179
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
180
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
181
|
1
|
tx.put::<u8, u8>(index_name, 10, 20).expect("put works correctly");
|
182
|
1
|
assert!(tx.prepare().is_err());
|
183
|
1
|
});
|
184
|
1
|
}
|
185
|
|
|
186
|
|
#[test]
|
187
|
1
|
fn test_same_value_no_duplicate_put() {
|
188
|
1
|
create_and_drop_index_mode("create_drop_index", ValueMode::EXCLUSIVE, |persy, index_name| {
|
189
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
190
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
191
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
192
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
193
|
1
|
prep.commit().expect("commit with index works");
|
194
|
|
|
195
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
196
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
197
|
1
|
});
|
198
|
1
|
}
|
199
|
|
|
200
|
|
#[test]
|
201
|
1
|
fn test_duplicate_second_put() {
|
202
|
1
|
create_and_drop_index_mode("create_drop_index", ValueMode::EXCLUSIVE, |persy, index_name| {
|
203
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
204
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
205
|
|
|
206
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
207
|
1
|
prep.commit().expect("commit with index works");
|
208
|
|
|
209
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
210
|
1
|
tx.put::<u8, u8>(index_name, 10, 20).expect("put works correctly");
|
211
|
1
|
assert!(tx.prepare().is_err());
|
212
|
1
|
});
|
213
|
1
|
}
|
214
|
|
|
215
|
|
#[test]
|
216
|
1
|
fn test_duplicate_tx_put() {
|
217
|
1
|
create_and_drop_index_mode("create_drop_index", ValueMode::EXCLUSIVE, |persy, index_name| {
|
218
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
219
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
220
|
1
|
tx.put::<u8, u8>(index_name, 10, 20).expect("put works correctly");
|
221
|
1
|
assert!(tx.get::<u8, u8>(index_name, &10).is_err());
|
222
|
1
|
});
|
223
|
1
|
}
|
224
|
|
|
225
|
|
#[test]
|
226
|
1
|
fn test_same_value_no_duplicate_put_tx() {
|
227
|
1
|
create_and_drop_index_mode("create_drop_index", ValueMode::EXCLUSIVE, |persy, index_name| {
|
228
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
229
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
230
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
231
|
1
|
assert_eq!(tx.get::<u8, u8>(index_name, &10).ok(), Some(Some(Value::SINGLE(12))));
|
232
|
1
|
});
|
233
|
1
|
}
|
234
|
|
|
235
|
|
#[test]
|
236
|
1
|
fn test_put_remove_index_one_tx() {
|
237
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
238
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
239
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
240
|
1
|
tx.remove::<u8, u8>(index_name, 10, None).expect("put works correctly");
|
241
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
242
|
1
|
prep.commit().expect("commit with index works");
|
243
|
|
|
244
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
245
|
1
|
assert_eq!(res, None);
|
246
|
1
|
});
|
247
|
1
|
}
|
248
|
|
|
249
|
|
#[test]
|
250
|
1
|
fn test_mupltiple_values_put_get() {
|
251
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
252
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
253
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
254
|
|
|
255
|
1
|
tx.put::<u8, u8>(index_name, 10, 14).expect("put works correctly");
|
256
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
257
|
1
|
prep.commit().expect("commit with index works");
|
258
|
|
|
259
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
260
|
1
|
assert_eq!(res, Some(Value::CLUSTER(vec![12, 14])));
|
261
|
1
|
});
|
262
|
1
|
}
|
263
|
|
|
264
|
|
#[test]
|
265
|
1
|
fn test_mupltiple_values_put_get_tx() {
|
266
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
267
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
268
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
269
|
|
|
270
|
1
|
tx.put::<u8, u8>(index_name, 10, 14).expect("put works correctly");
|
271
|
1
|
let res = tx.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
272
|
1
|
assert_eq!(res, Some(Value::CLUSTER(vec![12, 14])));
|
273
|
1
|
});
|
274
|
1
|
}
|
275
|
|
|
276
|
|
#[test]
|
277
|
1
|
fn test_mupltiple_put_same_value_get() {
|
278
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
279
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
280
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
281
|
|
|
282
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
283
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
284
|
1
|
prep.commit().expect("commit with index works");
|
285
|
|
|
286
|
1
|
let res = persy.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
287
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
288
|
1
|
});
|
289
|
1
|
}
|
290
|
|
|
291
|
|
#[test]
|
292
|
1
|
fn test_index_browse() {
|
293
|
1
|
create_and_drop_index("test_index_browse", |persy, index_name| {
|
294
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
295
|
1
|
for n in 10..30 {
|
296
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
297
|
|
}
|
298
|
|
|
299
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
300
|
1
|
prep.commit().expect("commit with index works");
|
301
|
|
|
302
|
1
|
let start = 10;
|
303
|
1
|
let to = 29;
|
304
|
1
|
let mut count = to - start;
|
305
|
1
|
let mut iter: IndexIter<u8, u8> = persy.range(index_name, ..).expect("browse works correctly");
|
306
|
1
|
assert_eq!(iter.next().unwrap().0, start);
|
307
|
1
|
let mut last = None;
|
308
|
1
|
for x in iter {
|
309
|
1
|
last = Some(x.0.clone());
|
310
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
311
|
1
|
count -= 1;
|
312
|
1
|
}
|
313
|
1
|
assert_eq!(Some(to), last);
|
314
|
1
|
assert_eq!(0, count);
|
315
|
1
|
});
|
316
|
1
|
}
|
317
|
|
|
318
|
|
#[test]
|
319
|
1
|
fn test_index_range() {
|
320
|
1
|
create_and_drop_index("test_index_range", |persy, index_name| {
|
321
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
322
|
1
|
for n in 10..30 {
|
323
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
324
|
|
}
|
325
|
|
|
326
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
327
|
1
|
prep.commit().expect("commit with index works");
|
328
|
|
|
329
|
1
|
let start = 13;
|
330
|
1
|
let to = 24;
|
331
|
1
|
let mut count = to - start;
|
332
|
1
|
let mut iter: IndexIter<u8, u8> = persy.range(index_name, start..=to).expect("range works correctly");
|
333
|
1
|
assert_eq!(iter.next().unwrap().0, start);
|
334
|
1
|
let mut last = None;
|
335
|
1
|
for x in iter {
|
336
|
1
|
last = Some(x.0.clone());
|
337
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
338
|
1
|
count -= 1;
|
339
|
1
|
}
|
340
|
1
|
assert_eq!(Some(to), last);
|
341
|
1
|
assert_eq!(0, count);
|
342
|
1
|
});
|
343
|
1
|
}
|
344
|
|
|
345
|
|
#[test]
|
346
|
1
|
fn test_index_range_rev() {
|
347
|
1
|
create_and_drop_index("test_index_range_rev", |persy, index_name| {
|
348
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
349
|
1
|
for n in 10..30 {
|
350
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
351
|
|
}
|
352
|
|
|
353
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
354
|
1
|
prep.commit().expect("commit with index works");
|
355
|
|
|
356
|
1
|
let start = 13;
|
357
|
1
|
let to = 24;
|
358
|
1
|
let mut count = to - start;
|
359
|
1
|
let base: IndexIter<u8, u8> = persy.range(index_name, start..=to).expect("range works correctly");
|
360
|
1
|
let mut iter = base.rev();
|
361
|
1
|
assert_eq!(iter.next().unwrap().0, to);
|
362
|
1
|
let mut last = None;
|
363
|
1
|
for x in iter {
|
364
|
1
|
last = Some(x.0.clone());
|
365
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
366
|
1
|
count -= 1;
|
367
|
1
|
}
|
368
|
1
|
assert_eq!(Some(start), last);
|
369
|
1
|
assert_eq!(0, count);
|
370
|
1
|
});
|
371
|
1
|
}
|
372
|
|
|
373
|
|
#[test]
|
374
|
1
|
fn test_index_browse_tx() {
|
375
|
1
|
create_and_drop_index("test_index_browse_tx", |persy, index_name| {
|
376
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
377
|
1
|
for n in 10..30 {
|
378
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
379
|
|
}
|
380
|
1
|
let start = 10;
|
381
|
1
|
let to = 29;
|
382
|
1
|
let mut count = to - start;
|
383
|
1
|
let mut last = None;
|
384
|
|
{
|
385
|
1
|
let mut iter: TxIndexIter<u8, u8> = tx.range(index_name, ..).expect("browse works correctly");
|
386
|
1
|
assert_eq!(iter.next().unwrap().0, start);
|
387
|
1
|
for x in iter {
|
388
|
1
|
last = Some(x.0.clone());
|
389
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
390
|
1
|
count -= 1;
|
391
|
1
|
}
|
392
|
1
|
}
|
393
|
1
|
assert_eq!(Some(to), last);
|
394
|
1
|
assert_eq!(0, count);
|
395
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
396
|
1
|
prep.commit().expect("commit with index works");
|
397
|
1
|
});
|
398
|
1
|
}
|
399
|
|
|
400
|
|
#[test]
|
401
|
1
|
fn test_index_range_tx() {
|
402
|
1
|
create_and_drop_index("test_index_range_tx", |persy, index_name| {
|
403
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
404
|
1
|
for n in 10..30 {
|
405
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
406
|
|
}
|
407
|
1
|
tx.remove::<u8, u8>(index_name, 20, None).expect("remove works");
|
408
|
|
|
409
|
1
|
let start = 13;
|
410
|
1
|
let to = 24;
|
411
|
1
|
let mut count = to - start - 1;
|
412
|
|
{
|
413
|
1
|
let mut iter: TxIndexIter<u8, u8> = tx.range(index_name, start..=to).expect("range works correctly");
|
414
|
1
|
assert_eq!(iter.next().unwrap().0, start);
|
415
|
1
|
let mut last = None;
|
416
|
1
|
for x in iter {
|
417
|
1
|
last = Some(x.0.clone());
|
418
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
419
|
1
|
assert!(x.0 != 20u8);
|
420
|
1
|
count -= 1;
|
421
|
1
|
}
|
422
|
1
|
assert_eq!(Some(to), last);
|
423
|
1
|
assert_eq!(0, count);
|
424
|
1
|
}
|
425
|
|
|
426
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
427
|
1
|
prep.commit().expect("commit with index works");
|
428
|
1
|
});
|
429
|
1
|
}
|
430
|
|
|
431
|
|
#[test]
|
432
|
1
|
fn test_index_range_tx_access() {
|
433
|
1
|
create_and_drop_index("test_index_range_tx_access", |persy, index_name| {
|
434
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
435
|
1
|
tx.create_segment("seg").expect("create segment");
|
436
|
1
|
for n in 10..30 {
|
437
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
438
|
|
}
|
439
|
|
|
440
|
1
|
let start = 13;
|
441
|
1
|
let to = 24;
|
442
|
1
|
let mut count = to - start;
|
443
|
|
{
|
444
|
1
|
let mut iter: TxIndexIter<u8, u8> = tx.range(index_name, start..=to).expect("range works correctly");
|
445
|
1
|
assert_eq!(iter.next().unwrap().0, start);
|
446
|
1
|
iter.tx().insert("seg", "data".as_bytes()).expect("insert works");
|
447
|
1
|
let mut last = None;
|
448
|
1
|
while let Some(x) = iter.next_tx() {
|
449
|
1
|
last = Some(x.0.clone());
|
450
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
451
|
1
|
x.2.insert("seg", "data".as_bytes()).expect("insert works");
|
452
|
1
|
count -= 1;
|
453
|
1
|
}
|
454
|
1
|
assert_eq!(Some(to), last);
|
455
|
1
|
assert_eq!(0, count);
|
456
|
1
|
}
|
457
|
|
|
458
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
459
|
1
|
prep.commit().expect("commit with index works");
|
460
|
1
|
});
|
461
|
1
|
}
|
462
|
|
|
463
|
|
#[test]
|
464
|
1
|
fn test_index_range_tx_rev() {
|
465
|
1
|
create_and_drop_index("test_index_range_tx_rev", |persy, index_name| {
|
466
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
467
|
1
|
for n in 10..30 {
|
468
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
469
|
|
}
|
470
|
1
|
tx.remove::<u8, u8>(index_name, 20, None).expect("remove works");
|
471
|
|
|
472
|
1
|
let start = 13;
|
473
|
1
|
let to = 24;
|
474
|
1
|
let mut count = to - start - 1;
|
475
|
|
{
|
476
|
1
|
let base: TxIndexIter<u8, u8> = tx.range(index_name, start..=to).expect("range works correctly");
|
477
|
1
|
let mut iter = base.rev();
|
478
|
1
|
assert_eq!(iter.next().unwrap().0, to);
|
479
|
1
|
let mut last = None;
|
480
|
1
|
for x in iter {
|
481
|
1
|
last = Some(x.0.clone());
|
482
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
483
|
1
|
assert!(x.0 != 20u8);
|
484
|
1
|
count -= 1;
|
485
|
1
|
}
|
486
|
1
|
assert_eq!(Some(start), last);
|
487
|
1
|
assert_eq!(0, count);
|
488
|
1
|
}
|
489
|
|
|
490
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
491
|
1
|
prep.commit().expect("commit with index works");
|
492
|
1
|
});
|
493
|
1
|
}
|
494
|
|
|
495
|
|
#[test]
|
496
|
1
|
fn test_index_range_tx_change_committed() {
|
497
|
1
|
create_and_drop_index("test_index_range_tx", |persy, index_name| {
|
498
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
499
|
1
|
for n in 10..30 {
|
500
|
1
|
tx.put::<u8, u8>(index_name, n, 12).expect("put works correctly");
|
501
|
|
}
|
502
|
1
|
tx.prepare().expect("prepare works").commit().expect("commit works");
|
503
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
504
|
1
|
tx.remove::<u8, u8>(index_name, 20, None).expect("remove works");
|
505
|
1
|
tx.put::<u8, u8>(index_name, 31, 12).expect("put works correctly");
|
506
|
|
|
507
|
1
|
let start = 13;
|
508
|
1
|
let to = 31;
|
509
|
1
|
let mut count = to - start - 2;
|
510
|
|
{
|
511
|
1
|
let mut iter: TxIndexIter<u8, u8> = tx.range(index_name, start..=to).expect("range works correctly");
|
512
|
1
|
assert_eq!(iter.next().unwrap().0, start);
|
513
|
1
|
let mut last = None;
|
514
|
1
|
for x in iter {
|
515
|
1
|
last = Some(x.0.clone());
|
516
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
517
|
1
|
assert!(x.0 != 20u8);
|
518
|
1
|
count -= 1;
|
519
|
1
|
}
|
520
|
1
|
assert_eq!(Some(to), last);
|
521
|
1
|
assert_eq!(0, count);
|
522
|
1
|
}
|
523
|
1
|
let mut count = to - start - 2;
|
524
|
|
{
|
525
|
1
|
let base: TxIndexIter<u8, u8> = tx.range(index_name, start..=to).expect("range works correctly");
|
526
|
1
|
let mut iter = base.rev();
|
527
|
1
|
assert_eq!(iter.next().unwrap().0, to);
|
528
|
1
|
let mut last = None;
|
529
|
1
|
for x in iter {
|
530
|
1
|
last = Some(x.0.clone());
|
531
|
1
|
assert_eq!(x.1, Value::SINGLE(12));
|
532
|
1
|
assert!(x.0 != 20u8);
|
533
|
1
|
count -= 1;
|
534
|
1
|
}
|
535
|
1
|
assert_eq!(Some(start), last);
|
536
|
1
|
assert_eq!(0, count);
|
537
|
1
|
}
|
538
|
|
|
539
|
1
|
let prep = tx.prepare().expect("prepare with index works");
|
540
|
1
|
prep.commit().expect("commit with index works");
|
541
|
1
|
});
|
542
|
1
|
}
|
543
|
|
|
544
|
|
#[test]
|
545
|
1
|
fn test_mupltiple_put_same_value_get_tx() {
|
546
|
1
|
create_and_drop_index("create_drop_index", |persy, index_name| {
|
547
|
1
|
let mut tx = persy.begin().expect("begin transaction works");
|
548
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
549
|
|
|
550
|
1
|
tx.put::<u8, u8>(index_name, 10, 12).expect("put works correctly");
|
551
|
1
|
let res = tx.get::<u8, u8>(index_name, &10).expect("get works correctly");
|
552
|
1
|
assert_eq!(res, Some(Value::SINGLE(12)));
|
553
|
1
|
});
|
554
|
1
|
}
|
555
|
|
|
556
|
|
#[test]
|
557
|
1
|
pub fn test_list_indexes() {
|
558
|
1
|
create_and_drop("test_list_indexes", |persy| {
|
559
|
1
|
let mut tx = persy.begin().expect("error on transaction begin");
|
560
|
1
|
tx.create_index::<u8, u8>("one", ValueMode::REPLACE)
|
561
|
|
.expect("error on index creation");
|
562
|
1
|
tx.create_index::<u32, u32>("two", ValueMode::REPLACE)
|
563
|
|
.expect("error on index creation");
|
564
|
1
|
let fin = tx.prepare().expect("error on commit prepare");
|
565
|
1
|
fin.commit().expect("error on commit");
|
566
|
1
|
let indexes = persy.list_indexes().expect("list indexes works as expected");
|
567
|
1
|
assert_eq!(indexes.len(), 2);
|
568
|
1
|
let names = indexes.into_iter().map(|x| x.0).collect::<Vec<String>>();
|
569
|
1
|
assert!(names.contains(&"one".to_string()));
|
570
|
1
|
assert!(names.contains(&"two".to_string()));
|
571
|
1
|
let segments = persy.list_segments().expect("list segments works as expected");
|
572
|
1
|
assert_eq!(segments.len(), 0);
|
573
|
1
|
});
|
574
|
1
|
}
|
575
|
|
|
576
|
|
#[test]
|
577
|
1
|
pub fn test_list_indexes_tx() {
|
578
|
1
|
create_and_drop("test_list_indexes", |persy| {
|
579
|
1
|
let mut tx = persy.begin().expect("error on transaction begin");
|
580
|
1
|
tx.create_index::<u8, u8>("one", ValueMode::REPLACE)
|
581
|
|
.expect("error on index creation");
|
582
|
1
|
tx.create_index::<u32, u32>("two", ValueMode::REPLACE)
|
583
|
|
.expect("error on index creation");
|
584
|
1
|
let fin = tx.prepare().expect("error on commit prepare");
|
585
|
1
|
fin.commit().expect("error on commit");
|
586
|
1
|
let mut tx = persy.begin().expect("error on transaction begin");
|
587
|
1
|
tx.create_index::<u8, u8>("three", ValueMode::REPLACE)
|
588
|
|
.expect("error on index creation");
|
589
|
1
|
tx.drop_index("two").expect("error on index creation");
|
590
|
|
|
591
|
1
|
let indexes = tx.list_indexes().expect("list indexes works as expected");
|
592
|
1
|
assert_eq!(indexes.len(), 2);
|
593
|
1
|
let names = indexes.into_iter().map(|x| x.0).collect::<Vec<String>>();
|
594
|
1
|
assert!(names.contains(&"one".to_string()));
|
595
|
1
|
assert!(names.contains(&"three".to_string()));
|
596
|
|
|
597
|
1
|
let segments = tx.list_segments().expect("list segments works as expected");
|
598
|
1
|
assert_eq!(segments.len(), 0);
|
599
|
1
|
});
|
600
|
1
|
}
|