mod compressed;
pub mod sql_parser;
mod traits;
mod uncompressed;
pub use compressed::{CompressedStorage, CompressionIndex, StatementOffset};
pub use sql_parser::SqlStreamParser;
pub use traits::TransactionStorage;
pub use uncompressed::UncompressedStorage;
use std::sync::Arc;
use tracing::info;
pub struct StorageFactory;
impl StorageFactory {
pub fn from_env() -> Arc<dyn TransactionStorage> {
let compression_enabled = std::env::var("PG2ANY_ENABLE_COMPRESSION")
.map(|v| v.to_lowercase() == "true" || v == "1")
.unwrap_or(false);
Self::create(compression_enabled)
}
pub fn create(compressed: bool) -> Arc<dyn TransactionStorage> {
if compressed {
info!("Using CompressedStorage: SQL files will be written as .sql.gz with index files");
Arc::new(CompressedStorage::new())
} else {
info!(
"Using UncompressedStorage: SQL files will be written as uncompressed .sql files"
);
Arc::new(UncompressedStorage::new())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_factory_create_uncompressed() {
let storage = StorageFactory::create(false);
assert_eq!(storage.file_extension(), "sql");
}
#[test]
fn test_factory_create_compressed() {
let storage = StorageFactory::create(true);
assert_eq!(storage.file_extension(), "sql.gz");
}
#[test]
fn test_factory_from_env_default() {
std::env::remove_var("PG2ANY_ENABLE_COMPRESSION");
let storage = StorageFactory::from_env();
assert_eq!(storage.file_extension(), "sql");
}
#[test]
fn test_factory_from_env_true() {
std::env::set_var("PG2ANY_ENABLE_COMPRESSION", "true");
let storage = StorageFactory::from_env();
assert_eq!(storage.file_extension(), "sql.gz");
std::env::remove_var("PG2ANY_ENABLE_COMPRESSION");
}
#[test]
fn test_factory_from_env_one() {
std::env::set_var("PG2ANY_ENABLE_COMPRESSION", "1");
let storage = StorageFactory::from_env();
assert_eq!(storage.file_extension(), "sql.gz");
std::env::remove_var("PG2ANY_ENABLE_COMPRESSION");
}
}