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
#[allow(non_camel_case_types,dead_code,non_snake_case,private_in_public)]
mod ffi;
use std::path::Path;
use std::ffi::CString;
use std::fmt;
use std::mem::{transmute, forget};
use std::cell::RefCell;

use std::collections::HashMap;

#[derive(Debug)]
#[derive(PartialEq)]
#[repr(C)]
pub enum Error {
    Nomem = -2,
    PathTooLong = -3,
    UnknownField = -4,
    UnknownUuid = -5,
    InvalidTrailId = -6,
    HandleIsNull = -7,
    HandleAlreadyOpened = -8,
    UnknownOption = -9,
    InvalidOptionValue = -10,
    InvalidUuid = -11,
    IoOpen = -65,
    IoClose = -66,
    IoWrite = -67,
    IoRead = -68,
    IoTruncate = -69,
    IoPackage = -70,
    InvalidInfoFile = -129,
    InvalidVersionFile = -130,
    IncompatibleVersion = -131,
    InvalidFieldsFile = -132,
    InvalidUuidsFile = -133,
    InvalidCodebookFile = -134,
    InvalidTrailsFile = -135,
    InvalidLexiconFile = -136,
    InvalidPackage = -137,
    TooManyFields = -257,
    DuplicateFields = -258,
    InvalidFieldname = -259,
    TooManyTrails = -260,
    ValueTooLong = -261,
    AppendFieldsMismatch = -262,
    LexiconTooLarge = -263,
    TimestampTooLarge = -264,
    TrailTooLong = -265,
    OnlyDiffFilter = -513,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match *self {
            Error::Nomem => "Nomem",
            Error::PathTooLong => "PathTooLong",
            Error::UnknownField => "UnknownField",
            Error::UnknownUuid => "UnknownUuid",
            Error::InvalidTrailId => "InvalidTrailId",
            Error::HandleIsNull => "HandleIsNull",
            Error::HandleAlreadyOpened => "HandleAlreadyOpened",
            Error::UnknownOption => "UnknownOption",
            Error::InvalidOptionValue => "InvalidOptionValue",
            Error::InvalidUuid => "InvalidUuid",
            Error::IoOpen => "IoOpen",
            Error::IoClose => "IoClose",
            Error::IoWrite => "IoWrite",
            Error::IoRead => "IoRead",
            Error::IoTruncate => "IoTruncate",
            Error::IoPackage => "IoPackage",
            Error::InvalidInfoFile => "InvalidInfoFile",
            Error::InvalidVersionFile => "InvalidVersionFile",
            Error::IncompatibleVersion => "IncompatibleVersion",
            Error::InvalidFieldsFile => "InvalidFieldsFile",
            Error::InvalidUuidsFile => "InvalidUuidsFile",
            Error::InvalidCodebookFile => "InvalidCodebookFile",
            Error::InvalidTrailsFile => "InvalidTrailsFile",
            Error::InvalidLexiconFile => "InvalidLexiconFile",
            Error::InvalidPackage => "InvalidPackage",
            Error::TooManyFields => "TooManyFields",
            Error::DuplicateFields => "DuplicateFields",
            Error::InvalidFieldname => "InvalidFieldname",
            Error::TooManyTrails => "TooManyTrails",
            Error::ValueTooLong => "ValueTooLong",
            Error::AppendFieldsMismatch => "AppendFieldsMismatch",
            Error::LexiconTooLarge => "LexiconTooLarge",
            Error::TimestampTooLarge => "TimestampTooLarge",
            Error::TrailTooLong => "TrailTooLong",
            Error::OnlyDiffFilter => "OnlyDiffFilter",
        };
        write!(f, "Error::{}", s)
    }
}

/// Convert a `tdb_error` either to either a `Ok(T)` or `Err(Error)`
fn wrap_tdb_err<T>(err: ffi::tdb_error, val: T) -> Result<T, Error> {
    match err {
        ffi::tdb_error::TDB_ERR_OK => Ok(val),
        _ => Err(unsafe { transmute(err) }),
    }
}

