Update database layout for rooms

This commit is contained in:
Niels Andriesse 2021-03-18 15:24:47 +11:00
parent 1e93f9472e
commit 2e24270092
3 changed files with 38 additions and 6 deletions

View file

@ -17,9 +17,9 @@ mod tests;
async fn main() {
let public_key = hex::encode(crypto::PUBLIC_KEY.as_bytes());
println!("The public key of this server is: {}", public_key);
storage::create_main_database_if_needed();
let main_room = "main";
storage::create_database_if_needed(main_room);
storage::create_database_if_needed(main_room);
let prune_pending_tokens_future = storage::prune_pending_tokens_periodically();
let prune_tokens_future = storage::prune_tokens_periodically();
let routes = routes::root().or(routes::lsrpc());

View file

@ -23,7 +23,7 @@ pub struct QueryOptions {
pub async fn handle_rpc_call(rpc_call: RpcCall) -> Result<Response, Rejection> {
// Get a connection pool for the given room
let room = "main";
let room = "main"; // TODO: Get room from RPC call
let pool = storage::pool(room);
// Check that the endpoint is a valid URI
let uri = match rpc_call.endpoint.parse::<http::Uri>() {

View file

@ -8,6 +8,36 @@ use r2d2_sqlite::SqliteConnectionManager;
pub type DatabaseConnection = r2d2::PooledConnection<SqliteConnectionManager>;
pub type DatabaseConnectionPool = r2d2::Pool<SqliteConnectionManager>;
// Main
pub const MAIN_TABLE: &str = "main";
lazy_static::lazy_static! {
static ref MAIN_POOL: DatabaseConnectionPool = {
let file_name = format!("database.db");
let db_manager = r2d2_sqlite::SqliteConnectionManager::file(file_name);
return r2d2::Pool::new(db_manager).unwrap();
};
}
pub fn create_main_database_if_needed() {
let pool = &MAIN_POOL;
let conn = pool.get().unwrap();
create_main_tables_if_needed(&conn);
}
fn create_main_tables_if_needed(conn: &DatabaseConnection) {
let main_table_cmd = format!(
"CREATE TABLE IF NOT EXISTS {} (
id TEXT PRIMARY KEY,
name TEXT
)", MAIN_TABLE);
conn.execute(&main_table_cmd, params![]).expect("Couldn't create main table.");
}
// Rooms
pub const PENDING_TOKEN_EXPIRATION: i64 = 10 * 60;
pub const TOKEN_EXPIRATION: i64 = 7 * 24 * 60 * 60;
@ -29,7 +59,7 @@ pub fn pool(room: &str) -> DatabaseConnectionPool {
return pool.clone();
} else {
let file_name = format!("{}.db", room);
let db_manager = r2d2_sqlite::SqliteConnectionManager::file(file_name);
let db_manager = r2d2_sqlite::SqliteConnectionManager::file(format!("rooms/{}", file_name));
let pool = r2d2::Pool::new(db_manager).unwrap();
pools.insert(room.to_string(), pool);
return pools[room].clone();
@ -39,10 +69,10 @@ pub fn pool(room: &str) -> DatabaseConnectionPool {
pub fn create_database_if_needed(room: &str) {
let pool = pool(room);
let conn = pool.get().unwrap();
create_tables_if_needed(&conn);
create_room_tables_if_needed(&conn);
}
fn create_tables_if_needed(conn: &DatabaseConnection) {
fn create_room_tables_if_needed(conn: &DatabaseConnection) {
// Messages
// The `id` field is needed to make `rowid` stable, which is important because otherwise
// the `id`s in this table won't correspond to those in the deleted messages table
@ -91,6 +121,8 @@ fn create_tables_if_needed(conn: &DatabaseConnection) {
conn.execute(&tokens_table_cmd, params![]).expect("Couldn't create tokens table.");
}
// Pruning
pub async fn prune_tokens_periodically() {
let mut timer = tokio::time::interval(chrono::Duration::minutes(10).to_std().unwrap());
loop {