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