mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-06 05:24:42 -04:00
Remove support for the sled database
This commit is contained in:
parent
97c11e4177
commit
d1f6739e59
9 changed files with 23 additions and 552 deletions
|
@ -1,4 +1,3 @@
|
|||
pub use self::sled::SledDatabase;
|
||||
pub use alice::Alice;
|
||||
pub use bob::Bob;
|
||||
pub use sqlite::SqliteDatabase;
|
||||
|
@ -13,7 +12,6 @@ use std::sync::Arc;
|
|||
|
||||
mod alice;
|
||||
mod bob;
|
||||
mod sled;
|
||||
mod sqlite;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
||||
|
@ -85,74 +83,16 @@ impl Swap {
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn open_db(
|
||||
sled_path: impl AsRef<Path>,
|
||||
sqlite_path: impl AsRef<Path>,
|
||||
force_sled: bool,
|
||||
) -> Result<Arc<dyn Database + Send + Sync>> {
|
||||
// if sled exists and sqlite doesnt exist try and migrate
|
||||
// if sled and sqlite exists and the sled flag is set, use sled
|
||||
// if sled and sqlite exists, use sqlite
|
||||
match (
|
||||
sled_path.as_ref().exists(),
|
||||
sqlite_path.as_ref().exists(),
|
||||
force_sled,
|
||||
) {
|
||||
(true, false, false) => {
|
||||
tracing::info!("Attempting to migrate old data to the new sqlite database...");
|
||||
let sled_db = SledDatabase::open(sled_path.as_ref()).await?;
|
||||
|
||||
ensure_directory_exists(sqlite_path.as_ref())?;
|
||||
tokio::fs::File::create(&sqlite_path).await?;
|
||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
||||
|
||||
let swap_states = sled_db.all().await?;
|
||||
for (swap_id, state) in swap_states.iter() {
|
||||
sqlite.insert_latest_state(*swap_id, state.clone()).await?;
|
||||
}
|
||||
|
||||
let monero_addresses = sled_db.get_all_monero_addresses();
|
||||
for (swap_id, monero_address) in monero_addresses.flatten() {
|
||||
sqlite
|
||||
.insert_monero_address(swap_id, monero_address)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let peer_addresses = sled_db.get_all_addresses();
|
||||
for (peer_id, addresses) in peer_addresses.flatten() {
|
||||
for address in addresses {
|
||||
sqlite.insert_address(peer_id, address).await?;
|
||||
}
|
||||
}
|
||||
|
||||
let peers = sled_db.get_all_peers();
|
||||
for (swap_id, peer_id) in peers.flatten() {
|
||||
sqlite.insert_peer_id(swap_id, peer_id).await?;
|
||||
}
|
||||
|
||||
tracing::info!("Sucessfully migrated data to sqlite! Using sqlite.");
|
||||
|
||||
Ok(Arc::new(sqlite))
|
||||
}
|
||||
(_, false, false) => {
|
||||
tracing::debug!("Creating and using new sqlite database.");
|
||||
ensure_directory_exists(sqlite_path.as_ref())?;
|
||||
tokio::fs::File::create(&sqlite_path).await?;
|
||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
||||
Ok(Arc::new(sqlite))
|
||||
}
|
||||
(_, true, false) => {
|
||||
tracing::debug!("Using existing sqlite database.");
|
||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
||||
Ok(Arc::new(sqlite))
|
||||
}
|
||||
(false, _, true) => {
|
||||
bail!("Sled database does not exist at specified location")
|
||||
}
|
||||
(true, _, true) => {
|
||||
tracing::debug!("Sled flag set. Using sled database.");
|
||||
let sled = SledDatabase::open(sled_path.as_ref()).await?;
|
||||
Ok(Arc::new(sled))
|
||||
}
|
||||
pub async fn open_db(sqlite_path: impl AsRef<Path>) -> Result<Arc<dyn Database + Send + Sync>> {
|
||||
if sqlite_path.as_ref().exists() {
|
||||
tracing::debug!("Using existing sqlite database.");
|
||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
||||
Ok(Arc::new(sqlite))
|
||||
} else {
|
||||
tracing::debug!("Creating and using new sqlite database.");
|
||||
ensure_directory_exists(sqlite_path.as_ref())?;
|
||||
tokio::fs::File::create(&sqlite_path).await?;
|
||||
let sqlite = SqliteDatabase::open(sqlite_path).await?;
|
||||
Ok(Arc::new(sqlite))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue