mentedb-server 0.39.0

MenteDB server with REST API, WebSocket, auth, and observability
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Named databases: one MenteDB per tenant (a person, a team, an
//! organization), each in its own directory with its own lock, indexes and
//! files, so nothing is shared between them but the process.
//!
//! The legacy default database lives at the data directory root and backs the
//! unprefixed `/v1/...` routes. Named databases live under `<data>/dbs/<name>`
//! and back `/v1/db/{name}/...`. They open on first use, and the least recently
//! used idle handle is flushed and closed once more than `max_open` are open.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::time::Instant;

use mentedb::{CognitiveConfig, MenteDb};
use mentedb_core::error::MenteResult;
use mentedb_embedding::provider::EmbeddingProvider;

use crate::error::ApiError;

/// Name of the database at the data directory root.
pub const DEFAULT_DB: &str = "default";

/// Most characters a database name may have.
const MAX_NAME_LEN: usize = 128;

/// Limits for named databases.
#[derive(Clone, Debug)]
pub struct DatabasesConfig {
    /// Named databases kept open at once; idle ones beyond this are closed.
    pub max_open: usize,
    /// Create a named database on first use instead of returning 404.
    pub auto_create: bool,
}

impl Default for DatabasesConfig {
    fn default() -> Self {
        Self {
            max_open: 256,
            auto_create: true,
        }
    }
}

/// One embedder shared by every open database.
struct SharedEmbedder(Arc<dyn EmbeddingProvider>);

impl EmbeddingProvider for SharedEmbedder {
    fn embed(&self, text: &str) -> MenteResult<Vec<f32>> {
        self.0.embed(text)
    }
    fn embed_batch(&self, texts: &[&str]) -> MenteResult<Vec<Vec<f32>>> {
        self.0.embed_batch(texts)
    }
    fn dimensions(&self) -> usize {
        self.0.dimensions()
    }
    fn model_name(&self) -> &str {
        self.0.model_name()
    }
}

struct OpenDb {
    db: Arc<MenteDb>,
    last_used: Instant,
}

/// Open handles plus the names whose old handle is still being closed. A
/// name in `closing` must not be reopened until its close has released the
/// directory lock, or the open fails with "locked by another process".
#[derive(Default)]
struct Registry {
    open: HashMap<String, OpenDb>,
    closing: HashSet<String>,
}

/// Registry of the default database and the named ones.
pub struct Databases {
    root: PathBuf,
    default: Arc<MenteDb>,
    embedder: Option<Arc<dyn EmbeddingProvider>>,
    cognitive: CognitiveConfig,
    config: DatabasesConfig,
    registry: Mutex<Registry>,
    /// Signalled whenever a name leaves `Registry::closing`.
    closed: Condvar,
}

impl Databases {
    /// Open the default database at `root` and prepare the registry.
    pub fn open(
        root: &Path,
        embedder: Option<Arc<dyn EmbeddingProvider>>,
        cognitive: CognitiveConfig,
        config: DatabasesConfig,
    ) -> MenteResult<Self> {
        std::fs::create_dir_all(root)?;
        let default = Arc::new(open_at(root, embedder.clone(), cognitive.clone())?);
        Ok(Self {
            root: root.to_path_buf(),
            default,
            embedder,
            cognitive,
            config,
            registry: Mutex::new(Registry::default()),
            closed: Condvar::new(),
        })
    }

    /// The database at the data directory root.
    pub fn default_db(&self) -> Arc<MenteDb> {
        self.default.clone()
    }

    /// The data directory root (the default database's directory).
    pub(crate) fn root(&self) -> &Path {
        &self.root
    }

    /// The shared embedder, when one is configured.
    pub fn embedder(&self) -> Option<Arc<dyn EmbeddingProvider>> {
        self.embedder.clone()
    }

    fn lock(&self) -> MutexGuard<'_, Registry> {
        self.registry.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// Wait until no close of `name` is in flight. Returns the reacquired
    /// guard.
    fn wait_until_closed<'a>(
        &'a self,
        mut reg: MutexGuard<'a, Registry>,
        name: &str,
    ) -> MutexGuard<'a, Registry> {
        while reg.closing.contains(name) {
            reg = self.closed.wait(reg).unwrap_or_else(|e| e.into_inner());
        }
        reg
    }

    /// Close handles taken out of the registry, then release their names so
    /// waiting opens can proceed. Each handle is dropped (releasing its
    /// directory lock) before its name is released.
    fn finish_closing(&self, handles: Vec<(String, Arc<MenteDb>)>) {
        let mut names = Vec::with_capacity(handles.len());
        for (name, handle) in handles {
            if let Err(e) = handle.close() {
                tracing::warn!(db = %name, error = %e, "closing idle database failed");
            }
            drop(handle);
            names.push(name);
        }
        let mut reg = self.lock();
        for name in names {
            reg.closing.remove(&name);
        }
        drop(reg);
        self.closed.notify_all();
    }

    fn dir(&self, name: &str) -> PathBuf {
        self.root.join("dbs").join(name)
    }

    /// Whether a named database exists on disk.
    pub fn exists(&self, name: &str) -> bool {
        name == DEFAULT_DB || self.dir(name).join(".mentedb-db").exists()
    }

    /// Get a database by name, opening it if needed. Blocking: call from
    /// `spawn_blocking`. `create` allows making a new database.
    pub fn get(&self, name: &str, create: bool) -> Result<Arc<MenteDb>, ApiError> {
        if name == DEFAULT_DB {
            return Ok(self.default.clone());
        }
        validate_name(name)?;
        let reg = self.lock();
        let mut reg = self.wait_until_closed(reg, name);
        if let Some(entry) = reg.open.get_mut(name) {
            entry.last_used = Instant::now();
            return Ok(entry.db.clone());
        }
        let dir = self.dir(name);
        let marker = dir.join(".mentedb-db");
        if !marker.exists() {
            if !(create || self.config.auto_create) {
                return Err(ApiError::NotFound(format!("database '{name}' not found")));
            }
            std::fs::create_dir_all(&dir)
                .map_err(|e| ApiError::Internal(format!("create database '{name}': {e}")))?;
            std::fs::write(&marker, name.as_bytes())
                .map_err(|e| ApiError::Internal(format!("create database '{name}': {e}")))?;
        }
        // Opening holds the registry lock so one name is never opened twice;
        // the engine's own file lock would refuse the second open anyway.
        let db = Arc::new(
            open_at(&dir, self.embedder.clone(), self.cognitive.clone())
                .map_err(|e| ApiError::Internal(format!("open database '{name}': {e}")))?,
        );
        reg.open.insert(
            name.to_string(),
            OpenDb {
                db: db.clone(),
                last_used: Instant::now(),
            },
        );
        let evicted = take_idle_overflow(&mut reg.open, self.config.max_open);
        for (evicted_name, _) in &evicted {
            reg.closing.insert(evicted_name.clone());
        }
        drop(reg);
        if !evicted.is_empty() {
            self.finish_closing(evicted);
        }
        Ok(db)
    }

    /// Named databases on disk, sorted.
    pub fn list(&self) -> Vec<String> {
        let mut names: Vec<String> = std::fs::read_dir(self.root.join("dbs"))
            .into_iter()
            .flatten()
            .flatten()
            .filter(|e| e.path().join(".mentedb-db").exists())
            .filter_map(|e| e.file_name().into_string().ok())
            .collect();
        names.sort();
        names
    }

    /// Close and permanently delete a named database. Blocking.
    pub fn delete(&self, name: &str) -> Result<(), ApiError> {
        if name == DEFAULT_DB {
            return Err(ApiError::BadRequest(
                "the default database cannot be deleted".into(),
            ));
        }
        validate_name(name)?;
        let reg = self.lock();
        let mut reg = self.wait_until_closed(reg, name);
        if let Some(entry) = reg.open.get(name)
            && Arc::strong_count(&entry.db) > 1
        {
            return Err(ApiError::Conflict(format!(
                "database '{name}' is in use; retry"
            )));
        }
        // Closed and removed while holding the registry, so no open of this
        // name can start until the directory is gone.
        if let Some(entry) = reg.open.remove(name) {
            let _ = entry.db.close();
        }
        let dir = self.dir(name);
        if !dir.join(".mentedb-db").exists() {
            return Err(ApiError::NotFound(format!("database '{name}' not found")));
        }
        std::fs::remove_dir_all(&dir)
            .map_err(|e| ApiError::Internal(format!("delete database '{name}': {e}")))
    }

    /// Directory a database's files live in.
    pub fn path_of(&self, name: &str) -> Result<PathBuf, ApiError> {
        if name == DEFAULT_DB {
            return Ok(self.root.clone());
        }
        validate_name(name)?;
        Ok(self.dir(name))
    }

    /// Every currently open database, the default first.
    pub fn open_handles(&self) -> Vec<(String, Arc<MenteDb>)> {
        let mut all = vec![(DEFAULT_DB.to_string(), self.default.clone())];
        let reg = self.lock();
        all.extend(reg.open.iter().map(|(n, e)| (n.clone(), e.db.clone())));
        all
    }

    /// Flush and close every open database. Used at shutdown.
    pub fn close_all(&self) {
        let handles: Vec<(String, Arc<MenteDb>)> = {
            let mut reg = self.lock();
            let drained: Vec<_> = reg.open.drain().map(|(n, e)| (n, e.db)).collect();
            for (n, _) in &drained {
                reg.closing.insert(n.clone());
            }
            drained
        };
        self.finish_closing(handles);
        if let Err(e) = self.default.close() {
            tracing::warn!(error = %e, "closing default database failed");
        }
    }
}