/// A timestamp must provided with added events.
pub type Timestamp = u64;
/// The type returned by `Db::version`.
pub type Version = u64;
/// An integer type that identifies an individual traul in a `Db`.
pub type TrailId = u64;
/// A [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier)
/// must be included with all added events.
pub type Uuid = [u8; 16];

/// TODO: Document me
#[derive(Debug,Clone,Copy,PartialEq,Eq,Hash)]
pub struct Item(pub u64);
/// TODO: Document me
pub type Value = u64;
/// TODO: Document me
pub type Field = u32;



/// A structure that represents a `TrailDB` constructor.
///
/// A constructor lives in RAM. All events are added to the constructor.
/// After being written to disk, the `TrailDB` is immutable.
///
/// # Examples
///
/// ```
/// use traildb::{Constructor, Uuid};
/// use std::path::Path;
///
/// // Names relevent to our event type
/// let db_fields = ["user", "action"];
/// // Where to write our dabase to disk when we're done adding events to it
/// let db_path = Path::new("my_traildb");
/// // Create a constructor
/// let mut cons = Constructor::new(db_path, &db_fields).unwrap();
///
/// // Let's gather necessary data to create and event
/// // Time is stored as a `u64`. What that represents (e.g. UNIX time) is up to you
/// let timestamp: u64 = 0;
/// // Every trail need a UUID
/// let uuid: Uuid = [0u8;16];
/// // The values for for fields `"user"` and `"action"`
/// let event_vals = ["Alice", "login"];
///
/// // Now lets add our event data to the constructor
/// assert!(cons.add(&uuid, timestamp, &event_vals).is_ok());
///
/// // Finally, let's write our database to disk by calling `finalize`
/// assert!(cons.finalize().is_ok());
/// ```
pub struct Constructor {
    obj: *mut ffi::tdb_cons,
}

impl Constructor {
    /// Create a new TrailDB constructor.
    pub fn new(path: &Path, fields: &[&str]) -> Result<Self, Error> {

        let mut field_ptrs = Vec::new();
        for f in fields.iter() {
            let s = CString::new(*f).unwrap();
            field_ptrs.push(s.as_ptr());
            forget(s);
        }

        let ptr = unsafe { ffi::tdb_cons_init() };
        let ret = unsafe {
            ffi::tdb_cons_open(ptr,
                               path_cstr(path).as_ptr(),
                               field_ptrs.as_slice().as_ptr() as *mut *const i8,
                               field_ptrs.len() as u64)
        };
        wrap_tdb_err(ret, Constructor { obj: ptr })
    }

    /// Add an event to the constructor.
    pub fn add(&mut self, uuid: &Uuid, timestamp: Timestamp, values: &[&str]) -> Result<(), Error> {
        let mut val_ptrs = Vec::new();
        let mut val_lens = Vec::new();
        for v in values.iter() {
            val_ptrs.push(v.as_ptr());
            val_lens.push(v.len() as u64);
        }
        let ret = unsafe {
            ffi::tdb_cons_add(self.obj,
                              uuid.as_ptr() as *mut u8,
                              timestamp,
                              val_ptrs.as_slice().as_ptr() as *mut *const i8,
                              val_lens.as_slice().as_ptr() as *const u64)
        };
        wrap_tdb_err(ret, ())
    }

    /// Close a constructor without writing it to disk.
    pub fn close(&mut self) {
        unsafe { ffi::tdb_cons_close(self.obj) };
    }

    /// Write the TrailDB to disk and close it.
    pub fn finalize(&mut self) -> Result<(), Error> {
        let ret = unsafe { ffi::tdb_cons_finalize(self.obj) };
        wrap_tdb_err(ret, ())
    }

    /// Combine an already finalized TrailDB with a constructor.
    pub fn append(&mut self, db: &Db) -> Result<(), Error> {
        let ret = unsafe { ffi::tdb_cons_append(self.obj, transmute(db)) };
        wrap_tdb_err(ret, ())
    }
}

impl Drop for Constructor {
    fn drop(&mut self) {
        unsafe { ffi::tdb_cons_close(self.obj) };
    }
}


pub struct Db<'a> {
    obj: &'a mut ffi::tdb,
}

