Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[storage] Add insertion of slices to Storage #184 #185

Merged
merged 6 commits into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub(crate) trait Storage<T: StorageImpl = Self>: StorageImpl<T> {
let bytes = value.serialize();
let index = self.create_index(position, bytes.len() as u64);

self.append(index.serialize())?;
self.append((bytes.len() as u64).serialize())?;
self.append(bytes)?;
self.append(&index.serialize())?;
self.append(&(bytes.len() as u64).serialize())?;
self.append(&bytes)?;
self.commit()?;

Ok(index)
Expand All @@ -45,7 +45,7 @@ pub(crate) trait Storage<T: StorageImpl = Self>: StorageImpl<T> {
let mut record = self.record(index)?;
let bytes = V::serialize(value);
self.ensure_record_size(&mut record, index, offset, bytes.len() as u64)?;
self.write(Self::value_position(record.position, offset), bytes)?;
self.write(Self::value_position(record.position, offset), &bytes)?;
self.commit()
}

Expand Down
17 changes: 17 additions & 0 deletions src/storage/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ mod tests {
);
}

#[test]
fn insert_at_bytes() {
let test_file = TestFile::from("./file_storage-insert_at_bytes.agdb");
let mut storage = FileStorage::try_from(test_file.file_name().as_str()).unwrap();

let index = storage.insert(&vec![1_i64, 2_i64, 3_i64]).unwrap();
let offset = (std::mem::size_of::<u64>() + std::mem::size_of::<i64>() * 1) as u64;
let size = std::mem::size_of::<i64>() * 2;

storage.insert_at(index, offset, &vec![0_u8; size]).unwrap();

assert_eq!(
storage.value::<Vec<i64>>(index).unwrap(),
vec![1_i64, 0_i64, 0_i64]
);
}

#[test]
fn move_at() {
let test_file = TestFile::from("./file_storage-move_at.agdb");
Expand Down
23 changes: 21 additions & 2 deletions src/storage/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Serialize for i64 {
}

fn serialize(&self) -> Vec<u8> {
self.to_le_bytes().to_vec()
self.to_le_bytes().into()
}
}

Expand All @@ -31,7 +31,7 @@ impl Serialize for u64 {
}

fn serialize(&self) -> Vec<u8> {
self.to_le_bytes().to_vec()
self.to_le_bytes().into()
}
}

Expand Down Expand Up @@ -68,6 +68,16 @@ impl<T: Serialize> Serialize for Vec<T> {
}
}

impl Serialize for Vec<u8> {
fn deserialize(bytes: &[u8]) -> Result<Self, DbError> {
Ok(bytes.to_vec())
}

fn serialize(&self) -> Vec<u8> {
self.to_vec()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -129,6 +139,15 @@ mod tests {
);
}

#[test]
fn vec_u8() {
let data = vec![1_u8, 2_u8, 3_u8];
let bytes = data.serialize();
let actual = Vec::<u8>::deserialize(&bytes);

assert_eq!(actual, Ok(data));
}

#[test]
fn vec_value_out_of_bounds() {
let bytes = 1_u64.serialize();
Expand Down
20 changes: 10 additions & 10 deletions src/storage/storage_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::write_ahead_log_record::WriteAheadLogRecord;
use crate::db_error::DbError;

pub(crate) trait StorageImpl<T = Self> {
fn append(&mut self, bytes: Vec<u8>) -> Result<(), DbError> {
fn append(&mut self, bytes: &[u8]) -> Result<(), DbError> {
self.write(std::io::SeekFrom::End(0), bytes)
}

Expand Down Expand Up @@ -46,7 +46,7 @@ pub(crate) trait StorageImpl<T = Self> {
new_position: u64,
) -> Result<(), DbError> {
let bytes = self.read(std::io::SeekFrom::Start(old_position), size)?;
self.write(std::io::SeekFrom::Start(new_position), bytes)?;
self.write(std::io::SeekFrom::Start(new_position), &bytes)?;
self.record_mut(index).position = new_position;

Ok(())
Expand All @@ -60,9 +60,9 @@ pub(crate) trait StorageImpl<T = Self> {
) -> Result<StorageRecord, DbError> {
let new_position = self.seek(std::io::SeekFrom::End(0))?;
let bytes = self.read(std::io::SeekFrom::Start(from), size)?;
self.append(record_index.serialize())?;
self.append(record_size.serialize())?;
self.append(bytes)?;
self.append(&record_index.serialize())?;
self.append(&record_size.serialize())?;
self.append(&bytes)?;

Ok(StorageRecord {
position: new_position,
Expand Down Expand Up @@ -93,15 +93,15 @@ pub(crate) trait StorageImpl<T = Self> {
fn erase_bytes(&mut self, position: u64, size: u64) -> Result<(), DbError> {
self.write(
std::io::SeekFrom::Start(position),
vec![0_u8; size as usize],
&vec![0_u8; size as usize],
)
}

fn indexes_by_position(&self) -> Vec<i64>;
fn insert_wal_record(&mut self, record: WriteAheadLogRecord) -> Result<(), DbError>;

fn invalidate_record(&mut self, index: i64, position: u64) -> Result<(), DbError> {
self.write(std::io::SeekFrom::Start(position), (-index).serialize())
self.write(std::io::SeekFrom::Start(position), &(-index).serialize())
}

fn is_at_end(&mut self, record: &StorageRecord) -> Result<bool, DbError> {
Expand Down Expand Up @@ -134,7 +134,7 @@ pub(crate) trait StorageImpl<T = Self> {

fn move_bytes(&mut self, from: u64, to: u64, size: u64) -> Result<(), DbError> {
let bytes = self.read(std::io::SeekFrom::Start(from), size)?;
self.write(std::io::SeekFrom::Start(to), bytes)?;
self.write(std::io::SeekFrom::Start(to), &bytes)?;

if from < to {
self.erase_bytes(from, std::cmp::min(size, to - from))?;
Expand Down Expand Up @@ -294,7 +294,7 @@ pub(crate) trait StorageImpl<T = Self> {

fn wal_records(&mut self) -> Result<Vec<WriteAheadLogRecord>, DbError>;

fn write(&mut self, position: std::io::SeekFrom, bytes: Vec<u8>) -> Result<(), DbError> {
fn write(&mut self, position: std::io::SeekFrom, bytes: &[u8]) -> Result<(), DbError> {
let current_end = self.seek(std::io::SeekFrom::End(0))?;
let write_pos = self.seek(position)?;

Expand All @@ -315,7 +315,7 @@ pub(crate) trait StorageImpl<T = Self> {
}

self.seek(position)?;
self.write_all(&bytes)
self.write_all(bytes)
}

fn write_all(&mut self, bytes: &[u8]) -> Result<(), DbError>;
Expand Down