Skip to content

Commit

Permalink
refactor(storage): fix writing cells to file; finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pashinov authored and 0xdeafbeef committed Mar 27, 2024
1 parent 4843914 commit 1b25703
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 251 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ target/
perf.data*
.scratch

.DS_Store
storage/tmp/
.DS_Store
26 changes: 21 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion collator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description = "A collator node."
# local deps
tycho-core = { path = "../core", version = "=0.0.1" }
tycho-consensus = { path = "../consensus", version = "=0.0.1" }
tycho-storage = { path = "../storage", version = "=0.1.0" }
tycho-storage = { path = "../storage", version = "=0.0.1" }
tycho-util = { path = "../util", version = "=0.0.1" }

[lints]
Expand Down
1 change: 1 addition & 0 deletions storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ serde_json = "1.0.114"
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-test = "0.2"
tempfile = "3.10"

[lints]
workspace = true
10 changes: 9 additions & 1 deletion storage/src/db/file_db/mapped_file.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::path::Path;

use anyhow::Result;
Expand All @@ -17,7 +18,14 @@ impl MappedFile {
where
P: AsRef<Path>,
{
let file_db = FileDb::open(path)?;
let file_db = FileDb::new(
path,
fs::OpenOptions::new()
.write(true)
.read(true)
.truncate(true)
.create(true),
)?;
file_db.file.set_len(length as u64)?;

Self::from_existing_file(file_db)
Expand Down
54 changes: 22 additions & 32 deletions storage/src/db/file_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,57 @@ use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use everscale_types::models::*;

pub use mapped_file::MappedFile;

mod mapped_file;

pub struct FileDb {
file: File,
path: PathBuf,
_path: PathBuf,
}

impl FileDb {
pub fn open<P>(path: P) -> Result<Self>
pub fn new<P>(path: P, options: &mut std::fs::OpenOptions) -> std::io::Result<Self>
where
P: AsRef<Path>,
{
let file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.read(true)
.open(&path)
.context(format!("Failed to create file {:?}", path.as_ref()))?;
let file = options.open(&path)?;

Ok(Self {
file,
path: PathBuf::from(path.as_ref()),
_path: PathBuf::from(path.as_ref()),
})
}

pub fn write(&mut self, buf: &[u8]) -> Result<()> {
self.file.write(buf)?;
Ok(())
}

pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.file.write_all(buf)?;
Ok(())
}

pub fn flush(&mut self) -> Result<()> {
self.file.flush()?;
Ok(())
pub fn file(&self) -> &File {
&self.file
}
}

pub fn seek(&mut self, pos: SeekFrom) -> Result<()> {
self.file.seek(pos)?;
Ok(())
impl Write for FileDb {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.file.write(buf)
}

pub fn file(&self) -> &File {
&self.file
#[inline]
fn flush(&mut self) -> std::io::Result<()> {
self.file.flush()
}
}

pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
impl Read for FileDb {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let bytes = self.file.read(buf)?;
Ok(bytes)
}
}

impl Seek for FileDb {
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
self.file.seek(pos)
}
}

impl Into<File> for FileDb {
fn into(self) -> File {
self.file
Expand Down
1 change: 0 additions & 1 deletion storage/src/db/kv_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::thread::available_parallelism;

use anyhow::{Context, Result};
use bytesize::ByteSize;
use serde::{Deserialize, Serialize};
use weedb::{Caches, WeeDb};

pub use weedb::Stats as RocksdbStats;
Expand Down
Loading

0 comments on commit 1b25703

Please sign in to comment.