impl<'a> Db<'a> {
    pub fn open(path: &Path) -> Result<Self, Error> {
        let ptr = unsafe { ffi::tdb_init() };
        let ret = unsafe { ffi::tdb_open(ptr, path_cstr(path).as_ptr()) };
        unsafe { wrap_tdb_err(ret, Db { obj: transmute(ptr) }) }
    }

    pub fn close(&mut self) {
        unsafe {
            ffi::tdb_close(self.obj);
        }
    }

    pub fn num_trails(&self) -> u64 {
        unsafe { ffi::tdb_num_trails(self.obj) }
    }

    pub fn num_events(&self) -> u64 {
        unsafe { ffi::tdb_num_events(self.obj) }
    }

    pub fn num_fields(&self) -> u64 {
        unsafe { ffi::tdb_num_fields(self.obj) }
    }

    pub fn min_timestamp(&self) -> Timestamp {
        unsafe { ffi::tdb_min_timestamp(self.obj) }
    }

    pub fn max_timestamp(&self) -> Timestamp {
        unsafe { ffi::tdb_max_timestamp(self.obj) }
    }

    pub fn version(&self) -> Version {
        unsafe { ffi::tdb_version(self.obj) }
    }

    pub fn will_need(&self) {
        unsafe { ffi::tdb_willneed(self.obj) };
    }

    pub fn dont_need(&self) {
        unsafe { ffi::tdb_dontneed(self.obj) };
    }

    pub fn get_trail(&self, trail_id: TrailId) -> Option<Trail> {
        let mut cursor = self.cursor();
        if cursor.get_trail(trail_id).is_err() {
            return None;
        };
        Some(Trail {
                 id: trail_id,
                 cursor: cursor,
             })
    }

    pub fn get_trail_id(&self, uuid: &Uuid) -> Option<TrailId> {
        let mut id: TrailId = 0;
        let ret = unsafe {
            ffi::tdb_get_trail_id(self.obj, uuid.as_ptr() as *mut u8, &mut id as *mut TrailId)
        };
        match ret {
            ffi::tdb_error::TDB_ERR_OK => Some(id),
            _ => None,
        }
    }

    pub fn get_uuid(&self, trail_id: TrailId) -> Option<&Uuid> {
        unsafe {
            let ptr = ffi::tdb_get_uuid(self.obj, trail_id) as *const [u8; 16];
            ptr.as_ref()
        }
    }

