ledb 0.4.0

Lightweight embedded database built over LMDB
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
use std::{
    collections::HashSet,
    ops::Deref,
    sync::{
        atomic::{AtomicBool, Ordering as AtomicOrdering},
        Arc,
    },
};

use crate::float::F64;

use lmdb::{
    put::{NODUPDATA, NOOVERWRITE},
    traits::CreateCursor,
    ConstAccessor, Cursor, CursorIter, Database, DatabaseOptions, LmdbResultExt, MaybeOwned,
    ReadTransaction, Unaligned, WriteAccessor,
};
use ron::ser::to_string as to_db_name;
use serde::{Deserialize, Serialize};
use supercow::{ext::ConstDeref, Supercow};

use super::{
    DatabaseDef, Enumerable, IndexKind, KeyData, KeyField, KeyType, OrderKind, Primary,
    RawDocument, Result, ResultWrap, Serial, Storage, Value,
};

/// The definition of index
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct IndexDef(
    /// Unique serial
    pub Serial,
    /// Collection name
    pub String,
    /// Field path
    pub String,
    pub IndexKind,
    pub KeyType,
);

impl IndexDef {
    pub fn new<C: Into<String>, P: Into<String>>(
        coll: C,
        path: P,
        kind: IndexKind,
        key: KeyType,
    ) -> Self {
        IndexDef(0, coll.into(), path.into(), kind, key)
    }
}

impl Enumerable for IndexDef {
    fn enumerate(&mut self, serial: Serial) {
        self.0 = serial;
    }
}

struct IndexData {
    path: String,
    kind: IndexKind,
    key: KeyType,
    db: Database<'static>,
    // Remove marker
    delete: AtomicBool,
}

/// Index for document field
#[derive(Clone)]
pub(crate) struct Index(Option<Arc<IndexData>>);

impl Index {
    pub(crate) fn new(storage: Storage, def: IndexDef) -> Result<Self> {
        let db_name = to_db_name(&DatabaseDef::Index(def.clone())).wrap_err()?;

        let IndexDef(_serial, _coll, path, kind, key) = def;

        let db_opts = match (kind, key) {
            (IndexKind::Unique, KeyType::Int) => DatabaseOptions::create_map::<Unaligned<i64>>(),
            (IndexKind::Unique, KeyType::Float) => DatabaseOptions::create_map::<Unaligned<F64>>(),
            (IndexKind::Unique, KeyType::String) => DatabaseOptions::create_map::<str>(),
            (IndexKind::Unique, KeyType::Binary) => DatabaseOptions::create_map::<[u8]>(),
            (IndexKind::Unique, KeyType::Bool) => DatabaseOptions::create_map::<u8>(),
            (IndexKind::Index, KeyType::Int) => {
                DatabaseOptions::create_multimap::<Unaligned<i64>, Unaligned<Primary>>()
            }
            (IndexKind::Index, KeyType::Float) => {
                DatabaseOptions::create_multimap::<Unaligned<F64>, Unaligned<Primary>>()
            }
            (IndexKind::Index, KeyType::String) => {
                DatabaseOptions::create_multimap::<str, Unaligned<Primary>>()
            }
            (IndexKind::Index, KeyType::Binary) => {
                DatabaseOptions::create_multimap::<[u8], Unaligned<Primary>>()
            }
            (IndexKind::Index, KeyType::Bool) => {
                DatabaseOptions::create_multimap::<u8, Unaligned<Primary>>()
            }
        };

        let db = Database::open(storage, Some(&db_name), &db_opts).wrap_err()?;

        Ok(Index(Some(Arc::new(IndexData {
            path,
            kind,
            key,
            db,
            delete: AtomicBool::new(false),
        }))))
    }

    fn handle(&self) -> &IndexData {
        if let Some(handle) = &self.0 {
            handle
        } else {
            unreachable!();
        }
    }

