|
|
|
use super::*;
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
use generational_arena::{
|
|
|
|
Arena,Index,
|
|
|
|
};
|
|
|
|
|
|
|
|
use user::{User, UserID};
|
|
|
|
use post::{Post, PostID};
|
|
|
|
|
|
|
|
mod freeze;
|
|
|
|
pub use freeze::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Oneesan
|
|
|
|
{
|
|
|
|
users: Arena<Arc<RwLock<User>>>,
|
|
|
|
posts: Arena<Arc<RwLock<Post>>>,
|
|
|
|
|
|
|
|
/// Maps `UserID`s to indexes in the `users` arena.
|
|
|
|
users_map: HashMap<UserID, Index>,
|
|
|
|
/// Maps `PostID`s to indexies in the `posts` arena.
|
|
|
|
posts_map: HashMap<PostID, Index>,
|
|
|
|
|
|
|
|
/// Maps `UserID`s to the user's owned posts in the `posts` arena.
|
|
|
|
posts_user_map: HashMap<UserID, MaybeVec<Index>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Inner
|
|
|
|
{
|
|
|
|
/// The posts and user state.
|
|
|
|
oneesan: RwLock<Oneesan>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Contains all posts and users
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct State(Arc<Inner>);
|
|
|
|
|
|
|
|
impl State
|
|
|
|
{
|
|
|
|
/// Create a new empty state
|
|
|
|
pub fn new() -> Self
|
|
|
|
{
|
|
|
|
Self(Arc::new(
|
|
|
|
Inner {
|
|
|
|
oneesan: RwLock::new(Oneesan {
|
|
|
|
users: Arena::new(),
|
|
|
|
posts: Arena::new(),
|
|
|
|
|
|
|
|
users_map: HashMap::new(),
|
|
|
|
posts_map: HashMap::new(),
|
|
|
|
|
|
|
|
posts_user_map: HashMap::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|