1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::sync::Arc;
use bytes::Buf;
use crate::raw::*;
use crate::*;
/// Writer is designed to write data into given path in an asynchronous
/// manner.
///
/// ## Notes
///
/// Please make sure either `close` or `abort` has been called before
/// dropping the writer otherwise the data could be lost.
///
/// ## Usage
///
/// ### Write Multiple Chunks
///
/// Some services support to write multiple chunks of data into given path. Services that doesn't
/// support write multiple chunks will return [`ErrorKind::Unsupported`] error when calling `write`
/// at the second time.
///
/// ```
/// use opendal::Operator;
/// use opendal::Result;
///
/// async fn test(op: Operator) -> Result<()> {
/// let mut w = op.writer("path/to/file").await?;
/// w.write(vec![1; 1024]).await?;
/// w.write(vec![2; 1024]).await?;
/// w.close().await?;
/// Ok(())
/// }
/// ```
///
/// ### Write like `Sink`
///
/// ```
/// use anyhow::Result;
/// use futures::SinkExt;
/// use opendal::Operator;
///
/// async fn test(op: Operator) -> Result<()> {
/// let mut w = op.writer("path/to/file").await?.into_bytes_sink();
/// w.send(vec![1; 1024].into()).await?;
/// w.send(vec![2; 1024].into()).await?;
/// w.close().await?;
/// Ok(())
/// }
/// ```
///
/// ### Write like `AsyncWrite`
///
/// ```
/// use anyhow::Result;
/// use futures::AsyncWriteExt;
/// use opendal::Operator;
///
/// async fn test(op: Operator) -> Result<()> {
/// let mut w = op.writer("path/to/file").await?.into_futures_async_write();
/// w.write(&vec![1; 1024]).await?;
/// w.write(&vec![2; 1024]).await?;
/// w.close().await?;
/// Ok(())
/// }
/// ```
///
/// ### Write with append enabled
///
/// Writer also supports to write with append enabled. This is useful when users want to append
/// some data to the end of the file.
///
/// - If file doesn't exist, it will be created and just like calling `write`.
/// - If file exists, data will be appended to the end of the file.
///
/// Possible Errors:
///
/// - Some services store normal file and appendable file in different way. Trying to append
/// on non-appendable file could return [`ErrorKind::ConditionNotMatch`] error.
/// - Services that doesn't support append will return [`ErrorKind::Unsupported`] error when
/// creating writer with `append` enabled.
pub struct Writer {
/// Keep a reference to write context in writer.
_ctx: Arc<WriteContext>,
inner: WriteGenerator<oio::Writer>,
}
impl Writer {
/// Create a new writer from an `oio::Writer`.
pub(crate) async fn new(ctx: WriteContext) -> Result<Self> {
let ctx = Arc::new(ctx);
let inner = WriteGenerator::create(ctx.clone()).await?;
Ok(Self { _ctx: ctx, inner })
}
/// Write [`Buffer`] into writer.
///
/// This operation will write all data in given buffer into writer.
///
/// ## Examples
///
/// ```
/// use bytes::Bytes;
/// use opendal::Operator;
/// use opendal::Result;
///
/// async fn test(op: Operator) -> Result<()> {
/// let mut w = op.writer("hello.txt").await?;
/// // Buffer can be created from continues bytes.
/// w.write("hello, world").await?;
/// // Buffer can also be created from non-continues bytes.
/// w.write(vec![Bytes::from("hello,"), Bytes::from("world!")])
/// .await?;
///
/// // Make sure file has been written completely.
/// w.close().await?;
/// Ok(())
/// }
/// ```
pub async fn write(&mut self, bs: impl Into<Buffer>) -> Result<()> {
let mut bs = bs.into();
while !bs.is_empty() {
let n = self.inner.write(bs.clone()).await?;
bs.advance(n);
}
Ok(())
}
/// Write [`bytes::Buf`] into inner writer.
///
/// This operation will write all data in given buffer into writer.
///
/// # TODO
///
/// Optimize this function to avoid unnecessary copy.
pub async fn write_from(&mut self, bs: impl Buf) -> Result<()> {
let mut bs = bs;
let bs = Buffer::from(bs.copy_to_bytes(bs.remaining()));
self.write(bs).await
}
/// Abort the writer and clean up all written data.
///
/// ## Notes
///
/// Abort should only be called when the writer is not closed or
/// aborted, otherwise an unexpected error could be returned.
pub async fn abort(&mut self) -> Result<()> {
self.inner.abort().await
}
/// Close the writer and make sure all data have been committed.
///
/// ## Notes
///
/// Close should only be called when the writer is not closed or
/// aborted, otherwise an unexpected error could be returned.
pub async fn close(&mut self) -> Result<()> {
self.inner.close().await
}
/// Convert writer into [`FuturesAsyncWriter`] which implements [`futures::AsyncWrite`],
///
/// # Notes
///
/// FuturesAsyncWriter is not a zero-cost abstraction. The underlying writer
/// requires an owned [`Buffer`], which involves an extra copy operation.
///
/// FuturesAsyncWriter is required to call `close()` to make sure all
/// data have been written to the storage.
///
/// # Examples
///
/// ## Basic Usage
///
/// ```
/// use std::io;
///
/// use futures::io::AsyncWriteExt;
/// use opendal::Operator;
/// use opendal::Result;
///
/// async fn test(op: Operator) -> io::Result<()> {
/// let mut w = op.writer("hello.txt").await?.into_futures_async_write();
/// let bs = "Hello, World!".as_bytes();
/// w.write_all(bs).await?;
/// w.close().await?;
///
/// Ok(())
/// }
/// ```
///
/// ## Concurrent Write
///
/// ```
/// use std::io;
///
/// use futures::io::AsyncWriteExt;
/// use opendal::Operator;
/// use opendal::Result;
///
/// async fn test(op: Operator) -> io::Result<()> {
/// let mut w = op
/// .writer_with("hello.txt")
/// .concurrent(8)
/// .chunk(256)
/// .await?
/// .into_futures_async_write();
/// let bs = "Hello, World!".as_bytes();
/// w.write_all(bs).await?;
/// w.close().await?;
///
/// Ok(())
/// }
/// ```
pub fn into_futures_async_write(self) -> FuturesAsyncWriter {
FuturesAsyncWriter::new(self.inner)
}
/// Convert writer into [`FuturesBytesSink`] which implements [`futures::Sink<Bytes>`].
///
/// # Notes
///
/// FuturesBytesSink is a zero-cost abstraction. The underlying writer
/// will reuse the Bytes and won't perform any copy operation.
///
/// # Examples
///
/// ## Basic Usage
///
/// ```
/// use std::io;
///
/// use bytes::Bytes;
/// use futures::SinkExt;
/// use opendal::Operator;
/// use opendal::Result;
///
/// async fn test(op: Operator) -> io::Result<()> {
/// let mut w = op.writer("hello.txt").await?.into_bytes_sink();
/// let bs = "Hello, World!".as_bytes();
/// w.send(Bytes::from(bs)).await?;
/// w.close().await?;
///
/// Ok(())
/// }
/// ```
///
/// ## Concurrent Write
///
/// ```
/// use std::io;
///
/// use bytes::Bytes;
/// use futures::SinkExt;
/// use opendal::Operator;
/// use opendal::Result;
///
/// async fn test(op: Operator) -> io::Result<()> {
/// let mut w = op
/// .writer_with("hello.txt")
/// .concurrent(8)
/// .chunk(256)
/// .await?
/// .into_bytes_sink();
/// let bs = "Hello, World!".as_bytes();
/// w.send(Bytes::from(bs)).await?;
/// w.close().await?;
///
/// Ok(())
/// }
/// ```
pub fn into_bytes_sink(self) -> FuturesBytesSink {
FuturesBytesSink::new(self.inner)
}
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
use rand::rngs::ThreadRng;
use rand::Rng;
use rand::RngCore;
use crate::services;
use crate::Operator;
fn gen_random_bytes() -> Vec<u8> {
let mut rng = ThreadRng::default();
// Generate size between 1B..16MB.
let size = rng.gen_range(1..16 * 1024 * 1024);
let mut content = vec![0; size];
rng.fill_bytes(&mut content);
content
}
#[tokio::test]
async fn test_writer_write() {
let op = Operator::new(services::Memory::default()).unwrap().finish();
let path = "test_file";
let content = gen_random_bytes();
let mut writer = op.writer(path).await.unwrap();
writer
.write(content.clone())
.await
.expect("write must succeed");
writer.close().await.expect("close must succeed");
let buf = op.read(path).await.expect("read to end mut succeed");
assert_eq!(buf.to_bytes(), content);
}
#[tokio::test]
async fn test_writer_write_from() {
let op = Operator::new(services::Memory::default()).unwrap().finish();
let path = "test_file";
let content = gen_random_bytes();
let mut writer = op.writer(path).await.unwrap();
writer
.write_from(Bytes::from(content.clone()))
.await
.expect("write must succeed");
writer.close().await.expect("close must succeed");
let buf = op.read(path).await.expect("read to end mut succeed");
assert_eq!(buf.to_bytes(), content);
}
}