use std::collections::BTreeMap;
use bytes::Bytes;
use crate::encode::Canonical;
use crate::error::{Error, Result};
pub const MAX_KEY_BYTES: usize = 1024;
pub const DEFAULT_IMPORTANCE: f32 = 0.5;
pub const WRITTEN_BY: &str = "memfork.by";
#[derive(Debug, Clone, PartialEq)]
pub struct Entry {
pub value: Bytes,
pub embedding: Option<Vec<f32>>,
pub importance: f32,
pub created_seq: u64,
pub last_access_seq: u64,
pub ttl_commits: Option<u64>,
pub meta: BTreeMap<String, String>,
}
impl Entry {
pub fn content_eq(&self, other: &Entry) -> bool {
fn described(meta: &BTreeMap<String, String>) -> impl Iterator<Item = (&String, &String)> {
meta.iter().filter(|(k, _)| k.as_str() != WRITTEN_BY)
}
self.value == other.value
&& self.embedding == other.embedding
&& self.importance.to_bits() == other.importance.to_bits()
&& self.ttl_commits == other.ttl_commits
&& described(&self.meta).eq(described(&other.meta))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Value {
pub value: Bytes,
pub embedding: Option<Vec<f32>>,
pub importance: f32,
pub ttl_commits: Option<u64>,
pub meta: BTreeMap<String, String>,
}
impl Value {
pub fn new(value: impl Into<Bytes>) -> Self {
Self {
value: value.into(),
embedding: None,
importance: DEFAULT_IMPORTANCE,
ttl_commits: None,
meta: BTreeMap::new(),
}
}
#[must_use]
pub fn with_embedding(mut self, embedding: Vec<f32>) -> Self {
self.embedding = Some(embedding);
self
}
#[must_use]
pub fn with_importance(mut self, importance: f32) -> Self {
self.importance = importance;
self
}
#[must_use]
pub fn with_ttl_commits(mut self, ttl: u64) -> Self {
self.ttl_commits = Some(ttl);
self
}
#[must_use]
pub fn with_meta(mut self, k: impl Into<String>, v: impl Into<String>) -> Self {
self.meta.insert(k.into(), v.into());
self
}
pub(crate) fn validate(&self) -> Result<()> {
if !(0.0..=1.0).contains(&self.importance) {
return Err(Error::BadImportance(self.importance));
}
if let Some(e) = &self.embedding {
if e.is_empty() {
return Err(Error::EmptyEmbedding);
}
}
Ok(())
}
pub(crate) fn into_entry(self, seq: u64, created_seq: u64) -> Entry {
Entry {
value: self.value,
embedding: self.embedding,
importance: self.importance,
created_seq,
last_access_seq: seq,
ttl_commits: self.ttl_commits,
meta: self.meta,
}
}
}
impl From<&Entry> for Value {
fn from(e: &Entry) -> Self {
Value {
value: e.value.clone(),
embedding: e.embedding.clone(),
importance: e.importance,
ttl_commits: e.ttl_commits,
meta: e.meta.clone(),
}
}
}
pub fn validate_key(key: &str) -> Result<()> {
if key.is_empty() {
return Err(Error::EmptyKey);
}
if key.len() > MAX_KEY_BYTES {
return Err(Error::KeyTooLong(key.len()));
}
Ok(())
}
#[derive(Debug, Clone, PartialEq)]
pub enum Op {
Put {
key: String,
value: Value,
},
Delete {
key: String,
},
Evict {
key: String,
},
}
impl Op {
pub fn key(&self) -> &str {
match self {
Op::Put { key, .. } | Op::Delete { key } | Op::Evict { key } => key,
}
}
pub(crate) fn encode(&self, c: &mut Canonical) {
match self {
Op::Put { key, value } => {
c.u8(1).str(key);
c.bytes(&value.value);
c.opt_f32_slice(value.embedding.as_deref());
c.f32(value.importance);
c.opt_u64(value.ttl_commits);
c.u64(value.meta.len() as u64);
for (k, v) in &value.meta {
c.str(k).str(v);
}
}
Op::Delete { key } => {
c.u8(2).str(key);
}
Op::Evict { key } => {
c.u8(3).str(key);
}
}
}
}