Skip to content

Commit

Permalink
uses bytesMut to read from file
Browse files Browse the repository at this point in the history
  • Loading branch information
RPallas92 committed Nov 10, 2024
1 parent 96eae17 commit a8ef904
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "graus-db"
version = "0.2.0"
authors = ["Ricardo Pallas Roman <[email protected]>"]
description = "A high-performance, thread-safe key-value embedded data store."
edition = "2018"
edition = "2021"
categories = ["algorithms", "asynchronous", "data-structures", "distributed-systems"]
homepage = "https://github.com/rpallas92/GrausDB"
keywords = ["database", "performance", "data-storage"]
Expand Down
27 changes: 22 additions & 5 deletions src/io_types.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
use crate::Result;
use std::io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write};
use std::{
fs::{File, Metadata},
io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write},
};

pub trait MetadataReader {
fn metadata(&self) -> io::Result<Metadata>;
}

impl MetadataReader for File {
fn metadata(&self) -> io::Result<Metadata> {
self.metadata()
}
}

/// A buffered reader that stores the current position
pub struct BufReaderWithPos<R: Read + Seek> {
pub struct BufReaderWithPos<R: Read + Seek + MetadataReader> {
pub pos: u64,
reader: BufReader<R>,
}

impl<R: Read + Seek> BufReaderWithPos<R> {
impl<R: Read + Seek + MetadataReader> BufReaderWithPos<R> {
pub fn new(mut inner: R) -> Result<Self> {
let pos = inner.seek(SeekFrom::Current(0))?;
Ok(BufReaderWithPos {
reader: BufReader::new(inner),
pos,
})
}

pub fn get_metadata(&self) -> io::Result<Metadata> {
self.reader.get_ref().metadata()
}
}

impl<R: Read + Seek> Read for BufReaderWithPos<R> {
impl<R: Read + Seek + MetadataReader> Read for BufReaderWithPos<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let len = self.reader.read(buf)?;
self.pos += len as u64;
Ok(len)
}
}

impl<R: Read + Seek> Seek for BufReaderWithPos<R> {
impl<R: Read + Seek + MetadataReader> Seek for BufReaderWithPos<R> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.pos = self.reader.seek(pos)?;
Ok(self.pos)
Expand Down
11 changes: 7 additions & 4 deletions src/log_storage/log_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
db_command::{Command, CommandPos},
io_types::{BufReaderWithPos, BufWriterWithPos},
};
use bytes::Bytes;
use bytes::{Bytes, BytesMut};
use crossbeam_skiplist::SkipMap;
use std::io::{Read, Seek};
use std::{
Expand Down Expand Up @@ -56,9 +56,12 @@ pub fn load_log(
let mut pos = reader.seek(SeekFrom::Start(0))?;
let mut uncompacted = 0; // number of bytes that can be saved after a compaction.

// Read the entire content into a buffer.
let mut buf = Vec::new();
reader.read_to_end(&mut buf)?;
let file_metadata = reader.get_metadata()?;
let file_size = file_metadata.len() as usize;

let mut buf = BytesMut::with_capacity(file_size);
buf.resize(file_size, 0);
reader.read_exact(&mut buf)?;

// Create an iterator for deserializing commands.
let mut deserializer = CommandDeserializer::new(Bytes::from(buf));
Expand Down
7 changes: 4 additions & 3 deletions src/log_storage/log_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bytes::Bytes;
use bytes::{Bytes, BytesMut};

use super::db_command_serde::deserialize_command;
use super::log_helpers::log_path;
Expand Down Expand Up @@ -71,8 +71,9 @@ impl LogReader {

pub fn read_command(&self, cmd_pos: CommandPos) -> Result<Command> {
self.read_and(cmd_pos, |mut cmd_reader| {
let mut buf = Vec::with_capacity(cmd_pos.len as usize);
cmd_reader.read_to_end(&mut buf)?;
let mut buf = BytesMut::with_capacity(cmd_pos.len as usize);
buf.resize(cmd_pos.len as usize, 0);
cmd_reader.read_exact(&mut buf)?;
let (_, command) = deserialize_command(Bytes::from(buf))?;
Ok(command)
})
Expand Down

0 comments on commit a8ef904

Please sign in to comment.