Skip to content

Commit

Permalink
Sarthak | Adds LogOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jul 14, 2024
1 parent 5074e28 commit 94c11f3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/memory/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod segment;
pub(crate) mod segment;
pub(crate) mod options;
38 changes: 38 additions & 0 deletions src/memory/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub(crate) struct LogOptions {
log_size_bytes: usize,
segment_size_bytes: usize,
}

impl LogOptions {
pub(crate) fn new(log_size_bytes: usize, segment_size_bytes: usize) -> LogOptions {
assert!(log_size_bytes >= segment_size_bytes);
LogOptions {
log_size_bytes,
segment_size_bytes,
}
}

pub(crate) fn number_of_segments(&self) -> usize {
if self.log_size_bytes % self.segment_size_bytes != 0 {
return (self.log_size_bytes / self.segment_size_bytes) + 1;
}
return self.log_size_bytes / self.segment_size_bytes;
}
}

#[cfg(test)]
mod tests {
use crate::memory::options::LogOptions;

#[test]
fn number_of_segments_1() {
let log_options = LogOptions::new(100, 10);
assert_eq!(10, log_options.number_of_segments());
}

#[test]
fn number_of_segments_2() {
let log_options = LogOptions::new(50, 3);
assert_eq!(17, log_options.number_of_segments());
}
}
12 changes: 6 additions & 6 deletions src/memory/segment.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
struct Segment {
pub(crate) struct Segment {
buffer: Vec<u8>,
available_capacity: usize,
}

impl Segment {
fn new(capacity: usize) -> Segment {
pub(crate) fn new(capacity: usize) -> Segment {
assert!(capacity > 0);
Segment {
buffer: Vec::with_capacity(capacity),
available_capacity: capacity,
}
}

fn try_append(&mut self, slice: &[u8]) -> bool {
pub(crate) fn try_append(&mut self, slice: &[u8]) -> bool {
if self.available_capacity >= slice.len() {
self.buffer.extend_from_slice(slice);
self.available_capacity -= slice.len();
Expand All @@ -21,18 +21,18 @@ impl Segment {
return false;
}

fn get(&self, index: usize, size: usize) -> &[u8] {
pub(crate) fn get(&self, index: usize, size: usize) -> &[u8] {
assert!(size > 0);
assert!(self.buffer.len() >= (index + size - 1));

return &self.buffer[index..index + size];
}

fn is_empty(&self) -> bool {
pub(crate) fn is_empty(&self) -> bool {
self.available_capacity == self.buffer.capacity()
}

fn is_full(&self) -> bool {
pub(crate) fn is_full(&self) -> bool {
self.available_capacity <= 0
}
}
Expand Down

0 comments on commit 94c11f3

Please sign in to comment.