Skip to content

Commit

Permalink
Added buffered IO for filesystem storages. (#41)
Browse files Browse the repository at this point in the history
* Added buffered IO for filesystem storages.

Signed-off-by: Pavel Kirilin <[email protected]>
  • Loading branch information
s3rius authored Feb 23, 2022
1 parent 1e28f68 commit dc1b702
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
18 changes: 14 additions & 4 deletions src/info_storages/file_info_storage.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand All @@ -56,7 +66,7 @@ impl InfoStorage for FileInfoStorage {
.to_string(),
)
})?;
file.sync_data().await?;
writer.flush().await?;
Ok(())
}

Expand Down
24 changes: 9 additions & 15 deletions src/storages/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")]
Expand Down Expand Up @@ -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)
Expand All @@ -96,38 +97,31 @@ 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(())
}

async fn create_file(&self, file_info: &FileInfo) -> RustusResult<String> {
// 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
.map_err(|err| {
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())
}

Expand Down

0 comments on commit dc1b702

Please sign in to comment.