Documentation
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
use std::env;
use std::fs::{self};
use std::path::{PathBuf};
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::sync::{Once, ONCE_INIT};
use std::thread;

use libc::c_int;

use core::{self, EnvBuilder, DbFlags, MdbValue, EnvNoMemInit, EnvNoMetaSync, KeyExists};
use ffi::MDB_val;
use traits::FromMdbValue;

const USER_DIR: u32 = 0o777;
static TEST_ROOT_DIR: &'static str = "test-dbs";
static NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
static INIT_DIR_ONCE: Once = ONCE_INIT;

fn next_path() -> PathBuf {
    let out_dir = PathBuf::from(&env::var("OUT_DIR").unwrap());
    let root_dir = out_dir.join(TEST_ROOT_DIR);

    INIT_DIR_ONCE.call_once(|| {
        if let Ok(root_meta) = fs::metadata(root_dir.clone()) {
            if root_meta.is_dir() {
                let _ = fs::remove_dir_all(&root_dir);
            }
        }
        assert!(fs::create_dir_all(&root_dir).is_ok());
    });

    let cur_id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
    let res = root_dir.join(&format!("db-{}", cur_id));
    println!("Testing db in {}", res.display());
    res
}

#[test]
fn test_environment() {
    let mut env = EnvBuilder::new()
        .max_readers(33)
        .open(&next_path(), USER_DIR).unwrap();

    env.sync(true).unwrap();

    let test_flags = EnvNoMemInit | EnvNoMetaSync;

    env.set_flags(test_flags, true).unwrap();
    let new_flags = env.get_flags().unwrap();
    assert!((new_flags & test_flags) == test_flags, "Get flags != set flags");

    let db = env.get_default_db(DbFlags::empty()).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let key = "hello";
    let value = "world";

    db.set(&key, &value).unwrap();

    let v = db.get::<&str>(&key).unwrap();
    assert!(v == value, "Written {} and read {}", &value, &v);
}

#[test]
fn test_single_values() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(DbFlags::empty()).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";
    let test_data1 = "value1";
    let test_data2 = "value2";

    assert!(db.get::<()>(&test_key1).is_err(), "Key shouldn't exist yet");

    assert!(db.set(&test_key1, &test_data1).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data1, "Data written differs from data read");

    assert!(db.set(&test_key1, &test_data2).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data2, "Data written differs from data read");

    assert!(db.del(&test_key1).is_ok());
    assert!(db.get::<()>(&test_key1).is_err(), "Key should be deleted");
}

#[test]
fn test_multiple_values() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(core::DbAllowDups).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";
    let test_data1 = "value1";
    let test_data2 = "value2";

    assert!(db.get::<()>(&test_key1).is_err(), "Key shouldn't exist yet");

    assert!(db.set(&test_key1, &test_data1).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data1, "Data written differs from data read");

    assert!(db.set(&test_key1, &test_data2).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data1, "It should still return first value");

    assert!(db.del_item(&test_key1, &test_data1).is_ok());

    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data2, "It should return second value");
    assert!(db.del(&test_key1).is_ok());

    assert!(db.get::<()>(&test_key1).is_err(), "Key shouldn't exist anymore!");
}

#[test]
fn test_append_duplicate() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(core::DbAllowDups).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";
    let test_data1 = "value1";
    let test_data2 = "value2";

    assert!(db.append(&test_key1, &test_data1).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data1, "Data written differs from data read");

    assert!(db.append_duplicate(&test_key1, &test_data2).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data1, "It should still return first value");

    assert!(db.del_item(&test_key1, &test_data1).is_ok());

    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data2, "It should return second value");

    match db.append_duplicate(&test_key1, &test_data1).err().unwrap() {
        KeyExists => (),
        _ => panic!("Expected KeyExists error")
    }
}

#[test]
fn test_insert_values() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(DbFlags::empty()).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";
    let test_data1 = "value1";
    let test_data2 = "value2";

    assert!(db.get::<()>(&test_key1).is_err(), "Key shouldn't exist yet");

    assert!(db.set(&test_key1, &test_data1).is_ok());
    let v = db.get::<&str>(&test_key1).unwrap();
    assert!(v == test_data1, "Data written differs from data read");

    assert!(db.insert(&test_key1, &test_data2).is_err(), "Inserting should fail if key exists");

    assert!(db.del(&test_key1).is_ok());
    assert!(db.get::<()>(&test_key1).is_err(), "Key should be deleted");

    assert!(db.insert(&test_key1, &test_data2).is_ok(), "Inserting should succeed");
}

