opendal/services/azdls/
writer.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 http::StatusCode;
21
22use super::core::AzdlsCore;
23use super::error::parse_error;
24use crate::raw::*;
25use crate::*;
26
27pub type AzdlsWriters = TwoWays<oio::OneShotWriter<AzdlsWriter>, oio::AppendWriter<AzdlsWriter>>;
28
29pub struct AzdlsWriter {
30    core: Arc<AzdlsCore>,
31
32    op: OpWrite,
33    path: String,
34}
35
36impl AzdlsWriter {
37    pub fn new(core: Arc<AzdlsCore>, op: OpWrite, path: String) -> Self {
38        AzdlsWriter { core, op, path }
39    }
40}
41
42impl oio::OneShotWrite for AzdlsWriter {
43    async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
44        let resp = self
45            .core
46            .azdls_create(&self.path, "file", &self.op, Buffer::new())
47            .await?;
48
49        let status = resp.status();
50        match status {
51            StatusCode::CREATED | StatusCode::OK => {}
52            _ => {
53                return Err(parse_error(resp).with_operation("Backend::azdls_create_request"));
54            }
55        }
56
57        let resp = self
58            .core
59            .azdls_update(&self.path, Some(bs.len() as u64), 0, bs)
60            .await?;
61
62        let status = resp.status();
63        match status {
64            StatusCode::OK | StatusCode::ACCEPTED => Ok(Metadata::default()),
65            _ => Err(parse_error(resp).with_operation("Backend::azdls_update_request")),
66        }
67    }
68}
69
70impl oio::AppendWrite for AzdlsWriter {
71    async fn offset(&self) -> Result<u64> {
72        let resp = self.core.azdls_get_properties(&self.path).await?;
73
74        let status = resp.status();
75        let headers = resp.headers();
76
77        match status {
78            StatusCode::OK => Ok(parse_content_length(headers)?.unwrap_or_default()),
79            StatusCode::NOT_FOUND => Ok(0),
80            _ => Err(parse_error(resp)),
81        }
82    }
83
84    async fn append(&self, offset: u64, size: u64, body: Buffer) -> Result<Metadata> {
85        if offset == 0 {
86            let resp = self
87                .core
88                .azdls_create(&self.path, "file", &self.op, Buffer::new())
89                .await?;
90
91            let status = resp.status();
92            match status {
93                StatusCode::CREATED | StatusCode::OK => {}
94                _ => {
95                    return Err(parse_error(resp).with_operation("Backend::azdls_create_request"));
96                }
97            }
98        }
99
100        let resp = self
101            .core
102            .azdls_update(&self.path, Some(size), offset, body)
103            .await?;
104
105        let status = resp.status();
106        match status {
107            StatusCode::OK | StatusCode::ACCEPTED => Ok(Metadata::default()),
108            _ => Err(parse_error(resp).with_operation("Backend::azdls_update_request")),
109        }
110    }
111}