bison-db 1.0.0

An embedded, document-oriented database for Rust - schemaless documents, secondary indexes, and ACID single-file storage, with zero network and zero external services.
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
//! The document model: [`Value`] and [`Document`].
//!
//! A [`Document`] is an ordered set of named fields — the unit a store holds.
//! Each field's content is a [`Value`], a small tagged union covering the
//! JSON-like shapes a schemaless record needs: null, boolean, signed integer,
//! float, UTF-8 string, raw bytes, array, and nested document.
//!
//! Fields keep insertion order. Two documents are equal when they carry the
//! same fields in the same order, so encode/decode round-trips compare equal.
//! Order is preserved because it is cheap to maintain over a flat vector and
//! because a stable field order keeps the on-disk encoding deterministic.

use alloc::string::String;
use alloc::vec::Vec;

/// A single field value inside a [`Document`].
///
/// `Value` is deliberately small and closed: it models the shapes a schemaless
/// document store needs and nothing more. Nesting is expressed through
/// [`Value::Array`] and [`Value::Object`], so an arbitrarily deep record is
/// just a tree of `Value`s.
///
/// # Examples
///
/// ```
/// use bison_db::Value;
///
/// let v = Value::from("hello");
/// assert_eq!(v.as_str(), Some("hello"));
/// assert!(Value::from(42_i64).as_int() == Some(42));
/// assert!(Value::Null.is_null());
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Value {
    /// The absence of a value.
    #[default]
    Null,
    /// A boolean.
    Bool(bool),
    /// A signed 64-bit integer. All integral fields are stored at this width.
    Int(i64),
    /// A 64-bit IEEE-754 float.
    Float(f64),
    /// A UTF-8 string.
    Str(String),
    /// An opaque byte string, for binary fields that are not valid UTF-8.
    Bytes(Vec<u8>),
    /// An ordered list of values.
    Array(Vec<Value>),
    /// A nested document.
    Object(Document),
}

impl Value {
    /// Returns `true` if this value is [`Value::Null`].
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert!(Value::Null.is_null());
    /// assert!(!Value::from(0_i64).is_null());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_null(&self) -> bool {
        matches!(self, Value::Null)
    }

    /// Returns the boolean if this is a [`Value::Bool`], otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert_eq!(Value::from(true).as_bool(), Some(true));
    /// assert_eq!(Value::from(1_i64).as_bool(), None);
    /// ```
    #[inline]
    #[must_use]
    pub const fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Bool(b) => Some(*b),
            _ => None,
        }
    }

    /// Returns the integer if this is a [`Value::Int`], otherwise `None`.
    ///
    /// Floats are not coerced; a [`Value::Float`] returns `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert_eq!(Value::from(7_i64).as_int(), Some(7));
    /// assert_eq!(Value::from(7.0_f64).as_int(), None);
    /// ```
    #[inline]
    #[must_use]
    pub const fn as_int(&self) -> Option<i64> {
        match self {
            Value::Int(n) => Some(*n),
            _ => None,
        }
    }

    /// Returns the float if this is a [`Value::Float`], otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert_eq!(Value::from(1.5_f64).as_float(), Some(1.5));
    /// assert_eq!(Value::from(1_i64).as_float(), None);
    /// ```
    #[inline]
    #[must_use]
    pub const fn as_float(&self) -> Option<f64> {
        match self {
            Value::Float(f) => Some(*f),
            _ => None,
        }
    }

    /// Returns the string slice if this is a [`Value::Str`], otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert_eq!(Value::from("bison").as_str(), Some("bison"));
    /// ```
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Value::Str(s) => Some(s.as_str()),
            _ => None,
        }
    }

    /// Returns the byte slice if this is a [`Value::Bytes`], otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert_eq!(Value::Bytes(vec![1, 2, 3]).as_bytes(), Some(&[1, 2, 3][..]));
    /// ```
    #[inline]
    #[must_use]
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Value::Bytes(b) => Some(b.as_slice()),
            _ => None,
        }
    }

    /// Returns the element slice if this is a [`Value::Array`], otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// let v = Value::Array(vec![Value::from(1_i64), Value::from(2_i64)]);
    /// assert_eq!(v.as_array().map(<[_]>::len), Some(2));
    /// ```
    #[inline]
    #[must_use]
    pub fn as_array(&self) -> Option<&[Value]> {
        match self {
            Value::Array(a) => Some(a.as_slice()),
            _ => None,
        }
    }

    /// Returns the nested document if this is a [`Value::Object`], otherwise `None`.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::{Document, Value};
    /// let mut inner = Document::new();
    /// inner.set("k", 1_i64);
    /// let v = Value::Object(inner);
    /// assert_eq!(v.as_object().and_then(|d| d.get("k")).and_then(Value::as_int), Some(1));
    /// ```
    #[inline]
    #[must_use]
    pub fn as_object(&self) -> Option<&Document> {
        match self {
            Value::Object(d) => Some(d),
            _ => None,
        }
    }

    /// Returns a short, stable name for the value's variant.
    ///
    /// Intended for diagnostics and error messages, not for logic; match on the
    /// variant directly when behaviour depends on the type.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Value;
    /// assert_eq!(Value::from(1_i64).type_name(), "int");
    /// assert_eq!(Value::Null.type_name(), "null");
    /// ```
    #[must_use]
    pub const fn type_name(&self) -> &'static str {
        match self {
            Value::Null => "null",
            Value::Bool(_) => "bool",
            Value::Int(_) => "int",
            Value::Float(_) => "float",
            Value::Str(_) => "string",
            Value::Bytes(_) => "bytes",
            Value::Array(_) => "array",
            Value::Object(_) => "object",
        }
    }
}

