2020-10-15 16:52:08 +00:00
|
|
|
use byteorder::LittleEndian;
|
2020-10-20 20:54:20 +00:00
|
|
|
use sled::transaction::abort;
|
|
|
|
use sled::transaction::{TransactionError, TransactionalTree, UnabortableTransactionError};
|
|
|
|
use sled::{Db, Tree};
|
2020-10-18 00:35:09 +00:00
|
|
|
use std::collections::HashMap;
|
2020-10-23 20:19:04 +00:00
|
|
|
use std::path::Path;
|
|
|
|
use std::str;
|
2020-10-15 16:52:08 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
use zerocopy::byteorder::I32;
|
|
|
|
use zerocopy::{AsBytes, LayoutVerified};
|
|
|
|
|
2020-10-16 13:07:19 +00:00
|
|
|
/// User variables are stored as little-endian 32-bit integers in the
|
|
|
|
/// database. This type alias makes the database code more pleasant to
|
|
|
|
/// read.
|
|
|
|
type LittleEndianI32Layout<'a> = LayoutVerified<&'a [u8], I32<LittleEndian>>;
|
|
|
|
|
2020-10-23 15:53:35 +00:00
|
|
|
const METADATA_KEY: &'static str = "metadata";
|
2020-10-20 20:54:20 +00:00
|
|
|
const VARIABLE_COUNT_KEY: &'static str = "variable_count";
|
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Database {
|
|
|
|
db: Db,
|
2020-10-18 15:13:34 +00:00
|
|
|
variables: Tree,
|
|
|
|
rooms: Tree,
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 00:35:09 +00:00
|
|
|
//TODO better combining of key and value in certain errors (namely
|
|
|
|
//I32SchemaViolation).
|
2020-10-15 16:52:08 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum DataError {
|
|
|
|
#[error("value does not exist for key: {0}")]
|
|
|
|
KeyDoesNotExist(String),
|
|
|
|
|
2020-10-18 00:35:09 +00:00
|
|
|
#[error("expected i32, but i32 schema was violated")]
|
|
|
|
I32SchemaViolation,
|
|
|
|
|
|
|
|
#[error("expected string, but utf8 schema was violated: {0}")]
|
|
|
|
Utf8chemaViolation(#[from] std::str::Utf8Error),
|
2020-10-16 13:07:19 +00:00
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
#[error("internal database error: {0}")]
|
|
|
|
InternalError(#[from] sled::Error),
|
2020-10-20 20:54:20 +00:00
|
|
|
|
|
|
|
#[error("transaction error: {0}")]
|
|
|
|
TransactionError(#[from] sled::transaction::TransactionError),
|
|
|
|
|
|
|
|
#[error("unabortable transaction error: {0}")]
|
|
|
|
UnabortableTransactionError(#[from] UnabortableTransactionError),
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:57:31 +00:00
|
|
|
/// This From implementation is necessary to deal with the recursive
|
|
|
|
/// error type in the error enum. We defined a transaction error, but
|
|
|
|
/// the only place we use it is when converting from
|
|
|
|
/// sled::transaction::TransactionError<DataError>. This converter
|
|
|
|
/// extracts the inner data error from transaction aborted errors, and
|
|
|
|
/// forwards anything else onward as-is, but wrapped in DataError.
|
2020-10-20 20:54:20 +00:00
|
|
|
impl From<TransactionError<DataError>> for DataError {
|
|
|
|
fn from(error: TransactionError<DataError>) -> Self {
|
|
|
|
match error {
|
|
|
|
TransactionError::Abort(data_err) => data_err,
|
|
|
|
TransactionError::Storage(storage_err) => {
|
|
|
|
DataError::TransactionError(TransactionError::Storage(storage_err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_key(room_id: &str, username: &str, variable_name: &str) -> Vec<u8> {
|
|
|
|
let mut key = vec![];
|
|
|
|
key.extend_from_slice(room_id.as_bytes());
|
|
|
|
key.extend_from_slice(username.as_bytes());
|
|
|
|
key.extend_from_slice(variable_name.as_bytes());
|
|
|
|
key
|
|
|
|
}
|
|
|
|
|
2020-10-23 15:53:35 +00:00
|
|
|
fn metadata_key(room_id: &str, username: &str, metadata_key: &str) -> Vec<u8> {
|
|
|
|
let mut key = vec![];
|
|
|
|
key.extend_from_slice(room_id.as_bytes());
|
|
|
|
key.extend_from_slice(METADATA_KEY.as_bytes());
|
|
|
|
key.extend_from_slice(username.as_bytes());
|
|
|
|
key.extend_from_slice(metadata_key.as_bytes());
|
|
|
|
key
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:54:20 +00:00
|
|
|
fn room_variable_count_key(room_id: &str, username: &str) -> Vec<u8> {
|
2020-10-23 15:53:35 +00:00
|
|
|
metadata_key(room_id, username, VARIABLE_COUNT_KEY)
|
2020-10-20 20:54:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 00:35:09 +00:00
|
|
|
fn to_prefix(room_id: &str, username: &str) -> Vec<u8> {
|
|
|
|
let mut prefix = vec![];
|
|
|
|
prefix.extend_from_slice(room_id.as_bytes());
|
|
|
|
prefix.extend_from_slice(username.as_bytes());
|
|
|
|
prefix
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:54:20 +00:00
|
|
|
/// Convert bytes to an i32 with zero-copy deserialization. An error
|
|
|
|
/// is returned if the bytes do not represent an i32.
|
|
|
|
fn convert_i32(raw_value: &[u8]) -> Result<i32, DataError> {
|
2020-10-18 00:35:09 +00:00
|
|
|
let layout = LittleEndianI32Layout::new_unaligned(raw_value.as_ref());
|
|
|
|
|
|
|
|
if let Some(layout) = layout {
|
|
|
|
let value: I32<LittleEndian> = *layout;
|
|
|
|
Ok(value.get())
|
|
|
|
} else {
|
|
|
|
Err(DataError::I32SchemaViolation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 20:19:04 +00:00
|
|
|
/// Use a transaction to atomically alter the count of variables in
|
|
|
|
/// the database by the given amount. Count cannot go below 0.
|
2020-10-20 20:54:20 +00:00
|
|
|
fn alter_room_variable_count(
|
|
|
|
variables: &TransactionalTree,
|
|
|
|
room_id: &str,
|
|
|
|
username: &str,
|
|
|
|
amount: i32,
|
|
|
|
) -> Result<i32, DataError> {
|
|
|
|
let key = room_variable_count_key(room_id, username);
|
|
|
|
let mut new_count = match variables.get(&key)? {
|
|
|
|
Some(bytes) => convert_i32(&bytes)? + amount,
|
|
|
|
None => amount,
|
|
|
|
};
|
|
|
|
|
|
|
|
if new_count < 0 {
|
|
|
|
new_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let db_value: I32<LittleEndian> = I32::new(new_count);
|
|
|
|
variables.insert(key, db_value.as_bytes())?;
|
|
|
|
Ok(new_count)
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:52:08 +00:00
|
|
|
impl Database {
|
2020-10-23 20:19:04 +00:00
|
|
|
pub fn new<P: AsRef<Path>>(path: P) -> Result<Database, DataError> {
|
2020-10-18 15:13:34 +00:00
|
|
|
let db = sled::open(path)?;
|
|
|
|
let variables = db.open_tree("variables")?;
|
|
|
|
let rooms = db.open_tree("rooms")?;
|
|
|
|
|
|
|
|
Ok(Database {
|
|
|
|
db: db.clone(),
|
|
|
|
variables: variables,
|
|
|
|
rooms: rooms,
|
|
|
|
})
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 00:35:09 +00:00
|
|
|
pub async fn get_user_variables(
|
|
|
|
&self,
|
|
|
|
room_id: &str,
|
|
|
|
username: &str,
|
|
|
|
) -> Result<HashMap<String, i32>, DataError> {
|
|
|
|
let prefix = to_prefix(&room_id, &username);
|
|
|
|
let prefix_len: usize = prefix.len();
|
|
|
|
|
|
|
|
let variables: Result<Vec<_>, DataError> = self
|
2020-10-18 20:36:47 +00:00
|
|
|
.variables
|
2020-10-18 00:35:09 +00:00
|
|
|
.scan_prefix(prefix)
|
|
|
|
.map(|entry| match entry {
|
|
|
|
Ok((key, raw_value)) => {
|
2020-10-23 20:19:04 +00:00
|
|
|
//Strips room and username from key, leaving behind name.
|
|
|
|
let variable_name = str::from_utf8(&key[prefix_len..])?;
|
2020-10-20 20:54:20 +00:00
|
|
|
Ok((variable_name.to_owned(), convert_i32(&raw_value)?))
|
2020-10-18 00:35:09 +00:00
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-10-23 20:19:04 +00:00
|
|
|
//Convert I32 to hash map. Can we do this in the first mapping
|
2020-10-20 20:54:20 +00:00
|
|
|
//step instead? For some reason this is faster.
|
2020-10-18 00:35:09 +00:00
|
|
|
variables.map(|entries| entries.into_iter().collect())
|
|
|
|
}
|
|
|
|
|
2020-10-20 20:54:20 +00:00
|
|
|
pub async fn get_variable_count(
|
|
|
|
&self,
|
|
|
|
room_id: &str,
|
|
|
|
username: &str,
|
|
|
|
) -> Result<i32, DataError> {
|
|
|
|
let key = room_variable_count_key(room_id, username);
|
|
|
|
if let Some(raw_value) = self.variables.get(&key)? {
|
|
|
|
convert_i32(&raw_value)
|
|
|
|
} else {
|
|
|
|
Ok(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 20:24:24 +00:00
|
|
|
pub async fn get_user_variable(
|
2020-10-15 16:52:08 +00:00
|
|
|
&self,
|
|
|
|
room_id: &str,
|
|
|
|
username: &str,
|
|
|
|
variable_name: &str,
|
2020-10-16 12:40:25 +00:00
|
|
|
) -> Result<i32, DataError> {
|
2020-10-15 16:52:08 +00:00
|
|
|
let key = to_key(room_id, username, variable_name);
|
|
|
|
|
2020-10-18 20:36:47 +00:00
|
|
|
if let Some(raw_value) = self.variables.get(&key)? {
|
2020-10-20 20:54:20 +00:00
|
|
|
convert_i32(&raw_value)
|
2020-10-15 16:52:08 +00:00
|
|
|
} else {
|
2020-10-16 12:40:25 +00:00
|
|
|
Err(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 20:24:24 +00:00
|
|
|
pub async fn set_user_variable(
|
2020-10-15 16:52:08 +00:00
|
|
|
&self,
|
|
|
|
room_id: &str,
|
|
|
|
username: &str,
|
|
|
|
variable_name: &str,
|
|
|
|
value: i32,
|
|
|
|
) -> Result<(), DataError> {
|
2020-10-18 20:36:47 +00:00
|
|
|
self.variables
|
2020-10-20 20:54:20 +00:00
|
|
|
.transaction(|tx| {
|
|
|
|
let key = to_key(room_id, username, variable_name);
|
|
|
|
let db_value: I32<LittleEndian> = I32::new(value);
|
|
|
|
let old_value = tx.insert(key, db_value.as_bytes())?;
|
|
|
|
|
|
|
|
//Only increment variable count on new keys.
|
|
|
|
if let None = old_value {
|
|
|
|
match alter_room_variable_count(&tx, room_id, username, 1) {
|
|
|
|
Err(e) => abort(e),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(|e| e.into())
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 20:24:24 +00:00
|
|
|
pub async fn delete_user_variable(
|
2020-10-15 16:52:08 +00:00
|
|
|
&self,
|
|
|
|
room_id: &str,
|
|
|
|
username: &str,
|
|
|
|
variable_name: &str,
|
|
|
|
) -> Result<(), DataError> {
|
2020-10-20 20:54:20 +00:00
|
|
|
self.variables
|
|
|
|
.transaction(|tx| {
|
|
|
|
let key = to_key(room_id, username, variable_name);
|
|
|
|
if let Some(_) = tx.remove(key.clone())? {
|
|
|
|
match alter_room_variable_count(&tx, room_id, username, -1) {
|
|
|
|
Err(e) => abort(e),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
abort(DataError::KeyDoesNotExist(String::from_utf8(key).unwrap()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
//Room Variable count tests
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn alter_room_variable_count_test() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
|
|
|
|
let alter_count = |amount: i32| {
|
|
|
|
db.variables
|
|
|
|
.transaction(|tx| {
|
|
|
|
match alter_room_variable_count(&tx, "room", "username", amount) {
|
|
|
|
Err(e) => abort(e),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.expect("got transaction failure");
|
|
|
|
};
|
|
|
|
|
|
|
|
async fn get_count(db: &Database) -> i32 {
|
|
|
|
db.get_variable_count("room", "username")
|
|
|
|
.await
|
|
|
|
.expect("could not get variable count")
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
2020-10-20 20:54:20 +00:00
|
|
|
|
|
|
|
//addition
|
|
|
|
alter_count(5);
|
|
|
|
assert_eq!(5, get_count(&db).await);
|
|
|
|
|
|
|
|
//subtraction
|
|
|
|
alter_count(-3);
|
|
|
|
assert_eq!(2, get_count(&db).await);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn alter_room_variable_count_cannot_go_below_0_test() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
db.variables
|
|
|
|
.transaction(
|
|
|
|
|tx| match alter_room_variable_count(&tx, "room", "username", -1000) {
|
|
|
|
Err(e) => abort(e),
|
|
|
|
_ => Ok(()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("got transaction failure");
|
|
|
|
|
|
|
|
let count = db
|
|
|
|
.get_variable_count("room", "username")
|
|
|
|
.await
|
|
|
|
.expect("could not get variable count");
|
|
|
|
|
|
|
|
assert_eq!(0, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn empty_db_reports_0_room_variable_count_test() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
|
|
|
|
let count = db
|
|
|
|
.get_variable_count("room", "username")
|
|
|
|
.await
|
|
|
|
.expect("could not get variable count");
|
|
|
|
|
|
|
|
assert_eq!(0, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_user_variable_increments_count() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
db.set_user_variable("room", "username", "myvariable", 5)
|
|
|
|
.await
|
|
|
|
.expect("could not insert variable");
|
|
|
|
|
|
|
|
let count = db
|
|
|
|
.get_variable_count("room", "username")
|
|
|
|
.await
|
|
|
|
.expect("could not get variable count");
|
|
|
|
|
|
|
|
assert_eq!(1, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn update_user_variable_does_not_increment_count() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
db.set_user_variable("room", "username", "myvariable", 5)
|
|
|
|
.await
|
|
|
|
.expect("could not insert variable");
|
|
|
|
|
|
|
|
db.set_user_variable("room", "username", "myvariable", 10)
|
|
|
|
.await
|
|
|
|
.expect("could not update variable");
|
|
|
|
|
|
|
|
let count = db
|
|
|
|
.get_variable_count("room", "username")
|
|
|
|
.await
|
|
|
|
.expect("could not get variable count");
|
|
|
|
|
|
|
|
assert_eq!(1, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set/get/delete variable tests
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn set_and_get_variable_test() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
db.set_user_variable("room", "username", "myvariable", 5)
|
|
|
|
.await
|
|
|
|
.expect("could not insert variable");
|
|
|
|
|
|
|
|
let value = db
|
|
|
|
.get_user_variable("room", "username", "myvariable")
|
|
|
|
.await
|
|
|
|
.expect("could not get value");
|
|
|
|
|
|
|
|
assert_eq!(5, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn delete_variable_test() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
db.set_user_variable("room", "username", "myvariable", 5)
|
|
|
|
.await
|
|
|
|
.expect("could not insert variable");
|
|
|
|
|
|
|
|
db.delete_user_variable("room", "username", "myvariable")
|
|
|
|
.await
|
|
|
|
.expect("could not delete value");
|
|
|
|
|
|
|
|
let result = db.get_user_variable("room", "username", "myvariable").await;
|
|
|
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn get_missing_variable_returns_key_does_not_exist() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
let result = db.get_user_variable("room", "username", "myvariable").await;
|
|
|
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn remove_missing_variable_returns_key_does_not_exist() {
|
|
|
|
let db = Database::new(&tempdir().unwrap()).unwrap();
|
|
|
|
let result = db
|
|
|
|
.delete_user_variable("room", "username", "myvariable")
|
|
|
|
.await;
|
|
|
|
|
|
|
|
assert!(result.is_err());
|
|
|
|
assert!(matches!(result, Err(DataError::KeyDoesNotExist(_))));
|
2020-10-15 16:52:08 +00:00
|
|
|
}
|
|
|
|
}
|