alamb commented on a change in pull request #811: URL: https://github.com/apache/arrow-datafusion/pull/811#discussion_r682087771
########## File path: datafusion/src/datasource/protocol_registry.rs ########## @@ -0,0 +1,83 @@ +// 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::datasource::local::LocalFSHandler; +use crate::error::Result; +use parquet::file::reader::ChunkReader; +use std::any::Any; +use std::collections::HashMap; +use std::sync::{Arc, RwLock}; + +pub trait ProtocolHandler<R>: Sync + Send { + /// Returns the protocol handler as [`Any`](std::any::Any) + /// so that it can be downcast to a specific implementation. + fn as_any(&self) -> &dyn Any; + + fn list_all_files(&self, root_path: &str, ext: &str) -> Result<Vec<String>>; + + fn get_reader(&self, file_path: &str) -> Result<Arc<dyn ChunkReader<T = R>>>; + + fn handler_name(&self) -> String; +} + +static LOCAL_SCHEME: &str = "file"; + +pub struct ProtocolRegistry { + pub protocol_handlers: RwLock<HashMap<String, Arc<dyn ProtocolHandler>>>, +} + +impl ProtocolRegistry { + pub fn new() -> Self { + let mut map: HashMap<String, Arc<dyn ProtocolHandler>> = HashMap::new(); + map.insert(LOCAL_SCHEME.to_string(), Arc::new(LocalFSHandler)); + + Self { + protocol_handlers: RwLock::new(map), + } + } + + /// Adds a new handler to this registry. + /// If a handler of the same prefix existed before, it is replaced in the registry and returned. + pub fn register_handler( + &self, + scheme: &str, + handler: Arc<dyn ProtocolHandler>, + ) -> Option<Arc<dyn ProtocolHandler>> { + let mut handlers = self.protocol_handlers.write().unwrap(); + handlers.insert(scheme.to_string(), handler) + } + + pub fn handler(&self, scheme: &str) -> Option<Arc<dyn ProtocolHandler>> { + let handlers = self.protocol_handlers.read().unwrap(); + handlers.get(scheme).cloned() + } + + pub fn handler_for_path(&self, path: &str) -> Arc<dyn ProtocolHandler> { Review comment: It would be good to document the assumptions this function is making. Like it seems to be assuming uri style paths (e.g. `file://blah` or `s3://blah`) and falls back to treating paths as loca filenames if the path does not contain a `':'` ########## File path: datafusion/src/datasource/protocol_registry.rs ########## @@ -0,0 +1,83 @@ +// 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::datasource::local::LocalFSHandler; +use crate::error::Result; +use parquet::file::reader::ChunkReader; +use std::any::Any; +use std::collections::HashMap; +use std::sync::{Arc, RwLock}; + +pub trait ProtocolHandler<R>: Sync + Send { + /// Returns the protocol handler as [`Any`](std::any::Any) + /// so that it can be downcast to a specific implementation. + fn as_any(&self) -> &dyn Any; + + fn list_all_files(&self, root_path: &str, ext: &str) -> Result<Vec<String>>; Review comment: Something that might be worth thinking about in this API is how would one traverse the paths (for example, how would one get all sub directories of `root_path`, and then how would you concatenate them together)? This may not be an important usecase, but I do think it is worth thinking about ########## File path: datafusion/src/datasource/protocol_registry.rs ########## @@ -0,0 +1,83 @@ +// 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::datasource::local::LocalFSHandler; +use crate::error::Result; +use parquet::file::reader::ChunkReader; +use std::any::Any; +use std::collections::HashMap; +use std::sync::{Arc, RwLock}; + +pub trait ProtocolHandler<R>: Sync + Send { Review comment: I wonder if calling interface `ObjectStore` might be more standard -- it is basically an abstraction for something that maps string keys to sources of "objects" (aka bytes). I also suggest adding some documentation on what a protocol handler is. Perhaps like the following ```rust /// A ProtocolHandler abstracts access to an underlying file/object storage. /// It maps maps strings (e.g. URLs, filesystem paths, etc) to sources of bytes ``` ########## File path: datafusion/src/execution/context.rs ########## @@ -125,12 +127,26 @@ pub struct ExecutionContext { pub state: Arc<Mutex<ExecutionContextState>>, } +lazy_static! { Review comment: I don't understand the need for a global static execution context. I think the intent is that the user of DataFusion would determine if it wanted to use a single context or if it wanted to re-use the same context -- 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]
