2022-08-06 12:03:47 -04:00
|
|
|
pub mod error;
|
|
|
|
|
2022-12-05 23:23:39 -05:00
|
|
|
use async_std::{
|
2022-12-10 09:26:06 -05:00
|
|
|
fs::{create_dir, write},
|
2022-12-05 23:23:39 -05:00
|
|
|
path::Path,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
2022-09-27 07:31:59 -04:00
|
|
|
use error::DBError;
|
2022-12-04 11:09:56 -05:00
|
|
|
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
2022-12-05 23:21:13 -05:00
|
|
|
use std::{collections::HashMap, fmt};
|
2022-12-02 10:34:45 -05:00
|
|
|
|
|
|
|
const DATA: &str = "data";
|
2022-08-06 12:03:47 -04:00
|
|
|
|
2022-12-05 23:21:13 -05:00
|
|
|
#[derive(Clone)]
|
2022-12-03 09:15:58 -05:00
|
|
|
enum CacheEntry {
|
|
|
|
Raw(String),
|
|
|
|
}
|
|
|
|
|
2022-12-10 09:26:06 -05:00
|
|
|
impl CacheEntry {
|
|
|
|
fn entry_type(&self) -> String {
|
|
|
|
match self {
|
|
|
|
CacheEntry::Raw(_) => "raw".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:21:13 -05:00
|
|
|
impl fmt::Display for CacheEntry {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
CacheEntry::Raw(s) => write!(f, "{}", s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 17:24:45 -04:00
|
|
|
#[derive(Clone)]
|
2022-12-05 23:21:13 -05:00
|
|
|
pub struct MoreThanText {
|
|
|
|
cache: Arc<Mutex<HashMap<String, CacheEntry>>>,
|
2022-12-10 09:26:06 -05:00
|
|
|
dir: String,
|
2022-12-05 23:21:13 -05:00
|
|
|
}
|
2022-07-12 16:55:20 -04:00
|
|
|
|
2022-07-18 17:24:45 -04:00
|
|
|
impl MoreThanText {
|
2022-12-02 10:34:45 -05:00
|
|
|
pub async fn new(dir: &str) -> Result<Self, DBError> {
|
|
|
|
let data_dir = Path::new(dir).join(DATA);
|
|
|
|
if !data_dir.is_dir().await {
|
2022-12-10 09:26:06 -05:00
|
|
|
match create_dir(&data_dir).await {
|
2022-12-02 10:34:45 -05:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => {
|
2022-12-03 08:26:21 -05:00
|
|
|
let mut error = DBError::new("failed to create data directory");
|
2022-12-02 10:34:45 -05:00
|
|
|
error.add_source(err);
|
|
|
|
return Err(error);
|
|
|
|
}
|
2022-08-06 12:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 23:21:13 -05:00
|
|
|
Ok(Self {
|
|
|
|
cache: Arc::new(Mutex::new(HashMap::new())),
|
2022-12-10 09:26:06 -05:00
|
|
|
dir: data_dir.to_str().unwrap().to_string(),
|
2022-12-05 23:21:13 -05:00
|
|
|
})
|
2022-08-07 09:29:08 -04:00
|
|
|
}
|
2022-12-03 09:15:58 -05:00
|
|
|
|
2022-12-05 23:21:13 -05:00
|
|
|
async fn add_entry(&self, entry: CacheEntry) -> String {
|
2022-12-04 11:09:56 -05:00
|
|
|
let id: String = thread_rng().sample_iter(&Alphanumeric).take(32).collect();
|
2022-12-10 09:26:06 -05:00
|
|
|
let file = Path::new(&self.dir).join(&id);
|
|
|
|
write(file, "a").await.unwrap();
|
2022-12-05 23:21:13 -05:00
|
|
|
let mut cache = self.cache.lock().await;
|
|
|
|
cache.insert(id.clone(), entry);
|
2022-12-04 11:09:56 -05:00
|
|
|
return id;
|
2022-12-03 09:15:58 -05:00
|
|
|
}
|
2022-12-05 23:21:13 -05:00
|
|
|
|
2022-12-06 11:42:11 -05:00
|
|
|
async fn get_entry(&self, id: &str) -> Result<CacheEntry, DBError> {
|
2022-12-05 23:21:13 -05:00
|
|
|
let cache = self.cache.lock().await;
|
2022-12-06 11:42:11 -05:00
|
|
|
match cache.get(id) {
|
|
|
|
Some(id) => Ok(id.clone()),
|
|
|
|
None => Err(DBError::new("cache entry not found")),
|
|
|
|
}
|
2022-12-05 23:21:13 -05:00
|
|
|
}
|
2022-12-08 10:58:29 -05:00
|
|
|
|
|
|
|
async fn update_entry(&self, id: &str, entry: CacheEntry) -> Result<(), DBError> {
|
|
|
|
match self.get_entry(id).await {
|
|
|
|
Ok(_) => (),
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
let mut cache = self.cache.lock().await;
|
|
|
|
cache.insert(id.to_string(), entry);
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
|
|
|
|
2022-12-03 10:22:54 -05:00
|
|
|
#[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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-12 16:55:20 -04:00
|
|
|
#[cfg(test)]
|
2022-12-02 10:34:45 -05:00
|
|
|
mod init {
|
2022-07-12 16:55:20 -04:00
|
|
|
use super::*;
|
2022-12-02 10:34:45 -05:00
|
|
|
use std::error::Error;
|
|
|
|
use tempfile::tempdir;
|
2022-07-12 16:55:20 -04:00
|
|
|
|
|
|
|
#[async_std::test]
|
2022-12-02 10:34:45 -05:00
|
|
|
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();
|
2022-08-06 12:38:58 -04:00
|
|
|
}
|
|
|
|
|
2022-08-06 12:03:47 -04:00
|
|
|
#[async_std::test]
|
2022-12-02 10:34:45 -05:00
|
|
|
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();
|
2022-07-12 16:55:20 -04:00
|
|
|
}
|
2022-08-07 09:29:08 -04:00
|
|
|
|
|
|
|
#[async_std::test]
|
2022-12-02 10:34:45 -05:00
|
|
|
async fn bad_data_dir() {
|
|
|
|
match MoreThanText::new("kljsdgfhslkfrh").await {
|
|
|
|
Ok(_) => assert!(false, "This test should fail to create a data directory"),
|
2022-09-27 07:31:59 -04:00
|
|
|
Err(err) => {
|
2022-12-03 08:26:21 -05:00
|
|
|
assert_eq!(err.to_string(), "failed to create data directory");
|
2022-12-02 10:34:45 -05:00
|
|
|
assert!(err.source().is_some(), "Must include the source error.");
|
2022-09-27 07:31:59 -04:00
|
|
|
}
|
2022-12-02 10:34:45 -05:00
|
|
|
};
|
2022-08-05 16:47:01 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 09:15:58 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod cache {
|
|
|
|
use super::*;
|
2022-12-03 10:22:54 -05:00
|
|
|
use setup::MTT;
|
2022-12-03 09:15:58 -05:00
|
|
|
|
|
|
|
#[async_std::test]
|
2022-12-04 11:09:56 -05:00
|
|
|
async fn ids_are_random() {
|
2022-12-03 10:22:54 -05:00
|
|
|
let mtt = MTT::new().await;
|
2022-12-04 11:09:56 -05:00
|
|
|
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.")
|
2022-12-03 09:15:58 -05:00
|
|
|
}
|
2022-12-05 23:21:13 -05:00
|
|
|
|
|
|
|
#[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;
|
2022-12-06 11:42:11 -05:00
|
|
|
let output = mtt.db.get_entry(&id).await.unwrap();
|
2022-12-05 23:21:13 -05:00
|
|
|
assert_eq!(output.to_string(), data);
|
2022-12-10 09:26:06 -05:00
|
|
|
let dfile = mtt.dir.path().join(DATA).join(&id);
|
|
|
|
assert!(dfile.is_file(), "Cache file should exist.");
|
2022-12-05 23:21:13 -05:00
|
|
|
}
|
2022-12-06 11:42:11 -05:00
|
|
|
|
|
|
|
#[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"),
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 10:58:29 -05:00
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn update_cache_entry() {
|
|
|
|
let mtt = MTT::new().await;
|
|
|
|
let id = mtt.db.add_entry(CacheEntry::Raw("same".to_string())).await;
|
|
|
|
let expected = "different";
|
2022-12-08 11:08:05 -05:00
|
|
|
mtt.db
|
|
|
|
.update_entry(&id, CacheEntry::Raw(expected.to_string()))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-12-08 10:58:29 -05:00
|
|
|
let output = mtt.db.get_entry(&id).await.unwrap();
|
|
|
|
assert_eq!(output.to_string(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_std::test]
|
|
|
|
async fn update_bad_id() {
|
|
|
|
let mtt = MTT::new().await;
|
2022-12-08 11:08:05 -05:00
|
|
|
match mtt
|
|
|
|
.db
|
|
|
|
.update_entry("wilma", CacheEntry::Raw("wrong".to_string()))
|
|
|
|
.await
|
|
|
|
{
|
2022-12-08 10:58:29 -05:00
|
|
|
Ok(_) => assert!(false, "Bad id should raise an error."),
|
|
|
|
Err(err) => assert_eq!(err.to_string(), "cache entry not found"),
|
|
|
|
}
|
|
|
|
}
|
2022-12-03 09:15:58 -05:00
|
|
|
}
|
2022-12-10 09:26:06 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod cache_entry {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn raw_type() {
|
|
|
|
let holder = CacheEntry::Raw("nothing important".to_string());
|
|
|
|
assert_eq!(holder.entry_type(), "raw");
|
|
|
|
}
|
|
|
|
}
|