    pub fn cursor(&'a self) -> Cursor<'a> {
        unsafe {
            let ptr = ffi::tdb_cursor_new(self.obj);
            Cursor { obj: transmute(ptr) }
        }
    }

    pub fn iter(&'a self) -> DbIter<'a> {
        DbIter { pos: 0, db: self }
    }

    pub fn get_item_value(&'a self, item: Item) -> Option<&'a str> {
        unsafe {
            let mut len = 0u64;
            let ptr = ffi::tdb_get_item_value(self.obj, transmute(item), &mut len);
            if len > 0 {
                let s = std::slice::from_raw_parts(ptr as *const u8, len as usize);
                Some(std::str::from_utf8_unchecked(s))
            } else {
                None
            }
        }
    }

    pub fn get_item(&'a self, field: Field, value: &str) -> Option<Item> {
        unsafe {
            let item = ffi::tdb_get_item(self.obj,
                                         transmute(field),
                                         value.as_ptr() as *const i8,
                                         value.len() as u64);

            if item == 0 { None } else { Some(Item(item)) }
        }
    }

    pub fn get_field_name(&'a self, field: Field) -> Option<&'a str> {
        unsafe {
            let ptr = ffi::tdb_get_field_name(self.obj, field);
            match std::ffi::CStr::from_ptr(ptr).to_str() {
                Ok(s) => Some(s),
                Err(_) => None,
            }
        }
    }

    pub fn lexicon_size(&'a self, field: Field) -> u64 {
        unsafe { ffi::tdb_lexicon_size(self.obj, field) }
    }

    pub fn lexicon(&'a self, field: Field) -> Vec<&'a str> {
        let mut vec = Vec::with_capacity(self.lexicon_size(field) as usize);
        for i in 1..self.lexicon_size(field) {
            let value = unsafe {
                let mut len = 0u64;
                let ptr = ffi::tdb_get_value(self.obj, field, i, &mut len);
                let s = std::slice::from_raw_parts(ptr as *const u8, len as usize);
                std::str::from_utf8_unchecked(s)
            };
            vec.push(value);
        }
        vec
    }

    pub fn fields(&'a self) -> HashMap<&str, Field> {
        let num_fields = self.num_fields();
        let mut fields: HashMap<&'a str, Field> = HashMap::with_capacity(num_fields as usize);

        for i in 1..num_fields {
            let field: Field = i as u32;
            let name = self.get_field_name(field).unwrap().clone();
            fields.insert(name, field);
        }
        fields
    }
}


impl<'a> Drop for Db<'a> {
    fn drop(&mut self) {
        unsafe { ffi::tdb_close(self.obj) };
    }
}




pub struct DbIter<'a> {
    pos: u64,
    db: &'a Db<'a>,
}

impl<'a> Iterator for DbIter<'a> {
    type Item = Trail<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let id = self.pos;
        self.pos += 1;
        let mut cursor = self.db.cursor();
        match cursor.get_trail(id) {
            Err(_) => None,
            Ok(()) => {
                let trail = Trail {
                    id: id,
                    cursor: cursor,
                };
                Some(trail)
            }
        }
    }
}



/// A cursor allows you to iterate over the events in a single trail,
/// decoding a batch from the optimized storage format when necessary.
///
/// A cursor can be re-used across trails, by calling
/// `cursor.get_trail()` with the `TrailId`. Initializing a cursor is
/// expensive and using `DbIter` which initializes a new cursor for
/// each trail will be much slower, than re-using a cursor.
pub struct Cursor<'a> {
    obj: &'a mut ffi::tdb_cursor,
}



impl<'a> Cursor<'a> {
    pub fn get_trail(&mut self, trail_id: TrailId) -> Result<(), Error> {
        let ret = unsafe { ffi::tdb_get_trail(self.obj, trail_id) };
        wrap_tdb_err(ret, ())
    }

    pub fn len(&mut self) -> u64 {
        unsafe { ffi::tdb_get_trail_length(self.obj) }
    }

    pub fn set_filter(&mut self, filter: &EventFilter) -> Result<(), Error> {
        let ret = unsafe { ffi::tdb_cursor_set_event_filter(self.obj, filter.obj) };
        wrap_tdb_err(ret, ())
    }
}

impl<'a> Drop for Cursor<'a> {
    fn drop(&mut self) {
        unsafe { ffi::tdb_cursor_free(self.obj) };
    }
}

impl<'a> Iterator for Cursor<'a> {
    type Item = Event<'a>;

    fn next(&mut self) -> Option<Event<'a>> {
        unsafe {
            let e = ffi::tdb_cursor_next(self.obj);
            Event::from_tdb_event(e)
        }
    }
}

/// A `MultiCursor` allows you to iterate over multiple cursors at the
/// same time, even from different TrailDBs.
pub struct MultiCursor<'a> {
    obj: &'a mut ffi::tdb_multi_cursor,
}

impl<'a> MultiCursor<'a> {
    /// Open a cursor in each of the Dbs passed. If you want multiple
    /// cursors for the same db, include it multiple times.
    pub fn new(cursors: &[RefCell<Cursor<'a>>]) -> MultiCursor<'a> {
        let mut ptrs: Vec<*const ffi::tdb_cursor> = vec![];
        for refcell in cursors.iter() {
            let cursor = refcell.borrow();
            let ptr: *const ffi::tdb_cursor = cursor.obj;
            ptrs.push(ptr);
        }

        unsafe {
            let ptr = ffi::tdb_multi_cursor_new(ptrs.as_slice().as_ptr() as
                                                *mut *mut ffi::tdb_cursor,
                                                ptrs.len() as u64);
            MultiCursor { obj: transmute(ptr) }
        }
    }

    pub fn reset(&mut self) {
        unsafe { ffi::tdb_multi_cursor_reset(self.obj) };
    }
}

impl<'a> Drop for MultiCursor<'a> {
    fn drop(&mut self) {
        unsafe { ffi::tdb_multi_cursor_free(self.obj) };
    }
}

impl<'a> Iterator for MultiCursor<'a> {
    type Item = MultiEvent<'a>;

    fn next(&mut self) -> Option<MultiEvent<'a>> {
        unsafe {
            let e = ffi::tdb_multi_cursor_next(self.obj);
            MultiEvent::from_tdb_multi_event(e)
        }
    }
}




pub struct Trail<'a> {
    pub id: TrailId,
    cursor: Cursor<'a>,
}

