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
//! `DatabaseObserver` — extension hook for velesdb-premium.
//!
//! The core library has zero knowledge of Premium internals.
//! Premium implements this trait and injects it via
//! [`Database::open_with_observer`](crate::Database::open_with_observer).
//!
//! # Contract
//!
//! - All methods have default no-op implementations.
//! - Implementations MUST be `Send + Sync`.
//! - Implementations MUST NOT panic.
//! - Overhead when `observer` is `None` is a single pointer check.
use crate::collection::CollectionType;
/// Lifecycle hooks for database events.
///
/// Implement this trait in `velesdb-premium` to attach RBAC, audit logging,
/// multi-tenant routing, or replication logic without modifying the core.
///
/// # Example (Premium side)
///
/// ```rust,ignore
/// use velesdb_core::{DatabaseObserver, CollectionType};
///
/// struct PremiumObserver { /* audit_log, rbac, tenant_router */ }
///
/// impl DatabaseObserver for PremiumObserver {
/// fn on_collection_created(&self, name: &str, kind: &CollectionType) {
/// // self.audit_log.record(...)
/// }
/// }
/// ```
pub trait DatabaseObserver: Send + Sync {
/// Called after a collection is successfully created.
fn on_collection_created(&self, _name: &str, _kind: &CollectionType) {}
/// Called after a collection is successfully deleted.
fn on_collection_deleted(&self, _name: &str) {}
/// Called after points are upserted into a collection.
fn on_upsert(&self, _collection: &str, _point_count: usize) {}
/// Called after a query is executed, with the duration in microseconds.
fn on_query(&self, _collection: &str, _duration_us: u64) {}
/// Called before a DDL statement is executed.
///
/// Premium extensions can implement this to enforce RBAC policies
/// (e.g., only admin users can CREATE/DROP collections).
///
/// Returns `Ok(())` to allow the DDL operation, or `Err(Error)` to reject it.
/// Default implementation allows all DDL operations.
///
/// # Errors
///
/// Implementations should return an error to reject the DDL operation.
fn on_ddl_request(&self, operation: &str, collection_name: &str) -> crate::Result<()> {
let _ = (operation, collection_name);
Ok(())
}
/// Called before a mutating DML statement is executed.
///
/// Premium extensions can implement this to enforce RBAC policies
/// (e.g., restrict INSERT EDGE, DELETE, or DELETE EDGE to authorized users).
///
/// Returns `Ok(())` to allow the DML mutation, or `Err(Error)` to reject it.
/// Default implementation allows all DML mutations.
///
/// # Errors
///
/// Implementations should return an error to reject the DML mutation.
fn on_dml_mutation_request(&self, operation: &str, collection_name: &str) -> crate::Result<()> {
let _ = (operation, collection_name);
Ok(())
}
}