Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions crates/core/src/database_instance_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::messages::control_db::Database;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

pub type Result<T> = anyhow::Result<T>;

#[derive(Clone)]
pub struct DatabaseInstanceContext {
pub database_instance_id: u64,
Expand All @@ -24,7 +26,12 @@ pub struct DatabaseInstanceContext {
}

impl DatabaseInstanceContext {
pub fn from_database(config: Config, database: &Database, instance_id: u64, root_db_path: PathBuf) -> Arc<Self> {
pub fn from_database(
config: Config,
database: &Database,
instance_id: u64,
root_db_path: PathBuf,
) -> Result<Arc<Self>> {
let mut db_path = root_db_path;
db_path.extend([&*database.address.to_hex(), &*instance_id.to_string()]);
db_path.push("database");
Expand Down Expand Up @@ -60,43 +67,42 @@ impl DatabaseInstanceContext {
db_path: PathBuf,
log_path: &Path,
publisher_address: Option<Address>,
) -> Arc<Self> {
) -> Result<Arc<Self>> {
let message_log = match config.storage {
Storage::Memory => None,
Storage::Disk => {
let mlog_path = db_path.join("mlog");
Some(Arc::new(Mutex::new(MessageLog::open(mlog_path).unwrap())))
Some(Arc::new(Mutex::new(MessageLog::open(mlog_path)?)))
}
};

let odb = match config.storage {
Storage::Memory => Box::<MemoryObjectDB>::default(),
Storage::Disk => {
let odb_path = db_path.join("odb");
DatabaseInstanceContext::make_default_ostorage(odb_path)
Self::make_default_ostorage(odb_path)?
}
};
let odb = Arc::new(Mutex::new(odb));
let relational_db = RelationalDB::open(db_path, message_log, odb, address, config.fsync != FsyncPolicy::Never)?;

Arc::new(Self {
Ok(Arc::new(Self {
database_instance_id,
database_id,
identity,
address,
logger: Arc::new(DatabaseLogger::open(log_path)),
relational_db: Arc::new(
RelationalDB::open(db_path, message_log, odb, address, config.fsync != FsyncPolicy::Never).unwrap(),
),
relational_db: Arc::new(relational_db),
publisher_address,
})
}))
}

pub(crate) fn make_default_ostorage(path: impl AsRef<Path>) -> Box<dyn ObjectDB + Send> {
Box::new(SledObjectDB::open(path).unwrap())
pub(crate) fn make_default_ostorage(path: impl AsRef<Path>) -> Result<Box<dyn ObjectDB + Send>> {
Ok(SledObjectDB::open(path).map(Box::new)?)
}

/// The number of bytes on disk occupied by the [MessageLog].
pub fn message_log_size_on_disk(&self) -> Result<u64, DBError> {
pub fn message_log_size_on_disk(&self) -> std::result::Result<u64, DBError> {
let size = self
.relational_db
.commit_log()
Expand All @@ -107,7 +113,7 @@ impl DatabaseInstanceContext {
}

/// The number of bytes on disk occupied by the [ObjectDB].
pub fn object_db_size_on_disk(&self) -> Result<u64, DBError> {
pub fn object_db_size_on_disk(&self) -> std::result::Result<u64, DBError> {
let size = self
.relational_db
.commit_log()
Expand All @@ -118,7 +124,7 @@ impl DatabaseInstanceContext {
}

/// The size of the log file.
pub fn log_file_size(&self) -> Result<u64, DBError> {
pub fn log_file_size(&self) -> std::result::Result<u64, DBError> {
self.logger.size()
}
}
2 changes: 1 addition & 1 deletion crates/standalone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl StandaloneEnv {
let path = root_db_path.clone();
let config = self.config;
move || -> anyhow::Result<_> {
let dbic = DatabaseInstanceContext::from_database(config, &database, instance_id, path);
let dbic = DatabaseInstanceContext::from_database(config, &database, instance_id, path)?;
let sched = Scheduler::open(dbic.scheduler_db_path(root_db_path))?;
Ok((dbic, sched))
}
Expand Down