#[test]
fn test_stat() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    // ~ the two dataset; each to end up in its own database
    let dss = [
        // ~ keep the "default db" dataset here at the beginning (see
        // the assertion at the end of this test)
        ("", vec![("default", "db"), ("has", "some"), ("extras", "prepared")]),
        ("db1", vec![("foo", "bar"), ("quux", "qak")]),
        ("db2", vec![("a", "abc"), ("b", "bcd"), ("c", "cde"), ("d", "def")]),
        ("db3", vec![("hip", "hop")])];

    // ~ create each db, populate it, and assert db.stat() for each seperately
    for &(name, ref ds) in &dss {
        let db = env.create_db(name, DbFlags::empty()).unwrap();
        let tx = env.new_transaction().unwrap();
        {
            let db = tx.bind(&db);
            for &(k, v) in ds {
                assert!(db.set(&k, &v).is_ok());
            }
            // ~ verify the expected number of entries (key/value pairs) in the db
            let stat = db.stat().unwrap();
            assert_eq!(ds.len() as usize, stat.ms_entries);
        }
        tx.commit().unwrap();
    }

    // ~ now verify the number of data items in this _environment_ (this
    // is the number key/value pairs in the default database plus the
    // number of other databases)
    let stat = env.stat().unwrap();
    assert_eq!(dss[0].1.len() + dss[1..].len(), stat.ms_entries);
}


#[test]
fn test_cursors() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(core::DbAllowDups).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";
    let test_key2 = "key2";
    let test_values: Vec<&str> = vec!("value1", "value2", "value3", "value4");

    assert!(db.get::<()>(&test_key1).is_err(), "Key shouldn't exist yet");

    for t in test_values.iter() {
        let _ = db.set(&test_key1, t);
        let _ = db.set(&test_key2, t);
    }

    let mut cursor = db.new_cursor().unwrap();
    assert!(cursor.to_first().is_ok());

    assert!(cursor.to_key(&test_key1).is_ok());
    assert!(cursor.item_count().unwrap() == 4);

    assert!(cursor.del_item().is_ok());
    assert!(cursor.item_count().unwrap() == 3);

    assert!(cursor.to_key(&test_key1).is_ok());
    let new_value = "testme";

    assert!(cursor.replace(&new_value).is_ok());
    {
        let (_, v) = cursor.get::<(), &str>().unwrap();
        // NOTE: this asserting will work once new_value is
        // of the same length as it is inplace change
        assert!(v == new_value);
    }

    assert!(cursor.del_all().is_ok());
    assert!(cursor.to_key(&test_key1).is_err());

    assert!(cursor.to_key(&test_key2).is_ok());
}


#[test]
fn test_cursor_item_manip() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(core::DbAllowDups | core::DbAllowIntDups).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";

    assert!(db.set(&test_key1, &3u64).is_ok());

    let mut cursor = db.new_cursor().unwrap();
    assert!(cursor.to_key(&test_key1).is_ok());

    let values: Vec<u64> = db.item_iter(&test_key1).unwrap()
        .map(|cv| cv.get_value::<u64>())
        .collect();
    assert_eq!(values, vec![3u64]);

    assert!(cursor.add_item(&4u64).is_ok());
    assert!(cursor.add_item(&5u64).is_ok());

    let values: Vec<u64> = db.item_iter(&test_key1).unwrap()
        .map(|cv| cv.get_value::<u64>())
        .collect();
    assert_eq!(values, vec![3u64, 4, 5]);

    assert!(cursor.replace(&6u64).is_ok());
    let values: Vec<u64> = db.item_iter(&test_key1).unwrap()
        .map(|cv| cv.get_value::<u64>())
        .collect();

    assert_eq!(values, vec![3u64, 4, 6]);
}

fn as_slices(v: &Vec<String>) -> Vec<&str> {
    v.iter().map(|s| &s[..]).collect::<Vec<&str>>()
}

#[test]
fn test_item_iter() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let db = env.get_default_db(core::DbAllowDups).unwrap();
    let txn = env.new_transaction().unwrap();
    let db = txn.bind(&db);

    let test_key1 = "key1";
    let test_data1 = "value1";
    let test_data2 = "value2";
    let test_key2 = "key2";
    let test_key3 = "key3";

    assert!(db.set(&test_key1, &test_data1).is_ok());
    assert!(db.set(&test_key1, &test_data2).is_ok());
    assert!(db.set(&test_key2, &test_data1).is_ok());

    let iter = db.item_iter(&test_key1).unwrap();
    let values: Vec<String> = iter.map(|cv| cv.get_value::<String>()).collect();
    assert_eq!(as_slices(&values), vec![test_data1, test_data2]);

    let iter = db.item_iter(&test_key2).unwrap();
    let values: Vec<String> = iter.map(|cv| cv.get_value::<String>()).collect();
    assert_eq!(as_slices(&values), vec![test_data1]);

    let iter = db.item_iter(&test_key3).unwrap();
    let values: Vec<String> = iter.map(|cv| cv.get_value::<String>()).collect();
    assert_eq!(values.len(), 0);
}