impl From<bool> for Value {
    #[inline]
    fn from(v: bool) -> Self {
        Value::Bool(v)
    }
}

impl From<i32> for Value {
    #[inline]
    fn from(v: i32) -> Self {
        Value::Int(i64::from(v))
    }
}

impl From<i64> for Value {
    #[inline]
    fn from(v: i64) -> Self {
        Value::Int(v)
    }
}

impl From<u32> for Value {
    #[inline]
    fn from(v: u32) -> Self {
        Value::Int(i64::from(v))
    }
}

impl From<f64> for Value {
    #[inline]
    fn from(v: f64) -> Self {
        Value::Float(v)
    }
}

impl From<&str> for Value {
    #[inline]
    fn from(v: &str) -> Self {
        Value::Str(String::from(v))
    }
}

impl From<String> for Value {
    #[inline]
    fn from(v: String) -> Self {
        Value::Str(v)
    }
}

impl From<Vec<u8>> for Value {
    #[inline]
    fn from(v: Vec<u8>) -> Self {
        Value::Bytes(v)
    }
}

impl From<Vec<Value>> for Value {
    #[inline]
    fn from(v: Vec<Value>) -> Self {
        Value::Array(v)
    }
}

impl From<Document> for Value {
    #[inline]
    fn from(v: Document) -> Self {
        Value::Object(v)
    }
}

impl<T: Into<Value>> From<Option<T>> for Value {
    /// `Some(x)` becomes `x`'s value; `None` becomes [`Value::Null`].
    #[inline]
    fn from(v: Option<T>) -> Self {
        match v {
            Some(inner) => inner.into(),
            None => Value::Null,
        }
    }
}

/// An ordered collection of named fields — the record a store holds.
///
/// A document maps `String` keys to [`Value`]s and preserves the order in which
/// fields were first inserted. Lookups are a linear scan, which is the fastest
/// strategy for the small field counts typical of documents: it keeps the keys
/// contiguous in memory and avoids the hashing and pointer-chasing overhead a
/// map would add at this size.
///
/// # Examples
///
/// ```
/// use bison_db::Document;
///
/// let mut user = Document::new();
/// user.set("name", "ada").set("born", 1815_i64);
///
/// assert_eq!(user.len(), 2);
/// assert_eq!(user.get("name").and_then(|v| v.as_str()), Some("ada"));
/// ```
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Document {
    fields: Vec<(String, Value)>,
}