impl<'a> Iterator for Trail<'a> {
    type Item = Event<'a>;

    fn next(&mut self) -> Option<Event<'a>> {
        self.cursor.next()
    }
}




fn path_cstr(path: &Path) -> CString {
    CString::new(path.to_str().unwrap()).unwrap()
}



#[derive(Debug)]
pub struct Event<'a> {
    pub timestamp: Timestamp,
    pub items: &'a [Item],
}

impl<'a> Event<'a> {
    fn from_tdb_event(e: *const ffi::tdb_event) -> Option<Self> {
        unsafe {
            match e.as_ref() {
                None => None,
                Some(e) => {
                    Some(Event {
                             timestamp: e.timestamp,
                             items: std::slice::from_raw_parts(transmute(&e.items),
                                                               e.num_items as usize),
                         })
                }
            }
        }
    }
}

#[derive(Debug)]
pub struct MultiEvent<'a> {
    pub cursor_idx: usize,
    pub event: Event<'a>,
}


impl<'a> MultiEvent<'a> {
    fn from_tdb_multi_event(e: *const ffi::tdb_multi_event) -> Option<Self> {
        unsafe {
            match e.as_ref() {
                None => None,
                Some(multi_event) => {
                    Some(MultiEvent {
                             event: Event::from_tdb_event(multi_event.event).unwrap(),
                             cursor_idx: multi_event.cursor_idx as usize,
                         })
                }
            }
        }
    }
}


pub struct EventFilter<'b> {
    obj: &'b mut ffi::tdb_event_filter
}

impl<'b> EventFilter<'b> {
    pub fn new() -> EventFilter<'b> {
        let filter = unsafe { ffi::tdb_event_filter_new() };
        EventFilter { obj: unsafe { transmute(filter) } }
    }

    pub fn or(&mut self, item: Item) -> &mut EventFilter<'b> {
        unsafe { ffi::tdb_event_filter_add_term(self.obj, item.0, false as i32); };
        self
    }

    pub fn or_not(&mut self, item: Item) -> &mut EventFilter<'b> {
        unsafe { ffi::tdb_event_filter_add_term(self.obj, item.0, true as i32); };
        self
    }

    pub fn and(&mut self) -> &mut EventFilter<'b> {
        let ret = wrap_tdb_err(unsafe { ffi::tdb_event_filter_new_clause(self.obj) }, ());
        ret.expect("tdb_event_filter_new_clause failed");
        self
    }

    pub fn time_range(&mut self, start: u64, end: u64) -> &mut EventFilter<'b> {
        let ret = wrap_tdb_err(unsafe { ffi::tdb_event_filter_add_time_range(self.obj, start, end) }, ());
        ret.expect("tdb_event_filter_add_time_range failed");
        self
    }

    pub fn num_clauses(&mut self) -> u64 {
        unsafe { ffi::tdb_event_filter_num_clauses(self.obj) }
    }
}


impl<'b> Drop for EventFilter<'b> {
    fn drop(&mut self) {
        unsafe { ffi::tdb_event_filter_free(self.obj) };
    }
}




#[cfg(test)]
mod tests {
    extern crate uuid;
    extern crate tempdir;
    use super::{Constructor, Db, Cursor, MultiCursor, MultiEvent, EventFilter};
    use std::cell::RefCell;
    use std::collections::HashSet;
    use std::iter::FromIterator;
    use self::tempdir::TempDir;

