fc-db 1.0.0

Frontier database backend
Documentation
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2020 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::sync::Arc;
use crate::{Database, DbHash, DatabaseSettings, DatabaseSettingsSrc};

pub fn open_database(
	config: &DatabaseSettings,
) -> Result<Arc<dyn Database<DbHash>>, String> {
	let db: Arc<dyn Database<DbHash>> = match &config.source {
		DatabaseSettingsSrc::RocksDb { path, cache_size: _ } => {
			let db_config = kvdb_rocksdb::DatabaseConfig::with_columns(crate::columns::NUM_COLUMNS);
			let path = path.to_str()
				.ok_or_else(|| "Invalid database path".to_string())?;

			let db = kvdb_rocksdb::Database::open(&db_config, &path)
				.map_err(|err| format!("{}", err))?;
			sp_database::as_database(db)
		}
	};

	Ok(db)
}