Compare commits

...

104 Commits

Author SHA1 Message Date
2e73bddbab Return to orignal label.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Failing after 1m52s
2024-02-09 23:46:50 -05:00
800cad7ca3 Updated pipenv.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Has been cancelled
2024-02-07 10:36:37 -05:00
95b2763442 Changed to fedora image.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Has been cancelled
2024-02-01 20:59:52 -05:00
460ca4b5a7 Switching from jenkins.
Some checks failed
MoreThanText build / Build-MoreThanText (push) Failing after 1m50s
2024-02-01 20:49:39 -05:00
45522660bb Where I left off. 2024-02-01 20:40:29 -05:00
829b7242bd Added a table. 2023-07-21 18:10:38 -04:00
49b0eaf2ec Made sure that cache did not over write itself. 2023-07-18 11:44:49 -04:00
e49d6f5e46 Completed duplicate database in multiple sessions. 2023-07-13 09:39:25 -04:00
ea7dec2f4e Cache only stores references. 2023-07-06 17:28:10 -04:00
4dad6f7a05 Database is now stored as an entry. 2023-07-01 15:56:12 -04:00
de93ee1f2c Added random id generator. 2023-07-01 13:26:36 -04:00
c70c006abd Now returning duplicate database errors. 2023-06-30 22:38:18 -04:00
e73fdbff75 Commit adds db from each session. 2023-06-30 12:23:28 -04:00
05d445c58b Added a basic commit function. 2023-06-29 00:17:49 -04:00
933d48a47c Make store list work. 2023-06-25 10:39:14 -04:00
c26089caed Protect against duplicate databases. 2023-06-24 10:48:35 -04:00
d90dc3b9fc Can now add database to store. 2023-06-23 08:30:49 -04:00
a23b5d467e Setup store to hold databases. 2023-06-22 11:08:40 -04:00
c8b93d9922 Moved old database code. 2023-06-22 10:35:05 -04:00
ecfc8fdf90 Got the client and engine connecting. 2023-06-20 11:51:32 -04:00
dd1c45ddbe Continuing channel setup. 2023-06-17 12:01:58 -04:00
30ea8d978c Setup for data. 2023-06-03 15:27:26 -04:00
914e7a8146 Setup store. 2023-05-30 00:18:13 -04:00
646e0597ca Move store. 2023-05-29 23:34:52 -04:00
50a4431316 Setting up for data storage. 2023-05-29 15:42:32 -04:00
9f368b0b95 Added a requests trait. 2023-05-25 11:00:20 -04:00
69acfefc0c Added commit data. 2023-05-11 08:46:44 -04:00
0f8670f686 Adding putting a database into a store. 2023-04-23 13:13:38 -04:00
90ab5809b1 Improved query error handling. 2023-04-18 09:02:18 -04:00
41afc07bfa Final bit of interface cleanup. 2023-04-15 10:18:56 -04:00
9faa96d307 Cleaned up the cache interface. 2023-04-15 09:47:48 -04:00
30c9562542 Separated message functions. 2023-04-12 18:33:39 -04:00
448de012d3 Added empty commit. 2023-04-11 08:12:41 -04:00
bb23397eb0 Added the database datatype. 2023-04-10 07:52:53 -04:00
da26693242 Refactored test. 2023-04-09 09:45:39 -04:00
572abbeda8 Got the cache channels setup. 2023-04-08 15:04:04 -04:00
e2d32f4a8c Added new function to DataType 2023-04-04 12:36:10 -04:00
659a2758bb Pulled back to use async channels. 2023-04-04 09:59:29 -04:00
685ddfe32d Moved existing mod out of the way. 2023-04-03 14:59:40 -04:00
6d61af5136 Added id generator. 2023-03-28 07:38:12 -04:00
4b0af019c8 Moved session inside database. 2023-03-26 10:30:22 -04:00
ee45375085 Removed session as separate code. 2023-03-26 09:30:42 -04:00
c75bea3913 Setup initial db session. 2023-03-24 08:00:48 -04:00
40aae3ef5a Moved entry into separate file. 2023-03-22 22:41:40 -04:00
1f36848450 Moved in new mod file. 2023-03-21 18:35:42 -04:00
d4d42a599e Moved to new mod file. 2023-03-21 17:48:24 -04:00
3de5c2857b Removing old mod file. 2023-03-21 17:47:20 -04:00
abfa7114df got datatype read/write working with database. 2023-03-19 13:02:20 -04:00
418ba26da9 Stubbed out database. 2023-03-16 22:32:10 -04:00
e125d79a6c Added error codes to store. 2023-03-15 07:38:39 -04:00
a3d8feb37a Added cache initialization. 2023-03-14 11:32:37 -04:00
c477a92945 Added initialization failure. 2023-03-05 11:15:32 -05:00
8fada737ac Replaced filename string with PathBuf. 2023-03-05 00:19:11 -05:00
691ab6a27e Added entry remove function. 2023-02-19 11:27:28 -05:00
0ffb30d322 Added retrival function. 2023-02-17 09:05:06 -05:00
c0e4a5fad7 added Entry update function. 2023-02-15 12:13:19 -05:00
ef48fde890 Finished out Entry's new and get functions. 2023-02-15 09:47:15 -05:00
9b0154064f Handled unicode error. 2023-02-15 07:10:28 -05:00
894fcbcd02 In process of separating cache from database. 2023-02-13 10:19:20 -05:00
716b559062 Began reworking cache. 2023-02-12 10:39:39 -05:00
af1a60665a Removed the commented code. 2023-02-05 11:32:30 -05:00
5b291e3553 Completed making the session trait for databases. 2023-02-05 11:29:00 -05:00
51fc2c8921 Continued work to get items into a session. 2023-02-04 23:06:29 -05:00
60dae69788 Began abstracting functions for database sessions. 2023-02-04 14:29:07 -05:00
77c0f9f189 Removed session so it is part of the database. 2023-02-02 21:55:18 -05:00
ca418136a7 Added db session. 2023-02-01 07:59:15 -05:00
3ca71733c2 Switched add database from option to result. 2023-01-27 12:44:26 -05:00
766d2e753a Removed databases with empty names. 2023-01-23 11:05:55 -05:00
6e6de135ef Added FileData trait to Databases. 2023-01-22 14:15:34 -05:00
878e5277bf Added databases structure. 2023-01-21 21:46:39 -05:00
cef9843720 Consolidated code. 2023-01-20 09:34:35 -05:00
0a889b6c3c Removed false starts. 2023-01-19 21:52:04 -05:00
aef8c68853 Finished out the database stub. 2023-01-17 13:18:35 -05:00
13aa4328cc Added a database cache type. 2023-01-17 13:02:43 -05:00
809bcb641b Added an entry point to the database. 2023-01-16 12:07:33 -05:00
fa9cbe6a25 Added initial from bytes to databases. 2023-01-16 09:31:13 -05:00
43036bec84 Added read errors. 2023-01-13 07:54:27 -05:00
b5b0016aba Began work to add entry point. 2023-01-11 09:10:15 -05:00
d09474bb14 Removed Record items for now. 2023-01-10 10:12:44 -05:00
85d299b852 Added pull record information. 2023-01-05 11:01:05 -05:00
6983ee366d Added record entry. 2023-01-04 09:56:58 -05:00
58aba1a565 Added Record to cachetype. 2022-12-31 19:35:02 -05:00
4914754f43 Moved cachetype into seperate module. 2022-12-30 22:39:19 -05:00
bab5c27cee Removed the unused interprater code. 2022-12-30 22:09:36 -05:00
085972060b Added delete error. 2022-12-29 08:08:21 -05:00
a22207f3b3 Added delete entry. 2022-12-28 18:21:32 -05:00
6229671212 Refactor 2022-12-27 15:41:49 -05:00
5e0faf4da8 Removes unused entries from memory. 2022-12-26 17:48:50 -05:00
d3f53422df Refactored common filename function. 2022-12-24 11:30:45 -05:00
fe29841ffe Get cache updates time. 2022-12-24 10:28:49 -05:00
56005a5e47 Added time updates to CacheEntry. 2022-12-24 02:03:20 -05:00
f0f8e7455e Added last access time to cache. 2022-12-21 10:27:16 -05:00
3164da0f4c Reads cache entry from the drive. 2022-12-19 15:21:07 -05:00
1403aaa935 Added file errors to update entry. 2022-12-19 10:33:04 -05:00
96ed474568 update_entry updates file. 2022-12-19 08:12:32 -05:00
3e203b40c0 Added io errors to add_entry. 2022-12-18 11:12:58 -05:00
83e3b2ef9e Removed duplicate ids. 2022-12-16 08:22:36 -05:00
ab30604033 Writes out contents of the cache. 2022-12-15 12:20:40 -05:00
947adf6c1d Writes out contents of the cache. 2022-12-13 07:19:05 -05:00
8db7a6d9bf Turn cache entry into writable bytes. 2022-12-11 09:34:07 -05:00
eb97e3399a Added cache entry identifier. 2022-12-10 09:26:06 -05:00
470c6612de Formatting. 2022-12-08 11:08:05 -05:00
fcec57414a Added cache update function. 2022-12-08 10:58:29 -05:00
d73a18be2d Added error for missing cache entry. 2022-12-06 11:42:11 -05:00
15 changed files with 3545 additions and 167 deletions

View File

@ -0,0 +1,11 @@
---
name: MoreThanText build
run-name: ${{ gitea.actor }} runner
on: push
jobs:
Build-MoreThanText:
runs-on: ubuntu-latest
steps:
- run: cargo test
- run: cargo build --release

View File

@ -11,4 +11,4 @@ black = "*"
pylint = "*"
[requires]
python_version = "3.9"
python_version = "3"

View File

