value_log/
handle.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5use crate::{
6    coding::{Decode, DecodeError, Encode, EncodeError},
7    id::SegmentId,
8};
9use std::{
10    hash::Hash,
11    io::{Read, Write},
12};
13use varint_rs::{VarintReader, VarintWriter};
14
15/// A value handle points into the value log
16#[allow(clippy::module_name_repetitions)]
17#[derive(Clone, Debug, Eq, Hash, PartialEq)]
18#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
19pub struct ValueHandle {
20    /// Segment ID
21    pub segment_id: SegmentId,
22
23    /// Offset in file
24    pub offset: u64,
25}
26
27impl Encode for ValueHandle {
28    fn encode_into<W: Write>(&self, writer: &mut W) -> Result<(), EncodeError> {
29        writer.write_u64_varint(self.offset)?;
30        writer.write_u64_varint(self.segment_id)?;
31        Ok(())
32    }
33}
34
35impl Decode for ValueHandle {
36    fn decode_from<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
37        let offset = reader.read_u64_varint()?;
38        let segment_id = reader.read_u64_varint()?;
39        Ok(Self { segment_id, offset })
40    }
41}