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