    pub fn path(&self) -> &str {
        &self.handle().path
    }

    pub fn kind(&self) -> IndexKind {
        self.handle().kind
    }

    pub fn key(&self) -> KeyType {
        self.handle().key
    }

    pub fn field(&self) -> KeyField {
        let handle = self.handle();

        KeyField::new(handle.path.clone())
            .with_type(handle.key)
            .with_kind(handle.kind)
    }

    pub(crate) fn update_index(
        &self,
        access: &mut WriteAccessor,
        old_doc: Option<&RawDocument>,
        new_doc: Option<&RawDocument>,
    ) -> Result<()> {
        let doc = old_doc
            .or_else(|| new_doc)
            .ok_or_else(|| "Either old_doc or new_doc or both must present")
            .wrap_err()?;
        let id = doc.req_id()?;

        let old_keys = old_doc.map(|doc| self.extract(doc)).unwrap_or_default();
        let new_keys = new_doc.map(|doc| self.extract(doc)).unwrap_or_default();

        let (old_keys, new_keys) = (
            old_keys.difference(&new_keys),
            new_keys.difference(&old_keys),
        );

        let handle = self.handle();

        //println!("Update index {} --{:?} ++{:?}", &handle.path, &old_keys, &new_keys);

        for key in old_keys {
            access
                .del_item(&handle.db, key.as_raw(), &Unaligned::new(id))
                .wrap_err()?;
        }

        let f = match handle.kind {
            IndexKind::Unique => NOOVERWRITE,
            IndexKind::Index => NODUPDATA,
        };

        for key in new_keys {
            access
                .put(&handle.db, key.as_raw(), &Unaligned::new(id), f)
                .wrap_err()?;
        }

        Ok(())
    }

    fn extract(&self, doc: &RawDocument) -> HashSet<KeyData> {
        let mut keys = HashSet::new();
        let handle = self.handle();
        let path = handle.path.split('.');
        extract_field_values(&*doc, handle.key, &path, &mut keys);
        keys
    }

    pub(crate) fn query_set<'a, I: Iterator<Item = &'a KeyData>>(
        &self,
        txn: &ReadTransaction,
        access: &ConstAccessor,
        keys: I,
    ) -> Result<HashSet<Primary>> {
        let mut out = HashSet::new();
        let handle = self.handle();

        for key in keys {
            if let Some(key) = key.to_type(handle.key) {
                let mut cursor = txn.cursor(self.clone()).wrap_err()?;

                match handle.kind {
                    IndexKind::Unique => match cursor
                        .seek_k_both::<[u8], Unaligned<Primary>>(&access, key.as_raw())
                        .to_opt()
                    {
                        Ok(Some((_key, id))) => {
                            out.insert(id.get());
                        }
                        Err(e) => return Err(e).wrap_err(),
                        _ => (),
                    },
                    IndexKind::Index => {
                        match cursor
                            .seek_k::<[u8], Unaligned<Primary>>(&access, key.as_raw())
                            .to_opt()
                        {
                            Ok(Some(..)) => (),
                            Ok(None) => continue,
                            Err(e) => return Err(e).wrap_err(),
                        }

                        for res in CursorIter::new(
                            MaybeOwned::Owned(cursor),
                            &access,
                            |c, a| c.get_multiple::<[Unaligned<Primary>]>(&a),
                            Cursor::next_multiple::<[Unaligned<Primary>]>,
                        )
                        .wrap_err()?
                        {
                            if let Some(ids) = res.to_opt().wrap_err()? {
                                for id in ids {
                                    out.insert(id.get());
                                }
                            }
                        }
                    }
                }
            }
        }

        Ok(out)
    }