#[test]
fn test_db_creation() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();
    assert!(env.create_db("test-db", DbFlags::empty()).is_ok());
}

#[test]
fn test_read_only_txn() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();
    env.get_reader().unwrap();
}

#[test]
fn test_cursor_in_txns() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    {
        let db = env.create_db("test1", core::DbAllowDups | core::DbAllowIntDups).unwrap();
        let txn = env.new_transaction().unwrap();
        {
            let db = txn.bind(&db);

            let cursor = db.new_cursor();
            assert!(cursor.is_ok());
        }
        assert!(txn.commit().is_ok());
    }

    {
        let db = env.create_db("test1", core::DbAllowDups | core::DbAllowIntDups).unwrap();
        let txn = env.new_transaction().unwrap();
        {
            let db = txn.bind(&db);

            let cursor = db.new_cursor();
            assert!(cursor.is_ok());
        }
        assert!(txn.commit().is_ok());
    }
}

#[test]
fn test_multithread_env() {
    let env = EnvBuilder::new()
        .max_dbs(5)
        .open(&next_path(), USER_DIR)
        .unwrap();

    let shared_env = env.clone();
    let key = "key";
    let value = "value";

    let _ = thread::spawn(move || {
        let db = shared_env.create_db("test1", DbFlags::empty()).unwrap();
        let txn = shared_env.new_transaction().unwrap();
        {
            let db = txn.bind(&db);
            assert!(db.set(&key, &value).is_ok());
        }
        assert!(txn.commit().is_ok());
    }).join();

    let db = env.create_db("test1", DbFlags::empty()).unwrap();
    let txn = env.get_reader().unwrap();
    let db = txn.bind(&db);
    let value2: String = db.get(&key).unwrap();
    assert_eq!(value, value2);
}

