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

Function inject_str_field

nodedb-query/src/msgpack_scan/writer.rs:171–189  ·  view source on GitHub ↗

Inject a string field into a msgpack map without full decode. If `value` is a valid msgpack map that already contains `field_name`, the existing value is preserved and the input is returned unchanged — the caller's body is authoritative for its own primary key. If the map does not contain the field, it is prepended. If `value` is not a map, wraps as `{field_name: field_value, "value": raw}`.

(value: &[u8], field_name: &str, field_value: &str)

Source from the content-addressed store, hash-verified

169/// does not contain the field, it is prepended. If `value` is not a map,
170/// wraps as `{field_name: field_value, "value": raw}`.
171pub fn inject_str_field(value: &[u8], field_name: &str, field_value: &str) -> Vec<u8> {
172 if let Some((count, body_start)) = crate::msgpack_scan::reader::map_header(value, 0) {
173 if crate::msgpack_scan::extract_field(value, 0, field_name).is_some() {
174 return value.to_vec();
175 }
176 let mut buf = Vec::with_capacity(value.len() + field_name.len() + field_value.len() + 16);
177 write_map_header(&mut buf, count + 1);
178 write_kv_str(&mut buf, field_name, field_value);
179 buf.extend_from_slice(&value[body_start..]);
180 buf
181 } else {
182 let mut buf = Vec::with_capacity(value.len() + field_name.len() + field_value.len() + 16);
183 write_map_header(&mut buf, 2);
184 write_kv_str(&mut buf, field_name, field_value);
185 write_str(&mut buf, "value");
186 buf.extend_from_slice(value);
187 buf
188 }
189}
190
191/// Merge field updates into a msgpack map without full decode.
192///

Callers 7

maybe_wrap_kv_point_getFunction · 0.85
scan_kvMethod · 0.85
scan_sparseMethod · 0.85
execute_bulk_deleteMethod · 0.85
execute_kv_scanMethod · 0.85
execute_point_updateMethod · 0.85
execute_point_deleteMethod · 0.85

Calls 7

map_headerFunction · 0.85
extract_fieldFunction · 0.85
write_kv_strFunction · 0.85
write_map_headerFunction · 0.70
write_strFunction · 0.70
to_vecMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected