object_store_opendal/service/
deleter.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::sync::Arc;
19
20use futures::stream::{self, StreamExt};
21use object_store::path::Path as ObjectStorePath;
22use object_store::ObjectStore;
23use opendal::raw::oio::BatchDeleteResult;
24use opendal::raw::*;
25use opendal::*;
26
27use super::error::parse_error;
28
29pub struct ObjectStoreDeleter {
30    store: Arc<dyn ObjectStore + 'static>,
31}
32
33impl ObjectStoreDeleter {
34    pub(crate) fn new(store: Arc<dyn ObjectStore + 'static>) -> Self {
35        Self { store }
36    }
37}
38
39impl oio::BatchDelete for ObjectStoreDeleter {
40    async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
41        let object_path = ObjectStorePath::from(path);
42        self.store.delete(&object_path).await.map_err(parse_error)
43    }
44
45    async fn delete_batch(&self, paths: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
46        // convert paths to stream, then use [`ObjectStore::delete_stream`] to delete them in batch
47        let stream = stream::iter(paths.iter())
48            .map(|(path, _)| Ok::<_, object_store::Error>(ObjectStorePath::from(path.as_str())))
49            .boxed();
50        let results = self.store.delete_stream(stream).collect::<Vec<_>>().await;
51
52        // convert the results to [`BatchDeleteResult`]
53        let mut result_batch = BatchDeleteResult::default();
54        for (idx, result) in results.into_iter().enumerate() {
55            match result {
56                Ok(_) => result_batch
57                    .succeeded
58                    .push((paths[idx].0.clone(), paths[idx].1.clone())),
59                Err(e) => result_batch.failed.push((
60                    paths[idx].0.clone(),
61                    paths[idx].1.clone(),
62                    parse_error(e),
63                )),
64            }
65        }
66
67        Ok(result_batch)
68    }
69}