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, FILE};
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.core.azdls_create(&self.path, FILE, &self.op).await?;
45
46        let status = resp.status();
47        match status {
48            StatusCode::CREATED | StatusCode::OK => {}
49            _ => {
50                return Err(parse_error(resp).with_operation("Backend::azdls_create_request"));
51            }
52        }
53
54        let resp = self
55            .core
56            .azdls_update(&self.path, Some(bs.len() as u64), 0, bs)
57            .await?;
58
59        let status = resp.status();
60        match status {
61            StatusCode::OK | StatusCode::ACCEPTED => Ok(Metadata::default()),
62            _ => Err(parse_error(resp).with_operation("Backend::azdls_update_request")),
63        }
64    }
65}
66
67impl oio::AppendWrite for AzdlsWriter {
68    async fn offset(&self) -> Result<u64> {
69        let resp = self.core.azdls_get_properties(&self.path).await?;
70
71        let status = resp.status();
72        let headers = resp.headers();
73
74        match status {
75            StatusCode::OK => Ok(parse_content_length(headers)?.unwrap_or_default()),
76            StatusCode::NOT_FOUND => Ok(0),
77            _ => Err(parse_error(resp)),
78        }
79    }
80
81    async fn append(&self, offset: u64, size: u64, body: Buffer) -> Result<Metadata> {
82        if offset == 0 {
83            let resp = self.core.azdls_create(&self.path, FILE, &self.op).await?;
84
85            let status = resp.status();
86            match status {
87                StatusCode::CREATED | StatusCode::OK => {}
88                _ => {
89                    return Err(parse_error(resp).with_operation("Backend::azdls_create_request"));
90                }
91            }
92        }
93
94        let resp = self
95            .core
96            .azdls_update(&self.path, Some(size), offset, body)
97            .await?;
98
99        let status = resp.status();
100        match status {
101            StatusCode::OK | StatusCode::ACCEPTED => Ok(Metadata::default()),
102            _ => Err(parse_error(resp).with_operation("Backend::azdls_update_request")),
103        }
104    }
105}