vsdb 16.3.6

A std-collection-like database
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
use super::{Mapx, ValueEnDe};
use crate::{
    DEFAULT_NS_ID, InstanceId,
    basic::{mapx_ord::MapxOrd, orphan::Orphan},
};
use ruc::*;
use std::{cell::RefCell, fs};

#[test]
fn test_insert() {
    let mut hdr: Mapx<usize, usize> = Mapx::new();
    let max = 100;
    (0..max)
        .map(|i: usize| (i, (max + i)))
        .for_each(|(key, value)| {
            assert!(hdr.get(&key).is_none());
            hdr.insert(&key, &value);
            assert!(hdr.contains_key(&key));
            assert_eq!(pnk!(hdr.get(&key)), value);
            hdr.remove(&key);
            assert!(hdr.get(&key).is_none());
        });
    hdr.clear();
    (0..max).map(|i: usize| i).for_each(|key| {
        assert!(hdr.get(&key).is_none());
    });
}

#[test]
fn xx_test_valueende() {
    let cnt = 100;
    let dehdr = {
        let mut hdr: Mapx<usize, usize> = Mapx::new();
        (0..cnt).map(|i: usize| (i, i)).for_each(|(key, value)| {
            hdr.insert(&key, &value);
        });
        hdr.encode()
    };
    let reloaded = pnk!(<Mapx<usize, usize> as ValueEnDe>::decode(&dehdr));
    (0..cnt).map(|i: usize| i).for_each(|i| {
        assert_eq!(i, reloaded.get(&i).unwrap());
    });
}

#[test]
fn test_iter() {
    let mut hdr: Mapx<usize, usize> = Mapx::new();
    let max = 100;
    (0..max).map(|i: usize| (i, i)).for_each(|(key, value)| {
        hdr.insert(&key, &value);
    });
    for (key, value) in hdr.iter().collect::<Vec<_>>().into_iter() {
        assert_eq!(key, value);
        hdr.remove(&key);
    }
}

#[test]
fn test_first_last() {
    let mut hdr: Mapx<usize, usize> = Mapx::new();
    let max = 100;
    (0..max).map(|i: usize| (i, i)).for_each(|(key, value)| {
        hdr.insert(&key, &value);
    });
    let (key, value) = pnk!(hdr.iter().next());
    assert_eq!(key, value);
    // order is not guaranteed in Mapx
    // assert_eq!(0, key);

    let (key, value) = pnk!(hdr.iter().next_back());
    assert_eq!(key, value);
    // assert_eq!(max - 1, key);
}

#[test]
fn test_values() {
    let mut hdr: Mapx<usize, usize> = Mapx::new();
    let max = 100usize;
    (0..max).map(|i| (i, i)).for_each(|(key, value)| {
        hdr.insert(&key, &value);
    });
    for (k, v) in hdr.iter() {
        assert_eq!(k, v);
    }
}

#[test]
fn test_values_first_last() {
    let mut hdr: Mapx<usize, usize> = Mapx::new();
    let max = 100;
    (0..max).map(|i: usize| (i, i)).for_each(|(key, value)| {
        hdr.insert(&key, &value);
    });
    let _value = pnk!(hdr.values().next());
    // assert_eq!(0, value);

    let _value = pnk!(hdr.values().next_back());
    // assert_eq!(max - 1, value);
}

#[test]
fn test_save_and_from_meta() {
    let mut hdr: Mapx<usize, String> = Mapx::new();
    hdr.insert(&1, &"hello".to_string());
    hdr.insert(&2, &"world".to_string());

    let id = pnk!(hdr.save_meta());
    assert_eq!(id, hdr.instance_id());

    let restored: Mapx<usize, String> = pnk!(Mapx::from_meta(id));
    assert_eq!(restored.get(&1), Some("hello".to_string()));
    assert_eq!(restored.get(&2), Some("world".to_string()));
    assert!(restored.is_the_same_instance(&hdr));
}