impl Document {
    /// Creates an empty document.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let doc = Document::new();
    /// assert!(doc.is_empty());
    /// ```
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Document { fields: Vec::new() }
    }

    /// Creates an empty document with room for `capacity` fields before it
    /// needs to reallocate.
    ///
    /// Use this when the field count is known up front to avoid intermediate
    /// growth allocations.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let doc = Document::with_capacity(4);
    /// assert!(doc.is_empty());
    /// ```
    #[inline]
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Document {
            fields: Vec::with_capacity(capacity),
        }
    }

    /// Sets `key` to `value`, returning `&mut self` so calls can be chained.
    ///
    /// If `key` is already present its value is replaced in place, preserving
    /// the field's original position. Otherwise the field is appended.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("a", 1_i64).set("b", 2_i64).set("a", 3_i64);
    /// // "a" keeps its leading position but takes the new value.
    /// assert_eq!(doc.keys().collect::<Vec<_>>(), ["a", "b"]);
    /// assert_eq!(doc.get("a").and_then(|v| v.as_int()), Some(3));
    /// ```
    pub fn set<K, V>(&mut self, key: K, value: V) -> &mut Self
    where
        K: Into<String>,
        V: Into<Value>,
    {
        let key = key.into();
        let value = value.into();
        match self.fields.iter_mut().find(|(k, _)| *k == key) {
            Some(slot) => slot.1 = value,
            None => self.fields.push((key, value)),
        }
        self
    }

    /// Returns a reference to the value for `key`, or `None` if absent.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("k", "v");
    /// assert_eq!(doc.get("k").and_then(|v| v.as_str()), Some("v"));
    /// assert!(doc.get("missing").is_none());
    /// ```
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.fields.iter().find(|(k, _)| k == key).map(|(_, v)| v)
    }

    /// Returns a mutable reference to the value for `key`, or `None` if absent.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::{Document, Value};
    /// let mut doc = Document::new();
    /// doc.set("n", 1_i64);
    /// if let Some(Value::Int(n)) = doc.get_mut("n") {
    ///     *n += 41;
    /// }
    /// assert_eq!(doc.get("n").and_then(|v| v.as_int()), Some(42));
    /// ```
    #[must_use]
    pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
        self.fields
            .iter_mut()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v)
    }

    /// Returns `true` if `key` is present.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("k", 1_i64);
    /// assert!(doc.contains_key("k"));
    /// assert!(!doc.contains_key("other"));
    /// ```
    #[must_use]
    pub fn contains_key(&self, key: &str) -> bool {
        self.fields.iter().any(|(k, _)| k == key)
    }

    /// Removes `key`, returning its value if it was present.
    ///
    /// Remaining fields keep their relative order.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("a", 1_i64).set("b", 2_i64);
    /// assert_eq!(doc.remove("a").and_then(|v| v.as_int()), Some(1));
    /// assert!(!doc.contains_key("a"));
    /// assert_eq!(doc.keys().collect::<Vec<_>>(), ["b"]);
    /// ```
    pub fn remove(&mut self, key: &str) -> Option<Value> {
        let idx = self.fields.iter().position(|(k, _)| k == key)?;
        Some(self.fields.remove(idx).1)
    }

    /// Returns the number of fields.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("a", 1_i64);
    /// assert_eq!(doc.len(), 1);
    /// ```
    #[inline]
    #[must_use]
    pub fn len(&self) -> usize {
        self.fields.len()
    }

    /// Returns `true` if the document has no fields.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// assert!(Document::new().is_empty());
    /// ```
    #[inline]
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.fields.is_empty()
    }

    /// Removes all fields, keeping the allocated capacity for reuse.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("a", 1_i64);
    /// doc.clear();
    /// assert!(doc.is_empty());
    /// ```
    #[inline]
    pub fn clear(&mut self) {
        self.fields.clear();
    }

    /// Returns an iterator over the fields as `(&str, &Value)` pairs, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("a", 1_i64).set("b", 2_i64);
    /// let collected: Vec<_> = doc.iter().map(|(k, _)| k).collect();
    /// assert_eq!(collected, ["a", "b"]);
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
        self.fields.iter().map(|(k, v)| (k.as_str(), v))
    }

    /// Returns an iterator over the field keys, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("x", 1_i64);
    /// assert_eq!(doc.keys().collect::<Vec<_>>(), ["x"]);
    /// ```
    pub fn keys(&self) -> impl Iterator<Item = &str> {
        self.fields.iter().map(|(k, _)| k.as_str())
    }

    /// Returns an iterator over the field values, in order.
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::Document;
    /// let mut doc = Document::new();
    /// doc.set("x", 9_i64);
    /// assert_eq!(doc.values().filter_map(|v| v.as_int()).sum::<i64>(), 9);
    /// ```
    pub fn values(&self) -> impl Iterator<Item = &Value> {
        self.fields.iter().map(|(_, v)| v)
    }
}

