Refactored: extracted some duplicated code for storing interfaces to ... (#4272)

Co-authored-by: Tobie Morgan Hitchcock <tobie@surrealdb.com>
This commit is contained in:
유정인 2024-07-18 18:14:01 +09:00 committed by GitHub
parent a5a3545052
commit febca6464a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,19 +17,21 @@ use std::fs;
use std::sync::Arc; use std::sync::Arc;
use url::Url; use url::Url;
static STORE: Lazy<Arc<dyn ObjectStore>> = fn initialize_store(env_var: &str, default_dir: &str) -> Arc<dyn ObjectStore> {
Lazy::new(|| match std::env::var("SURREAL_OBJECT_STORE") { match std::env::var(env_var) {
Ok(url) => { Ok(url) => {
let url = Url::parse(&url).expect("Expected a valid url for SURREAL_OBJECT_STORE"); let url =
Url::parse(&url).unwrap_or_else(|_| panic!("Expected a valid url for {}", env_var));
let (store, _) = let (store, _) =
parse_url(&url).expect("Expected a valid url for SURREAL_OBJECT_STORE"); parse_url(&url).unwrap_or_else(|_| panic!("Expected a valid url for {}", env_var));
Arc::new(store) Arc::new(store)
} }
Err(_) => { Err(_) => {
let path = env::current_dir().unwrap().join("store"); let path = env::current_dir().unwrap().join(default_dir);
if !path.exists() || !path.is_dir() { if !path.exists() || !path.is_dir() {
fs::create_dir_all(&path) fs::create_dir_all(&path).unwrap_or_else(|_| {
.expect("Unable to create directory structure for SURREAL_OBJECT_STORE"); panic!("Unable to create directory structure for {}", env_var)
});
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
{ {
@ -41,33 +43,14 @@ static STORE: Lazy<Arc<dyn ObjectStore>> =
Arc::new(InMemory::new()) Arc::new(InMemory::new())
} }
} }
}); }
}
static STORE: Lazy<Arc<dyn ObjectStore>> =
Lazy::new(|| initialize_store("SURREAL_OBJECT_STORE", "store"));
static CACHE: Lazy<Arc<dyn ObjectStore>> = static CACHE: Lazy<Arc<dyn ObjectStore>> =
Lazy::new(|| match std::env::var("SURREAL_OBJECT_CACHE") { Lazy::new(|| initialize_store("SURREAL_CACHE_STORE", "cache"));
Ok(url) => {
let url = Url::parse(&url).expect("Expected a valid url for SURREAL_OBJECT_CACHE");
let (store, _) =
parse_url(&url).expect("Expected a valid url for SURREAL_OBJECT_CACHE");
Arc::new(store)
}
Err(_) => {
let path = env::current_dir().unwrap().join("cache");
if !path.exists() || !path.is_dir() {
fs::create_dir_all(&path)
.expect("Unable to create directory structure for SURREAL_OBJECT_CACHE");
}
#[cfg(not(target_arch = "wasm32"))]
{
// As long as the provided path is correct, the following should never panic
Arc::new(LocalFileSystem::new_with_prefix(path).unwrap())
}
#[cfg(target_arch = "wasm32")]
{
Arc::new(InMemory::new())
}
}
});
/// Streams the file from the local system or memory object storage. /// Streams the file from the local system or memory object storage.
pub async fn stream( pub async fn stream(