From dc1b702196b1039a308a06fef2f9607302bc6d4e Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Wed, 23 Feb 2022 20:49:51 +0400 Subject: [PATCH] Added buffered IO for filesystem storages. (#41) * Added buffered IO for filesystem storages. Signed-off-by: Pavel Kirilin --- src/info_storages/file_info_storage.rs | 18 ++++++++++++++---- src/storages/file_storage.rs | 24 +++++++++--------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/info_storages/file_info_storage.rs b/src/info_storages/file_info_storage.rs index 65cd3ca..bafa5bc 100644 --- a/src/info_storages/file_info_storage.rs +++ b/src/info_storages/file_info_storage.rs @@ -1,8 +1,9 @@ use std::path::PathBuf; use async_std::fs::{read_to_string, remove_file, DirBuilder, OpenOptions}; -use async_std::prelude::*; use async_trait::async_trait; +use futures::io::BufWriter; +use futures::AsyncWriteExt; use log::error; use crate::errors::{RustusError, RustusResult}; @@ -35,7 +36,7 @@ impl InfoStorage for FileInfoStorage { } async fn set_info(&self, file_info: &FileInfo, create: bool) -> RustusResult<()> { - let mut file = OpenOptions::new() + let file = OpenOptions::new() .write(true) .create(create) .truncate(true) @@ -45,7 +46,16 @@ impl InfoStorage for FileInfoStorage { error!("{:?}", err); RustusError::UnableToWrite(err.to_string()) })?; - file.write_all(serde_json::to_string(&file_info)?.as_bytes()) + let mut writer = BufWriter::new(file); + writer + .write_all( + serde_json::to_string(&file_info) + .map_err(|err| { + error!("{:#?}", err); + err + })? + .as_bytes(), + ) .await .map_err(|err| { error!("{:?}", err); @@ -56,7 +66,7 @@ impl InfoStorage for FileInfoStorage { .to_string(), ) })?; - file.sync_data().await?; + writer.flush().await?; Ok(()) } diff --git a/src/storages/file_storage.rs b/src/storages/file_storage.rs index ccc699c..ba44819 100644 --- a/src/storages/file_storage.rs +++ b/src/storages/file_storage.rs @@ -3,7 +3,6 @@ use std::path::PathBuf; use actix_files::NamedFile; use async_std::fs::{remove_file, DirBuilder, File, OpenOptions}; use async_std::io::copy; -use async_std::prelude::*; use async_trait::async_trait; use log::error; @@ -12,6 +11,8 @@ use crate::info_storages::FileInfo; use crate::storages::Storage; use crate::utils::dir_struct::dir_struct; use derive_more::Display; +use futures::io::BufWriter; +use futures::AsyncWriteExt; #[derive(Display)] #[display(fmt = "file_storage")] @@ -86,7 +87,7 @@ impl Storage for FileStorage { // Opening file in w+a mode. // It means that we're going to append some // bytes to the end of a file. - let mut file = OpenOptions::new() + let file = OpenOptions::new() .write(true) .append(true) .create(false) @@ -96,12 +97,13 @@ impl Storage for FileStorage { error!("{:?}", err); RustusError::UnableToWrite(err.to_string()) })?; - file.write_all(bytes).await.map_err(|err| { + let mut writer = BufWriter::new(file); + writer.write_all(bytes).await.map_err(|err| { error!("{:?}", err); RustusError::UnableToWrite(info.path.clone().unwrap()) })?; - file.sync_data().await?; // Updating information about file. + writer.flush().await?; Ok(()) } @@ -109,9 +111,10 @@ impl Storage for FileStorage { // New path to file. let file_path = self.data_file_path(file_info.id.as_str()).await?; // Creating new file. - let mut file = OpenOptions::new() - .write(true) + OpenOptions::new() .create(true) + .write(true) + .truncate(true) .create_new(true) .open(file_path.as_path()) .await @@ -119,15 +122,6 @@ impl Storage for FileStorage { error!("{:?}", err); RustusError::FileAlreadyExists(file_info.id.clone()) })?; - - // Let's write an empty string to the beginning of the file. - // Maybe remove it later. - file.write_all(b"").await.map_err(|err| { - error!("{:?}", err); - RustusError::UnableToWrite(file_path.display().to_string()) - })?; - file.sync_all().await?; - Ok(file_path.display().to_string()) }