#[test]
fn test_from_meta_rejects_misaddressed_typed_payload() {
    let a: Mapx<u32, String> = Mapx::new();
    let b: Mapx<u32, String> = Mapx::new();
    let id = a.instance_id();
    crate::common::save_instance_meta(id, &b).unwrap();

    assert!(Mapx::<u32, String>::from_meta(id).is_err());
}

#[test]
fn test_from_meta_canonicalizes_default_namespace_id() {
    let map: Mapx<u32, String> = Mapx::new();
    let id = map.save_meta().unwrap();
    let noncanonical = InstanceId {
        map_id: id.map_id,
        ns: Some(DEFAULT_NS_ID),
    };

    assert!(
        Mapx::<u32, String>::from_meta(noncanonical)
            .unwrap()
            .is_the_same_instance(&map)
    );
}

#[test]
fn test_from_meta_rejects_legacy_prefix_payload() {
    // The pre-v13.4 meta format was a bare 8-byte prefix; v14 removed the
    // legacy acceptance path, so such a payload must be rejected instead
    // of silently forging a handle.
    let mut hdr: Mapx<u32, String> = Mapx::new();
    hdr.insert(&1, &"legacy".to_string());

    let id = hdr.instance_id();
    let legacy_payload = postcard::to_allocvec(&hdr.as_bytes()).unwrap();
    fs::write(crate::common::vsdb_meta_path(id.map_id), legacy_payload).unwrap();

    assert!(Mapx::<u32, String>::from_meta(id).is_err());

    // Re-saving under the current format restores access.
    let id = hdr.save_meta().unwrap();
    let restored: Mapx<u32, String> = Mapx::from_meta(id).unwrap();
    assert_eq!(restored.get(&1), Some("legacy".to_string()));
    assert!(restored.is_the_same_instance(&hdr));
}

#[test]
fn test_from_meta_rejects_wrong_typed_handle() {
    let mut hdr: Mapx<u32, u32> = Mapx::new();
    hdr.insert(&1, &10);

    let id = hdr.save_meta().unwrap();
    assert!(Mapx::<String, u32>::from_meta(id).is_err());
}

/// Nested: `Mapx<String, MapxOrd<u32, String>>` — only the outer meta
/// is saved; restoring it must bring back all inner maps and their data.
#[test]
fn test_nested_mapx_of_mapxord() {
    let mut outer: Mapx<String, MapxOrd<u32, String>> = Mapx::new();

    let mut inner1: MapxOrd<u32, String> = MapxOrd::new();
    inner1.insert(&1, &"a".to_string());
    inner1.insert(&2, &"b".to_string());

    let mut inner2: MapxOrd<u32, String> = MapxOrd::new();
    inner2.insert(&10, &"x".to_string());
    inner2.insert(&20, &"y".to_string());
    inner2.insert(&30, &"z".to_string());

    outer.insert(&"first".to_string(), &inner1);
    outer.insert(&"second".to_string(), &inner2);

    // Save only the outermost instance
    let id = pnk!(outer.save_meta());

    // Restore and verify the full nested structure
    let restored: Mapx<String, MapxOrd<u32, String>> = pnk!(Mapx::from_meta(id));

    let r1 = restored.get(&"first".to_string()).unwrap();
    assert_eq!(r1.get(&1), Some("a".to_string()));
    assert_eq!(r1.get(&2), Some("b".to_string()));

    let r2 = restored.get(&"second".to_string()).unwrap();
    assert_eq!(r2.get(&10), Some("x".to_string()));
    assert_eq!(r2.get(&20), Some("y".to_string()));
    assert_eq!(r2.get(&30), Some("z".to_string()));
}

