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::ObjectStore;
22use object_store::ObjectStoreExt;
23use object_store::path::Path as ObjectStorePath;
24use opendal::raw::oio::BatchDeleteResult;
25use opendal::raw::*;
26use opendal::*;
27
28use super::error::parse_error;
29
30pub struct ObjectStoreDeleter {
31    store: Arc<dyn ObjectStore + 'static>,
32}
33
34impl ObjectStoreDeleter {
35    pub(crate) fn new(store: Arc<dyn ObjectStore + 'static>) -> Self {
36        Self { store }
37    }
38}
39
40impl oio::BatchDelete for ObjectStoreDeleter {
41    async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
42        let object_path = ObjectStorePath::from(path);
43        self.store.delete(&object_path).await.map_err(parse_error)
44    }
45
46    async fn delete_batch(&self, paths: Vec<(String, OpDelete)>) -> Result<BatchDeleteResult> {
47        let locations: Vec<_> = paths
48            .iter()
49            .map(|(path, _)| Ok::<_, object_store::Error>(ObjectStorePath::from(path.as_str())))
50            .collect();
51        let stream = stream::iter(locations).boxed();
52        let results = self.store.delete_stream(stream).collect::<Vec<_>>().await;
53
54        let mut result_batch = BatchDeleteResult::default();
55        for (idx, result) in results.into_iter().enumerate() {
56            match result {
57                Ok(_) => result_batch
58                    .succeeded
59                    .push((paths[idx].0.clone(), paths[idx].1.clone())),
60                Err(e) => result_batch.failed.push((
61                    paths[idx].0.clone(),
62                    paths[idx].1.clone(),
63                    parse_error(e),
64                )),
65            }
66        }
67
68        Ok(result_batch)
69    }
70}