impl<'a> IntoIterator for &'a Document {
    type Item = (&'a str, &'a Value);
    type IntoIter = core::iter::Map<
        core::slice::Iter<'a, (String, Value)>,
        fn(&'a (String, Value)) -> (&'a str, &'a Value),
    >;

    fn into_iter(self) -> Self::IntoIter {
        fn pair(kv: &(String, Value)) -> (&str, &Value) {
            (kv.0.as_str(), &kv.1)
        }
        self.fields.iter().map(pair)
    }
}

impl<K, V> FromIterator<(K, V)> for Document
where
    K: Into<String>,
    V: Into<Value>,
{
    /// Builds a document from key/value pairs. Later duplicates overwrite
    /// earlier ones, matching [`Document::set`].
    ///
    /// # Examples
    ///
    /// ```
    /// use bison_db::{Document, Value};
    /// let doc: Document = [("a", 1_i64), ("b", 2_i64)].into_iter().collect();
    /// assert_eq!(doc.len(), 2);
    /// ```
    fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
        let iter = iter.into_iter();
        let mut doc = Document::with_capacity(iter.size_hint().0);
        for (k, v) in iter {
            let _ = doc.set(k, v);
        }
        doc
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_set_existing_key_replaces_in_place() {
        let mut doc = Document::new();
        doc.set("a", 1_i64).set("b", 2_i64).set("a", 9_i64);
        assert_eq!(doc.keys().collect::<Vec<_>>(), ["a", "b"]);
        assert_eq!(doc.get("a").and_then(Value::as_int), Some(9));
    }

    #[test]
    fn test_remove_absent_key_returns_none() {
        let mut doc = Document::new();
        doc.set("a", 1_i64);
        assert!(doc.remove("missing").is_none());
        assert_eq!(doc.len(), 1);
    }

    #[test]
    fn test_remove_preserves_order() {
        let mut doc = Document::new();
        doc.set("a", 1_i64).set("b", 2_i64).set("c", 3_i64);
        let _ = doc.remove("b");
        assert_eq!(doc.keys().collect::<Vec<_>>(), ["a", "c"]);
    }

    #[test]
    fn test_value_accessors_reject_wrong_variant() {
        assert!(Value::from(1_i64).as_str().is_none());
        assert!(Value::from("x").as_int().is_none());
        assert!(Value::Null.as_bool().is_none());
    }

    #[test]
    fn test_from_option_maps_none_to_null() {
        let none: Option<i64> = None;
        assert!(Value::from(none).is_null());
        assert_eq!(Value::from(Some(5_i64)).as_int(), Some(5));
    }

    #[test]
    fn test_equality_is_order_sensitive() {
        let mut a = Document::new();
        a.set("x", 1_i64).set("y", 2_i64);
        let mut b = Document::new();
        b.set("y", 2_i64).set("x", 1_i64);
        assert_ne!(a, b);
    }
}