start major state rework

new-idea
Avril 4 years ago
parent a057022d48
commit f8efb25c30
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -77,6 +77,9 @@ pub struct Post
/// Unique ID for each post /// Unique ID for each post
id: PostID, id: PostID,
/// Who created the post, (if specified)
owner: Option<user::UserID>,
/// Identifiers for this post /// Identifiers for this post
ident: Ident, ident: Ident,
@ -220,6 +223,7 @@ mod tests
{ {
use std::convert::TryInto; use std::convert::TryInto;
let post = super::Post { let post = super::Post {
owner: None,
id: super::PostID::id_new(), id: super::PostID::id_new(),
ident: super::Ident { ident: super::Ident {
name: Some("Some name".to_owned().try_into().unwrap()), name: Some("Some name".to_owned().try_into().unwrap()),

@ -1,17 +1,19 @@
//! Frozen, serialisable state //! Frozen, serialisable state
use super::*; use super::*;
use futures::prelude::*; //use futures::prelude::*;
use std::io; //use std::io;
use tokio::prelude::*; //use tokio::prelude::*;
/// An immutable image of `State`. /// An immutable image of `State`.
//TODO: Implement this when `State` is solidified and working
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Freeze pub struct Freeze
{ {
users: HashSet<User>, users: HashSet<User>,
posts: Vec<(UserID, Post)>, posts: HashSet<Post>,
} }
/*
/// Reading and writing state /// Reading and writing state
//TODO: Compression //TODO: Compression
impl Freeze impl Freeze
@ -203,3 +205,4 @@ impl TryFrom<State> for Freeze
from.try_into_freeze() from.try_into_freeze()
} }
} }
*/

@ -1,32 +1,37 @@
use super::*; use super::*;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::{ use tokio::sync::RwLock;
RwLock, use generational_arena::{
RwLockReadGuard, Arena,Index,
}; };
use user::{User, UserID}; use user::{User, UserID};
use post::Post; use post::{Post, PostID};
mod freeze; mod freeze;
pub use freeze::*; pub use freeze::*;
#[derive(Debug)] #[derive(Debug)]
struct Posts struct Oneesan
{ {
users: HashMap<UserID, RwLock<User>>, users: Arena<Arc<RwLock<User>>>,
posts: HashMap<UserID, MaybeVec<RwLock<Post>>>, posts: Arena<Arc<RwLock<Post>>>,
}
/// A read lock over all posts and users /// Maps `UserID`s to indexes in the `users` arena.
#[derive(Debug)] users_map: HashMap<UserID, Index>,
pub struct PostsLock<'a>(RwLockReadGuard<'a, Posts>); /// 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)] #[derive(Debug)]
struct Inner struct Inner
{ {
posts: RwLock<Posts>, /// The posts and user state.
oneesan: RwLock<Oneesan>,
} }
/// Contains all posts and users /// Contains all posts and users
@ -40,42 +45,16 @@ impl State
{ {
Self(Arc::new( Self(Arc::new(
Inner { Inner {
posts: RwLock::new(Posts { oneesan: RwLock::new(Oneesan {
users: HashMap::new(), users: Arena::new(),
posts: HashMap::new(), posts: Arena::new(),
})
}
))
}
/// Read lock over all posts and users users_map: HashMap::new(),
pub async fn posts(&self) -> PostsLock<'_> posts_map: HashMap::new(),
{
PostsLock(self.0.posts.read().await)
}
}
impl<'a> PostsLock<'a>
{
/// An iterator over all users in this state
pub fn users<'b>(&'b self) -> impl Iterator<Item = &'b RwLock<User>> + ExactSizeIterator + 'b
{
self.0.users.iter().map(|(_, u)| u)
}
/// An iterator over all posts for this user posts_user_map: HashMap::new(),
/// })
/// # Notes
/// If this user does not exist, the returned iterator will yield 0 items.
pub fn posts_for<'b>(&'b self, user: &UserID) -> impl Iterator<Item= &'b RwLock<Post>> + ExactSizeIterator + 'b
where 'a: 'b
{
MaybeIter::from(self.0.posts.get(user).map(|x| x.iter()))
} }
/// An iterator over all posts in the state ))
pub fn all_posts<'b>(&'b self) -> impl Iterator<Item= &'b RwLock<Post>> + 'b
where 'a: 'b
{
self.0.posts.iter().map(|(_, p)| p.iter()).flatten()
} }
} }

Loading…
Cancel
Save