Kontinuation commented on code in PR #681: URL: https://github.com/apache/sedona-db/pull/681#discussion_r2881871547
########## c/sedona-gdal/src/register.rs: ########## @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::errors::GdalInitLibraryError; +use crate::gdal_api::GdalApi; +use std::path::PathBuf; +use std::sync::{Mutex, OnceLock}; + +/// Minimum GDAL version required by sedona-gdal. +#[cfg(feature = "gdal-sys")] +const MIN_GDAL_VERSION_MAJOR: i32 = 3; +#[cfg(feature = "gdal-sys")] +const MIN_GDAL_VERSION_MINOR: i32 = 4; + +static GDAL_API: OnceLock<GdalApi> = OnceLock::new(); +static GDAL_API_INIT_LOCK: Mutex<()> = Mutex::new(()); + +fn init_gdal_api<F>(init: F) -> Result<&'static GdalApi, GdalInitLibraryError> +where + F: FnOnce() -> Result<GdalApi, GdalInitLibraryError>, +{ + if let Some(api) = GDAL_API.get() { + return Ok(api); + } + + let _guard = GDAL_API_INIT_LOCK + .lock() + .map_err(|_| GdalInitLibraryError::Invalid("GDAL API init lock poisoned".to_string()))?; + + if let Some(api) = GDAL_API.get() { + return Ok(api); + } + + let api = init()?; + + // Register all GDAL drivers once, immediately after loading symbols. + // This mirrors georust/gdal's `_register_drivers()` pattern where + // `GDALAllRegister` is called via `std::sync::Once` before any driver + // lookup or dataset open. Here the `OnceLock` + `Mutex` already + // guarantees this runs exactly once. + unsafe { + let Some(gdal_all_register) = api.inner.GDALAllRegister else { + return Err(GdalInitLibraryError::LibraryError( + "GDALAllRegister symbol not loaded".to_string(), + )); + }; + gdal_all_register(); + } + + let _ = GDAL_API.set(api); + Ok(GDAL_API.get().expect("GDAL API should be set")) +} + +pub fn configure_global_gdal_api(shared_library: PathBuf) -> Result<(), GdalInitLibraryError> { Review Comment: I think we should load GDAL lazily just like `configure_global_proj_engine`. It will allow user to call `configure_gdal` after importing sedonadb. I'll work on refactoring it later. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