/// Triple nesting: `Orphan< Mapx<String, MapxOrd<u32, String>> >`
/// Save only the Orphan; everything underneath must survive.
#[test]
fn test_nested_orphan_of_mapx_of_mapxord() {
    let mut inner_map: MapxOrd<u32, String> = MapxOrd::new();
    inner_map.insert(&1, &"hello".to_string());
    inner_map.insert(&2, &"world".to_string());

    let mut mid: Mapx<String, MapxOrd<u32, String>> = Mapx::new();
    mid.insert(&"data".to_string(), &inner_map);

    let outer: Orphan<Mapx<String, MapxOrd<u32, String>>> = Orphan::new(mid);

    let id = pnk!(outer.save_meta());

    let restored: Orphan<Mapx<String, MapxOrd<u32, String>>> =
        pnk!(Orphan::from_meta(id));
    let restored_mid = restored.get_value();
    let restored_inner = restored_mid.get(&"data".to_string()).unwrap();
    assert_eq!(restored_inner.get(&1), Some("hello".to_string()));
    assert_eq!(restored_inner.get(&2), Some("world".to_string()));
}

/// Postcard serde roundtrip for Mapx — verifies the hand-written
/// Serialize/Deserialize impl produces a valid, live handle.
#[test]
fn test_serde_roundtrip() {
    let mut hdr: Mapx<u32, String> = Mapx::new();
    hdr.insert(&1, &"alpha".into());
    hdr.insert(&2, &"beta".into());
    hdr.insert(&3, &"gamma".into());

    let bytes = postcard::to_allocvec(&hdr).unwrap();
    let restored: Mapx<u32, String> = postcard::from_bytes(&bytes).unwrap();

    assert!(restored.is_the_same_instance(&hdr));
    assert_eq!(restored.get(&1), Some("alpha".into()));
    assert_eq!(restored.get(&2), Some("beta".into()));
    assert_eq!(restored.get(&3), Some("gamma".into()));
}

/// Serialized size should stay compact.
#[test]
fn test_serde_size() {
    let hdr: Mapx<String, String> = Mapx::new();
    let bytes = postcard::to_allocvec(&hdr).unwrap();
    assert!(bytes.len() <= 64, "expected ≤64 bytes, got {}", bytes.len());
}

/// from_meta with a nonexistent ID must return an error.
#[test]
fn test_from_meta_nonexistent() {
    assert!(Mapx::<u32, u32>::from_meta(u64::MAX).is_err());
}

/// Restore from meta, mutate, verify both handles share storage.
#[test]
fn test_meta_restore_then_mutate() {
    let mut hdr: Mapx<u32, u32> = Mapx::new();
    hdr.insert(&1, &100);
    hdr.insert(&2, &200);

    let id = pnk!(hdr.save_meta());
    let mut restored: Mapx<u32, u32> = pnk!(Mapx::from_meta(id));

    restored.insert(&3, &300);
    restored.remove(&1);

    // Both handles see the same state
    assert!(hdr.get(&1).is_none());
    assert_eq!(hdr.get(&3), Some(300));
}

/// ValueEnDe roundtrip: encode a Mapx as a value, then decode it.
/// This is the path exercised when Mapx is used as a nested value type.
#[test]
fn test_valueende_roundtrip() {
    let mut inner: Mapx<u32, String> = Mapx::new();
    inner.insert(&10, &"ten".into());
    inner.insert(&20, &"twenty".into());

    let encoded = inner.encode();
    let decoded: Mapx<u32, String> = Mapx::decode(&encoded).unwrap();

    assert!(decoded.is_the_same_instance(&inner));
    assert_eq!(decoded.get(&10), Some("ten".into()));
    assert_eq!(decoded.get(&20), Some("twenty".into()));
}

// =====================================================================
// IntoIterator + keys()
// =====================================================================

#[test]
fn test_into_iter_ref() {
    let mut hdr: Mapx<u32, u32> = Mapx::new();
    for i in 0..10u32 {
        hdr.insert(&i, &(i * 10));
    }
    let mut count = 0;
    for (k, v) in &hdr {
        assert_eq!(v, k * 10);
        count += 1;
    }
    assert_eq!(count, 10);
}