#[test]
fn test_keyrange_to() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbIntKey).unwrap();
    let keys:   Vec<u64> = vec![1, 2, 3];
    let values: Vec<u64> = vec![5, 6, 7];

    // to avoid problems caused by updates
    assert_eq!(keys.len(), values.len());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for (k, v) in keys.iter().zip(values.iter()) {
            assert!(db.set(k, v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        let last_idx = keys.len() - 1;
        let last_key: u64 = keys[last_idx];
        // last key is excluded
        let iter = db.keyrange_to(&last_key).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        assert_eq!(res, &values[..last_idx]);
    }
}

/// Test that selecting a key range with an upper bound smaller than
/// the smallest key in the db yields an empty range.
#[test]
fn test_keyrange_to_init_cursor() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbIntKey).unwrap();
    let recs: Vec<(u64, u64)> = vec![(10, 50), (11, 60), (12, 70)];

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for &(k, v) in recs.iter() {
            assert!(db.set(&k, &v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        // last key is excluded
        let upper_bound: u64 = 1;
        let iter = db.keyrange_to(&upper_bound).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        assert_eq!(res, &[]);
    }
}

#[test]
fn test_keyrange_from() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbIntKey).unwrap();
    let keys:   Vec<u64> = vec![1, 2, 3];
    let values: Vec<u64> = vec![5, 6, 7];

    // to avoid problems caused by updates
    assert_eq!(keys.len(), values.len());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for (k, v) in keys.iter().zip(values.iter()) {
            assert!(db.set(k, v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        let start_idx = 1; // second key
        let last_key: u64 = keys[start_idx];
        let iter = db.keyrange_from(&last_key).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        assert_eq!(res, &values[start_idx..]);
    }
}

/// Test that selecting a key range with a lower bound greater than
/// the biggest key in the db yields an empty range.
#[test]
fn test_keyrange_from_init_cursor() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbIntKey).unwrap();
    let recs: Vec<(u64, u64)> = vec![(10, 50), (11, 60), (12, 70)];

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for &(k, v) in recs.iter() {
            assert!(db.set(&k, &v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        // last key is excluded
        let lower_bound = recs[recs.len()-1].0 + 1;
        let iter = db.keyrange_from(&lower_bound).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        assert_eq!(res, &[]);
    }
}

#[test]
fn test_keyrange() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbAllowDups | core::DbIntKey).unwrap();
    let keys:   Vec<u64> = vec![ 1,  2,  3,  4,  5,  6];
    let values: Vec<u64> = vec![10, 11, 12, 13, 14, 15];

    // to avoid problems caused by updates
    assert_eq!(keys.len(), values.len());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for (k, v) in keys.iter().zip(values.iter()) {
            assert!(db.set(k, v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        let start_idx = 1;
        let end_idx = 3;
        let iter = db.keyrange(&keys[start_idx], &keys[end_idx]).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();

         //  +1 as Rust slices do not include end
        assert_eq!(res, &values[start_idx.. end_idx + 1]);
    }
}

/// Test that select a key range outside the available data correctly
/// yields an empty range.
#[test]
fn test_keyrange_init_cursor() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbAllowDups | core::DbIntKey).unwrap();
    let keys:   Vec<u64> = vec![ 1,  2,  3,  4,  5,  6];
    let values: Vec<u64> = vec![10, 11, 12, 13, 14, 15];

    // to avoid problems caused by updates
    assert_eq!(keys.len(), values.len());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for (k, v) in keys.iter().zip(values.iter()) {
            assert!(db.set(k, v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    // test the cursor initialization before the available data range
    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        let start_key = 0u64;
        let end_key = 0u64;
        let iter = db.keyrange(&start_key, &end_key).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        assert_eq!(res, &[]);
    }

    // test the cursor initialization after the available data range
    {
        let db = txn.bind(&db);

        let start_key = 10;
        let end_key = 20;
        let iter = db.keyrange(&start_key, &end_key).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        assert!(res.is_empty());
    }
}

#[test]
fn test_keyrange_from_to() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db = env.get_default_db(core::DbAllowDups | core::DbIntKey).unwrap();
    let recs: Vec<(u64, u64)> = vec![(10, 11), (20, 21), (30, 31), (40, 41), (50, 51)];

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db);
        for &(k, v) in recs.iter() {
            assert!(db.set(&k, &v).is_ok());
        }
    }
    assert!(txn.commit().is_ok());

    let txn = env.get_reader().unwrap();
    {
        let db = txn.bind(&db);

        let start_idx = 1;
        let end_idx = 3;
        let iter = db.keyrange_from_to(&recs[start_idx].0, &recs[end_idx].0).unwrap();

        let res: Vec<_> = iter.map(|cv| cv.get_value::<u64>()).collect();
        // ~ end_key must be excluded here
        let exp: Vec<_> = recs[start_idx .. end_idx].iter().map(|x| x.1).collect();
        assert_eq!(res, exp);
    }
}

#[test]
fn test_readonly_env() {
    let recs: Vec<(u32,u32)> = vec![(10, 11), (11, 12), (12, 13), (13,14)];

    // ~ first create a new read-write environment with its default
    // database containing a few entries
    let path = next_path();
    {
        let rw_env = EnvBuilder::new().open(&path, USER_DIR).unwrap();
        let dbh = rw_env.get_default_db(core::DbIntKey).unwrap();
        let tx = rw_env.new_transaction().unwrap();
        {
            let db = tx.bind(&dbh);
            for &rec in recs.iter() {
                db.set(&rec.0, &rec.1).unwrap();
            }
        }
        tx.commit().unwrap();
    }

    // ~ now re-open the previously created database in read-only mode
    // and iterate the key/value pairs
    let ro_env = EnvBuilder::new()
        .flags(core::EnvCreateReadOnly)
        .open(&path, USER_DIR).unwrap();
    let dbh = ro_env.get_default_db(core::DbIntKey).unwrap();
    assert!(ro_env.new_transaction().is_err());
    let mut tx = ro_env.get_reader().unwrap();
    {
        let db = tx.bind(&dbh);
        let kvs: Vec<(u32,u32)> = db.iter().unwrap().map(|c| c.get()).collect();
        assert_eq!(recs, kvs);
    }
    tx.abort();
}

unsafe fn negative_if_odd_i32_val(val: *const MDB_val) -> i32 {
    let v = MdbValue::from_raw(val);
    let i = i32::from_mdb_value(&v);
    if i % 2 == 0 {
        i
    } else {
        -i
    }
}

// A nonsensical comparison function that sorts differently that byte-by-byte comparison
extern "C" fn negative_odd_cmp_fn(lhs_val: *const MDB_val, rhs_val: *const MDB_val) -> c_int {
    unsafe {
        let lhs = negative_if_odd_i32_val(lhs_val);
        let rhs = negative_if_odd_i32_val(rhs_val);
        lhs - rhs
    }
}