fn open_at(
    dir: &Path,
    embedder: Option<Arc<dyn EmbeddingProvider>>,
    cognitive: CognitiveConfig,
) -> MenteResult<MenteDb> {
    match embedder {
        Some(e) => {
            MenteDb::open_with_embedder_and_config(dir, Box::new(SharedEmbedder(e)), cognitive)
        }
        None => MenteDb::open_with_config(dir, cognitive),
    }
}

/// Remove the least recently used handles nobody else holds until at most
/// `max_open` remain. Returned handles are closed by the caller off the lock.
fn take_idle_overflow(
    open: &mut HashMap<String, OpenDb>,
    max_open: usize,
) -> Vec<(String, Arc<MenteDb>)> {
    let mut evicted = Vec::new();
    while open.len() > max_open {
        let victim = open
            .iter()
            .filter(|(_, e)| Arc::strong_count(&e.db) == 1)
            .min_by_key(|(_, e)| e.last_used)
            .map(|(n, _)| n.clone());
        match victim {
            Some(name) => {
                if let Some(entry) = open.remove(&name) {
                    evicted.push((name, entry.db));
                }
            }
            // Every handle is in use; stay over the limit until one frees up.
            None => break,
        }
    }
    evicted
}

/// Database names are path segments: letters, digits, `-`, `_` and `.`, not
/// starting with `.`, at most 128 characters.
pub fn validate_name(name: &str) -> Result<(), ApiError> {
    let ok = !name.is_empty()
        && name.len() <= MAX_NAME_LEN
        && !name.starts_with('.')
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'));
    if ok {
        Ok(())
    } else {
        Err(ApiError::BadRequest(format!(
            "invalid database name '{name}': use letters, digits, '-', '_' or '.', at most \
             {MAX_NAME_LEN} characters, not starting with '.'"
        )))
    }
}

