tustvold commented on code in PR #375: URL: https://github.com/apache/arrow-rs-object-store/pull/375#discussion_r2106167957
########## src/registry.rs: ########## @@ -0,0 +1,337 @@ +// 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. + +//! Map object URLs to [`ObjectStore`] + +use crate::path::{InvalidPart, Path, PathPart}; +use crate::{parse_url_opts, ObjectStore}; +use parking_lot::RwLock; +use std::collections::HashMap; +use std::sync::Arc; +use url::Url; + +/// [`ObjectStoreRegistry`] maps a URL to an [`ObjectStore`] instance +pub trait ObjectStoreRegistry: Send + Sync + std::fmt::Debug + 'static { + /// Register a new store for the provided store URL + /// + /// If a store with the same URL existed before, it is replaced and returned + fn register(&self, url: Url, store: Arc<dyn ObjectStore>) -> Option<Arc<dyn ObjectStore>>; + + /// Resolve an object URL + /// + /// If [`ObjectStoreRegistry::register`] has been called with a URL with the same + /// scheme, and authority as the object URL, and a path that is a prefix of the object + /// URL's, it should be returned along with the trailing path. Paths should be matched + /// on a path segment basis, and in the event of multiple possibilities the longest + /// path match should be returned. + /// + /// If a store hasn't been registered, an [`ObjectStoreRegistry`] may lazily create + /// one if the URL is understood + /// + /// For example + /// + /// ``` + /// # use std::sync::Arc; + /// # use url::Url; + /// # use object_store::memory::InMemory; + /// # use object_store::ObjectStore; + /// # use object_store::prefix::PrefixStore; + /// # use object_store::registry::{DefaultObjectStoreRegistry, ObjectStoreRegistry}; + /// # + /// let registry = DefaultObjectStoreRegistry::new(); + /// + /// let bucket1 = Arc::new(InMemory::new()) as Arc<dyn ObjectStore>; + /// let base = Url::parse("s3://bucket1/").unwrap(); + /// registry.register(base, bucket1.clone()); + /// + /// let url = Url::parse("s3://bucket1/path/to/object").unwrap(); + /// let (ret, path) = registry.resolve(&url).unwrap(); + /// assert_eq!(path.as_ref(), "path/to/object"); + /// assert!(Arc::ptr_eq(&ret, &bucket1)); + /// + /// let bucket2 = Arc::new(InMemory::new()) as Arc<dyn ObjectStore>; + /// let base = Url::parse("https://s3.region.amazonaws.com/bucket").unwrap(); + /// registry.register(base, bucket2.clone()); + /// + /// let url = Url::parse("https://s3.region.amazonaws.com/bucket/path/to/object").unwrap(); + /// let (ret, path) = registry.resolve(&url).unwrap(); + /// assert_eq!(path.as_ref(), "path/to/object"); + /// assert!(Arc::ptr_eq(&ret, &bucket2)); + /// + /// let bucket3 = Arc::new(PrefixStore::new(InMemory::new(), "path")) as Arc<dyn ObjectStore>; + /// let base = Url::parse("https://s3.region.amazonaws.com/bucket/path").unwrap(); + /// registry.register(base, bucket3.clone()); + /// + /// let url = Url::parse("https://s3.region.amazonaws.com/bucket/path/to/object").unwrap(); + /// let (ret, path) = registry.resolve(&url).unwrap(); + /// assert_eq!(path.as_ref(), "to/object"); + /// assert!(Arc::ptr_eq(&ret, &bucket3)); + /// ``` + fn resolve(&self, url: &Url) -> crate::Result<(Arc<dyn ObjectStore>, Path)>; +} + +/// Error type for [`DefaultObjectStoreRegistry`] +#[derive(Debug, thiserror::Error)] +#[non_exhaustive] +enum Error { Review Comment: I've incorporated this in https://github.com/apache/arrow-rs-object-store/pull/375/commits/a3110f50177cd75b2acdbac85ee8a1e967f657f2 -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org