    #[test]
    #[no_mangle]
    fn test_traildb() {
        // create a new constructor
        let field_names = ["field1", "field2"];

        let mut path = TempDir::new("traildb-tmp").unwrap().into_path();
        path.push("kitchen-sink-test");
        let mut cons = Constructor::new(&path, &field_names).unwrap();
        let field_vals = ["cats", "dogs"];

        // add an event
        let events_per_trail = 10;
        let mut trail_cnt = 0;
        let mut event_cnt = 0;
        let mut uuids = Vec::new();
        let mut timestamp = 1;
        let mut timestamps = Vec::new();
        for _ in 0..10 {
            let uuid = *uuid::Uuid::new_v4().as_bytes();
            for _ in 0..events_per_trail {
                assert!(&cons.add(&uuid, timestamp, &field_vals).is_ok());
                timestamps.push(timestamp);
                event_cnt += 1;
                timestamp += 1;
            }
            uuids.push(uuid);
            trail_cnt += 1;
        }


        // finalize db (saves it to disk)
        assert!(cons.finalize().is_ok());

        // open test database
        let db = Db::open(&path).unwrap();

        // check number of fields
        let num_fields = db.num_fields();
        assert_eq!(num_fields, 1 + field_names.len() as u64);

        // check field names are correct
        let db_fields = db.fields();
        assert!(db_fields.contains_key("field1"));
        assert!(db_fields.contains_key("field2"));
        assert_eq!(None, db_fields.get("missing"));

        // check number of trails
        let num_trails = db.num_trails();
        assert_eq!(num_trails, trail_cnt);

        // check number of events
        let num_events = db.num_events();
        assert_eq!(num_events, event_cnt);

        // Check round-trip get_uuid/get_trail_id
        for uuid in &uuids {
            let trail_id = db.get_trail_id(&uuid).unwrap();
            let uuid_rt = db.get_uuid(trail_id).unwrap();
            assert_eq!(&uuid, &uuid_rt);
        }

        // check max/min timestamp
        let min_timestamp = *timestamps.iter().min().unwrap();
        let max_timestamp = *timestamps.iter().max().unwrap();
        assert_eq!(db.min_timestamp(), min_timestamp);
        assert_eq!(db.max_timestamp(), max_timestamp);

        // test cursor
        let mut cursor = db.cursor();
        for uuid in &uuids {
            let trail_id = db.get_trail_id(&uuid).unwrap();
            cursor.get_trail(trail_id).unwrap();
            assert_eq!(events_per_trail, cursor.len());
        }

        // test db iterator
        for trail in db.iter() {
            // test trail iterator
            for event in trail {
                // check that inserted event values match read values
                for (item, item_ref) in event.items.into_iter().zip(field_vals.iter()) {
                    let item = db.get_item_value(*item);
                    assert!(item.is_some());
                    assert_eq!(item.unwrap(), *item_ref);
                }
            }
        }
    }

    #[test]
    fn test_multi_cursor() {
        let field_names = ["field1"];
        let mut path = TempDir::new("traildb-tmp").unwrap().into_path();
        path.push("multicursor-test");

        let mut cons = Constructor::new(&path, &field_names).unwrap();

        let uuid1 = *uuid::Uuid::new_v4().as_bytes();
        let uuid2 = *uuid::Uuid::new_v4().as_bytes();

        assert!(cons.add(&uuid1, 10, &["foo1"]).is_ok());
        assert!(cons.add(&uuid1, 11, &["foo2"]).is_ok());
        assert!(cons.add(&uuid1, 12, &["foo3"]).is_ok());

        assert!(cons.add(&uuid2, 20, &["bar1"]).is_ok());
        assert!(cons.add(&uuid2, 21, &["bar2"]).is_ok());
        assert!(cons.add(&uuid2, 22, &["bar3"]).is_ok());

        assert!(cons.finalize().is_ok());


        let db = Db::open(&path).unwrap();

        let cursors = vec![RefCell::new(db.cursor()), RefCell::new(db.cursor())];
        let mut multi_cursor = MultiCursor::new(&cursors);
        let mut cursor1 = cursors[0].borrow_mut();
        let mut cursor2 = cursors[1].borrow_mut();

        assert!(cursor1.get_trail(0).is_ok());
        assert!(cursor2.get_trail(1).is_ok());
        multi_cursor.reset();

        let multi_events: Vec<MultiEvent> = multi_cursor.collect();
        assert_eq!(vec![10, 11, 12, 20, 21, 22],
                   multi_events.iter().map(|me| me.event.timestamp).collect::<Vec<u64>>());
        assert_eq!(HashSet::from_iter(vec![0, 0, 0, 1, 1, 1].into_iter()),
                   multi_events.iter().map(|me| me.cursor_idx).collect::<HashSet<usize>>());

        // TODO: Test dropping cursors, should not be allowed if multi
        // cursor is still around.
    }


