1use std::collections::BTreeMap;
4
5use bytes::Bytes;
6
7use crate::encode::Canonical;
8use crate::error::{Error, Result};
9
10pub const MAX_KEY_BYTES: usize = 1024;
12
13pub const DEFAULT_IMPORTANCE: f32 = 0.5;
15
16#[derive(Debug, Clone, PartialEq)]
23pub struct Entry {
24 pub value: Bytes,
26 pub embedding: Option<Vec<f32>>,
28 pub importance: f32,
30 pub created_seq: u64,
32 pub last_access_seq: u64,
34 pub ttl_commits: Option<u64>,
36 pub meta: BTreeMap<String, String>,
38}
39
40impl Entry {
41 pub fn content_eq(&self, other: &Entry) -> bool {
47 self.value == other.value
48 && self.embedding == other.embedding
49 && self.importance.to_bits() == other.importance.to_bits()
50 && self.ttl_commits == other.ttl_commits
51 && self.meta == other.meta
52 }
53}
54
55#[derive(Debug, Clone, PartialEq)]
57pub struct Value {
58 pub value: Bytes,
60 pub embedding: Option<Vec<f32>>,
62 pub importance: f32,
64 pub ttl_commits: Option<u64>,
66 pub meta: BTreeMap<String, String>,
68}
69
70impl Value {
71 pub fn new(value: impl Into<Bytes>) -> Self {
73 Self {
74 value: value.into(),
75 embedding: None,
76 importance: DEFAULT_IMPORTANCE,
77 ttl_commits: None,
78 meta: BTreeMap::new(),
79 }
80 }
81
82 #[must_use]
84 pub fn with_embedding(mut self, embedding: Vec<f32>) -> Self {
85 self.embedding = Some(embedding);
86 self
87 }
88
89 #[must_use]
91 pub fn with_importance(mut self, importance: f32) -> Self {
92 self.importance = importance;
93 self
94 }
95
96 #[must_use]
98 pub fn with_ttl_commits(mut self, ttl: u64) -> Self {
99 self.ttl_commits = Some(ttl);
100 self
101 }
102
103 #[must_use]
105 pub fn with_meta(mut self, k: impl Into<String>, v: impl Into<String>) -> Self {
106 self.meta.insert(k.into(), v.into());
107 self
108 }
109
110 pub(crate) fn validate(&self) -> Result<()> {
111 if !(0.0..=1.0).contains(&self.importance) {
112 return Err(Error::BadImportance(self.importance));
113 }
114 if let Some(e) = &self.embedding {
115 if e.is_empty() {
116 return Err(Error::EmptyEmbedding);
117 }
118 }
119 Ok(())
120 }
121
122 pub(crate) fn into_entry(self, seq: u64, created_seq: u64) -> Entry {
123 Entry {
124 value: self.value,
125 embedding: self.embedding,
126 importance: self.importance,
127 created_seq,
128 last_access_seq: seq,
129 ttl_commits: self.ttl_commits,
130 meta: self.meta,
131 }
132 }
133}
134
135impl From<&Entry> for Value {
136 fn from(e: &Entry) -> Self {
137 Value {
138 value: e.value.clone(),
139 embedding: e.embedding.clone(),
140 importance: e.importance,
141 ttl_commits: e.ttl_commits,
142 meta: e.meta.clone(),
143 }
144 }
145}
146
147pub fn validate_key(key: &str) -> Result<()> {
149 if key.is_empty() {
150 return Err(Error::EmptyKey);
151 }
152 if key.len() > MAX_KEY_BYTES {
153 return Err(Error::KeyTooLong(key.len()));
154 }
155 Ok(())
156}
157
158#[derive(Debug, Clone, PartialEq)]
161pub enum Op {
162 Put {
164 key: String,
166 value: Value,
168 },
169 Delete {
171 key: String,
173 },
174 Evict {
179 key: String,
181 },
182}
183
184impl Op {
185 pub fn key(&self) -> &str {
187 match self {
188 Op::Put { key, .. } | Op::Delete { key } | Op::Evict { key } => key,
189 }
190 }
191
192 pub(crate) fn encode(&self, c: &mut Canonical) {
193 match self {
194 Op::Put { key, value } => {
195 c.u8(1).str(key);
196 c.bytes(&value.value);
197 c.opt_f32_slice(value.embedding.as_deref());
198 c.f32(value.importance);
199 c.opt_u64(value.ttl_commits);
200 c.u64(value.meta.len() as u64);
201 for (k, v) in &value.meta {
202 c.str(k).str(v);
203 }
204 }
205 Op::Delete { key } => {
206 c.u8(2).str(key);
207 }
208 Op::Evict { key } => {
209 c.u8(3).str(key);
210 }
211 }
212 }
213}