store_api/logstore/
entry.rsuse std::mem::size_of;
use crate::logstore::provider::Provider;
use crate::storage::RegionId;
pub type Id = u64;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Entry {
Naive(NaiveEntry),
MultiplePart(MultiplePartEntry),
}
impl Entry {
pub fn into_naive_entry(self) -> Option<NaiveEntry> {
match self {
Entry::Naive(entry) => Some(entry),
Entry::MultiplePart(_) => None,
}
}
pub fn into_multiple_part_entry(self) -> Option<MultiplePartEntry> {
match self {
Entry::Naive(_) => None,
Entry::MultiplePart(entry) => Some(entry),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NaiveEntry {
pub provider: Provider,
pub region_id: RegionId,
pub entry_id: Id,
pub data: Vec<u8>,
}
impl NaiveEntry {
fn estimated_size(&self) -> usize {
size_of::<Self>() + self.data.capacity() * size_of::<u8>()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MultiplePartHeader {
First,
Middle(usize),
Last,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MultiplePartEntry {
pub provider: Provider,
pub region_id: RegionId,
pub entry_id: Id,
pub headers: Vec<MultiplePartHeader>,
pub parts: Vec<Vec<u8>>,
}
impl MultiplePartEntry {
fn is_complete(&self) -> bool {
self.headers.contains(&MultiplePartHeader::First)
&& self.headers.contains(&MultiplePartHeader::Last)
}
fn estimated_size(&self) -> usize {
size_of::<Self>()
+ self
.parts
.iter()
.map(|data| data.capacity() * size_of::<u8>())
.sum::<usize>()
+ self.headers.capacity() * size_of::<MultiplePartHeader>()
}
}
impl Entry {
pub fn provider(&self) -> &Provider {
match self {
Entry::Naive(entry) => &entry.provider,
Entry::MultiplePart(entry) => &entry.provider,
}
}
pub fn region_id(&self) -> RegionId {
match self {
Entry::Naive(entry) => entry.region_id,
Entry::MultiplePart(entry) => entry.region_id,
}
}
pub fn entry_id(&self) -> Id {
match self {
Entry::Naive(entry) => entry.entry_id,
Entry::MultiplePart(entry) => entry.entry_id,
}
}
pub fn set_entry_id(&mut self, id: Id) {
match self {
Entry::Naive(entry) => entry.entry_id = id,
Entry::MultiplePart(entry) => entry.entry_id = id,
}
}
pub fn is_complete(&self) -> bool {
match self {
Entry::Naive(_) => true,
Entry::MultiplePart(entry) => entry.is_complete(),
}
}
pub fn into_bytes(self) -> Vec<u8> {
match self {
Entry::Naive(entry) => entry.data,
Entry::MultiplePart(entry) => entry.parts.concat(),
}
}
pub fn estimated_size(&self) -> usize {
match self {
Entry::Naive(entry) => entry.estimated_size(),
Entry::MultiplePart(entry) => entry.estimated_size(),
}
}
}