MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / build

Method build

nodedb-query/src/msgpack_scan/index.rs:34–80  ·  view source on GitHub ↗

Build an index for the msgpack map at `offset` in `buf`. Scans all map keys once and records value byte ranges. Returns `None` if `buf` is not a valid map at `offset`.

(buf: &[u8], offset: usize)

Source from the content-addressed store, hash-verified

32 /// Scans all map keys once and records value byte ranges.
33 /// Returns `None` if `buf` is not a valid map at `offset`.
34 pub fn build(buf: &[u8], offset: usize) -> Option<Self> {
35 let (count, mut pos) = map_header(buf, offset)?;
36
37 if count <= HASH_THRESHOLD {
38 let mut entries = Vec::with_capacity(count);
39 for _ in 0..count {
40 let key_str = if let Some((start, len)) = str_bounds(buf, pos) {
41 std::str::from_utf8(buf.get(start..start + len)?).ok()
42 } else {
43 None
44 };
45 pos = skip_value(buf, pos)?;
46 let value_start = pos;
47 let value_end = skip_value(buf, pos)?;
48 if let Some(key) = key_str {
49 entries.push((key.into(), value_start, value_end));
50 }
51 pos = value_end;
52 }
53 Some(Self {
54 inner: IndexInner::Flat(entries),
55 })
56 } else {
57 // Cap pre-allocation: adversarial buffers may claim enormous counts.
58 // Actual insertions are bounded by the buffer size, so over-allocating
59 // wastes memory and under-allocating just triggers rehashing.
60 let cap = count.min(buf.len() / 2 + 1);
61 let mut offsets = std::collections::HashMap::with_capacity(cap);
62 for _ in 0..count {
63 let key_str = if let Some((start, len)) = str_bounds(buf, pos) {
64 std::str::from_utf8(buf.get(start..start + len)?).ok()
65 } else {
66 None
67 };
68 pos = skip_value(buf, pos)?;
69 let value_start = pos;
70 let value_end = skip_value(buf, pos)?;
71 if let Some(key) = key_str {
72 offsets.insert(key.into(), (value_start, value_end));
73 }
74 pos = value_end;
75 }
76 Some(Self {
77 inner: IndexInner::Map(offsets),
78 })
79 }
80 }
81
82 /// Create an empty index (no fields).
83 pub fn empty() -> Self {

Callers

nothing calls this directly

Calls 8

map_headerFunction · 0.85
str_boundsFunction · 0.85
skip_valueFunction · 0.85
okMethod · 0.45
getMethod · 0.45
pushMethod · 0.45
lenMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected