Merge a left and optional right document into a single msgpack map, prefixing each key with its source collection name. Returns raw msgpack bytes — no JSON decode, no serde_json::Value. Uses binary scan to iterate source map entries and writes directly to the output buffer.
(
left_bytes: &[u8],
right_bytes: Option<&[u8]>,
left_collection: &str,
right_collection: &str,
)
| 22 | /// Uses binary scan to iterate source map entries and writes directly |
| 23 | /// to the output buffer. |
| 24 | pub fn merge_join_docs_binary( |
| 25 | left_bytes: &[u8], |
| 26 | right_bytes: Option<&[u8]>, |
| 27 | left_collection: &str, |
| 28 | right_collection: &str, |
| 29 | ) -> Vec<u8> { |
| 30 | let left_count = count_map_entries(left_bytes); |
| 31 | let right_count = right_bytes.map_or(0, count_map_entries); |
| 32 | let total = left_count + right_count; |
| 33 | |
| 34 | // Estimate capacity: original data + prefixed keys overhead. |
| 35 | let cap = left_bytes.len() |
| 36 | + right_bytes.map_or(0, |b| b.len()) |
| 37 | + total * (left_collection.len().max(right_collection.len()) + 8); |
| 38 | let mut buf = Vec::with_capacity(cap); |
| 39 | |
| 40 | write_map_header(&mut buf, total); |
| 41 | write_prefixed_entries(&mut buf, left_bytes, left_collection); |
| 42 | if let Some(rb) = right_bytes { |
| 43 | write_prefixed_entries(&mut buf, rb, right_collection); |
| 44 | } |
| 45 | buf |
| 46 | } |
| 47 | |
| 48 | /// Count entries in a msgpack map. |
| 49 | fn count_map_entries(bytes: &[u8]) -> usize { |