opendal_core/services/dashmap/
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 super::core::DashmapCore;
21use super::core::DashmapValue;
22use crate::raw::*;
23use crate::*;
24
25pub struct DashmapWriter {
26    core: Arc<DashmapCore>,
27    path: String,
28    op: OpWrite,
29
30    buf: oio::QueueBuf,
31}
32
33impl DashmapWriter {
34    pub fn new(core: Arc<DashmapCore>, path: String, op: OpWrite) -> Self {
35        DashmapWriter {
36            core,
37            path,
38            op,
39            buf: oio::QueueBuf::new(),
40        }
41    }
42}
43
44impl oio::Write for DashmapWriter {
45    async fn write(&mut self, bs: Buffer) -> Result<()> {
46        self.buf.push(bs);
47        Ok(())
48    }
49
50    async fn close(&mut self) -> Result<Metadata> {
51        let content = self.buf.clone().collect();
52
53        let entry_mode = EntryMode::from_path(&self.path);
54        let mut meta = Metadata::new(entry_mode);
55        meta.set_content_length(content.len() as u64);
56        meta.set_last_modified(Timestamp::now());
57
58        if let Some(v) = self.op.content_type() {
59            meta.set_content_type(v);
60        }
61        if let Some(v) = self.op.content_disposition() {
62            meta.set_content_disposition(v);
63        }
64        if let Some(v) = self.op.cache_control() {
65            meta.set_cache_control(v);
66        }
67        if let Some(v) = self.op.content_encoding() {
68            meta.set_content_encoding(v);
69        }
70
71        self.core.set(
72            &self.path,
73            DashmapValue {
74                metadata: meta.clone(),
75                content,
76            },
77        )?;
78
79        Ok(meta)
80    }
81
82    async fn abort(&mut self) -> Result<()> {
83        self.buf.clear();
84        Ok(())
85    }
86}