use std::sync::Arc;
use std::time::Duration;
use crate::core::{LifecycleState, OperationGuard};
use crate::database::{Database, DatabaseCore};
use crate::error::Result;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HandleIdentity {
Credentialless,
CatalogUser {
username: String,
user_id: u64,
created_version: u64,
},
ServicePrincipal { principal_id: [u8; 16] },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OpenIdentity {
Credentialless,
ServicePrincipal { principal_id: [u8; 16] },
}
impl OpenIdentity {
pub(crate) fn handle_identity(&self) -> HandleIdentity {
match self {
OpenIdentity::Credentialless => HandleIdentity::Credentialless,
OpenIdentity::ServicePrincipal { principal_id } => HandleIdentity::ServicePrincipal {
principal_id: *principal_id,
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct HandleAccess {
read_only: bool,
}
impl HandleAccess {
pub fn read_write() -> Self {
Self { read_only: false }
}
pub fn read_only() -> Self {
Self { read_only: true }
}
pub fn is_read_only(&self) -> bool {
self.read_only
}
}
pub struct DatabaseHandle {
database: Database,
identity: HandleIdentity,
access: HandleAccess,
}
impl DatabaseHandle {
pub(crate) fn new(database: Database, identity: HandleIdentity, access: HandleAccess) -> Self {
Self {
database,
identity,
access,
}
}
pub fn identity(&self) -> &HandleIdentity {
&self.identity
}
pub fn access(&self) -> HandleAccess {
self.access
}
pub fn core(&self) -> Arc<DatabaseCore> {
self.database.core()
}
pub fn lifecycle_state(&self) -> LifecycleState {
self.database.lifecycle_state()
}
pub fn operation_guard(&self) -> Result<OperationGuard> {
self.database.operation_guard()
}
pub fn shutdown(&self, drain_deadline: Duration) -> Result<()> {
self.database.core().shutdown(drain_deadline)
}
}
impl std::ops::Deref for DatabaseHandle {
type Target = Database;
fn deref(&self) -> &Database {
&self.database
}
}
impl std::fmt::Debug for DatabaseHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DatabaseHandle")
.field("identity", &self.identity)
.field("access", &self.access)
.field("database", &self.database)
.finish()
}
}