use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct MetricSample {
pub timestamp_ms: i64,
pub value: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
pub timestamp_ms: i64,
pub data: Vec<u8>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IngestResult {
Ok,
FlushNeeded,
Rejected,
}
impl IngestResult {
pub fn is_flush_needed(&self) -> bool {
matches!(self, Self::FlushNeeded)
}
pub fn is_rejected(&self) -> bool {
matches!(self, Self::Rejected)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TimeRange {
pub start_ms: i64,
pub end_ms: i64,
}
impl TimeRange {
pub fn new(start_ms: i64, end_ms: i64) -> Self {
Self { start_ms, end_ms }
}
pub fn contains(&self, ts: i64) -> bool {
ts >= self.start_ms && ts <= self.end_ms
}
pub fn overlaps(&self, other: &TimeRange) -> bool {
self.start_ms <= other.end_ms && other.start_ms <= self.end_ms
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SymbolDictionary {
forward: HashMap<String, u32>,
reverse: Vec<String>,
}
impl SymbolDictionary {
pub fn new() -> Self {
Self::default()
}
pub fn resolve(&mut self, value: &str, max_cardinality: u32) -> Option<u32> {
if let Some(&id) = self.forward.get(value) {
return Some(id);
}
if self.reverse.len() as u32 >= max_cardinality {
return None;
}
let id = self.reverse.len() as u32;
self.forward.insert(value.to_string(), id);
self.reverse.push(value.to_string());
Some(id)
}
pub fn get(&self, id: u32) -> Option<&str> {
self.reverse.get(id as usize).map(|s| s.as_str())
}
pub fn get_id(&self, value: &str) -> Option<u32> {
self.forward.get(value).copied()
}
pub fn len(&self) -> usize {
self.reverse.len()
}
pub fn is_empty(&self) -> bool {
self.reverse.is_empty()
}
pub fn merge(&mut self, other: &SymbolDictionary, max_cardinality: u32) -> Vec<u32> {
let mut remap = Vec::with_capacity(other.reverse.len());
for symbol in &other.reverse {
match self.resolve(symbol, max_cardinality) {
Some(new_id) => remap.push(new_id),
None => remap.push(u32::MAX),
}
}
remap
}
}