/// Paths under a database that only read, so read-only tokens may POST them.
const READ_ONLY_POSTS: &[&str] = &["/recall", "/search", "/search/hybrid", "/context", "/query"];

/// The database a request targets: `{db}` in `/v1/db/{db}/...`, otherwise the
/// default. Checks the caller's token scope and opens the database.
pub struct Db {
    pub name: String,
    pub handle: Arc<MenteDb>,
}

impl std::ops::Deref for Db {
    type Target = MenteDb;
    fn deref(&self) -> &MenteDb {
        &self.handle
    }
}

impl axum::extract::FromRequestParts<Arc<crate::state::AppState>> for Db {
    type Rejection = ApiError;

    async fn from_request_parts(
        parts: &mut axum::http::request::Parts,
        state: &Arc<crate::state::AppState>,
    ) -> Result<Self, ApiError> {
        use axum::extract::RawPathParams;
        let params = RawPathParams::from_request_parts(parts, state)
            .await
            .map_err(|e| ApiError::BadRequest(e.to_string()))?;
        let name = params
            .iter()
            .find(|(k, _)| *k == "db")
            .map(|(_, v)| v.to_string())
            .unwrap_or_else(|| DEFAULT_DB.to_string());
        let path = parts.uri.path();
        let write = match parts.method {
            axum::http::Method::GET | axum::http::Method::HEAD => false,
            axum::http::Method::POST => !READ_ONLY_POSTS.iter().any(|p| path.ends_with(p)),
            _ => true,
        };
        if let Some(agent) = parts.extensions.get::<crate::auth::AuthenticatedAgent>() {
            agent.authorize_db(&name, write)?;
        }
        let dbs = state.databases.clone();
        let lookup = name.clone();
        let handle = tokio::task::spawn_blocking(move || dbs.get(&lookup, false))
            .await
            .map_err(|e| ApiError::Internal(format!("open database task failed: {e}")))??;
        Ok(Self { name, handle })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn registry(dir: &Path, max_open: usize) -> Databases {
        Databases::open(
            dir,
            None,
            CognitiveConfig::default(),
            DatabasesConfig {
                max_open,
                auto_create: true,
            },
        )
        .unwrap()
    }

    #[test]
    fn names_are_safe_path_segments() {
        for ok in ["alice", "team-7", "org_1", "u.123"] {
            assert!(validate_name(ok).is_ok(), "{ok}");
        }
        for bad in ["", "..", ".hidden", "a/b", "a\\b", "x y", &"a".repeat(129)] {
            assert!(validate_name(bad).is_err(), "{bad}");
        }
    }

    #[test]
    fn named_databases_are_isolated_directories() {
        let tmp = tempfile::tempdir().unwrap();
        let dbs = registry(tmp.path(), 8);
        let a = dbs.get("alice", false).unwrap();
        let b = dbs.get("bob", false).unwrap();
        assert!(!Arc::ptr_eq(&a, &b));
        assert!(tmp.path().join("dbs/alice/.mentedb-db").exists());
        assert!(tmp.path().join("dbs/bob/.mentedb-db").exists());
        assert_eq!(dbs.list(), vec!["alice".to_string(), "bob".to_string()]);
        assert!(
            Arc::ptr_eq(&a, &dbs.get("alice", false).unwrap()),
            "cached handle"
        );
    }

    #[test]
    fn idle_databases_beyond_the_limit_are_closed() {
        let tmp = tempfile::tempdir().unwrap();
        let dbs = registry(tmp.path(), 2);
        drop(dbs.get("a", false).unwrap());
        drop(dbs.get("b", false).unwrap());
        drop(dbs.get("c", false).unwrap());
        let open: Vec<String> = dbs.open_handles().into_iter().map(|(n, _)| n).collect();
        assert_eq!(open.len(), 3, "default plus two named: {open:?}");
        assert!(
            !open.contains(&"a".to_string()),
            "least recently used closed"
        );
        // A closed database reopens with its data intact.
        assert!(dbs.get("a", false).is_ok());
    }

    #[test]
    fn missing_database_is_404_without_auto_create() {
        let tmp = tempfile::tempdir().unwrap();
        let dbs = Databases::open(
            tmp.path(),
            None,
            CognitiveConfig::default(),
            DatabasesConfig {
                max_open: 4,
                auto_create: false,
            },
        )
        .unwrap();
        assert!(matches!(
            dbs.get("nobody", false),
            Err(ApiError::NotFound(_))
        ));
        assert!(dbs.get("nobody", true).is_ok(), "explicit create works");
    }

    /// More tenants than `max_open`, hit from many threads at once, so idle
    /// handles are closed while other threads reopen the same names. Every
    /// lookup must succeed; a reopen must never race the close of the old
    /// handle and find the directory still locked.
    #[test]
    fn reopening_a_database_while_it_is_being_closed_waits_for_the_close() {
        let tmp = tempfile::tempdir().unwrap();
        let dbs = Arc::new(registry(tmp.path(), 2));
        let names: Vec<String> = (0..8).map(|i| format!("t{i}")).collect();
        let errors = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let threads: Vec<_> = (0..16)
            .map(|t| {
                let dbs = dbs.clone();
                let names = names.clone();
                let errors = errors.clone();
                std::thread::spawn(move || {
                    for i in 0..40 {
                        let name = &names[(t * 7 + i) % names.len()];
                        match dbs.get(name, false) {
                            Ok(db) => {
                                let _ = db.memory_count();
                            }
                            Err(e) => {
                                eprintln!("{name}: {e}");
                                errors.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                            }
                        }
                    }
                })
            })
            .collect();
        for t in threads {
            t.join().unwrap();
        }
        assert_eq!(errors.load(std::sync::atomic::Ordering::SeqCst), 0);
    }

    #[test]
    fn delete_removes_the_directory() {
        let tmp = tempfile::tempdir().unwrap();
        let dbs = registry(tmp.path(), 4);
        drop(dbs.get("gone", false).unwrap());
        dbs.delete("gone").unwrap();
        assert!(!tmp.path().join("dbs/gone").exists());
        assert!(dbs.delete(DEFAULT_DB).is_err());
    }
}