    pub(crate) fn query_range(
        &self,
        txn: &ReadTransaction,
        access: &ConstAccessor,
        beg: Option<(&KeyData, bool)>,
        end: Option<(&KeyData, bool)>,
    ) -> Result<HashSet<Primary>> {
        let mut out = HashSet::new();
        let handle = self.handle();

        let beg = beg.and_then(|(key, inc)| key.to_type(handle.key).map(|key| (key, inc)));
        let end = end.and_then(|(key, inc)| key.to_type(handle.key).map(|key| (key, inc)));
        let cursor = txn.cursor(self.clone()).wrap_err()?;

        match handle.kind {
            IndexKind::Unique => {
                for item in CursorIter::new(
                    MaybeOwned::Owned(cursor),
                    access,
                    |c, a| match beg {
                        Some((beg_key, beg_inc)) => {
                            let p = c.seek_range_k(a, beg_key.as_raw())?;
                            if beg_inc {
                                Ok(p)
                            } else {
                                c.next(a)
                            }
                        }
                        _ => c.first(a),
                    },
                    Cursor::next::<[u8], Unaligned<Primary>>,
                )
                .wrap_err()?
                {
                    match (item, &end) {
                        (Ok((key, id)), Some((end_key, end_inc))) => {
                            let key = KeyData::from_raw(end_key.get_type(), key)?;
                            #[allow(clippy::op_ref)]
                            {
                                if &key < end_key || *end_inc && &key <= end_key {
                                    out.insert(id.get());
                                } else {
                                    break;
                                }
                            }
                        }
                        (Ok((_, id)), _) => {
                            out.insert(id.get());
                        }
                        (Err(e), _) => return Err(e).wrap_err(),
                    }
                }
            }
            IndexKind::Index => {
                for item in CursorIter::new(
                    MaybeOwned::Owned(cursor),
                    access,
                    |c, a| {
                        let key = match beg {
                            Some((beg_key, beg_inc)) => {
                                let p = c.seek_range_k::<[u8], [u8]>(a, beg_key.as_raw())?.0;
                                if beg_inc {
                                    p
                                } else {
                                    c.next::<[u8], [u8]>(a)?.0
                                }
                            }
                            _ => c.first::<[u8], [u8]>(a)?.0,
                        };
                        c.get_multiple::<[Unaligned<Primary>]>(a)
                            .map(|val| (key, val))
                    },
                    |c, a| {
                        if let Some(ids) = c.next_multiple(a).to_opt()? {
                            c.get_current::<[u8], [u8]>(a).map(|(key, _val)| (key, ids))
                        } else {
                            let key = c.next::<[u8], Unaligned<Primary>>(a)?.0;
                            c.get_multiple(a).map(|ids| (key, ids))
                        }
                    },
                )
                .wrap_err()?
                {
                    match (item, &end) {
                        (Ok((key, ids)), Some((end_key, end_inc))) => {
                            let key = KeyData::from_raw(end_key.get_type(), key)?;
                            #[allow(clippy::op_ref)]
                            {
                                if &key < end_key || *end_inc && &key <= end_key {
                                    for id in ids {
                                        out.insert(id.get());
                                    }
                                } else {
                                    break;
                                }
                            }
                        }
                        (Ok((_, ids)), _) => {
                            for id in ids {
                                out.insert(id.get());
                            }
                        }
                        (Err(e), _) => return Err(e).wrap_err(),
                    }
                }
            }
        }

        Ok(out)
    }

    pub(crate) fn query_iter(
        &self,
        txn: Arc<ReadTransaction<'static>>,
        order: OrderKind,
    ) -> Result<IndexIterator> {
        IndexIterator::new(txn, self.clone(), order)
    }

    pub(crate) fn purge(&self, access: &mut WriteAccessor) -> Result<()> {
        let handle = self.handle();
        access.clear_db(&handle.db).wrap_err()
    }

    pub(crate) fn to_delete(&self, access: &mut WriteAccessor) -> Result<()> {
        self.purge(access)?;
        let handle = self.handle();
        handle.delete.store(true, AtomicOrdering::SeqCst);
        Ok(())
    }
}

