tustvold commented on code in PR #3329: URL: https://github.com/apache/arrow-rs/pull/3329#discussion_r1045836725
########## object_store/src/prefix.rs: ########## @@ -0,0 +1,286 @@ +// 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. + +//! An object store wrapper handling a constant path prefix +use bytes::Bytes; +use futures::{stream::BoxStream, StreamExt, TryStreamExt}; +use std::ops::Range; +use tokio::io::AsyncWrite; + +use crate::path::Path; +use crate::{ + GetResult, ListResult, MultipartId, ObjectMeta, ObjectStore, + Result as ObjectStoreResult, +}; + +/// Store wrapper that applies a constant prefix to all paths handled by the store. +#[derive(Debug, Clone)] +pub struct PrefixObjectStore<T: ObjectStore> { + prefix: Path, + inner: T, +} + +impl<T: ObjectStore> std::fmt::Display for PrefixObjectStore<T> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "PrefixObjectStore({})", self.prefix.as_ref()) + } +} + +impl<T: ObjectStore> PrefixObjectStore<T> { + /// Create a new instance of [`PrefixObjectStore`] + pub fn new(store: T, prefix: impl Into<Path>) -> Self { + Self { + prefix: prefix.into(), + inner: store, + } + } + + /// Create the full path from a path relative to prefix + fn full_path(&self, location: &Path) -> ObjectStoreResult<Path> { + let path: &str = location.as_ref(); + let stripped = match self.prefix.as_ref() { + "" => path.to_string(), + p => format!("{}/{}", p, path), + }; + Ok(Path::parse(stripped)?) + } Review Comment: ```suggestion fn full_path(&self, location: &Path) -> Path { self.prefix.parts().chain(location.parts()).collect() } ``` -- 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]