@ -7,7 +7,7 @@ use tide::{
mod morethantext;
mod settings;
use morethantext::MoreThanText;
use morethantext::{start_db, MoreThanText};
use settings::Settings;
#[async_std::main]
@ -20,7 +20,7 @@ async fn main() -> tide::Result<()> {
}
async fn app_setup(data_dir: &str) -> tide::Server<MoreThanText> {
let db = MoreThanText::new(data_dir).await.unwrap();
let db = start_db(data_dir).await.unwrap();
let mut app = tide::with_state(db);
app.at("/").get(home);
app.with(

336
src/morethantext/cache.rs Normal file
View File

@ -0,0 +1,336 @@
use super::{Database, ErrorCode, FromCache, MTTError, Store, ToCache, ENTRY};
use async_std::{channel::Receiver, path::PathBuf};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{
collections::{HashMap, VecDeque},
iter::Iterator,
};
struct IDGenerator {
ids: Option<VecDeque<String>>,
}
impl IDGenerator {
fn new() -> Self {
Self { ids: None }
}
fn with_ids<T, D>(ids: T) -> Self
where
T: Into<Vec<D>>,
D: Into<String>,
{
let id_list = ids.into();
let mut data = VecDeque::new();
for id in id_list {
data.push_back(id.into());
}
Self { ids: Some(data) }
}
}
impl Iterator for IDGenerator {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
match &self.ids {
Some(id_list) => {
let mut ids = id_list.clone();
let output = ids.pop_front();
self.ids = Some(ids);
output
}
None => Some(thread_rng().sample_iter(&Alphanumeric).take(64).collect()),
}
}
}
#[cfg(test)]
mod genid {
use super::*;
#[test]
fn unique_ids() {
let mut gen = IDGenerator::new();
let mut output: Vec<String> = Vec::new();
for _ in 0..10 {
let id = gen.next().unwrap();
assert!(!output.contains(&id), "{} found in {:?}", id, output);
output.push(id);
}
}
#[test]
fn controlled_ids() {
let ids = ["one", "two", "three"];
let mut gen = IDGenerator::with_ids(ids.clone());
for id in ids {
assert_eq!(id, gen.next().unwrap());
}
}
}
pub struct Cache {
data: HashMap<String, FromCache>,
ids: IDGenerator,
}
impl Cache {
pub async fn new<P>(_dir: P) -> Self
where
P: Into<PathBuf>,
{
let mut data = HashMap::new();
data.insert(ENTRY.to_string(), FromCache::Str(Store::new()));
Self {
data: data,
ids: IDGenerator::new(),
}
}
async fn with_ids<P, T, D>(dir: P, ids: T) -> Self
where
P: Into<PathBuf>,
T: Into<Vec<D>>,
D: Into<String>,
{
let mut output = Self::new(dir).await;
output.ids = IDGenerator::with_ids(ids);
output
}
fn next_id(&mut self) -> String {
let mut id: String;
loop {
id = self.ids.next().unwrap();
match self.get(&id) {
FromCache::Error(_) => break,
_ => (),
}
}
id
}
pub async fn listen(&mut self, listener: Receiver<ToCache>) {
loop {
match listener.recv().await.unwrap() {
ToCache::Get(data) => {
data.result.send(self.get(data.data)).await.unwrap();
}
ToCache::Commit(data) => {
data.result.send(self.commit(data.data)).await.unwrap();
}
}
}
}
pub fn get<S>(&self, id: S) -> FromCache
where
S: Into<String>,
{
let idd = id.into();
match self.data.get(&idd) {
Some(data) => data.clone(),
None => FromCache::Error(MTTError::from_code(ErrorCode::IDNotFound(idd))),
}
}
pub fn commit(&mut self, data: Store) -> FromCache {
let entry_data = self.data.get(ENTRY).unwrap();
let mut store = match entry_data {
FromCache::Str(ep) => ep.clone(),
_ => {
unreachable!()
}
};
for name in data.list() {
let id = self.next_id();
match store.add_by_id(name, &id) {
Ok(_) => {
self.data.insert(id, FromCache::DB(Database::new()));
}
Err(err) => return FromCache::Error(err),
}
}
self.data
.insert(ENTRY.to_string(), FromCache::Str(store))
.unwrap();
FromCache::Ok
}
}
#[cfg(test)]
mod engine {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn get_entry() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let expected: Vec<String> = Vec::new();
let result = cache.get(ENTRY);
match result {
FromCache::Str(store) => assert_eq!(store.list(), expected),
_ => assert!(false, "{:?} should be FromCache::Str", result),
}
}
#[async_std::test]
async fn get_bad_entry() -> Result<(), MTTError> {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await;
let ids = ["bad1", "bad2"];
for id in ids {
let output = cache.get(id);
match output {
FromCache::Error(err) => match err.code {
ErrorCode::IDNotFound(_) => {
assert!(
err.to_string().contains(id),
"Had error: {}, Did not contain: {}",
err.to_string(),
id
);
}
_ => return Err(MTTError::new(format!("{:?} is not IDNotFound", err.code))),
},
_ => {
return Err(MTTError::new(format!(
"{:?} is not FromCache::Error",
output
)))
}
}
}
Ok(())
}
#[async_std::test]
async fn commit_database() {
// remove this one for the one below, maybe.
let dir = tempdir().unwrap();
let mut cache = Cache::new(dir.path()).await;
let mut store = Store::new();
let db = "garfield";
store.add(db).unwrap();
cache.commit(store.clone());
let output = cache.get(ENTRY);
match output {
FromCache::Str(result) => assert_eq!(result.list(), store.list()),
_ => assert!(false, "{:?} is not FromCache::Str", output),
}
}
#[async_std::test]
async fn add_database_entry() {
let id = "an_id";
let name = "garfield";
let dir = tempdir().unwrap();
let mut cache = Cache::with_ids(dir.path(), [id]).await;
let mut store = Store::new();
store.add(name).unwrap();
cache.commit(store.clone());
let db_out = cache.get(id);
match db_out {
FromCache::DB(_) => (),
_ => assert!(
false,
"{:?} is not FromCache::DB -- cache is {:?}",
db_out, cache.data
),
}
let store_out = cache.get(ENTRY);
match store_out {
FromCache::Str(updated_store) => match updated_store.get(name) {
Some(output) => {
assert_eq!(output.id, Some(id.to_string()));
assert!(output.data.is_none(), "Should have removed the database.");
}
None => assert!(true, "Store should have stored the database."),
},
_ => assert!(
false,
"{:?} is not FromCache::Str -- cache is {:?}",
db_out, cache.data
),
}
}
#[async_std::test]
async fn ids_are_not_overwritten() {
let ids = ["first", "first", "second"];
let names = ["barney", "fred"];
let dir = tempdir().unwrap();
let mut cache = Cache::with_ids(dir.path(), ids).await;
let mut store1 = Store::new();
store1.add(names[0]).unwrap();
let mut store2 = Store::new();
store2.add(names[1]).unwrap();
cache.commit(store1);
cache.commit(store2);
assert_eq!(
cache.data.len(),
3,
"cache.data had the following entries {:?}",
cache.data.keys()
);
}
#[async_std::test]
async fn no_duplicate_ids() {
let ids = ["one", "two"];
let dir = tempdir().unwrap();
let mut cache = Cache::with_ids(dir.path(), ids).await;
cache
.data
.insert(ids[0].to_string(), FromCache::DB(Database::new()));
assert_eq!(cache.next_id(), ids[1]);
}
}
#[cfg(test)]
mod messages {
use super::{
super::{start_db, ToCacheMsg},
*,
};
use async_std::channel::unbounded;
use tempfile::tempdir;
#[async_std::test]
async fn get_the_store() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
let (out_s, out_r) = unbounded();
let msg = ToCacheMsg {
data: ENTRY.to_string(),
result: out_s,
};
in_s.send(ToCache::Get(msg)).await.unwrap();
let result = out_r.recv().await.unwrap();
match result {
FromCache::Str(_) => (),
_ => assert!(false, "{:?} is not FromCache::Str", result),
}
}
#[async_std::test]
async fn get_bad_id() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
let in_s = mtt.to_cache.clone();
let (out_s, out_r) = unbounded();
let msg = ToCacheMsg {
data: "bad_id!".to_string(),
result: out_s,
};
in_s.send(ToCache::Get(msg)).await.unwrap();
let output = out_r.recv().await.unwrap();
match output {
FromCache::Error(_) => (),
_ => assert!(false, "{:?} is not FromCache::Error", output),
}
}
}

View File

@ -0,0 +1,36 @@
use super::{DBError, FileData, SessionData};
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();
output
}
fn from_bytes(_data: &mut slice::Iter<u8>) -> Result<Self, DBError> {
Ok(Self {})
}
}
impl SessionData for Database {
fn add(&mut self, _key: &str, _value: &str, _data: &str) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
fn eq(&self, _key: &str, _value: &str) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
fn list(&self, _keys: Vec<&str>) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
}

View File

