opendal/raw/oio/delete/one_shot_delete.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 crate::raw::*;
19use crate::*;
20use std::future::Future;
21
22/// OneShotDelete is used to implement [`oio::Delete`] based on one shot operation.
23///
24/// OneShotDeleter will perform delete operation while calling `flush`.
25pub trait OneShotDelete: Send + Sync + Unpin + 'static {
26 /// delete_once delete one path at once.
27 ///
28 /// Implementations should make sure that the data is deleted correctly at once.
29 fn delete_once(
30 &self,
31 path: String,
32 args: OpDelete,
33 ) -> impl Future<Output = Result<()>> + MaybeSend;
34}
35
36/// BlockingOneShotDelete is used to implement [`oio::BlockingDelete`] based on one shot operation.
37///
38/// BlockingOneShotDeleter will perform delete operation while calling `flush`.
39pub trait BlockingOneShotDelete: Send + Sync + 'static {
40 /// delete_once delete one path at once.
41 ///
42 /// Implementations should make sure that the data is deleted correctly at once.
43 fn blocking_delete_once(&self, path: String, args: OpDelete) -> Result<()>;
44}
45
46/// OneShotDelete is used to implement [`oio::Delete`] based on one shot.
47pub struct OneShotDeleter<D> {
48 inner: D,
49 delete: Option<(String, OpDelete)>,
50}
51
52impl<D> OneShotDeleter<D> {
53 /// Create a new one shot deleter.
54 pub fn new(inner: D) -> Self {
55 Self {
56 inner,
57 delete: None,
58 }
59 }
60
61 fn delete_inner(&mut self, path: String, args: OpDelete) -> Result<()> {
62 if self.delete.is_some() {
63 return Err(Error::new(
64 ErrorKind::Unsupported,
65 "OneShotDeleter doesn't support batch delete",
66 ));
67 }
68
69 self.delete = Some((path, args));
70 Ok(())
71 }
72}
73
74impl<D: OneShotDelete> oio::Delete for OneShotDeleter<D> {
75 fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
76 self.delete_inner(path.to_string(), args)
77 }
78
79 async fn flush(&mut self) -> Result<usize> {
80 let Some((path, args)) = self.delete.clone() else {
81 return Ok(0);
82 };
83
84 self.inner.delete_once(path, args).await?;
85 self.delete = None;
86 Ok(1)
87 }
88}
89
90impl<D: BlockingOneShotDelete> oio::BlockingDelete for OneShotDeleter<D> {
91 fn delete(&mut self, path: &str, args: OpDelete) -> Result<()> {
92 self.delete_inner(path.to_string(), args)
93 }
94
95 fn flush(&mut self) -> Result<usize> {
96 let Some((path, args)) = self.delete.clone() else {
97 return Ok(0);
98 };
99
100 self.inner.blocking_delete_once(path, args)?;
101 self.delete = None;
102 Ok(1)
103 }
104}