#[test]
fn test_into_iter_mut() {
    let mut hdr: Mapx<u32, u32> = Mapx::new();
    for i in 0..5u32 {
        hdr.insert(&i, &i);
    }
    for (_k, mut v) in &mut hdr {
        *v += 100;
    }
    for i in 0..5u32 {
        assert_eq!(hdr.get(&i), Some(i + 100));
    }
}

#[test]
fn test_get_mut_persists_interior_mutability_change() {
    let mut hdr: Mapx<u32, RefCell<u32>> = Mapx::new();
    hdr.insert(&1, &RefCell::new(10));

    {
        let value = hdr.get_mut(&1).unwrap();
        *value.borrow_mut() = 20;
    }

    assert_eq!(*hdr.get(&1).unwrap().borrow(), 20);
}

#[test]
fn test_keys() {
    let mut hdr: Mapx<u32, String> = Mapx::new();
    hdr.insert(&3, &"c".into());
    hdr.insert(&1, &"a".into());
    hdr.insert(&2, &"b".into());
    let mut keys: Vec<u32> = hdr.keys().collect();
    keys.sort();
    assert_eq!(keys, vec![1, 2, 3]);
}

#[test]
fn test_keys_empty() {
    let hdr: Mapx<u32, u32> = Mapx::new();
    assert_eq!(hdr.keys().count(), 0);
}

// =====================================================================
// Persistence / nesting
// =====================================================================

/// Deep nesting: Mapx<String, Mapx<String, Mapx<u32, u64>>>
/// Only outer meta needed; all 3 layers survive.
#[test]
fn test_deep_triple_nesting() {
    let mut innermost: Mapx<u32, u64> = Mapx::new();
    innermost.insert(&1, &111);
    innermost.insert(&2, &222);

    let mut mid: Mapx<String, Mapx<u32, u64>> = Mapx::new();
    mid.insert(&"a".into(), &innermost);

    let mut outer: Mapx<String, Mapx<String, Mapx<u32, u64>>> = Mapx::new();
    outer.insert(&"top".into(), &mid);

    let id = pnk!(outer.save_meta());
    let restored: Mapx<String, Mapx<String, Mapx<u32, u64>>> = pnk!(Mapx::from_meta(id));

    let r_mid = restored.get(&"top".into()).unwrap();
    let r_inner = r_mid.get(&"a".into()).unwrap();
    assert_eq!(r_inner.get(&1), Some(111));
    assert_eq!(r_inner.get(&2), Some(222));
}

// =====================================================================
// PartialEq (regression: must compare *decoded* values, not raw bytes)
// =====================================================================

/// `0.0_f64 == -0.0_f64` decodes equal despite encoding to different
/// bytes — `Mapx`'s `PartialEq` must agree with `f64`'s own, not a
/// byte-level comparison.
#[test]
fn test_partial_eq_decodes_values_not_bytes() {
    let mut m1: Mapx<u32, f64> = Mapx::new();
    m1.insert(&1, &0.0_f64);

    let mut m2: Mapx<u32, f64> = Mapx::new();
    m2.insert(&1, &-0.0_f64);

    assert_eq!(0.0_f64, -0.0_f64);
    assert_eq!(
        m1, m2,
        "maps holding decode-equal values must compare equal"
    );
}

/// Conversely, two bit-identical NaNs decode as *unequal* per IEEE-754
/// (`NaN != NaN`) — a byte-derived `PartialEq` would wrongly call them
/// equal.
#[test]
fn test_partial_eq_nan_values_are_never_equal() {
    let mut m1: Mapx<u32, f64> = Mapx::new();
    m1.insert(&1, &f64::NAN);

    let mut m2: Mapx<u32, f64> = Mapx::new();
    m2.insert(&1, &f64::NAN);

    assert_ne!(m1, m2, "NaN must never compare equal, even to itself");
}