impl Drop for Index {
    fn drop(&mut self) {
        let data = self.0.take().unwrap();

        if let Ok(IndexData { db, delete, .. }) = Arc::try_unwrap(data) {
            if delete.load(AtomicOrdering::SeqCst) {
                if let Err(e) = db.delete() {
                    eprintln!("Error when deleting index db: {}", e);
                }
            }
        }
    }
}

impl Deref for Index {
    type Target = Database<'static>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        if let Some(data) = &self.0 {
            &data.db
        } else {
            unreachable!()
        }
    }
}

unsafe impl ConstDeref for Index {
    type Target = Database<'static>;

    #[inline]
    fn const_deref(&self) -> &Self::Target {
        if let Some(data) = &self.0 {
            &data.db
        } else {
            unreachable!()
        }
    }
}

impl<'a> Into<Supercow<'a, Database<'a>>> for Index {
    fn into(self) -> Supercow<'a, Database<'a>> {
        Supercow::shared(self)
    }
}

fn extract_field_values<'a, 'i: 'a, I: Iterator<Item = &'i str> + Clone>(
    doc: &'a Value,
    typ: KeyType,
    path: &'a I,
    keys: &mut HashSet<KeyData>,
) {
    let mut sub_path = path.clone();
    if let Some(name) = sub_path.next() {
        use Value::*;
        match doc {
            Array(val) => val
                .iter()
                .for_each(|doc| extract_field_values(doc, typ, path, keys)),
            Map(val) if name == "*" => val
                .iter()
                .for_each(|(_key, doc)| extract_field_values(doc, typ, path, keys)),
            Map(val) => {
                if let Some(doc) = val.get(&name.to_owned().into()) {
                    extract_field_values(doc, typ, &sub_path, keys);
                }
            }
            _ => (),
        }
    } else {
        extract_field_primitives(doc, typ, keys);
    }
}

fn extract_field_primitives(doc: &Value, typ: KeyType, keys: &mut HashSet<KeyData>) {
    use serde_cbor::Value::*;
    match (typ, doc) {
        (_, Array(val)) => val
            .iter()
            .for_each(|doc| extract_field_primitives(doc, typ, keys)),
        (_, Map(val)) => val
            .iter()
            .for_each(|(key, _doc)| extract_field_primitives(key, typ, keys)),
        (typ, val) => {
            if let Some(val) = KeyData::from_val(&val) {
                if let Some(val) = val.to_type(typ) {
                    keys.insert(val.into_owned());
                }
            }
        }
    }
}

pub(crate) struct IndexIterator {
    txn: Arc<ReadTransaction<'static>>,
    cur: Cursor<'static, 'static>,
    order: OrderKind,
    init: bool,
}

impl IndexIterator {
    pub fn new(txn: Arc<ReadTransaction<'static>>, coll: Index, order: OrderKind) -> Result<Self> {
        let cur = txn.cursor(coll)?;

        Ok(Self {
            txn,
            cur,
            order,
            init: false,
        })
    }
}

impl Iterator for IndexIterator {
    type Item = Result<Primary>;

    fn next(&mut self) -> Option<Self::Item> {
        let access = self.txn.access();
        match if self.init {
            match self.order {
                OrderKind::Asc => self.cur.next::<[u8], Unaligned<Primary>>(&access),
                OrderKind::Desc => self.cur.prev::<[u8], Unaligned<Primary>>(&access),
            }
        } else {
            self.init = true;
            match self.order {
                OrderKind::Asc => self.cur.first::<[u8], Unaligned<Primary>>(&access),
                OrderKind::Desc => self.cur.last::<[u8], Unaligned<Primary>>(&access),
            }
        }
        .to_opt()
        {
            Ok(Some((_key, id))) => Some(Ok(id.get())),
            Ok(None) => None,
            Err(e) => Some(Err(e).wrap_err()),
        }
    }
}