    #[test]
    fn filters() {
        let mut path = TempDir::new("traildb-tmp").unwrap().into_path();
        path.push("filters");

        let mut cons = Constructor::new(&path, &vec!["field1", "field2"]).unwrap();

        let uuid = *uuid::Uuid::new_v4().as_bytes();

        assert!(cons.add(&uuid, 0, &vec!["a", "0"]).is_ok());
        assert!(cons.add(&uuid, 1, &vec!["b", "1"]).is_ok());
        assert!(cons.add(&uuid, 2, &vec!["a", "2"]).is_ok());
        assert!(cons.add(&uuid, 3, &vec!["c", "3"]).is_ok());
        assert!(cons.add(&uuid, 4, &vec!["a", "4"]).is_ok());
        assert!(cons.add(&uuid, 5, &vec!["d", "5"]).is_ok());
        assert!(cons.finalize().is_ok());

        // Return a Vec with timestamps of event returned when the
        // given filter is applied.
        fn timestamps(c: &mut Cursor, f: &EventFilter) -> Vec<u64> {
            assert!(c.get_trail(0).is_ok());
            assert!(c.set_filter(f).is_ok());

            let mut result = vec![];
            for event in c {
                result.push(event.timestamp);
            }
            result
        }


        let db = Db::open(&path).unwrap();
        let fields = db.fields();
        let field1 = fields.get("field1").unwrap();
        let field2 = fields.get("field2").unwrap();

        let mut cursor = db.cursor();

        let mut f = EventFilter::new();

        // Empty filter doesn't match any events
        assert_eq!(1, f.num_clauses());
        assert_eq!(0, timestamps(&mut cursor, &f).len());

        // Events with field1=a
        f.or(db.get_item(*field1, "a").unwrap());
        assert_eq!(vec![0, 2, 4], timestamps(&mut cursor, &f));

        // Calling '.or(...)' again adds another OR clause
        // the filter is now: field1=a OR field1=b
        f.or(db.get_item(*field1, "b").unwrap());
        assert_eq!(vec![0, 1, 2, 4], timestamps(&mut cursor, &f));

        drop(f);


        // field1=a OR field1=b with more ergonomic API
        let mut f = EventFilter::new();
        f.or(db.get_item(*field1, "a").unwrap())
            .or(db.get_item(*field1, "b").unwrap());
        assert_eq!(vec![0, 1, 2, 4], timestamps(&mut cursor, &f));
        drop(f);

        // NOT field1=a
        let mut f = EventFilter::new();
        f.or(db.get_item(*field1, "a").unwrap());
        assert_eq!(vec![0, 2, 4], timestamps(&mut cursor, &f));
        drop(f);

        // field1=a AND (field2=0 OR field2=2)
        let mut f = EventFilter::new();
        f.or(db.get_item(*field1, "a").unwrap())
            .and()
            .or(db.get_item(*field2, "0").unwrap())
            .or(db.get_item(*field2, "2").unwrap());
        assert_eq!(vec![0, 2], timestamps(&mut cursor, &f));
        drop(f);

        // start_time <= timestamp < end_time
        let mut f = EventFilter::new();
        f.time_range(2, 4);
        assert_eq!(vec![2, 3], timestamps(&mut cursor, &f));
        drop(f);
    }
}