oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Multi-tenant manager - main interface for multi-tenancy

use crate::multi_tenancy::{
    access_control::AccessControl,
    billing::{BillingEngine, BillingMetrics, BillingPeriod, PricingModel},
    isolation::{IsolationLevel, IsolationStrategy, NamespaceManager},
    quota::{QuotaEnforcer, QuotaLimits, RateLimiter},
    tenant::{Tenant, TenantId, TenantMetadata, TenantStatus},
    types::{
        MultiTenancyError, MultiTenancyResult, TenantContext, TenantOperation, TenantStatistics,
    },
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Configuration for tenant
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TenantConfig {
    /// Tenant metadata
    pub metadata: TenantMetadata,

    /// Isolation strategy
    pub isolation: IsolationStrategy,

    /// Quota limits
    pub quotas: QuotaLimits,

    /// Pricing model
    pub pricing: PricingModel,

    /// Rate limit (requests per second)
    pub rate_limit: Option<f64>,
}

impl TenantConfig {
    /// Create config for free tier
    pub fn free_tier(tenant_id: impl Into<String>, name: impl Into<String>) -> Self {
        let tenant_id = tenant_id.into();
        Self {
            metadata: TenantMetadata::new(name, "free"),
            isolation: IsolationStrategy::free_tier(),
            quotas: QuotaLimits::free_tier(&tenant_id),
            pricing: PricingModel::PerRequest {
                cost_per_request: 0.001,
            },
            rate_limit: Some(10.0),
        }
    }

    /// Create config for pro tier
    pub fn pro_tier(tenant_id: impl Into<String>, name: impl Into<String>) -> Self {
        let tenant_id = tenant_id.into();
        Self {
            metadata: TenantMetadata::new(name, "pro"),
            isolation: IsolationStrategy::pro_tier(),
            quotas: QuotaLimits::pro_tier(&tenant_id),
            pricing: PricingModel::PerComputeUnit {
                cost_per_unit: 0.01,
            },
            rate_limit: Some(100.0),
        }
    }

    /// Create config for enterprise tier
    pub fn enterprise_tier(tenant_id: impl Into<String>, name: impl Into<String>) -> Self {
        let tenant_id = tenant_id.into();
        Self {
            metadata: TenantMetadata::new(name, "enterprise"),
            isolation: IsolationStrategy::enterprise_tier(),
            quotas: QuotaLimits::enterprise_tier(&tenant_id),
            pricing: PricingModel::Subscription {
                monthly_fee: 1000.0,
                included_requests: 1_000_000,
                overage_cost: 0.005,
            },
            rate_limit: None, // Unlimited
        }
    }
}

/// Configuration for multi-tenant manager
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TenantManagerConfig {
    /// Default isolation level
    pub default_isolation: IsolationLevel,

    /// Billing period
    pub billing_period: BillingPeriod,

    /// Enable strict quota enforcement
    pub strict_quotas: bool,

    /// Enable access control
    pub enable_access_control: bool,

    /// Enable billing/metering
    pub enable_billing: bool,

    /// Auto-suspend tenants on quota exceeded
    pub auto_suspend_on_quota_exceeded: bool,
}

impl TenantManagerConfig {
    /// Create default configuration
    pub fn default_config() -> Self {
        Self {
            default_isolation: IsolationLevel::Namespace,
            billing_period: BillingPeriod::Monthly,
            strict_quotas: true,
            enable_access_control: true,
            enable_billing: true,
            auto_suspend_on_quota_exceeded: false,
        }
    }

    /// Create production configuration
    pub fn production() -> Self {
        Self {
            default_isolation: IsolationLevel::SeparateIndex,
            billing_period: BillingPeriod::Monthly,
            strict_quotas: true,
            enable_access_control: true,
            enable_billing: true,
            auto_suspend_on_quota_exceeded: true,
        }
    }
}

/// Multi-tenant manager
pub struct MultiTenantManager {
    /// Configuration
    config: TenantManagerConfig,

    /// Tenant registry
    tenants: Arc<RwLock<HashMap<TenantId, Tenant>>>,

    /// Tenant statistics
    statistics: Arc<RwLock<HashMap<TenantId, TenantStatistics>>>,

    /// Namespace manager
    namespace_manager: Arc<NamespaceManager>,

    /// Quota enforcer
    quota_enforcer: Arc<QuotaEnforcer>,

    /// Rate limiter
    rate_limiter: Arc<RateLimiter>,

    /// Access control
    access_control: Arc<AccessControl>,

    /// Billing engine
    billing_engine: Arc<BillingEngine>,
}

impl MultiTenantManager {
    /// Create new multi-tenant manager
    pub fn new(config: TenantManagerConfig) -> Self {
        let isolation_strategy = IsolationStrategy::new(config.default_isolation);
        let namespace_manager = Arc::new(NamespaceManager::new(isolation_strategy));
        let quota_enforcer = Arc::new(QuotaEnforcer::new());
        let rate_limiter = Arc::new(RateLimiter::new());
        let access_control = Arc::new(AccessControl::new());
        let billing_engine = Arc::new(BillingEngine::new(config.billing_period));

        Self {
            config,
            tenants: Arc::new(RwLock::new(HashMap::new())),
            statistics: Arc::new(RwLock::new(HashMap::new())),
            namespace_manager,
            quota_enforcer,
            rate_limiter,
            access_control,
            billing_engine,
        }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self::new(TenantManagerConfig::default_config())
    }

    /// Create tenant with configuration
    pub fn create_tenant(
        &self,
        tenant_id: impl Into<String>,
        config: TenantConfig,
    ) -> MultiTenancyResult<()> {
        let tenant_id = tenant_id.into();
        let tenant = Tenant::new(tenant_id.clone(), config.metadata);

        // Register namespace
        self.namespace_manager.register_tenant(&tenant_id)?;

        // Set quota limits
        self.quota_enforcer.set_limits(config.quotas)?;

        // Set rate limit
        if let Some(rate) = config.rate_limit {
            self.rate_limiter.set_rate(&tenant_id, rate)?;
        }

        // Set pricing model
        self.billing_engine
            .set_pricing(&tenant_id, config.pricing)?;

        // Create default access policy
        if self.config.enable_access_control {
            self.access_control.create_default_policy(&tenant_id)?;
        }

        // Store tenant
        self.tenants
            .write()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .insert(tenant_id.clone(), tenant);

        // Initialize statistics
        self.statistics
            .write()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .insert(tenant_id.clone(), TenantStatistics::new(tenant_id));

        Ok(())
    }

    /// Get tenant by ID
    pub fn get_tenant(&self, tenant_id: &str) -> MultiTenancyResult<Tenant> {
        self.tenants
            .read()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .get(tenant_id)
            .cloned()
            .ok_or_else(|| MultiTenancyError::TenantNotFound {
                tenant_id: tenant_id.to_string(),
            })
    }

    /// Update tenant status
    pub fn update_tenant_status(
        &self,
        tenant_id: &str,
        status: TenantStatus,
    ) -> MultiTenancyResult<()> {
        let mut tenants = self
            .tenants
            .write()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?;

        let tenant =
            tenants
                .get_mut(tenant_id)
                .ok_or_else(|| MultiTenancyError::TenantNotFound {
                    tenant_id: tenant_id.to_string(),
                })?;

        tenant.set_status(status);
        Ok(())
    }

    /// Delete tenant
    pub fn delete_tenant(&self, tenant_id: &str) -> MultiTenancyResult<()> {
        // Remove from registry
        self.tenants
            .write()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .remove(tenant_id);

        // Remove namespace
        self.namespace_manager.unregister_tenant(tenant_id)?;

        // Remove statistics
        self.statistics
            .write()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .remove(tenant_id);

        Ok(())
    }

    /// Check if tenant can execute operation
    pub fn check_operation(
        &self,
        context: &TenantContext,
        _operation: TenantOperation,
        resource_delta: Option<(crate::multi_tenancy::quota::ResourceType, u64)>,
    ) -> MultiTenancyResult<()> {
        let tenant_id = &context.tenant_id;

        // Check tenant status
        let tenant = self.get_tenant(tenant_id)?;
        if !tenant.is_operational() {
            if tenant.status == TenantStatus::Suspended {
                return Err(MultiTenancyError::TenantSuspended {
                    tenant_id: tenant_id.clone(),
                });
            }
            return Err(MultiTenancyError::InternalError {
                message: format!("Tenant {} is not operational", tenant_id),
            });
        }

        // Check rate limit
        if !self.rate_limiter.allow_request(tenant_id)? {
            return Err(MultiTenancyError::RateLimitExceeded {
                tenant_id: tenant_id.clone(),
            });
        }

        // Check resource quota
        if let Some((resource_type, amount)) = resource_delta {
            if self.config.strict_quotas
                && !self
                    .quota_enforcer
                    .check_quota(tenant_id, resource_type, amount)?
            {
                if self.config.auto_suspend_on_quota_exceeded {
                    self.update_tenant_status(tenant_id, TenantStatus::Suspended)?;
                }
                return Err(MultiTenancyError::QuotaExceeded {
                    tenant_id: tenant_id.clone(),
                    resource: resource_type.name(),
                });
            }
        }

        Ok(())
    }

    /// Execute operation with full checks
    pub fn execute_operation<F, R>(
        &self,
        context: &TenantContext,
        operation: TenantOperation,
        func: F,
    ) -> MultiTenancyResult<R>
    where
        F: FnOnce() -> MultiTenancyResult<R>,
    {
        // Pre-execution checks
        self.check_operation(context, operation, None)?;

        // Execute operation
        let start = chrono::Utc::now();
        let result = func()?;
        let latency_ms = (chrono::Utc::now() - start).num_milliseconds() as f64;

        // Post-execution: record statistics and billing
        self.record_operation_completed(context, operation, latency_ms)?;

        Ok(result)
    }

    /// Record operation completion
    fn record_operation_completed(
        &self,
        context: &TenantContext,
        operation: TenantOperation,
        latency_ms: f64,
    ) -> MultiTenancyResult<()> {
        let tenant_id = &context.tenant_id;

        // Update statistics
        let mut stats = self
            .statistics
            .write()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?;

        stats
            .entry(tenant_id.clone())
            .or_insert_with(|| TenantStatistics::new(tenant_id))
            .record_operation(operation);

        if operation == TenantOperation::VectorSearch {
            stats
                .get_mut(tenant_id)
                .expect("tenant stats entry was just inserted via or_insert_with")
                .record_query(latency_ms);
        }

        // Record billing
        if self.config.enable_billing {
            self.billing_engine.record_usage(tenant_id, operation, 1)?;
        }

        Ok(())
    }

    /// Get tenant statistics
    pub fn get_statistics(&self, tenant_id: &str) -> MultiTenancyResult<TenantStatistics> {
        self.statistics
            .read()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .get(tenant_id)
            .cloned()
            .ok_or_else(|| MultiTenancyError::TenantNotFound {
                tenant_id: tenant_id.to_string(),
            })
    }

    /// Get billing metrics
    pub fn get_billing_metrics(&self, tenant_id: &str) -> MultiTenancyResult<BillingMetrics> {
        self.billing_engine.get_metrics(tenant_id)
    }

    /// List all tenants
    pub fn list_tenants(&self) -> MultiTenancyResult<Vec<Tenant>> {
        Ok(self
            .tenants
            .read()
            .map_err(|e| MultiTenancyError::InternalError {
                message: format!("Lock error: {}", e),
            })?
            .values()
            .cloned()
            .collect())
    }

    /// Get namespace manager
    pub fn namespace_manager(&self) -> &NamespaceManager {
        &self.namespace_manager
    }

    /// Get quota enforcer
    pub fn quota_enforcer(&self) -> &QuotaEnforcer {
        &self.quota_enforcer
    }

    /// Get access control
    pub fn access_control(&self) -> &AccessControl {
        &self.access_control
    }

    /// Get billing engine
    pub fn billing_engine(&self) -> &BillingEngine {
        &self.billing_engine
    }
}

#[cfg(test)]
mod tests {
    type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
    use super::*;

    #[test]
    fn test_tenant_config_tiers() {
        let free = TenantConfig::free_tier("t1", "Free Tenant");
        assert_eq!(free.metadata.tier, "free");
        assert!(free.rate_limit.is_some());

        let pro = TenantConfig::pro_tier("t2", "Pro Tenant");
        assert_eq!(pro.metadata.tier, "pro");

        let enterprise = TenantConfig::enterprise_tier("t3", "Enterprise");
        assert_eq!(enterprise.metadata.tier, "enterprise");
        assert!(enterprise.rate_limit.is_none()); // Unlimited
    }

    #[test]
    fn test_manager_creation() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();
        assert_eq!(manager.list_tenants().expect("test value").len(), 0);
        Ok(())
    }

    #[test]
    fn test_create_and_get_tenant() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();
        let config = TenantConfig::free_tier("tenant1", "Test Tenant");

        manager.create_tenant("tenant1", config)?;

        let tenant = manager.get_tenant("tenant1")?;
        assert_eq!(tenant.id, "tenant1");
        assert_eq!(tenant.metadata.name, "Test Tenant");
        assert_eq!(tenant.status, TenantStatus::Active);
        Ok(())
    }

    #[test]
    fn test_tenant_operations() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();
        let config = TenantConfig::free_tier("tenant1", "Test");
        manager.create_tenant("tenant1", config)?;

        let context = TenantContext::new("tenant1");

        // Should allow operation
        manager.check_operation(&context, TenantOperation::VectorSearch, None)?;

        // Execute operation with closure
        let result: MultiTenancyResult<i32> =
            manager.execute_operation(&context, TenantOperation::VectorSearch, || Ok(42));
        let __val = result?;
        assert_eq!(__val, 42);

        // Check statistics updated
        let stats = manager.get_statistics("tenant1")?;
        assert_eq!(stats.total_queries, 1);
        Ok(())
    }

    #[test]
    fn test_tenant_status_changes() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();
        let config = TenantConfig::free_tier("tenant1", "Test");
        manager.create_tenant("tenant1", config)?;

        // Suspend tenant
        manager.update_tenant_status("tenant1", TenantStatus::Suspended)?;

        let tenant = manager.get_tenant("tenant1")?;
        assert_eq!(tenant.status, TenantStatus::Suspended);

        // Operations should fail when suspended
        let context = TenantContext::new("tenant1");
        assert!(manager
            .check_operation(&context, TenantOperation::VectorSearch, None)
            .is_err());
        Ok(())
    }

    #[test]
    fn test_delete_tenant() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();
        let config = TenantConfig::free_tier("tenant1", "Test");
        manager.create_tenant("tenant1", config)?;

        assert!(manager.get_tenant("tenant1").is_ok());

        manager.delete_tenant("tenant1")?;

        assert!(manager.get_tenant("tenant1").is_err());
        Ok(())
    }

    #[test]
    fn test_list_tenants() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();

        manager.create_tenant("tenant1", TenantConfig::free_tier("tenant1", "T1"))?;
        manager.create_tenant("tenant2", TenantConfig::pro_tier("tenant2", "T2"))?;
        manager.create_tenant("tenant3", TenantConfig::enterprise_tier("tenant3", "T3"))?;

        let tenants = manager.list_tenants()?;
        assert_eq!(tenants.len(), 3);
        Ok(())
    }

    #[test]
    fn test_billing_integration() -> Result<()> {
        let manager = MultiTenantManager::with_defaults();
        let config = TenantConfig::free_tier("tenant1", "Test");
        manager.create_tenant("tenant1", config)?;

        let context = TenantContext::new("tenant1");

        // Execute some operations
        for _ in 0..10 {
            let _ = manager.execute_operation(&context, TenantOperation::VectorSearch, || Ok(()));
        }

        // Check billing metrics
        let metrics = manager.get_billing_metrics("tenant1")?;
        assert_eq!(metrics.total_requests, 10);
        assert!(metrics.total_cost > 0.0);
        Ok(())
    }

    #[test]
    fn test_manager_config() {
        let config = TenantManagerConfig::default_config();
        assert!(config.strict_quotas);
        assert!(config.enable_access_control);
        assert!(config.enable_billing);

        let prod_config = TenantManagerConfig::production();
        assert_eq!(prod_config.default_isolation, IsolationLevel::SeparateIndex);
        assert!(prod_config.auto_suspend_on_quota_exceeded);
    }
}