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

Function extract_field

nodedb-query/src/msgpack_scan/field.rs:24–53  ·  view source on GitHub ↗

Locate the value for `field` in a MessagePack map starting at `offset`. Returns the byte range `(value_start, value_end)` of the matched value. Scans map keys sequentially — O(n) in number of keys. For documents with many fields queried repeatedly, see structural indexing (Phase 8). # Returns - `Some((start, end))` — the value's byte range (use offset `start` with readers) - `None` — field not f

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

Source from the content-addressed store, hash-verified

22/// - `Some((start, end))` — the value's byte range (use offset `start` with readers)
23/// - `None` — field not found, or buffer is not a valid map
24pub fn extract_field(buf: &[u8], offset: usize, field: &str) -> Option<FieldRange> {
25 let (count, mut pos) = map_header(buf, offset)?;
26 let field_bytes = field.as_bytes();
27
28 for _ in 0..count {
29 // Read key string bounds
30 let key_match = match str_bounds(buf, pos) {
31 Some((start, len)) => buf
32 .get(start..start + len)
33 .map(|kb| kb == field_bytes)
34 .unwrap_or(false),
35 None => false,
36 };
37
38 // Skip past the key
39 pos = skip_value(buf, pos)?;
40
41 if key_match {
42 // Found — return the value's byte range
43 let value_start = pos;
44 let value_end = skip_value(buf, pos)?;
45 return Some((value_start, value_end));
46 }
47
48 // Skip the value
49 pos = skip_value(buf, pos)?;
50 }
51
52 None
53}
54
55/// Extract a value at a nested path (e.g., `["address", "city"]`).
56/// Each segment must be a string key in a nested map.

Callers 15

matches_binaryMethod · 0.85
eval_opFunction · 0.85
extract_f64Function · 0.85
extract_strFunction · 0.85
extract_valueFunction · 0.85
extract_bytesFunction · 0.85
append_field_valueFunction · 0.85
extract_pathFunction · 0.85
extract_integer_fieldFunction · 0.85
extract_string_fieldFunction · 0.85
extract_float_fieldFunction · 0.85

Calls 5

map_headerFunction · 0.85
str_boundsFunction · 0.85
skip_valueFunction · 0.85
as_bytesMethod · 0.45
getMethod · 0.45

Tested by 14

extract_integer_fieldFunction · 0.68
extract_string_fieldFunction · 0.68
extract_float_fieldFunction · 0.68
extract_multiple_fieldsFunction · 0.68
extract_from_large_mapFunction · 0.68
fuzz_truncated_buffersFunction · 0.68
fuzz_random_payloadsFunction · 0.68