#[test]
fn test_compare() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db_handle = env.get_default_db(DbFlags::empty()).unwrap();
    let txn = env.new_transaction().unwrap();
    let val: i32 = 0;
    {
        let db = txn.bind(&db_handle);
        assert!(db.set_compare(negative_odd_cmp_fn).is_ok());

        let i: i32 = 2;
        db.set(&i, &val).unwrap();
        let i: i32 = 3;
        db.set(&i, &val).unwrap();
    }
    assert!(txn.commit().is_ok());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db_handle);
        let i: i32 = 4;
        db.set(&i, &val).unwrap();
        let i: i32 = 5;
        db.set(&i, &val).unwrap();
    }
    assert!(txn.commit().is_ok());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db_handle);
        let keys: Vec<_> = db.iter().unwrap().map(|cv| cv.get_key::<i32>()).collect();
        assert_eq!(keys, [5, 3, 2, 4]);
    }
    assert!(txn.commit().is_ok());
}

#[test]
fn test_dupsort() {
    let env = EnvBuilder::new().open(&next_path(), USER_DIR).unwrap();
    let db_handle = env.get_default_db(core::DbAllowDups).unwrap();
    let txn = env.new_transaction().unwrap();
    let key: i32 = 0;
    {
        let db = txn.bind(&db_handle);
        assert!(db.set_dupsort(negative_odd_cmp_fn).is_ok());

        let i: i32 = 2;
        db.set(&key, &i).unwrap();
        let i: i32 = 3;
        db.set(&key, &i).unwrap();
    }
    assert!(txn.commit().is_ok());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db_handle);
        let i: i32 = 4;
        db.set(&key, &i).unwrap();
        let i: i32 = 5;
        db.set(&key, &i).unwrap();
    }
    assert!(txn.commit().is_ok());

    let txn = env.new_transaction().unwrap();
    {
        let db = txn.bind(&db_handle);
        let vals: Vec<_> = db.item_iter(&key).unwrap().map(|cv| cv.get_value::<i32>()).collect();
        assert_eq!(vals, [5, 3, 2, 4]);
    }
    assert!(txn.commit().is_ok());
}

// ~ see #29
#[test]
fn test_conversion_to_vecu8() {
    let rec: (u32, Vec<u8>) = (10, vec![1,2,3,4,5]);

    let path = next_path();
    let env = EnvBuilder::new().open(&path, USER_DIR).unwrap();
    let db = env.get_default_db(core::DbIntKey).unwrap();

    // ~ add our test record
    {
        let tx = env.new_transaction().unwrap();
        {
            let db = tx.bind(&db);
            db.set(&rec.0, &rec.1).unwrap();
        }
        tx.commit().unwrap();
    }

    // ~ validate the behavior
    let tx = env.new_transaction().unwrap();
    {
        let db = tx.bind(&db);
        {
            // ~ now retrieve a Vec<u8> and make sure it is dropped
            // earlier than our database handle
            let xs: Vec<u8> = db.get(&rec.0).unwrap();
            assert_eq!(rec.1, xs);
        }
    }
    tx.abort();
}

// ~ see #29
#[test]
fn test_conversion_to_string() {
    let rec: (u32, String) = (10, "hello, world".to_owned());

    let path = next_path();
    let env = EnvBuilder::new().open(&path, USER_DIR).unwrap();
    let db = env.get_default_db(core::DbIntKey).unwrap();

    // ~ add our test record
    {
        let tx = env.new_transaction().unwrap();
        {
            let db = tx.bind(&db);
            db.set(&rec.0, &rec.1).unwrap();
        }
        tx.commit().unwrap();
    }

    // ~ validate the behavior
    let tx = env.new_transaction().unwrap();
    {
        let db = tx.bind(&db);
        {
            // ~ now retrieve a String and make sure it is dropped
            // earlier than our database handle
            let xs: String = db.get(&rec.0).unwrap();
            assert_eq!(rec.1, xs);
        }
    }
    tx.abort();
}

/*
#[test]
fn test_compilation_of_moved_items() {
    let path = Path::new("dbcom");
    test_db_in_path(&next_path(), || {
        let mut env = EnvBuilder::new()
            .max_dbs(5)
            .open(&next_path(), USER_DIR)
            .unwrap();

        let db = env.get_default_db(DbFlags::empty()).unwrap();
        let mut txn = env.new_transaction().unwrap();

        txn.commit();

        let test_key1 = "key1";
        let test_data1 = "value1";

        assert!(db.get::<()>(&txn, &test_key1).is_err(), "Key shouldn't exist yet"); // ~ERROR: use of moved value
    })
}
*/