1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::collections::HashMap;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use super::{serialization::BinarySerialization, PagedWriter};
use crate::{error::Error, io::File, vault::AnyVault, ArcBytes, ErrorKind};
#[derive(Debug, Clone)]
pub struct KeyEntry<Index> {
pub key: ArcBytes<'static>,
pub index: Index,
}
pub trait ValueIndex {
fn position(&self) -> u64;
}
impl<Index: ValueIndex + BinarySerialization> KeyEntry<Index> {
pub(crate) fn copy_data_to<Callback>(
&mut self,
file: &mut dyn File,
copied_chunks: &mut HashMap<u64, u64>,
writer: &mut PagedWriter<'_>,
vault: Option<&dyn AnyVault>,
index_callback: &mut Callback,
) -> Result<bool, Error>
where
Callback: FnMut(
&ArcBytes<'static>,
&mut Index,
&mut dyn File,
&mut HashMap<u64, u64>,
&mut PagedWriter<'_>,
Option<&dyn AnyVault>,
) -> Result<bool, Error>,
{
index_callback(
&self.key,
&mut self.index,
file,
copied_chunks,
writer,
vault,
)
}
}
impl<Index: BinarySerialization> BinarySerialization for KeyEntry<Index> {
fn serialize_to(
&mut self,
writer: &mut Vec<u8>,
paged_writer: &mut PagedWriter<'_>,
) -> Result<usize, Error> {
let mut bytes_written = 0;
let key_len = u16::try_from(self.key.len()).map_err(|_| ErrorKind::KeyTooLarge)?;
writer.write_u16::<BigEndian>(key_len)?;
writer.extend_from_slice(&self.key);
bytes_written += 2 + key_len as usize;
bytes_written += self.index.serialize_to(writer, paged_writer)?;
Ok(bytes_written)
}
fn deserialize_from(
reader: &mut ArcBytes<'_>,
current_order: Option<usize>,
) -> Result<Self, Error> {
let key_len = reader.read_u16::<BigEndian>()? as usize;
if key_len > reader.len() {
return Err(Error::data_integrity(format!(
"key length {} found but only {} bytes remaining",
key_len,
reader.len()
)));
}
let key = reader.read_bytes(key_len)?.into_owned();
let value = Index::deserialize_from(reader, current_order)?;
Ok(Self { key, index: value })
}
}