Skip to content

Commit 08c94e2

Browse files
authored
Dev (#18)
* refactored all the project from namespaces to collections * Migrated from rememer to add * Renamed ask to search * refactored other functions * Removed changelog file * Big refactor
1 parent 252553c commit 08c94e2

50 files changed

Lines changed: 785 additions & 835 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

benchmark/cortexadb_runner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def run_benchmark(
8383
db = CortexaDB.open(db_path, dimension=len(embeddings[0]))
8484

8585
for i, emb in enumerate(embeddings):
86-
db.remember(f"memory_{i}", embedding=emb)
86+
db.add(f"memory_{i}", embedding=emb)
8787

8888
# Force checkpoint to flush
8989
db.checkpoint()
@@ -108,7 +108,7 @@ def run_benchmark(
108108
# === WARMUP ===
109109
print(f"Warming up with {warmup_queries} queries...")
110110
for i in range(warmup_queries):
111-
_ = db._inner.ask_embedding(
111+
_ = db._inner.search_embedding(
112112
embedding=queries[i % len(queries)], top_k=top_k
113113
)
114114

@@ -120,7 +120,7 @@ def run_benchmark(
120120
query = queries[i % len(queries)]
121121

122122
start = time.perf_counter()
123-
hits = db._inner.ask_embedding(embedding=query, top_k=top_k)
123+
hits = db._inner.search_embedding(embedding=query, top_k=top_k)
124124
elapsed = (time.perf_counter() - start) * 1000 # ms
125125

126126
latencies.append(elapsed)
@@ -151,7 +151,7 @@ def run_benchmark(
151151
exact_ids = exact_search(embeddings, query, top_k)
152152

153153
# Get HNSW results
154-
hits = db._inner.ask_embedding(embedding=query, top_k=top_k)
154+
hits = db._inner.search_embedding(embedding=query, top_k=top_k)
155155
hnsw_ids = [hit.id for hit in hits]
156156

157157
# Calculate recall

crates/cortexadb-core/benches/storage_bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn bench_ingestion(c: &mut Criterion) {
2222

2323
c.bench_function("ingest_single_memory", |b| {
2424
b.iter(|| {
25-
db.remember(embedding.clone(), None).unwrap();
25+
db.add(embedding.clone(), None).unwrap();
2626
})
2727
});
2828
}

crates/cortexadb-core/src/bin/manual_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4949
store.add_edge(MemoryId(1), MemoryId(2), "relates_to".to_string())?;
5050

5151
let mut options = QueryOptions::with_top_k(5);
52-
options.namespace = Some("agent1".to_string());
52+
options.collection = Some("agent1".to_string());
5353

5454
let out = store.query("rust", options, &DemoEmbedder)?;
5555

crates/cortexadb-core/src/bin/startup_bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4545
for i in 0..entry_count {
4646
let embedding: Vec<f32> =
4747
(0..vector_dim).map(|d| ((i * 7 + d * 13) % 100) as f32 / 100.0).collect();
48-
db.remember(embedding, None)?;
48+
db.add(embedding, None)?;
4949
}
5050
}
5151
let seed_elapsed = seed_start.elapsed();
@@ -105,7 +105,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
105105
for i in 0..tail_count {
106106
let embedding: Vec<f32> =
107107
(0..vector_dim).map(|d| ((i * 11 + d * 3) % 100) as f32 / 100.0).collect();
108-
db.remember(embedding, None)?;
108+
db.add(embedding, None)?;
109109
}
110110
}
111111

crates/cortexadb-core/src/bin/sync_bench.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2424
for i in 0..cfg.ops {
2525
let entry = MemoryEntry::new(
2626
MemoryId(i),
27-
cfg.namespace.clone(),
27+
cfg.collection.clone(),
2828
format!("bench_mem_{}", i).into_bytes(),
2929
1_700_000_000 + i,
3030
)
@@ -76,7 +76,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
7676
struct BenchConfig {
7777
ops: u64,
7878
vector_dim: usize,
79-
namespace: String,
79+
collection: String,
8080
data_dir: PathBuf,
8181
policy: SyncPolicy,
8282
}
@@ -85,7 +85,7 @@ impl BenchConfig {
8585
fn from_args(args: Vec<String>) -> Result<Self, Box<dyn std::error::Error>> {
8686
let mut ops: u64 = 20_000;
8787
let mut vector_dim: usize = 3;
88-
let mut namespace = "bench".to_string();
88+
let mut collection = "bench".to_string();
8989
let mut data_dir = std::env::temp_dir().join("cortexadb_sync_bench");
9090

9191
let mut mode = "strict".to_string();
@@ -104,9 +104,9 @@ impl BenchConfig {
104104
i += 1;
105105
vector_dim = args.get(i).ok_or("missing value for --vector-dim")?.parse()?;
106106
}
107-
"--namespace" => {
107+
"--collection" => {
108108
i += 1;
109-
namespace = args.get(i).ok_or("missing value for --namespace")?.clone();
109+
collection = args.get(i).ok_or("missing value for --collection")?.clone();
110110
}
111111
"--data-dir" => {
112112
i += 1;
@@ -151,7 +151,7 @@ impl BenchConfig {
151151
_ => return Err(format!("invalid mode: {} (use strict|batch|async)", mode).into()),
152152
};
153153

154-
Ok(Self { ops, vector_dim, namespace, data_dir, policy })
154+
Ok(Self { ops, vector_dim, collection, data_dir, policy })
155155
}
156156
}
157157

@@ -169,7 +169,7 @@ fn print_help() {
169169
--mode strict|batch|async\n\
170170
--ops <u64> (default: 20000)\n\
171171
--vector-dim <usize> (default: 3)\n\
172-
--namespace <string> (default: bench)\n\
172+
--collection <string> (default: bench)\n\
173173
--data-dir <path> (default: /tmp/cortexadb_sync_bench)\n\
174174
--batch-max-ops <usize> (default: 64)\n\
175175
--batch-max-delay-ms <u64> (default: 25)\n\

crates/cortexadb-core/src/core/command.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub enum Command {
88
/// Insert or update a memory entry
99
InsertMemory(MemoryEntry),
1010
/// Delete a memory entry by ID
11-
DeleteMemory(MemoryId),
11+
Delete(MemoryId),
1212
/// Add an edge between two memories with a relation type
1313
AddEdge { from: MemoryId, to: MemoryId, relation: String },
1414
/// Remove an edge between two memories
@@ -20,8 +20,8 @@ impl Command {
2020
Command::InsertMemory(entry)
2121
}
2222

23-
pub fn delete_memory(id: MemoryId) -> Self {
24-
Command::DeleteMemory(id)
23+
pub fn delete(id: MemoryId) -> Self {
24+
Command::Delete(id)
2525
}
2626

2727
pub fn add_edge(from: MemoryId, to: MemoryId, relation: String) -> Self {
@@ -49,9 +49,9 @@ mod tests {
4949

5050
#[test]
5151
fn test_delete_command() {
52-
let cmd = Command::delete_memory(MemoryId(1));
52+
let cmd = Command::delete(MemoryId(1));
5353
match cmd {
54-
Command::DeleteMemory(id) => assert_eq!(id, MemoryId(1)),
54+
Command::Delete(id) => assert_eq!(id, MemoryId(1)),
5555
_ => panic!("Expected DeleteMemory"),
5656
}
5757
}

crates/cortexadb-core/src/core/memory_entry.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct MemoryId(pub u64);
99
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1010
pub struct MemoryEntry {
1111
pub id: MemoryId,
12-
pub namespace: String,
12+
pub collection: String,
1313
pub content: Vec<u8>,
1414
pub embedding: Option<Vec<f32>>,
1515
pub metadata: HashMap<String, String>,
@@ -18,10 +18,10 @@ pub struct MemoryEntry {
1818
}
1919

2020
impl MemoryEntry {
21-
pub fn new(id: MemoryId, namespace: String, content: Vec<u8>, created_at: u64) -> Self {
21+
pub fn new(id: MemoryId, collection: String, content: Vec<u8>, created_at: u64) -> Self {
2222
Self {
2323
id,
24-
namespace,
24+
collection,
2525
content,
2626
embedding: None,
2727
metadata: HashMap::new(),
@@ -50,7 +50,7 @@ mod tests {
5050
let entry =
5151
MemoryEntry::new(MemoryId(1), "default".to_string(), b"test content".to_vec(), 1000);
5252
assert_eq!(entry.id, MemoryId(1));
53-
assert_eq!(entry.namespace, "default");
53+
assert_eq!(entry.collection, "default");
5454
assert_eq!(entry.importance, 0.0);
5555
assert_eq!(entry.embedding, None);
5656
}

crates/cortexadb-core/src/core/state_machine.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub enum StateMachineError {
1111
MemoryNotFound(MemoryId),
1212
#[error("Invalid state: {0}")]
1313
InvalidState(String),
14-
#[error("Cross-namespace edge is not allowed: from={from:?} ({from_ns}) to={to:?} ({to_ns})")]
15-
CrossNamespaceEdge { from: MemoryId, from_ns: String, to: MemoryId, to_ns: String },
14+
#[error("Cross-collection edge is not allowed: from={from:?} ({from_col}) to={to:?} ({to_col})")]
15+
CrossCollectionEdge { from: MemoryId, from_col: String, to: MemoryId, to_col: String },
1616
}
1717

1818
pub type Result<T> = std::result::Result<T, StateMachineError>;
@@ -44,7 +44,7 @@ impl StateMachine {
4444
pub fn apply_command(&mut self, cmd: Command) -> Result<()> {
4545
match cmd {
4646
Command::InsertMemory(entry) => self.insert_memory(entry),
47-
Command::DeleteMemory(id) => self.delete_memory(id),
47+
Command::Delete(id) => self.delete(id),
4848
Command::AddEdge { from, to, relation } => self.add_edge(from, to, relation),
4949
Command::RemoveEdge { from, to } => self.remove_edge(from, to),
5050
}
@@ -83,7 +83,7 @@ impl StateMachine {
8383
}
8484

8585
/// Delete a memory entry and its edges
86-
pub fn delete_memory(&mut self, id: MemoryId) -> Result<()> {
86+
pub fn delete(&mut self, id: MemoryId) -> Result<()> {
8787
if !self.memories.contains_key(&id) {
8888
return Err(StateMachineError::MemoryNotFound(id));
8989
}
@@ -109,12 +109,12 @@ impl StateMachine {
109109
let from_entry = self.memories.get(&from).ok_or(StateMachineError::MemoryNotFound(from))?;
110110
let to_entry = self.memories.get(&to).ok_or(StateMachineError::MemoryNotFound(to))?;
111111

112-
if from_entry.namespace != to_entry.namespace {
113-
return Err(StateMachineError::CrossNamespaceEdge {
112+
if from_entry.collection != to_entry.collection {
113+
return Err(StateMachineError::CrossCollectionEdge {
114114
from,
115-
from_ns: from_entry.namespace.clone(),
115+
from_col: from_entry.collection.clone(),
116116
to,
117-
to_ns: to_entry.namespace.clone(),
117+
to_col: to_entry.collection.clone(),
118118
});
119119
}
120120

@@ -129,10 +129,10 @@ impl StateMachine {
129129
Ok(())
130130
}
131131

132-
pub fn namespace_of(&self, id: MemoryId) -> Result<&str> {
132+
pub fn collection_of(&self, id: MemoryId) -> Result<&str> {
133133
self.memories
134134
.get(&id)
135-
.map(|e| e.namespace.as_str())
135+
.map(|e| e.collection.as_str())
136136
.ok_or(StateMachineError::MemoryNotFound(id))
137137
}
138138

@@ -149,10 +149,10 @@ impl StateMachine {
149149
self.memories.get(&id).ok_or(StateMachineError::MemoryNotFound(id))
150150
}
151151

152-
/// Get all memories in a namespace
153-
pub fn get_memories_in_namespace(&self, namespace: &str) -> Vec<&MemoryEntry> {
152+
/// Get all memories in a collection
153+
pub fn get_memories_in_collection(&self, collection: &str) -> Vec<&MemoryEntry> {
154154
let mut entries: Vec<_> =
155-
self.memories.values().filter(|e| e.namespace == namespace).collect();
155+
self.memories.values().filter(|e| e.collection == collection).collect();
156156
entries.sort_by_key(|e| e.id);
157157
entries
158158
}
@@ -210,10 +210,10 @@ impl Default for StateMachine {
210210
mod tests {
211211
use super::*;
212212

213-
fn create_test_entry(id: u64, namespace: &str, timestamp: u64) -> MemoryEntry {
213+
fn create_test_entry(id: u64, collection: &str, timestamp: u64) -> MemoryEntry {
214214
MemoryEntry::new(
215215
MemoryId(id),
216-
namespace.to_string(),
216+
collection.to_string(),
217217
format!("content_{}", id).into_bytes(),
218218
timestamp,
219219
)
@@ -238,13 +238,13 @@ mod tests {
238238
}
239239

240240
#[test]
241-
fn test_delete_memory() {
241+
fn test_delete() {
242242
let mut sm = StateMachine::new();
243243
let entry = create_test_entry(1, "default", 1000);
244244
sm.insert_memory(entry).unwrap();
245245
assert_eq!(sm.len(), 1);
246246

247-
sm.delete_memory(MemoryId(1)).unwrap();
247+
sm.delete(MemoryId(1)).unwrap();
248248
assert_eq!(sm.len(), 0);
249249
assert!(sm.get_memory(MemoryId(1)).is_err());
250250
}
@@ -309,13 +309,13 @@ mod tests {
309309
}
310310

311311
#[test]
312-
fn test_namespace_filtering() {
312+
fn test_collection_filtering() {
313313
let mut sm = StateMachine::new();
314314
sm.insert_memory(create_test_entry(1, "ns1", 1000)).unwrap();
315315
sm.insert_memory(create_test_entry(2, "ns2", 1000)).unwrap();
316316
sm.insert_memory(create_test_entry(3, "ns1", 1000)).unwrap();
317317

318-
let ns1_entries = sm.get_memories_in_namespace("ns1");
318+
let ns1_entries = sm.get_memories_in_collection("ns1");
319319
assert_eq!(ns1_entries.len(), 2);
320320
assert_eq!(ns1_entries[0].id, MemoryId(1));
321321
assert_eq!(ns1_entries[1].id, MemoryId(3));
@@ -365,20 +365,20 @@ mod tests {
365365
sm.add_edge(MemoryId(1), MemoryId(2), "refers".to_string()).unwrap();
366366

367367
// Delete memory 2
368-
sm.delete_memory(MemoryId(2)).unwrap();
368+
sm.delete(MemoryId(2)).unwrap();
369369

370370
// Edge should be cleaned up
371371
let neighbors = sm.get_neighbors(MemoryId(1)).unwrap();
372372
assert!(neighbors.is_empty());
373373
}
374374

375375
#[test]
376-
fn test_cross_namespace_edge_rejected() {
376+
fn test_cross_collection_edge_rejected() {
377377
let mut sm = StateMachine::new();
378378
sm.insert_memory(create_test_entry(1, "ns1", 1000)).unwrap();
379379
sm.insert_memory(create_test_entry(2, "ns2", 1000)).unwrap();
380380

381381
let result = sm.add_edge(MemoryId(1), MemoryId(2), "bad".to_string());
382-
assert!(matches!(result, Err(StateMachineError::CrossNamespaceEdge { .. })));
382+
assert!(matches!(result, Err(StateMachineError::CrossCollectionEdge { .. })));
383383
}
384384
}

0 commit comments

Comments
 (0)