got datatype read/write working with database.

This commit is contained in:
Jeff Baskin 2023-03-19 13:02:20 -04:00
parent 418ba26da9
commit abfa7114df
3 changed files with 113 additions and 57 deletions

View File

@ -1,4 +1,4 @@
use super::{Database, DBError, ErrorCode, FileData, SessionData, Store};
use super::{DBError, Database, ErrorCode, FileData, SessionData, Store};
use async_std::{
fs::{read, remove_file, write},
path::{Path, PathBuf},
@ -22,6 +22,7 @@ impl DataType {
fn new(data_type: &str) -> Result<Self, DBError> {
match data_type {
"store" => Ok(DataType::DBMap(Store::new())),
"database" => Ok(DataType::TableMap(Database::new())),
_ => Err(DBError::from_code(ErrorCode::DataTypeIncorrect(
data_type.to_string(),
))),
@ -56,12 +57,13 @@ impl FileData<Self> for DataType {
fn to_bytes(&self) -> Vec<u8> {
let mut output = Vec::new();
match self {
DataType::DBMap(store) => {
output.append(&mut "DBMap".as_bytes().to_vec());
DataType::DBMap(_) => output.append(&mut "DBMap".as_bytes().to_vec()),
DataType::TableMap(_) => output.append(&mut "TableMap".as_bytes().to_vec()),
}
output.push(0);
output.append(&mut store.to_bytes());
},
DataType::TableMap(_) => todo!(),
match self {
DataType::DBMap(store) => output.append(&mut store.to_bytes()),
DataType::TableMap(db) => (),
}
output
}
@ -88,6 +90,7 @@ impl FileData<Self> for DataType {
Ok(store) => Ok(DataType::DBMap(store)),
Err(err) => Err(err),
},
"TableMap" => Ok(DataType::new("database").unwrap()),
_ => Err(DBError::from_code(ErrorCode::CorruptFile)),
}
}
@ -166,7 +169,8 @@ impl Entry {
match write(&self.filename, data.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryWriteFailure(self.filename.clone()));
let mut error =
DBError::from_code(ErrorCode::EntryWriteFailure(self.filename.clone()));
error.add_source(err);
return Err(error);
}
@ -179,7 +183,8 @@ impl Entry {
match remove_file(&self.filename).await {
Ok(_) => Ok(()),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryDeleteFailure(self.filename.clone()));
let mut error =
DBError::from_code(ErrorCode::EntryDeleteFailure(self.filename.clone()));
error.add_source(err);
Err(error)
}
@ -206,35 +211,63 @@ impl Cache {
let mut error = DBError::from_code(ErrorCode::CacheReadWrite);
error.add_source(err);
Err(error)
},
}
}
}
}
}
}
#[cfg(test)]
mod datatype {
use super::*;
#[test]
fn bad_data_type() -> Result<(), DBError> {
let dt = "bufcuss";
match DataType::new(dt) {
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::DataTypeIncorrect(value) => {
assert_eq!(value, dt, "Incorrect input value");
Ok(())
}
_ => {
let mut error = DBError::new("incorrect error");
error.add_source(err);
Err(error)
}
},
}
}
#[test]
fn create_store() -> Result<(), DBError> {
match DataType::new("store") {
Ok(dt) => match dt {
DataType::DBMap(_) => Ok(()),
_ => Err(DBError::new("incorrect data type")),
},
Err(err) => Err(err),
}
}
#[test]
fn create_database() -> Result<(), DBError> {
match DataType::new("database") {
Ok(dt) => match dt {
DataType::TableMap(_) => Ok(()),
_ => Err(DBError::new("incorrect data type")),
},
Err(err) => Err(err),
}
}
}
#[cfg(test)]
mod datatype_sesssion {
use super::*;
#[test]
fn invalid_cache_type() -> Result<(), DBError> {
match DataType::new("dkhgdl") {
Ok(_) => Err(DBError::new("invalid data type should raise an error")),
Err(err) => match err.code {
ErrorCode::DataTypeIncorrect(_) => Ok(()),
_ => Err(DBError::new("Invalid error code")),
},
}
}
#[test]
fn create_storage() {
let dbs = DataType::new("store").unwrap();
let expected: Vec<String> = Vec::new();
assert_eq!(dbs.list(["database"].to_vec()).unwrap(), expected);
}
#[test]
fn update_storage() {
let mut dbs = DataType::new("store").unwrap();
@ -296,6 +329,25 @@ mod datatype_file {
);
}
#[test]
fn new_database_bytes() {
let db = DataType::new("database").unwrap();
let mut expected = "TableMap".as_bytes().to_vec();
expected.push(0);
assert_eq!(db.to_bytes(), expected);
}
#[test]
fn read_empty_database() {
let dt = DataType::new("database").unwrap();
let data = dt.to_bytes();
let mut feed = data.iter();
match DataType::from_bytes(&mut feed).unwrap() {
DataType::TableMap(_) => (),
_ => assert!(false, "Incorrect data type"),
}
}
#[test]
fn read_bad_header() -> Result<(), DBError> {
let data = "sdghsdl".as_bytes().to_vec();
@ -320,7 +372,7 @@ mod datatype_file {
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
}
},
}
}
}
@ -481,9 +533,9 @@ mod entry {
.to_string()
.contains("could not write to file"));
Ok(())
},
_ => Err(DBError::new("incorrect error code")),
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
@ -530,7 +582,7 @@ mod entry {
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
}
},
}
}
@ -551,9 +603,9 @@ mod entry {
err.source().unwrap().to_string()
);
Ok(())
},
_ => Err(DBError::new("incorrect error code")),
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
@ -594,9 +646,9 @@ mod entry {
err.source().unwrap().to_string()
);
Ok(())
},
_ => Err(DBError::new("incorrect error code")),
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
}
@ -630,8 +682,7 @@ mod cache {
let path = dir.path().join("bad").join("path");
match Cache::new(path).await {
Ok(_) => Err(DBError::new("Should have produced an error.")),
Err(err) => {
match err.code {
Err(err) => match err.code {
ErrorCode::CacheReadWrite => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
@ -640,10 +691,9 @@ mod cache {
err.source().unwrap().to_string()
);
Ok(())
},
}
_ => Err(DBError::new("incorrect error code")),
}
}
},
}
}

View File

@ -4,6 +4,12 @@ use std::slice;
#[derive(Clone)]
pub struct Database;
impl Database {
pub fn new() -> Self {
Self
}
}
impl FileData<Self> for Database {
fn to_bytes(&self) -> Vec<u8> {
let output = Vec::new();

View File

@ -191,7 +191,7 @@ mod file_data {
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
}
},
}
}
@ -220,9 +220,9 @@ mod file_data {
format!("database {} already exists", name)
);
Ok(())
},
_ => Err(DBError::new("incorrect error code")),
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
}