@ -0,0 +1,171 @@
use super::{Data, ErrorCode, MTTError, Table};
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct Database {
data: HashMap<String, Data<Table>>,
}
impl Database {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn add<S>(&mut self, name: S) -> Result<(), MTTError>
where
S: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateTable(db_name))),
None => {
self.data.insert(db_name, Data::from_data(Table::new()));
Ok(())
}
}
}
pub fn add_by_id<S, D>(&mut self, name: S, id: D) -> Result<(), MTTError>
where
S: Into<String>,
D: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateTable(db_name))),
None => {
self.data.insert(db_name, Data::from_id(id.into()));
Ok(())
}
}
}
pub fn get(&self, name: &str) -> Option<&Data<Table>> {
self.data.get(name)
}
pub fn list(&self) -> Vec<String> {
let mut names = Vec::new();
for name in self.data.keys() {
names.push(name.to_string());
}
names.sort();
names
}
}
#[cfg(test)]
mod databases {
use super::*;
#[test]
fn create_new() {
let db = Database::new();
let expected: Vec<String> = Vec::new();
assert_eq!(db.list(), expected);
}
#[test]
fn add_db_by_str() {
let mut db = Database::new();
let name = "Melvin";
db.add(name).unwrap();
let output = db.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn add_db_by_string() {
let mut db = Database::new();
let name = "Marvin";
db.add(name.to_string()).unwrap();
let output = db.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn fail_on_duplicates() -> Result<(), MTTError> {
let mut db = Database::new();
let name = "Mickie";
db.add(name).unwrap();
match db.add(name) {
Ok(_) => Err(MTTError::new("duplicates should error")),
Err(err) => match err.code {
ErrorCode::DuplicateTable(db_name) => {
assert_eq!(db_name, name);
Ok(())
}
_ => Err(MTTError::new(format!("{:?} is not DuplicateTable", err))),
},
}
}
#[test]
fn add_using_cache_id() {
let mut db = Database::new();
let name = "fred";
let id = "12345";
db.add_by_id(name, id).unwrap();
let output = db.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn add_by_cache_id_name_string() {
let mut db = Database::new();
let name = "barney";
let id = "67890";
db.add_by_id(name.to_string(), id).unwrap();
let output = db.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn no_duplicate_databases_for_add_by_id() {
let mut db = Database::new();
let name = "betty";
db.add_by_id(name, "fghij").unwrap();
match db.add_by_id(name, "klmno") {
Ok(_) => assert!(false, "Duplicates should error."),
Err(err) => match err.code {
ErrorCode::DuplicateTable(db_name) => assert_eq!(db_name, name),
_ => assert!(false, "{:?} is not DuplicateTable", err),
},
}
}
#[test]
fn add_by_cache_id_string() {
let mut db = Database::new();
let name = "wilma";
let id = "abcdef";
db.add_by_id(name, id.to_string()).unwrap();
let output = db.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn get_bad_database() -> Result<(), MTTError> {
let db = Database::new();
match db.get("missing") {
Some(_) => Err(MTTError::new("Should have returned None.")),
None => Ok(()),
}
}
#[test]
fn get_list() {
let mut db = Database::new();
let mut ids = ["one", "two", "three", "four", "five"];
for name in ids {
db.add(name.to_string()).unwrap();
}
ids.sort();
assert_eq!(db.list(), ids);
}
}

381
src/morethantext/entry.rs Normal file
View File

@ -0,0 +1,381 @@
use super::{DBError, DataType, ErrorCode, FileData, SessionData};
use async_std::{
fs::{read, remove_file, write},
path::PathBuf,
};
use std::{
cell::Cell,
time::{Duration, Instant},
};
pub struct Entry {
data: DataType,
filename: PathBuf,
last_used: Cell<Instant>,
}
impl Entry {
pub async fn new<P>(filename: P, data: DataType) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = filename.into();
if pathbuf.as_path().exists().await {
return Err(DBError::from_code(ErrorCode::EntryExists(pathbuf)));
} else {
match write(&pathbuf, data.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryWriteFailure(pathbuf));
error.add_source(err);
return Err(error);
}
}
}
Ok(Self {
data: data,
filename: pathbuf,
last_used: Cell::new(Instant::now()),
})
}
pub async fn get<P>(filename: P) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = filename.into();
let content = match read(&pathbuf).await {
Ok(text) => text,
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryReadFailure(pathbuf));
error.add_source(err);
return Err(error);
}
};
let data = match DataType::from_bytes(&mut content.iter()) {
Ok(raw) => raw,
Err(err) => {
let mut error = DBError::from_code(ErrorCode::EntryReadFailure(pathbuf));
error.add_source(err);
return Err(error);
}
};
Ok(Self {
data: data,
filename: pathbuf,
last_used: Cell::new(Instant::now()),
})
}
pub fn elapsed(&self) -> Duration {
self.last_used.get().elapsed()
}
pub fn data(&self) -> DataType {
self.last_used.set(Instant::now());
self.data.clone()
}
async fn update(&mut self, data: DataType) -> Result<(), DBError> {
self.last_used.set(Instant::now());
match write(&self.filename, data.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error =
DBError::from_code(ErrorCode::EntryWriteFailure(self.filename.clone()));
error.add_source(err);
return Err(error);
}
};
self.data = data;
Ok(())
}
async fn remove(&self) -> Result<(), DBError> {
match remove_file(&self.filename).await {
Ok(_) => Ok(()),
Err(err) => {
let mut error =
DBError::from_code(ErrorCode::EntryDeleteFailure(self.filename.clone()));
error.add_source(err);
Err(error)
}
}
}
}
#[cfg(test)]
mod entry {
use super::*;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn get_elapsed_time() {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("count");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data).await.unwrap();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been now."
);
item.last_used
.set(Instant::now() - Duration::from_secs(500));
assert!(
Duration::from_secs(499) < item.elapsed(),
"The duration should have increased."
);
}
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
let mut data = DataType::new("store").unwrap();
data.add("database", "roger", "moore").unwrap();
let filepath = dir.path().join("wiliam");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been now."
);
let output = item.data();
assert_eq!(
data.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
assert!(filepath.is_file(), "Should have created the entry file.");
let content = read(&filepath).await.unwrap();
assert_eq!(content, data.to_bytes());
}
#[async_std::test]
async fn create_errors_on_bad_files() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("bad").join("path");
let filename = filepath.to_str().unwrap();
match Entry::new(filename.to_string(), data).await {
Ok(_) => Err(DBError::new("bad file names should raise an error")),
Err(err) => match err.code {
ErrorCode::EntryWriteFailure(_) => {
assert!(err.source().is_some(), "Must include the source error.");
assert!(err
.source()
.unwrap()
.to_string()
.contains("could not write to file"));
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn create_does_not_over_writes() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let id = "wicked";
let file = dir.path().join(id);
let filename = file.to_str().unwrap();
write(&file, b"previous").await.unwrap();
let data = DataType::new("store").unwrap();
match Entry::new(filename.to_string(), data).await {
Ok(_) => {
return Err(DBError::new(
"Should produce an error for an existing Entry",
))
}
Err(err) => match err.code {
ErrorCode::EntryExists(_) => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn get_updates_last_used() {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("holder");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data).await.unwrap();
item.last_used
.set(Instant::now() - Duration::from_secs(300));
item.data();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been reset."
);
}
#[async_std::test]
async fn update_entry() {
let dir = tempdir().unwrap();
let mut data = DataType::new("store").unwrap();
let filepath = dir.path().join("changing");
let filename = filepath.to_str().unwrap();
let mut item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
item.last_used
.set(Instant::now() - Duration::from_secs(500));
data.add("database", "new", "stuff").unwrap();
item.update(data.clone()).await.unwrap();
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been reset."
);
let output = item.data();
assert_eq!(
data.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
let content = read(&filepath).await.unwrap();
assert_eq!(content, data.to_bytes());
}
#[async_std::test]
async fn update_write_errors() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
let filepath = dir.path().join("changing");
let filename = filepath.to_str().unwrap();
let mut item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
drop(dir);
match item.update(data).await {
Ok(_) => Err(DBError::new("file writes should return an error")),
Err(err) => match err.code {
ErrorCode::EntryWriteFailure(_) => {
assert!(err.source().is_some(), "Must include the source error.");
assert!(err
.source()
.unwrap()
.to_string()
.contains("could not write to file"));
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn retrieve() {
let dir = tempdir().unwrap();
let mut data = DataType::new("store").unwrap();
data.add("database", "something_old", "3.14159").unwrap();
let filepath = dir.path().join("existing");
let filename = filepath.to_str().unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
let output = Entry::get(filename).await.unwrap();
assert_eq!(
output.data().list(["database"].to_vec()).unwrap(),
data.list(["database"].to_vec()).unwrap()
);
assert_eq!(output.filename.to_str().unwrap(), filename);
assert!(
Duration::from_secs(1) > item.elapsed(),
"last_used should have been reset."
);
}
#[async_std::test]
async fn retrieve_file_missing() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let filepath = dir.path().join("justnotthere");
let filename = filepath.to_str().unwrap();
match Entry::get(filename).await {
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::EntryReadFailure(_) => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source()
.unwrap()
.to_string()
.contains("could not read file"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn retrieve_corrupt_file() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let filepath = dir.path().join("garbage");
let filename = filepath.to_str().unwrap();
write(&filepath, b"jhsdfghlsdf").await.unwrap();
match Entry::get(filename).await {
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::EntryReadFailure(_) => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source().unwrap().to_string().contains("corrupt file"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn delete() {
let dir = tempdir().unwrap();
let filepath = dir.path().join("byebye");
let filename = filepath.to_str().unwrap();
let data = DataType::new("store").unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
item.remove().await.unwrap();
assert!(!filepath.exists(), "Entry file should be removed.");
}
#[async_std::test]
async fn delete_bad_file() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let filepath = dir.path().join("itsnotthere");
let filename = filepath.to_str().unwrap();
let data = DataType::new("store").unwrap();
let item = Entry::new(filename.to_string(), data.clone())
.await
.unwrap();
remove_file(filename).await.unwrap();
match item.remove().await {
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::EntryDeleteFailure(_) => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source()
.unwrap()
.to_string()
.contains("could not remove file"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
}

View File

@ -1,90 +1,135 @@
use std::{error::Error, fmt};
#[derive(Debug)]
pub struct DBError {
msg: String,
src: Option<Box<dyn Error + 'static>>,
#[derive(Clone, Debug)]
pub enum ErrorCode {
// General
Undefined(String),
// Cache
IDNotFound(String),
// Store
DuplicateDatabase(String),
// Database
DuplicateTable(String),
}
impl DBError {
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
ErrorCode::IDNotFound(id) => write!(f, "ID '{}' not found", id),
ErrorCode::DuplicateDatabase(name) => write!(f, "database '{}' already exists", name),
ErrorCode::DuplicateTable(name) => write!(f, "table '{}' already exists", name),
}
}
}
mod errorcodes {
use super::*;
const ITEMS: [&str; 2] = ["one", "two"];
#[test]
fn undefined_display() {
for item in ITEMS {
let err = ErrorCode::Undefined(item.to_string());
assert_eq!(err.to_string(), item);
}
}
#[test]
fn cache_missing() {
for item in ITEMS {
let err = ErrorCode::IDNotFound(item.to_string());
assert_eq!(err.to_string(), format!("ID '{}' not found", item));
}
}
#[test]
fn duplicate_database() {
for item in ITEMS {
let err = ErrorCode::DuplicateDatabase(item.to_string());
assert_eq!(
err.to_string(),
format!("database '{}' already exists", item)
);
}
}
#[test]
fn duplicate_table() {
for item in ITEMS {
let err = ErrorCode::DuplicateTable(item.to_string());
assert_eq!(err.to_string(), format!("table '{}' already exists", item));
}
}
}
#[derive(Clone, Debug)]
pub struct MTTError {
pub code: ErrorCode,
}
impl MTTError {
pub fn new<S>(msg: S) -> Self
where
S: Into<String>,
{
let text = msg.into();
Self {
msg: msg.into(),
src: None,
code: ErrorCode::Undefined(text),
}
}
pub fn add_source<E>(&mut self, src: E)
where
E: Error + 'static,
{
self.src = Some(Box::new(src));
pub fn from_code(code: ErrorCode) -> Self {
Self { code: code }
}
}
impl Error for DBError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.src {
Some(err) => Some(err.as_ref()),
None => None,
}
}
}
impl Error for MTTError {}
impl fmt::Display for DBError {
impl fmt::Display for MTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
write!(f, "{}", self.code)
}
}
#[cfg(test)]
mod create {
mod errors {
use super::*;
#[test]
fn with_str() {
let msg = "something happened";
let err = DBError::new(msg);
assert!(
err.to_string() == msg,
"Got: {} -- Want: {}",
err.to_string(),
msg
);
assert!(
err.source().is_none(),
"Error should initialize with no source."
);
fn create_with_str() {
let msgs = ["one", "two"];
for msg in msgs {
let err = MTTError::new(msg);
assert_eq!(err.to_string(), msg);
}
}
#[test]
fn with_string() {
let msg = "it went boom".to_string();
let err = DBError::new(msg.clone());
assert!(
err.to_string() == msg,
"Got: {} -- Want: {}",
err.to_string(),
msg
);
assert!(
err.source().is_none(),
"Error should initialize with no source."
);
fn create_with_string() {
let msg = "three";
let err = MTTError::new(msg.to_string());
assert_eq!(err.to_string(), msg);
}
#[test]
fn with_source() {
let msg = "but this caused the problem";
let mut par = DBError::new("parent error");
let src = DBError::new(msg);
par.add_source(src);
let output = par.source();
assert!(output.is_some(), "Should return source.");
let source = output.unwrap();
assert!(source.to_string() == msg);
fn create_from_code() {
let code = ErrorCode::Undefined("oops".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::Undefined(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
#[test]
fn create_missing_id_from_code() {
let code = ErrorCode::IDNotFound("123".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::IDNotFound(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
}

402
src/morethantext/mod-2.rs Normal file
View File

@ -0,0 +1,402 @@
mod database;
mod entry;
mod error;
mod store;
use async_std::path::PathBuf;
use database::Database;
use entry::Entry;
use error::{DBError, ErrorCode};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{slice, str};
use store::Store;
const ENTRY: &str = "EntryPoint";
trait ID {
fn next(&self) -> String;
}
trait FileData<F> {
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>;
}
trait SessionData {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>;
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>;
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
}
#[derive(Clone)]
pub enum DataType {
DBMap(Store),
TableMap(Database),
}
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(),
))),
}
}
}
impl SessionData for DataType {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError> {
match self {
DataType::DBMap(dbs) => dbs.add(key, value, data),
DataType::TableMap(_) => todo!(),
}
}
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError> {
match self {
DataType::DBMap(dbs) => dbs.eq(key, value),
DataType::TableMap(_) => todo!(),
}
}
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> {
match self {
DataType::DBMap(dbs) => dbs.list(keys),
DataType::TableMap(_) => todo!(),
}
}
}
impl FileData<Self> for DataType {
fn to_bytes(&self) -> Vec<u8> {
let mut output = Vec::new();
match self {
DataType::DBMap(_) => output.append(&mut "DBMap".as_bytes().to_vec()),
DataType::TableMap(_) => output.append(&mut "TableMap".as_bytes().to_vec()),
}
output.push(0);
match self {
DataType::DBMap(store) => output.append(&mut store.to_bytes()),
DataType::TableMap(_) => (),
}
output
}
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<Self, DBError> {
let mut header: Vec<u8> = Vec::new();
loop {
let letter = match data.next() {
Some(a) => a.clone(),
None => 0,
};
if letter == 0 {
break;
} else {
header.push(letter);
}
}
let header = match str::from_utf8(&header) {
Ok(item) => item,
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
match header {
"DBMap" => match Store::from_bytes(data) {
Ok(store) => Ok(DataType::DBMap(store)),
Err(err) => Err(err),
},
"TableMap" => Ok(DataType::new("database").unwrap()),
_ => Err(DBError::from_code(ErrorCode::CorruptFile)),
}
}
}
#[derive(Clone)]
pub struct MoreThanText {
next_id: &dyn Fn() -> String,
session: Vec<String>,
}
impl MoreThanText {
pub async fn new<P>(dir: P) -> Result<Self, DBError>
where
P: Into<PathBuf>,
{
let pathbuf = dir.into();
let entry = pathbuf.as_path().join(ENTRY);
match Entry::get(entry.clone()).await {
Ok(_) => (),
Err(_) => {
let store = DataType::new("store").unwrap();
match Entry::new(entry, store).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::CacheReadWrite);
error.add_source(err);
return Err(error);
}
}
}
}
Ok(Self {
next_id: fn id() -> String { thread_rng().sample_iter(&Alphanumeric).take(64).collect() },
session: [ENTRY.to_string()].to_vec(),
})
}
fn set_session(&mut self, sess: Vec<String>) {
self.session = sess;
}
async fn new_entry(&self, _name: &str) -> Self {
self.clone()
}
}
#[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 update_storage() {
let mut dbs = DataType::new("store").unwrap();
let name = "new_database";
let id = "someid";
dbs.add("database", name, id).unwrap();
assert_eq!(dbs.eq("database", name).unwrap(), [id].to_vec());
assert_eq!(dbs.list(["database"].to_vec()).unwrap(), [name].to_vec());
}
}
#[cfg(test)]
mod datatype_file {
use super::*;
#[test]
fn new_store_bytes() {
let dbs = DataType::new("store").unwrap();
let mut expected = "DBMap".as_bytes().to_vec();
expected.push(0);
assert_eq!(dbs.to_bytes(), expected);
}
#[test]
fn store_bytes_with_info() {
let name = "title";
let id = "king";
let mut store = Store::new();
let mut dt_store = DataType::new("store").unwrap();
let mut expected = dt_store.to_bytes();
store.add("database", name, id).unwrap();
expected.append(&mut store.to_bytes());
dt_store.add("database", name, id).unwrap();
assert_eq!(dt_store.to_bytes(), expected);
}
#[test]
fn read_empty_store() {
let dt_store = DataType::new("store").unwrap();
let data = dt_store.to_bytes();
let mut feed = data.iter();
let output = DataType::from_bytes(&mut feed).unwrap();
assert_eq!(
dt_store.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
}
#[test]
fn read_store_info() {
let mut dt_store = DataType::new("store").unwrap();
dt_store.add("database", "raven", "beastboy").unwrap();
let data = dt_store.to_bytes();
let mut feed = data.iter();
let output = DataType::from_bytes(&mut feed).unwrap();
assert_eq!(
dt_store.list(["database"].to_vec()).unwrap(),
output.list(["database"].to_vec()).unwrap()
);
}
#[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();
let mut feed = data.iter();
match DataType::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have raised an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error")),
},
}
}
#[test]
fn read_bad_store() -> Result<(), DBError> {
let mut data = "DBMap".as_bytes().to_vec();
data.push(0);
data.append(&mut "sdfgs".as_bytes().to_vec());
let mut feed = data.iter();
match DataType::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have raised an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
}
#[cfg(test)]
mod db {
use super::*;
use async_std::fs::write;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
let mtt = MoreThanText::new(dir.path()).await.unwrap();
let epoint = dir.path().join(ENTRY);
assert!(
epoint.is_file(),
"{} did not get created.",
epoint.display()
);
let entry = Entry::get(epoint.to_str().unwrap()).await.unwrap();
assert_eq!(
entry.data().list(["database"].to_vec()).unwrap(),
Vec::<String>::new()
);
let sess = [ENTRY];
assert_eq!(mtt.session, sess);
}
#[async_std::test]
async fn entry_failure() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let path = dir.path().join("bad").join("path");
match MoreThanText::new(path).await {
Ok(_) => Err(DBError::new("Should have produced an error.")),
Err(err) => match err.code {
ErrorCode::CacheReadWrite => {
assert!(err.source().is_some(), "Error should have a source.");
assert!(
err.source().unwrap().to_string().contains("write failure"),
"Source Error Message: {}",
err.source().unwrap().to_string()
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[async_std::test]
async fn existing_entry_point() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let data = DataType::new("store").unwrap();
Entry::new(dir.path().join(ENTRY), data.clone())
.await
.unwrap();
match MoreThanText::new(dir.path()).await {
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}
#[async_std::test]
async fn corrupt_enty_point() -> Result<(), DBError> {
let dir = tempdir().unwrap();
let file = dir.path().join(ENTRY);
write(file, b"Really bad data.").await.unwrap();
match MoreThanText::new(dir.path()).await {
Ok(_) => Err(DBError::new("should have errored")),
Err(_) => Ok(()),
}
}
#[async_std::test]
async fn set_session() {
let dir = tempdir().unwrap();
let mut mtt = MoreThanText::new(dir.path()).await.unwrap();
let sess = ["different".to_string()];
mtt.set_session(sess.to_vec());
assert_eq!(mtt.session, sess);
}
#[async_std::test]
async fn add_a_database() {
let dir = tempdir().unwrap();
let mtt = MoreThanText::new(dir.path()).await.unwrap();
mtt.new_entry("wilbur").await;
}
}

640
src/morethantext/mod-3.rs Normal file
View File

@ -0,0 +1,640 @@
use async_std::{
channel::{unbounded, Receiver, Sender},
path::PathBuf,
task::spawn,
};
use std::{collections::HashMap, error::Error, fmt};
const ENTRY: &str = "EntryPoint";
#[derive(Debug)]
enum ErrorCode {
// General
Undefined(String),
// Cache
EntryNotFound(String),
InvalidCommitData,
// Store
DatabaseAlreadyExists(String),
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ErrorCode::Undefined(msg) => write!(f, "{}", msg),
ErrorCode::EntryNotFound(id) => write!(f, "entry '{}' was not found", id),
ErrorCode::InvalidCommitData => write!(f, "commit data was not a database store"),
ErrorCode::DatabaseAlreadyExists(name) => {
write!(f, "database '{}' already exists", name)
}
}
}
}
mod errorcodes {
use super::*;
const ITEMS: [&str; 2] = ["one", "two"];
#[test]
fn undefined_display() {
for item in ITEMS {
let err = ErrorCode::Undefined(item.to_string());
assert_eq!(err.to_string(), item);
}
}
#[test]
fn bad_entry() {
for item in ITEMS {
let err = ErrorCode::EntryNotFound(item.to_string());
assert_eq!(err.to_string(), format!("entry '{}' was not found", item));
}
}
#[test]
fn invalid_commit_data() {
let err = ErrorCode::InvalidCommitData;
assert_eq!(err.to_string(), "commit data was not a database store");
}
#[test]
fn database_already_exists() {
for item in ITEMS {
let err = ErrorCode::DatabaseAlreadyExists(item.to_string());
assert_eq!(
err.to_string(),
format!("database '{}' already exists", item)
);
}
}
}
#[derive(Debug)]
pub struct MTTError {
code: ErrorCode,
}
impl MTTError {
fn new<S>(msg: S) -> Self
where
S: Into<String>,
{
let text = msg.into();
Self {
code: ErrorCode::Undefined(text),
}
}
fn from_code(code: ErrorCode) -> Self {
Self { code: code }
}
}
impl Error for MTTError {}
impl fmt::Display for MTTError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.code)
}
}
#[cfg(test)]
mod errors {
use super::*;
#[test]
fn create_with_str() {
let msgs = ["one", "two"];
for msg in msgs {
let err = MTTError::new(msg);
assert_eq!(err.to_string(), msg);
}
}
#[test]
fn create_with_string() {
let msg = "three";
let err = MTTError::new(msg.to_string());
assert_eq!(err.to_string(), msg);
}
#[test]
fn create_from_code() {
let code = ErrorCode::Undefined("oops".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::Undefined(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
#[test]
fn create_missing_entry() {
let code = ErrorCode::EntryNotFound("an_id".to_string());
let err = MTTError::from_code(code);
match err.code {
ErrorCode::EntryNotFound(_) => (),
_ => assert!(false, "{:?} is not undefined", err.code),
}
}
}
#[derive(Clone, Debug)]
struct Storage<D> {
id: Option<String>,
data: Option<D>,
// delete: bool,
}
impl Storage<D> {
fn from_id<S>(id: S) -> Self
where
S: Into<String>,
{
Self {
id: Some(id.into()),
data: None,
}
}
fn from_datatype(dt: DataType) -> Self {
Self {
id: None,
data: Some(dt),
}
}
}
#[cfg(test)]
mod storage {
use super::*;
#[test]
fn from_id_with_str() {
let ids = ["first", "second"];
for id in ids {
let output = Storage::from_id(id);
assert_eq!(output.id, Some(id.to_string()));
assert!(
output.data.is_none(),
"The storage data should have been Non."
);
}
}
#[test]
fn from_id_with_string() {
let id = "my_id".to_string();
let output = Storage::from_id(id.clone());
assert_eq!(output.id, Some(id));
}
#[test]
fn from_store() {
let output = Storage::from_datatype(DataType::new("store"));
assert!(output.id.is_none(), "id should be None.");
assert!(output.data.is_some(), "There should be data");
let result = output.data.unwrap();
match result {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} should have been DataType::DBMap.", result),
}
}
#[test]
fn from_database() {
let output = Storage::from_datatype(DataType::new("database"));
let result = output.data.unwrap();
match result {
DataType::TableMap(_) => (),
_ => assert!(false, "{:?} should have been DataType::TableMap.", result),
}
}
}
#[derive(Clone, Debug)]
struct Store {
data: HashMap<String, Storage<Database>>,
}
impl Store {
fn new() -> Self {
Self {
data: HashMap::new(),
}
}
fn add_new<S>(&mut self, name: S) -> Result<(), MTTError>
where
S: Into<String>,
{
let dbname = name.into();
match self.get(&dbname) {
Some(_) => Err(MTTError::from_code(ErrorCode::DatabaseAlreadyExists(
dbname,
))),
None => {
self.data
.insert(dbname, Storage::from_datatype(DataType::new("database")));
Ok(())
}
}
}
fn get(&self, name: &str) -> Option<&Storage<Database>> {
self.data.get(name)
}
}
#[cfg(test)]
mod stores {
use super::*;
#[test]
fn get_no_database() -> Result<(), MTTError> {
let store = Store::new();
match store.get("missing_name") {
Some(_) => Err(MTTError::new("should have returned None")),
None => Ok(()),
}
}
#[test]
fn add_database_str() {
let mut store = Store::new();
let names = ["first", "second"];
for name in names {
store.add_new(name).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_some(), "There should be a data type.");
match output.data.clone().unwrap() {
DataType::TableMap(_) => (),
_ => assert!(
false,
"{:?} should have been DataType::TableMap.",
output.data
),
}
assert!(output.id.is_none(), "Should not have an id.");
}
}
#[test]
fn add_database_string() {
let mut store = Store::new();
let name = "third".to_string();
store.add_new(name.clone()).unwrap();
let output = store.get(&name).unwrap();
match output.data.clone().unwrap() {
DataType::TableMap(_) => (),
_ => assert!(
false,
"{:?} should have been DataType::TableMap.",
output.data
),
}
}
#[test]
fn no_duplicate_database_names() -> Result<(), MTTError> {
let mut store = Store::new();
let name = "duplicate";
store.add_new(name).unwrap();
match store.add_new(name) {
Ok(_) => Err(MTTError::new("should have been an error")),
Err(err) => match err.code {
ErrorCode::DatabaseAlreadyExists(dbname) => {
assert_eq!(dbname, name);
Ok(())
}
_ => Err(MTTError::new(format!(
"{:?} should have been DatabaseAlreadyExists.",
err.code
))),
},
}
}
}
#[derive(Clone, Debug)]
struct Database;
#[cfg(test)]
mod databases {
use super::*;
#[test]
fn create() {
Database::new();
}
}
impl Database {
fn new() -> Self {
Self {}
}
}
#[derive(Clone, Debug)]
enum DataType {
DBMap(Store),
TableMap(Database),
}
impl DataType {
fn new(dtype: &str) -> DataType {
match dtype {
"store" => Self::DBMap(Store::new()),
"database" => Self::TableMap(Database::new()),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod datatypes {
use super::*;
#[test]
fn create_store() {
let dtype = DataType::new("store");
match dtype {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} is not incorrect data type", dtype),
}
}
#[test]
fn create_database() {
let dtype = DataType::new("database");
match dtype {
DataType::TableMap(_) => (),
_ => assert!(false, "{:?} is not incorrect data type", dtype),
}
}
}
#[derive(Debug)]
enum FromCache {
Ok,
Data(HashMap<String, DataType>),
Error(MTTError),
}
struct CacheQuery {
ids: Vec<String>,
reply: Sender<FromCache>,
}
struct CacheCommit {
reply: Sender<FromCache>,
data: DataType,
}
impl CacheCommit {
fn new(data: DataType, channel: Sender<FromCache>) -> Result<Self, MTTError> {
match data {
DataType::DBMap(_) => (),
_ => return Err(MTTError::from_code(ErrorCode::InvalidCommitData)),
}
Ok(Self {
data: data,
reply: channel,
})
}
}
mod commits {
use super::*;
#[test]
fn create() -> Result<(), MTTError> {
let (s, _) = unbounded();
match CacheCommit::new(DataType::new("store"), s) {
Ok(output) => match output.data {
DataType::DBMap(_) => Ok(()),
_ => Err(MTTError::new(format!(
"{:?} should have been DBMap",
output.data
))),
},
Err(err) => Err(err),
}
}
#[test]
fn bad_data_type() -> Result<(), MTTError> {
let (s, _) = unbounded();
match CacheCommit::new(DataType::new("database"), s) {
Ok(_) => Err(MTTError::new("CacheCommit::new did not return error")),
Err(err) => match err.code {
ErrorCode::InvalidCommitData => Ok(()),
_ => Err(MTTError::new(format!(
"{:?} is not the correct error",
err.code
))),
},
}
}
}
enum ToCache {
Query(CacheQuery),
Commit(CacheCommit),
}
#[derive(Clone)]
pub struct MoreThanText {
session: Vec<String>,
cache: Sender<Vec<String>>,
}
impl MoreThanText {
async fn new(cache: Sender<Vec<String>>) -> Result<Self, MTTError> {
Ok(Self {
session: [ENTRY.to_string()].to_vec(),
cache: cache,
})
}
}
#[cfg(test)]
mod mtt {
use super::*;
#[async_std::test]
async fn create() {
let (s, _) = unbounded();
let mtt = MoreThanText::new(s).await.unwrap();
assert_eq!(mtt.session, [ENTRY]);
}
}
struct Cache;
impl Cache {
async fn new<P>(_dir: P) -> Result<Self, MTTError>
where
P: Into<PathBuf>,
{
Ok(Self {})
}
async fn query(&self, qry: &Vec<String>) -> Result<HashMap<String, DataType>, MTTError> {
let mut output = HashMap::new();
for id in qry {
if id == ENTRY {
output.insert(ENTRY.to_string(), DataType::new("store"));
} else {
return Err(MTTError::from_code(ErrorCode::EntryNotFound(
id.to_string(),
)));
}
}
Ok(output)
}
async fn commit(&self) -> Result<(), MTTError> {
Ok(())
}
async fn start(&self, listener: Receiver<ToCache>) {
loop {
match listener.recv().await.unwrap() {
ToCache::Query(qry) => match self.query(&qry.ids).await {
Ok(data) => qry.reply.send(FromCache::Data(data)).await.unwrap(),
Err(error) => qry.reply.send(FromCache::Error(error)).await.unwrap(),
},
ToCache::Commit(commit) => match self.commit().await {
Ok(_) => commit.reply.send(FromCache::Ok).await.unwrap(),
Err(error) => commit.reply.send(FromCache::Error(error)).await.unwrap(),
},
}
}
}
}
#[cfg(test)]
mod caches {
use super::*;
use tempfile::tempdir;
async fn start_cache<P>(dir: P) -> Sender<ToCache>
where
P: Into<PathBuf>,
{
let (s, r) = unbounded();
let datadir = dir.into();
spawn(async move {
let cache = Cache::new(datadir).await.unwrap();
cache.start(r).await;
});
s
}
async fn send_request(data: Vec<&str>, channel: Sender<ToCache>) -> FromCache {
let mut ids = Vec::new();
for id in data.iter() {
ids.push(id.to_string());
}
let (s, r) = unbounded();
let msg = ToCache::Query(CacheQuery { ids: ids, reply: s });
channel.send(msg).await.unwrap();
r.recv().await.unwrap()
}
#[async_std::test]
async fn create() {
let dir = tempdir().unwrap();
let s_cache = start_cache(dir.path()).await;
let result = send_request(vec![ENTRY], s_cache).await;
match result {
FromCache::Data(data) => match data.get(ENTRY) {
Some(output) => match output {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} is not a database store.", output),
},
None => assert!(false, "Should contain entry point."),
},
_ => assert!(false, "{:?} should have been a store.", result),
}
}
#[async_std::test]
async fn bad_entry() {
let dir = tempdir().unwrap();
let s_cache = start_cache(dir.path()).await;
let result = send_request(vec!["bad_id"], s_cache).await;
match result {
FromCache::Error(_) => (),
_ => assert!(false, "{:?} should have been an error.", result),
}
}
#[async_std::test]
async fn empty_commit() {
let dir = tempdir().unwrap();
let s_cache = start_cache(dir.path()).await;
let (s, r) = unbounded();
let msg = ToCache::Commit(CacheCommit::new(DataType::new("store"), s).unwrap());
s_cache.send(msg).await.unwrap();
let result = r.recv().await.unwrap();
match result {
FromCache::Ok => (),
_ => assert!(false, "{:?} should have been an Ok.", result),
}
}
#[async_std::test]
async fn get_store() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await.unwrap();
let output = cache.query(&[ENTRY.to_string()].to_vec()).await.unwrap();
let result = output.get(ENTRY).unwrap();
match result {
DataType::DBMap(_) => (),
_ => assert!(false, "{:?} should have been an Ok.", result),
}
}
#[async_std::test]
async fn bad_get() {
let dir = tempdir().unwrap();
let cache = Cache::new(dir.path()).await.unwrap();
let bad_id = "really_bad_id";
match cache.query(&[bad_id.to_string()].to_vec()).await {
Ok(_) => assert!(false, "Should have produced an error."),
Err(err) => match err.code {
ErrorCode::EntryNotFound(id) => assert_eq!(id, bad_id),
_ => assert!(false, "{:?} should have been EntryNotFound.", err.code),
},
}
}
}
pub async fn start_db<P>(_dir: P) -> Result<MoreThanText, MTTError>
where
P: Into<PathBuf>,
{
let (s, r) = unbounded();
spawn(async move {
loop {
r.recv().await.unwrap();
}
});
Ok(MoreThanText::new(s).await.unwrap())
}
#[cfg(test)]
mod db_start_up {
use super::*;
use tempfile::tempdir;
#[async_std::test]
async fn initial_session() {
let dir = tempdir().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.session, [ENTRY]);
}
}

823
src/morethantext/mod-old.rs Normal file
View File

@ -0,0 +1,823 @@
mod cache;
mod database;
pub mod error;
mod store;
use async_std::{
fs::{create_dir, read, remove_file, write},
path::Path,
sync::{Arc, Mutex},
task::{sleep, spawn},
};
use database::Database;
use error::{DBError, ErrorCode};
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{
collections::HashMap,
fmt, slice, str,
time::{Duration, Instant},
};
use store::Store;
const DATA: &str = "data";
const ENTRY: &str = "databases";
trait FileData<F> {
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<F, DBError>;
}
trait SessionData {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError>;
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError>;
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError>;
}
#[derive(Clone)]
pub enum CacheType {
Raw(String),
DBMap(Store),
TableMap,
}
impl CacheType {
pub fn entry_type(&self) -> String {
match self {
CacheType::Raw(_) => "Raw".to_string(),
CacheType::DBMap(_) => "DBMap".to_string(),
CacheType::TableMap => "TableMap".to_string(),
}
}
pub fn to_bytes(&self) -> Vec<u8> {
let mut output = self.entry_type().into_bytes();
output.push(0);
match self {
CacheType::Raw(s) => output.append(&mut s.as_bytes().to_vec()),
CacheType::DBMap(_) => (),
CacheType::TableMap => (),
}
return output;
}
pub fn from_bytes(data: Vec<u8>) -> Result<CacheType, DBError> {
let mut data_iter = data.iter();
let mut letter: u8;
match data_iter.next() {
Some(item) => letter = *item,
None => return Err(DBError::new("empty file")),
}
let mut header: Vec<u8> = Vec::new();
while letter != 0 {
header.push(letter.clone());
match data_iter.next() {
Some(item) => letter = *item,
None => return Err(DBError::new("incomplete file")),
}
}
let header = str::from_utf8(&header).unwrap().to_string();
match header.as_str() {
"Raw" => {
let mut output: Vec<u8> = Vec::new();
for letter in data_iter {
output.push(letter.clone());
}
Ok(CacheType::Raw(str::from_utf8(&output).unwrap().to_string()))
}
"DBMap" => Ok(CacheType::DBMap(Store::new())),
"TableMap" => Ok(CacheType::TableMap),
_ => Err(DBError::new("data corruption")),
}
}
}
impl fmt::Display for CacheType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CacheType::Raw(s) => write!(f, "{}", s),
CacheType::DBMap(_) => todo!(),
CacheType::TableMap => todo!(),
}
}
}
#[derive(Clone)]
struct CacheEntry {
data: CacheType,
last_used: Instant,
}
impl CacheEntry {
fn new(data: CacheType) -> Self {
Self {
data: data,
last_used: Instant::now(),
}
}
fn elapsed(&self) -> Duration {
self.last_used.elapsed()
}
fn touch(&mut self) {
self.last_used = Instant::now();
}
fn update(&mut self, data: CacheType) {
self.data = data;
self.touch();
}
}
impl fmt::Display for CacheEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.data)
}
}
#[derive(Clone)]
pub struct MoreThanText {
cache: Arc<Mutex<HashMap<String, CacheEntry>>>,
dir: String,
session: Vec<String>,
}
impl MoreThanText {
pub async fn new(dir: &str) -> Result<Self, DBError> {
let data_dir = Path::new(dir).join(DATA);
if !data_dir.is_dir().await {
match create_dir(&data_dir).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("failed to create data directory");
error.add_source(err);
return Err(error);
}
}
}
let mut output = Self {
cache: Arc::new(Mutex::new(HashMap::new())),
dir: data_dir.to_str().unwrap().to_string(),
session: Vec::new(),
};
let entry_file = Path::new(dir).join(ENTRY);
let id: String;
if entry_file.is_file().await {
let holder = read(entry_file).await.unwrap();
id = str::from_utf8(&holder).unwrap().to_string();
} else {
id = output
.add_entry(CacheType::DBMap(Store::new()))
.await
.unwrap();
write(entry_file, id.as_bytes()).await.unwrap();
}
output.session.push(id);
let looper = output.cache.clone();
spawn(async move {
let hold_time = Duration::from_secs(300);
loop {
sleep(Duration::from_secs(1)).await;
let mut ids: Vec<String> = Vec::new();
let mut cache = looper.lock().await;
for (id, entry) in cache.iter() {
if entry.elapsed() > hold_time {
ids.push(id.to_string());
}
}
for id in ids.iter() {
cache.remove(id);
}
}
});
Ok(output)
}
fn filename(&self, id: &str) -> String {
let filename = Path::new(&self.dir).join(&id);
filename.into_os_string().into_string().unwrap()
}
fn new_id(&self) -> String {
thread_rng().sample_iter(&Alphanumeric).take(64).collect()
}
async fn add(&self, feature: &str, key: &str, value: &str) -> Self {
let mut ids: Vec<String> = Vec::new();
for id in self.session.clone().into_iter() {
let holder = self.get_entry(&id).await.unwrap();
//holder.add(feature, key, value);
}
let mut output = self.clone();
output.session.clear();
output.session.push(value.to_string());
output
}
async fn list(&self, feature: Vec<&str>) -> Result<Vec<String>, DBError> {
Ok(Vec::new())
}
async fn add_entry(&self, entry: CacheType) -> Result<String, DBError> {
let mut id: String = "".to_string();
let mut dup = true;
while dup {
id = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
dup = Path::new(&self.dir).join(&id).as_path().exists().await;
}
match write(Path::new(&self.filename(&id)), entry.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("data write");
error.add_source(err);
return Err(error);
}
};
let mut cache = self.cache.lock().await;
let data = CacheEntry::new(entry);
cache.insert(id.clone(), data);
Ok(id)
}
async fn get_entry(&self, id: &str) -> Result<CacheEntry, DBError> {
let mut cache = self.cache.lock().await;
match cache.get_mut(id) {
Some(entry) => {
entry.touch();
Ok(entry.clone())
}
None => match read(Path::new(&self.filename(id))).await {
Ok(content) => {
let data = CacheEntry::new(CacheType::from_bytes(content).unwrap());
cache.insert(id.to_string(), data.clone());
Ok(data)
}
Err(_) => Err(DBError::new("cache entry not found")),
},
}
}
async fn update_entry(&self, id: &str, entry: CacheType) -> Result<(), DBError> {
match self.get_entry(id).await {
Ok(_) => (),
Err(err) => return Err(err),
}
match write(Path::new(&self.filename(id)), entry.to_bytes()).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("data write");
error.add_source(err);
return Err(error);
}
}
let mut cache = self.cache.lock().await;
let data = CacheEntry::new(entry);
cache.insert(id.to_string(), data);
Ok(())
}
async fn delete_entry(&self, id: &str) -> Result<(), DBError> {
let mut cache = self.cache.lock().await;
cache.remove(id);
match remove_file(Path::new(&self.filename(id))).await {
Ok(_) => Ok(()),
Err(err) => {
let mut error = DBError::new("data delete");
error.add_source(err);
Err(error)
}
}
}
}
#[cfg(test)]
mod setup {
use super::*;
use async_std::fs::remove_dir_all;
use tempfile::{tempdir, TempDir};
pub struct MTT {
pub db: MoreThanText,
pub dir: TempDir,
}
impl MTT {
pub async fn new() -> Self {
let dir = tempdir().unwrap();
let db = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
Self { db: db, dir: dir }
}
pub async fn create_io_error(&self) {
remove_dir_all(self.dir.path().join(DATA)).await.unwrap();
}
}
}
#[cfg(test)]
mod init {
use super::*;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn create_data_dir() {
let dir = tempdir().unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let data_dir = dir.path().join(DATA);
assert!(data_dir.is_dir(), "Did not create the data directory.");
dir.close().unwrap();
}
#[async_std::test]
async fn existing_data_dir() {
let dir = tempdir().unwrap();
let data_dir = dir.path().join(DATA);
create_dir(data_dir).await.unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
dir.close().unwrap();
}
#[async_std::test]
async fn bad_data_dir() {
let msg = "could not create directory";
match MoreThanText::new("kljsdgfhslkfrh").await {
Ok(_) => assert!(false, "This test should fail to create a data directory"),
Err(err) => {
assert_eq!(err.to_string(), "failed to create data directory");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
};
}
#[async_std::test]
async fn creates_entry_point() {
let dir = tempdir().unwrap();
let db = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let entry = dir.path().join(ENTRY);
assert!(entry.is_file(), "Did not create entry point file.");
let data = read(entry).await.unwrap();
let id = str::from_utf8(&data).unwrap();
let cache = db.get_entry(&id).await.unwrap();
assert_eq!(cache.data.entry_type(), "DBMap");
assert_eq!(db.session, [id]);
}
#[async_std::test]
async fn use_existing_entry_point() {
let dir = tempdir().unwrap();
let db1 = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let db2 = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
assert_eq!(db1.session, db2.session, "Did not read existing entry.");
}
}
#[cfg(test)]
mod data {
use super::*;
use setup::MTT;
#[async_std::test]
async fn ids_are_random() {
let mtt = MTT::new().await;
let id1 = mtt.db.new_id();
let id2 = mtt.db.new_id();
assert_ne!(id1, id2, "Ids should be random");
}
#[async_std::test]
async fn add_database() {
let mtt = MTT::new().await;
let name = "fred";
let id = "*gsdfg";
let output = mtt.db.add("database", name, id).await;
assert_eq!(output.session, [id], "should update session info.");
/*
assert_eq!(
mtt.db.list(["database"].to_vec()).await.unwrap(),
[name],
"Should list the databases."
);
*/
}
}
#[cfg(test)]
mod cache_test {
use super::*;
use async_std::fs::read;
use setup::MTT;
use std::error::Error;
#[async_std::test]
async fn entry_ids_are_random() {
let mtt = MTT::new().await;
let data1 = CacheType::Raw("one".to_string());
let data2 = CacheType::Raw("two".to_string());
let id1 = mtt.db.add_entry(data1).await.unwrap();
let id2 = mtt.db.add_entry(data2).await.unwrap();
assert_ne!(id1, id2, "Ids should be unique.")
}
#[async_std::test]
async fn store_cache() {
let mtt = MTT::new().await;
let data = "something";
let expected = CacheType::Raw(data.to_string());
let id = mtt.db.add_entry(expected.clone()).await.unwrap();
let output = mtt.db.get_entry(&id).await.unwrap();
assert_eq!(output.to_string(), data);
let dfile = mtt.dir.path().join(DATA).join(&id);
assert!(dfile.is_file(), "Cache file should exist.");
let content = read(dfile).await.unwrap();
assert_eq!(content, expected.to_bytes());
}
#[async_std::test]
async fn get_entry_uodates_time() {
let mtt = MTT::new().await;
let id = "something";
let holder = CacheEntry {
data: CacheType::Raw("old".to_string()),
last_used: Instant::now() - Duration::from_secs(200),
};
let mut cache = mtt.db.cache.lock().await;
cache.insert(id.to_string(), holder);
drop(cache);
mtt.db.get_entry(&id).await.unwrap();
let cache = mtt.db.cache.lock().await;
let entry = cache.get(id).unwrap();
let held = entry.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
#[async_std::test]
async fn retrieve_from_disk() {
let mtt = MTT::new().await;
let id = "someid";
let data = CacheType::Raw("stored".to_string());
write(mtt.dir.path().join(DATA).join(id), data.to_bytes())
.await
.unwrap();
let output = mtt.db.get_entry(id).await.unwrap();
assert_eq!(output.to_string(), data.to_string());
let cache = mtt.db.cache.lock().await;
let stored = cache.get(id);
assert!(stored.is_some(), "Did not store entry in the cache.");
}
#[async_std::test]
async fn store_bad_file() {
let mtt = MTT::new().await;
let msg = "could not write to file";
mtt.create_io_error().await;
match mtt.db.add_entry(CacheType::Raw("fail".to_string())).await {
Ok(_) => assert!(false, "This test should fail."),
Err(err) => {
assert_eq!(err.to_string(), "data write");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
}
}
#[async_std::test]
async fn retrieve_bad_id() {
let mtt = MTT::new().await;
match mtt.db.get_entry(&"Not Valid").await {
Ok(_) => assert!(false, "Should have raised an error."),
Err(err) => assert_eq!(err.to_string(), "cache entry not found"),
}
}
#[async_std::test]
async fn update_cache_entry() {
let mtt = MTT::new().await;
let id = "updateable";
let holder = CacheEntry {
data: CacheType::Raw("elder".to_string()),
last_used: Instant::now() - Duration::from_secs(500),
};
let mut cache = mtt.db.cache.lock().await;
cache.insert(id.to_string(), holder);
drop(cache);
let expected = "different";
let expect = CacheType::Raw(expected.to_string());
mtt.db.update_entry(id, expect.clone()).await.unwrap();
let output = mtt.db.get_entry(id).await.unwrap();
assert_eq!(output.to_string(), expected);
let cache = mtt.db.cache.lock().await;
let entry = cache.get(id).unwrap();
let held = entry.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
drop(cache);
let content = read(mtt.dir.path().join(DATA).join(id)).await.unwrap();
assert_eq!(content, expect.to_bytes());
}
#[async_std::test]
async fn update_bad_id() {
let mtt = MTT::new().await;
match mtt
.db
.update_entry("wilma", CacheType::Raw("wrong".to_string()))
.await
{
Ok(_) => assert!(false, "Bad id should raise an error."),
Err(err) => assert_eq!(err.to_string(), "cache entry not found"),
}
}
#[async_std::test]
async fn update_bad_file() {
let mtt = MTT::new().await;
let msg = "could not write to file";
let id = mtt
.db
.add_entry(CacheType::Raw("fleeting".to_string()))
.await
.unwrap();
mtt.create_io_error().await;
match mtt
.db
.update_entry(&id, CacheType::Raw("failure".to_string()))
.await
{
Ok(_) => assert!(false, "This should produce a write failure."),
Err(err) => {
assert_eq!(err.to_string(), "data write");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
}
}
#[async_std::test]
async fn remove_entry() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("delete".to_string()))
.await
.unwrap();
mtt.db.delete_entry(&id).await.unwrap();
match mtt.db.get_entry(&id).await {
Ok(_) => assert!(false, "Entry should be removed from cache."),
Err(_) => (),
};
}
#[async_std::test]
async fn remove_missing_entry() {
let mtt = MTT::new().await;
let msg = "could not remove file";
match mtt.db.delete_entry("missing").await {
Ok(_) => assert!(false, "This should produce a write failure."),
Err(err) => {
assert_eq!(err.to_string(), "data delete");
assert!(err.source().is_some(), "Must include the source error.");
let err_msg = err.source().unwrap().to_string();
assert!(err_msg.contains(msg), "'{}' not in '{}'", msg, err_msg);
}
}
}
#[async_std::test]
async fn remove_older() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("removed".to_string()))
.await
.unwrap();
let mut cache = mtt.db.cache.lock().await;
let entry = cache.get_mut(&id).unwrap();
entry.last_used = Instant::now() - Duration::from_secs(1000);
drop(cache);
sleep(Duration::from_secs(2)).await;
let cache = mtt.db.cache.lock().await;
let output = cache.get(&id);
assert!(output.is_none(), "The entry shoould not be in memory.");
drop(cache);
let filename = mtt.db.filename(&id);
let fpath = Path::new(&filename);
assert!(
fpath.is_file().await,
"The stored version should still exist."
);
}
#[async_std::test]
async fn keep_newer() {
let mtt = MTT::new().await;
let id = mtt
.db
.add_entry(CacheType::Raw("keep".to_string()))
.await
.unwrap();
sleep(Duration::from_secs(2)).await;
let cache = mtt.db.cache.lock().await;
let output = cache.get(&id);
assert!(output.is_some(), "The entry shoould be in memory.");
}
}
#[cfg(test)]
mod cache_entry {
use super::*;
#[test]
fn init() {
let text = "new entry";
let holder = CacheEntry::new(CacheType::Raw(text.to_string()));
assert_eq!(holder.to_string(), text);
let held = holder.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
#[test]
fn older() {
let secs = 800;
let holder = CacheEntry {
data: CacheType::Raw("older".to_string()),
last_used: Instant::now() - Duration::from_secs(secs),
};
let held = holder.elapsed() - Duration::from_secs(secs);
assert!(
Duration::from_secs(1) > held,
"{:?} should be close to {}s",
holder.elapsed(),
secs
);
}
#[test]
fn accessed() {
let mut holder = CacheEntry {
data: CacheType::Raw("older".to_string()),
last_used: Instant::now() - Duration::from_secs(700),
};
holder.touch();
let held = holder.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
#[test]
fn updated() {
let text = "new data";
let mut holder = CacheEntry {
data: CacheType::Raw("old data".to_string()),
last_used: Instant::now() - Duration::from_secs(900),
};
holder.update(CacheType::Raw(text.to_string()));
assert_eq!(holder.to_string(), text);
let held = holder.elapsed();
assert!(
Duration::from_secs(1) > held,
"Duration was {:?}, should have been close to 0s.",
held
);
}
}
#[cfg(test)]
mod enum_ctype {
use super::*;
#[test]
fn bad_file_header() {
let mut data: Vec<u8> = Vec::new();
let mut ctype = "jlksdfg".as_bytes().to_vec();
let mut cdata = "ghjk".as_bytes().to_vec();
data.append(&mut ctype);
data.push(0);
data.append(&mut cdata);
match CacheType::from_bytes(data) {
Ok(_) => assert!(false, "This should fail."),
Err(err) => assert_eq!(err.to_string(), "data corruption"),
}
}
#[test]
fn incomplete_file() {
let mut data: Vec<u8> = Vec::new();
let mut ctype = "uoisfde".as_bytes().to_vec();
data.append(&mut ctype);
match CacheType::from_bytes(data) {
Ok(_) => assert!(false, "This should fail."),
Err(err) => assert_eq!(err.to_string(), "incomplete file"),
}
}
#[test]
fn empty_file() {
let data: Vec<u8> = Vec::new();
match CacheType::from_bytes(data) {
Ok(_) => assert!(false, "This should fail."),
Err(err) => assert_eq!(err.to_string(), "empty file"),
}
}
#[test]
fn get_raw_type() {
let holder = CacheType::Raw("nothing important".to_string());
assert_eq!(holder.entry_type(), "Raw");
}
#[test]
fn get_raw_bytes() {
let data = "addams";
let holder = CacheType::Raw(data.to_string());
let mut expected = holder.entry_type().into_bytes();
expected.push(0);
expected.append(&mut data.as_bytes().to_vec());
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_raw_bytes() {
let holder = CacheType::Raw("stored item".to_string());
let data = holder.to_bytes();
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.to_string(), holder.to_string());
}
#[test]
fn get_dbmap_type() {
let holder = CacheType::DBMap(Store::new());
assert_eq!(holder.entry_type(), "DBMap");
}
#[test]
fn get_new_databases_bytes() {
let holder = CacheType::DBMap(Store::new());
let mut expected = "DBMap".as_bytes().to_vec();
expected.push(0);
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_new_databases_bytes() {
let mut data = "DBMap".as_bytes().to_vec();
data.push(0);
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.entry_type(), "DBMap");
}
#[test]
fn get_tablemap_type() {
let holder = CacheType::TableMap;
assert_eq!(holder.entry_type(), "TableMap");
}
#[test]
fn get_new_database_bytes() {
let holder = CacheType::TableMap;
let mut expected = "TableMap".as_bytes().to_vec();
expected.push(0);
let output = holder.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_new_database_bytes() {
let mut data = "TableMap".as_bytes().to_vec();
data.push(0);
let output = CacheType::from_bytes(data).unwrap();
assert_eq!(output.entry_type(), "TableMap");
}
}

View File

@ -1,148 +1,186 @@
pub mod error;
mod cache;
mod database;
mod error;
mod store;
mod table;
use async_std::{
fs::create_dir,
path::Path,
sync::{Arc, Mutex},
channel::{unbounded, Sender},
path::PathBuf,
task::spawn,
};
use error::DBError;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{collections::HashMap, fmt};
use cache::Cache;
use database::Database;
use error::{ErrorCode, MTTError};
use store::Store;
use table::Table;
const DATA: &str = "data";
const ENTRY: &str = "EntryPoint";
#[derive(Clone)]
enum CacheEntry {
Raw(String),
#[derive(Debug)]
pub struct ToCacheMsg<D> {
data: D,
result: Sender<FromCache>,
}
impl fmt::Display for CacheEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CacheEntry::Raw(s) => write!(f, "{}", s),
#[derive(Debug)]
pub enum ToCache {
Get(ToCacheMsg<String>),
Commit(ToCacheMsg<Store>),
}
#[derive(Clone, Debug)]
pub enum FromCache {
Ok,
Str(Store),
DB(Database),
Error(MTTError),
}
#[derive(Clone, Debug)]
pub struct Data<D> {
id: Option<String>,
data: Option<D>,
}
impl<D> Data<D> {
fn from_id<S>(id: S) -> Self
where
S: Into<String>,
{
Self {
id: Some(id.into()),
data: None,
}
}
fn from_data(data: D) -> Self {
Self {
id: None,
data: Some(data),
}
}
}
#[derive(Clone)]
pub struct MoreThanText {
cache: Arc<Mutex<HashMap<String, CacheEntry>>>,
to_cache: Sender<ToCache>,
entry: Data<Store>,
}
impl MoreThanText {
pub async fn new(dir: &str) -> Result<Self, DBError> {
let data_dir = Path::new(dir).join(DATA);
if !data_dir.is_dir().await {
match create_dir(data_dir).await {
Ok(_) => (),
Err(err) => {
let mut error = DBError::new("failed to create data directory");
error.add_source(err);
return Err(error);
}
}
fn new(to_cache: Sender<ToCache>) -> Self {
Self {
to_cache: to_cache,
entry: Data::from_id(ENTRY),
}
Ok(Self {
cache: Arc::new(Mutex::new(HashMap::new())),
})
}
async fn add_entry(&self, entry: CacheEntry) -> String {
let id: String = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
let mut cache = self.cache.lock().await;
cache.insert(id.clone(), entry);
return id;
async fn session(&self) -> Result<Store, MTTError> {
let (s, r) = unbounded();
let msg = ToCacheMsg {
data: ENTRY.to_string(),
result: s,
};
self.to_cache.send(ToCache::Get(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Str(store) => Ok(store),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
}
}
async fn get_entry(&self, id: &str) -> CacheEntry {
let cache = self.cache.lock().await;
cache.get(id).unwrap().clone()
}
}
#[cfg(test)]
mod setup {
use super::*;
use tempfile::{tempdir, TempDir};
pub struct MTT {
pub db: MoreThanText,
pub dir: TempDir,
}
impl MTT {
pub async fn new() -> Self {
let dir = tempdir().unwrap();
let db = MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
Self { db: db, dir: dir }
async fn commit(&self, store: Store) -> Result<(), MTTError> {
let (s, r) = unbounded();
let msg = ToCacheMsg {
data: store,
result: s,
};
self.to_cache.send(ToCache::Commit(msg)).await.unwrap();
match r.recv().await.unwrap() {
FromCache::Ok => Ok(()),
FromCache::Error(err) => Err(err),
_ => unreachable!(),
}
}
}
#[cfg(test)]
mod init {
mod mtt {
use super::*;
use std::error::Error;
use tempfile::tempdir;
#[async_std::test]
async fn create_data_dir() {
async fn create_new() {
let dir = tempdir().unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
let data_dir = dir.path().join(DATA);
assert!(data_dir.is_dir(), "Did not create the data directory.");
dir.close().unwrap();
let mtt = start_db(dir.path()).await.unwrap();
assert_eq!(mtt.entry.id, Some(ENTRY.to_string()));
assert!(mtt.entry.data.is_none());
let store = mtt.session().await.unwrap();
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
}
#[async_std::test]
async fn existing_data_dir() {
async fn commit_db() {
let dir = tempdir().unwrap();
let data_dir = dir.path().join(DATA);
create_dir(data_dir).await.unwrap();
MoreThanText::new(dir.path().to_str().unwrap())
.await
.unwrap();
dir.close().unwrap();
let db = "fred";
let mtt = start_db(dir.path()).await.unwrap();
let mut store = mtt.session().await.unwrap();
store.add(db).unwrap();
mtt.commit(store).await.unwrap();
let store2 = mtt.session().await.unwrap();
assert_eq!(store2.list(), [db]);
}
#[async_std::test]
async fn bad_data_dir() {
match MoreThanText::new("kljsdgfhslkfrh").await {
Ok(_) => assert!(false, "This test should fail to create a data directory"),
Err(err) => {
assert_eq!(err.to_string(), "failed to create data directory");
assert!(err.source().is_some(), "Must include the source error.");
}
};
async fn commit_from_multiple_sources() {
let dir = tempdir().unwrap();
let mtt1 = start_db(dir.path()).await.unwrap();
let mtt2 = mtt1.clone();
let db1 = "first";
let db2 = "second";
let mut store1 = mtt1.session().await.unwrap();
let mut store2 = mtt2.session().await.unwrap();
store1.add(db1).unwrap();
store2.add(db2).unwrap();
mtt1.commit(store1).await.unwrap();
mtt2.commit(store2).await.unwrap();
let output = mtt1.session().await.unwrap();
assert_eq!(output.list(), [db1, db2]);
}
#[async_std::test]
async fn fail_on_duplicates() {
let dir = tempdir().unwrap();
let mtt1 = start_db(dir.path()).await.unwrap();
let mtt2 = mtt1.clone();
let name = "unique_only";
let mut store1 = mtt1.session().await.unwrap();
let mut store2 = mtt2.session().await.unwrap();
store1.add(name).unwrap();
store2.add(name).unwrap();
mtt1.commit(store1).await.unwrap();
let output = mtt2.commit(store2).await;
match output {
Ok(_) => assert!(false, "Should have returned an error"),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(_) => (),
_ => assert!(false, "{:?} is not ErrorCode::DuplicateDatabase", err.code),
},
}
}
}
#[cfg(test)]
mod cache {
use super::*;
use setup::MTT;
#[async_std::test]
async fn ids_are_random() {
let mtt = MTT::new().await;
let data1 = CacheEntry::Raw("one".to_string());
let data2 = CacheEntry::Raw("two".to_string());
let id1 = mtt.db.add_entry(data1).await;
let id2 = mtt.db.add_entry(data2).await;
assert_ne!(id1, id2, "Ids should be unique.")
}
#[async_std::test]
async fn retrieve_cache() {
let mtt = MTT::new().await;
let data = "something";
let expected = CacheEntry::Raw(data.to_string());
let id = mtt.db.add_entry(expected).await;
let output = mtt.db.get_entry(&id).await;
assert_eq!(output.to_string(), data);
}
pub async fn start_db<P>(dir: P) -> Result<MoreThanText, MTTError>
where
P: Into<PathBuf>,
{
let path = dir.into();
let (s, r) = unbounded();
spawn(async move {
let mut cache = Cache::new(path).await;
cache.listen(r).await;
});
Ok(MoreThanText::new(s))
}

View File

@ -0,0 +1,306 @@
use super::{DBError, ErrorCode, FileData, SessionData};
use std::{collections::HashMap, slice, str};
#[derive(Clone)]
pub struct Store {
db_map: HashMap<String, String>,
}
impl Store {
pub fn new() -> Self {
Self {
db_map: HashMap::new(),
}
}
fn test_key(key: &str) -> Result<(), DBError> {
match key {
"database" => (),
_ => return Err(DBError::new(format!("databases do not have a {}", key))),
}
Ok(())
}
}
impl FileData<Self> for Store {
fn to_bytes(&self) -> Vec<u8> {
let mut output = Vec::new();
for (name, id) in self.db_map.iter() {
output.append(&mut name.as_bytes().to_vec());
output.push(0);
output.append(&mut id.as_bytes().to_vec());
output.push(0);
}
output
}
fn from_bytes(data: &mut slice::Iter<u8>) -> Result<Self, DBError> {
let mut output = Store::new();
let mut name: Vec<u8> = Vec::new();
let mut id: Vec<u8> = Vec::new();
let mut get_id = false;
let mut letter: u8;
loop {
match data.next() {
Some(a) => letter = a.clone(),
None => {
if !name.is_empty() {
return Err(DBError::from_code(ErrorCode::CorruptFile));
}
break;
}
}
if letter == 0 {
if get_id {
let name_holder = match str::from_utf8(&name) {
Ok(item) => item,
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
let id_holder = match str::from_utf8(&id) {
Ok(item) => item,
Err(_) => return Err(DBError::from_code(ErrorCode::CorruptFile)),
};
match output.add("database", name_holder, id_holder) {
Ok(_) => (),
Err(err) => {
let mut error = DBError::from_code(ErrorCode::CorruptFile);
error.add_source(err);
return Err(error);
}
};
name.clear();
id.clear();
}
get_id = !get_id;
} else {
if get_id {
id.push(letter);
} else {
name.push(letter);
}
}
}
Ok(output)
}
}
impl SessionData for Store {
fn add(&mut self, key: &str, value: &str, data: &str) -> Result<Vec<String>, DBError> {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
match self.db_map.get(value) {
Some(_) => return Err(DBError::new(format!("database {} already exists", value))),
None => (),
}
self.db_map.insert(value.to_string(), data.to_string());
let mut output = Vec::new();
output.push(data.to_string());
Ok(output)
}
fn eq(&self, key: &str, value: &str) -> Result<Vec<String>, DBError> {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
let mut output = Vec::new();
match self.db_map.get(value) {
Some(data) => output.push(data.to_string()),
None => (),
}
Ok(output)
}
fn list(&self, keys: Vec<&str>) -> Result<Vec<String>, DBError> {
for key in keys {
match Self::test_key(key) {
Ok(_) => (),
Err(err) => return Err(err),
}
}
let mut names: Vec<String> = self.db_map.clone().into_keys().collect();
names.sort();
Ok(names)
}
}
#[cfg(test)]
mod file_data {
use super::*;
use std::error::Error;
#[test]
fn to_bytes_new() {
let dbs = Store::new();
let expected: Vec<u8> = Vec::new();
let output = dbs.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn to_bytes_with_database() {
let mut dbs = Store::new();
let name = "something";
let id = "id";
dbs.add("database", name, id).unwrap();
let mut expected: Vec<u8> = Vec::new();
expected.append(&mut name.as_bytes().to_vec());
expected.push(0);
expected.append(&mut id.as_bytes().to_vec());
expected.push(0);
let output = dbs.to_bytes();
assert_eq!(output, expected);
}
#[test]
fn from_bytes() {
let mut dbs = Store::new();
dbs.add("database", "one", "1").unwrap();
dbs.add("database", "two", "2").unwrap();
dbs.add("database", "three", "3").unwrap();
let data = dbs.to_bytes();
let mut feed = data.iter();
let output = Store::from_bytes(&mut feed).unwrap();
assert_eq!(output.db_map, dbs.db_map);
}
#[test]
fn from_bytes_incomplete_name() -> Result<(), DBError> {
let data = "notName".as_bytes();
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have produced an errpr")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[test]
fn from_bytes_incomplete_id() -> Result<(), DBError> {
let mut data = "proper".as_bytes().to_vec();
data.push(0);
data.append(&mut "nope".as_bytes().to_vec());
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have produced an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => Ok(()),
_ => Err(DBError::new("incorrect error code")),
},
}
}
#[test]
fn from_bytes_handles_error() -> Result<(), DBError> {
let name = "duplicate";
let mut data = name.as_bytes().to_vec();
data.push(0);
data.append(&mut "first".as_bytes().to_vec());
data.push(0);
data.append(&mut "duplicate".as_bytes().to_vec());
data.push(0);
data.append(&mut "second".as_bytes().to_vec());
data.push(0);
let mut feed = data.iter();
match Store::from_bytes(&mut feed) {
Ok(_) => Err(DBError::new("should have returned an error")),
Err(err) => match err.code {
ErrorCode::CorruptFile => {
assert!(
err.source().is_some(),
"Should state file corruption cause."
);
assert_eq!(
err.source().unwrap().to_string(),
format!("database {} already exists", name)
);
Ok(())
}
_ => Err(DBError::new("incorrect error code")),
},
}
}
}
#[cfg(test)]
mod session_data {
use super::*;
#[test]
fn add_new() {
let mut dbs = Store::new();
let key = "database";
let value = "marvin";
let data = "123456";
assert_eq!(dbs.add(key, value, data).unwrap(), [data]);
let output = dbs.eq(key, value).unwrap();
assert_eq!(output, [data]);
}
#[test]
fn add_bad_key() {
let mut dbs = Store::new();
let key = "sdgfjksg";
match dbs.add(key, "fred", "barney") {
Ok(_) => assert!(false, "Bad keys should produce an error."),
Err(err) => assert_eq!(err.to_string(), format!("databases do not have a {}", key)),
}
}
#[test]
fn get_bad_key() {
let dbs = Store::new();
let key = "bvdfgert";
match dbs.eq(key, "fred") {
Ok(_) => assert!(false, "Bad keys should produce an error."),
Err(_) => (),
}
}
#[test]
fn unique_names() {
let mut dbs = Store::new();
let value = "wilma";
dbs.add("database", value, "something").unwrap();
match dbs.add("database", value, "overwrite") {
Ok(_) => assert!(false, "Duplicate names should produce an error."),
Err(err) => assert_eq!(
err.to_string(),
format!("database {} already exists", value)
),
}
}
#[test]
fn get_missing() {
let dbs = Store::new();
let output = dbs.eq("database", "melvin").unwrap();
assert_eq!(output, Vec::<String>::new());
}
#[test]
fn list_bad_keys() {
let dbs = Store::new();
let key = "sdfgren";
let keys = [key];
match dbs.list(keys.to_vec()) {
Ok(_) => assert!(false, "Bad keys should error."),
Err(err) => assert_eq!(err.to_string(), format!("databases do not have a {}", key)),
}
}
#[test]
fn list_is_sorted() {
let mut dbs = Store::new();
let mut data = ["fred", "barney", "wilma", "betty", "pebbles", "bambam"];
for db in data {
dbs.add("database", db, db).unwrap();
}
data.sort();
assert_eq!(dbs.list(["database"].to_vec()).unwrap(), data);
}
}

171
src/morethantext/store.rs Normal file
View File

@ -0,0 +1,171 @@
use super::{Data, Database, ErrorCode, MTTError};
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct Store {
data: HashMap<String, Data<Database>>,
}
impl Store {
pub fn new() -> Self {
Self {
data: HashMap::new(),
}
}
pub fn add<S>(&mut self, name: S) -> Result<(), MTTError>
where
S: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateDatabase(db_name))),
None => {
self.data.insert(db_name, Data::from_data(Database::new()));
Ok(())
}
}
}
pub fn add_by_id<S, D>(&mut self, name: S, id: D) -> Result<(), MTTError>
where
S: Into<String>,
D: Into<String>,
{
let db_name = name.into();
match self.get(&db_name) {
Some(_) => Err(MTTError::from_code(ErrorCode::DuplicateDatabase(db_name))),
None => {
self.data.insert(db_name, Data::from_id(id.into()));
Ok(())
}
}
}
pub fn get(&self, name: &str) -> Option<&Data<Database>> {
self.data.get(name)
}
pub fn list(&self) -> Vec<String> {
let mut names = Vec::new();
for name in self.data.keys() {
names.push(name.to_string());
}
names.sort();
names
}
}
#[cfg(test)]
mod storage {
use super::*;
#[test]
fn create_new() {
let store = Store::new();
let expected: Vec<String> = Vec::new();
assert_eq!(store.list(), expected);
}
#[test]
fn add_db_by_str() {
let mut store = Store::new();
let name = "Melvin";
store.add(name).unwrap();
let output = store.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn add_db_by_string() {
let mut store = Store::new();
let name = "Marvin";
store.add(name.to_string()).unwrap();
let output = store.get(name);
assert!(output.is_some(), "Get returned none.");
}
#[test]
fn fail_on_duplicates() -> Result<(), MTTError> {
let mut store = Store::new();
let name = "Mickie";
store.add(name).unwrap();
match store.add(name) {
Ok(_) => Err(MTTError::new("duplicates should error")),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(db_name) => {
assert_eq!(db_name, name);
Ok(())
}
_ => Err(MTTError::new(format!("{:?} is not DuplicateDatabase", err))),
},
}
}
#[test]
fn add_using_cache_id() {
let mut store = Store::new();
let name = "fred";
let id = "12345";
store.add_by_id(name, id).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn add_by_cache_id_name_string() {
let mut store = Store::new();
let name = "barney";
let id = "67890";
store.add_by_id(name.to_string(), id).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn no_duplicate_databases_for_add_by_id() {
let mut store = Store::new();
let name = "betty";
store.add_by_id(name, "fghij").unwrap();
match store.add_by_id(name, "klmno") {
Ok(_) => assert!(false, "Duplicates should error."),
Err(err) => match err.code {
ErrorCode::DuplicateDatabase(db_name) => assert_eq!(db_name, name),
_ => assert!(false, "{:?} is not DuplicateDatabase", err),
},
}
}
#[test]
fn add_by_cache_id_string() {
let mut store = Store::new();
let name = "wilma";
let id = "abcdef";
store.add_by_id(name, id.to_string()).unwrap();
let output = store.get(name).unwrap();
assert!(output.data.is_none(), "there should be no data");
assert_eq!(output.id, Some(id.to_string()));
}
#[test]
fn get_bad_database() -> Result<(), MTTError> {
let store = Store::new();
match store.get("missing") {
Some(_) => Err(MTTError::new("Should have returned None.")),
None => Ok(()),
}
}
#[test]
fn get_list() {
let mut store = Store::new();
let mut ids = ["one", "two", "three", "four", "five"];
for name in ids {
store.add(name.to_string()).unwrap();
}
ids.sort();
assert_eq!(store.list(), ids);
}
}

18
src/morethantext/table.rs Normal file
View File

@ -0,0 +1,18 @@
#[derive(Clone, Debug)]
pub struct Table;
impl Table {
pub fn new() -> Self {
Self {}
}
}
#[cfg(test)]
mod tables {
use super::*;
#[test]
fn create_new() {
Table::new();
}
}