简体   繁体   English

压平 Map <vec<u8> , 向量<u8> &gt; 进入 Vec<u8> 然后将其返回给 Map <vec<u8> , 向量<u8> &gt; </u8></vec<u8></u8></u8></vec<u8>

[英]Flatten a Map<Vec<u8>, Vec<u8>> into a Vec<u8> and then return it to a Map<Vec<u8>, Vec<u8>>

I've have data in a HashMap<Vec<u8>, Vec<u8>> and I want to write that data to a file as a byte buffer (a single Vec<u8> ) and then read it back from the file and reconstruct the HashMap structure.我在HashMap<Vec<u8>, Vec<u8>>中有数据,我想将该数据作为字节缓冲区(单个Vec<u8> )写入文件,然后从文件中读回重构HashMap结构。

Is there an established algorithm for flattening and recovering maps like this?有没有一种既定的算法可以像这样展平和恢复地图? I could write metadata into the file to distinguish where the data partitions etc. I can't use structured serialization because of the nature of this project — I am encrypting the data and the file.我可以将元数据写入文件以区分数据分区的位置等。由于这个项目的性质,我不能使用结构化序列化——我正在加密数据和文件。

You may store this with the following format:您可以使用以下格式存储它:

value1_len | value1_bytes | key1_len | key1_bytes | value2_len | value2_bytes | key2_len | key2_bytes |...

what can be fairly easily done with the standard library ( playground ):使用标准库( 操场)可以很容易地完成什么:

use std::collections::HashMap;
use std::convert::TryInto;

fn serialize(map: &HashMap<Vec<u8>, Vec<u8>>) -> Vec<u8> {
    map.iter().fold(Vec::new(), |mut acc, (k, v)| {
        acc.extend(&k.len().to_le_bytes());
        acc.extend(k.as_slice());
        acc.extend(&v.len().to_le_bytes());
        acc.extend(v.as_slice());
        acc
    })
}

fn read_vec(input: &mut &[u8]) -> Vec<u8> {
    let (len, rest) = input.split_at(std::mem::size_of::<usize>());
    let len = usize::from_le_bytes(len.try_into().unwrap());
    let (v, rest) = rest.split_at(len);
    *input = rest;
    v.to_vec()
}

fn deserialize(bytes: &Vec<u8>) -> HashMap<Vec<u8>, Vec<u8>> {
    let mut map = HashMap::new();

    let mut left = &bytes[..];
    while left.len() > 0 {
        let k = read_vec(&mut left);
        let v = read_vec(&mut left);
        map.insert(k, v);
    }

    map
}

fn main() {
    let mut map = HashMap::new();
    map.insert(vec![1, 2, 3], vec![4, 5, 6]);
    map.insert(vec![4, 5, 6], vec![1, 2, 3]);
    map.insert(vec![1, 5, 3], vec![4, 2, 6]);

    let array = serialize(&map);
    let recovered_map = deserialize(&array);

    assert_eq!(map, recovered_map);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM