tantivy_sstable/value/
mod.rs

1pub(crate) mod index;
2mod range;
3mod u64_monotonic;
4mod void;
5
6use std::io;
7
8/// `ValueReader` is a trait describing the contract of something
9/// reading blocks of value, and offering random access within this values.
10pub trait ValueReader: Default {
11    /// Type of the value being read.
12    type Value;
13
14    /// Access the value at index `idx`, in the last block that was read
15    /// via a call to `ValueReader::read`.
16    fn value(&self, idx: usize) -> &Self::Value;
17
18    /// Loads a block.
19    ///
20    /// Returns the number of bytes that were read.
21    fn load(&mut self, data: &[u8]) -> io::Result<usize>;
22}
23
24/// `ValueWriter` is a trait to make it possible to write blocks
25/// of value.
26pub trait ValueWriter: Default {
27    /// Type of the value being written.
28    type Value;
29
30    /// Records a new value.
31    /// This method usually just accumulates data in a `Vec`,
32    /// only to be serialized on the call to `ValueWriter::serialize_block`.
33    fn write(&mut self, val: &Self::Value);
34
35    /// Serializes the accumulated values into the output buffer.
36    fn serialize_block(&self, output: &mut Vec<u8>);
37
38    /// Clears the `ValueWriter`. After a call to clear, the `ValueWriter`
39    /// should behave like a fresh `ValueWriter::default()`.
40    fn clear(&mut self);
41}
42
43pub use range::{RangeValueReader, RangeValueWriter};
44pub use u64_monotonic::{U64MonotonicValueReader, U64MonotonicValueWriter};
45pub use void::{VoidValueReader, VoidValueWriter};
46
47fn deserialize_vint_u64(data: &mut &[u8]) -> u64 {
48    let (num_bytes, val) = super::vint::deserialize_read(data);
49    *data = &data[num_bytes..];
50    val
51}
52
53#[cfg(test)]
54pub(crate) mod tests {
55    use std::fmt;
56
57    use super::{ValueReader, ValueWriter};
58
59    pub(crate) fn test_value_reader_writer<
60        V: Eq + fmt::Debug,
61        TReader: ValueReader<Value = V>,
62        TWriter: ValueWriter<Value = V>,
63    >(
64        value_block: &[V],
65    ) {
66        let mut buffer = Vec::new();
67        {
68            let mut writer = TWriter::default();
69            for value in value_block {
70                writer.write(value);
71            }
72            writer.serialize_block(&mut buffer);
73            writer.clear();
74        }
75        let data_len = buffer.len();
76        buffer.extend_from_slice(&b"extradata"[..]);
77        let mut reader = TReader::default();
78        assert_eq!(reader.load(&buffer[..]).unwrap(), data_len);
79        for (i, val) in value_block.iter().enumerate() {
80            assert_eq!(reader.value(i), val);
81        }
82    }
83}