Cache guilds.

This commit is contained in:
Paul Makles
2020-08-03 15:23:32 +02:00
parent fa6ed75ed8
commit 6a1912c27b
7 changed files with 327 additions and 221 deletions

View File

@@ -1,9 +1,11 @@
use crate::database::get_collection;
use super::get_collection;
use serde::{Deserialize, Serialize};
use rocket::request::FromParam;
use std::sync::{Arc, Mutex};
use lru::LruCache;
use bson::{doc, from_bson};
use rocket::http::RawStr;
use lru::LruCache;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
@@ -42,6 +44,100 @@ lazy_static! {
static ref CACHE: Arc<Mutex<LruCache<String, Channel>>> = Arc::new(Mutex::new(LruCache::new(4_000_000)));
}
pub fn fetch_channel(id: &str) -> Result<Option<Channel>, String> {
{
if let Ok(mut cache) = CACHE.lock() {
let existing = cache.get(&id.to_string());
if let Some(channel) = existing {
return Ok(Some((*channel).clone()));
}
} else {
return Err("Failed to lock cache.".to_string());
}
}
let col = get_collection("channels");
if let Ok(result) = col.find_one(doc! { "_id": id }, None) {
if let Some(doc) = result {
if let Ok(channel) = from_bson(bson::Bson::Document(doc)) as Result<Channel, _> {
let mut cache = CACHE.lock().unwrap();
cache.put(id.to_string(), channel.clone());
Ok(Some(channel))
} else {
Err("Failed to deserialize channel!".to_string())
}
} else {
Ok(None)
}
} else {
Err("Failed to fetch channel from database.".to_string())
}
}
pub fn fetch_channels(ids: &Vec<String>) -> Result<Option<Vec<Channel>>, String> {
let mut missing = vec![];
let mut channels = vec![];
{
if let Ok(mut cache) = CACHE.lock() {
for gid in ids {
let existing = cache.get(gid);
if let Some(channel) = existing {
channels.push((*channel).clone());
} else {
missing.push(gid);
}
}
} else {
return Err("Failed to lock cache.".to_string());
}
}
if missing.len() == 0 {
return Ok(Some(channels))
}
let col = get_collection("channels");
if let Ok(result) = col.find(doc! { "_id": { "$in": missing } }, None) {
for item in result {
let mut cache = CACHE.lock().unwrap();
if let Ok(doc) = item {
if let Ok(channel) = from_bson(bson::Bson::Document(doc)) as Result<Channel, _> {
cache.put(channel.id.clone(), channel.clone());
channels.push(channel);
} else {
return Err("Failed to deserialize channel!".to_string())
}
} else {
return Err("Failed to fetch channel.".to_string());
}
}
Ok(Some(channels))
} else {
Err("Failed to fetch channel from database.".to_string())
}
}
impl<'r> FromParam<'r> for Channel {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
if let Ok(result) = fetch_channel(param) {
if let Some(channel) = result {
Ok(channel)
} else {
Err(param)
}
} else {
Err(param)
}
}
}
/*pub fn test() {
use std::time::Instant;
@@ -75,28 +171,3 @@ lazy_static! {
println!("It took {} seconds, roughly {}ms per entry.", now.elapsed().as_secs_f64(), now.elapsed().as_millis() as f64 / 1_000_000.0);
}*/
pub fn fetch_channel(id: &str) -> Channel {
{
let mut cache = CACHE.lock().unwrap();
let existing = cache.get(&id.to_string());
if let Some(channel) = existing {
return (*channel).clone();
}
}
let col = get_collection("channels");
let result = col.find_one(doc! { "_id": id }, None).unwrap();
if let Some(doc) = result {
let channel: Channel = from_bson(bson::Bson::Document(doc)).expect("Failed to unwrap channel.");
let mut cache = CACHE.lock().unwrap();
cache.put(id.to_string(), channel.clone());
return channel;
}
panic!("Channel does not exist!");
}

View File

@@ -1,20 +1,26 @@
use bson::doc;
use serde::{Deserialize, Serialize};
use super::get_collection;
#[derive(Serialize, Deserialize, Debug)]
use serde::{Deserialize, Serialize};
use rocket::request::FromParam;
use std::sync::{Arc, Mutex};
use bson::{doc, from_bson};
use rocket::http::RawStr;
use lru::LruCache;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemberRef {
pub guild: String,
pub user: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Member {
#[serde(rename = "_id")]
pub id: MemberRef,
pub nickname: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Invite {
pub code: String,
pub creator: String,
@@ -27,7 +33,7 @@ pub struct Ban {
pub reason: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Guild {
#[serde(rename = "_id")]
pub id: String,
@@ -41,3 +47,131 @@ pub struct Guild {
pub default_permissions: u32,
}
lazy_static! {
static ref CACHE: Arc<Mutex<LruCache<String, Guild>>> = Arc::new(Mutex::new(LruCache::new(4_000_000)));
}
pub fn fetch_guild(id: &str) -> Result<Option<Guild>, String> {
{
if let Ok(mut cache) = CACHE.lock() {
let existing = cache.get(&id.to_string());
if let Some(guild) = existing {
return Ok(Some((*guild).clone()));
}
} else {
return Err("Failed to lock cache.".to_string());
}
}
let col = get_collection("guilds");
if let Ok(result) = col.find_one(doc! { "_id": id }, None) {
if let Some(doc) = result {
if let Ok(guild) = from_bson(bson::Bson::Document(doc)) as Result<Guild, _> {
let mut cache = CACHE.lock().unwrap();
cache.put(id.to_string(), guild.clone());
Ok(Some(guild))
} else {
Err("Failed to deserialize guild!".to_string())
}
} else {
Ok(None)
}
} else {
Err("Failed to fetch guild from database.".to_string())
}
}
impl<'r> FromParam<'r> for Guild {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
if let Ok(result) = fetch_guild(param) {
if let Some(channel) = result {
Ok(channel)
} else {
Err(param)
}
} else {
Err(param)
}
}
}
pub fn get_member(guild_id: &String, member: &String) -> Option<Member> {
if let Ok(result) = get_collection("members").find_one(
doc! {
"_id.guild": &guild_id,
"_id.user": &member,
},
None,
) {
if let Some(doc) = result {
Some(from_bson(bson::Bson::Document(doc)).expect("Failed to unwrap member."))
} else {
None
}
} else {
None
}
}
pub fn get_invite<U: Into<Option<String>>>(
code: &String,
user: U,
) -> Option<(String, String, Invite)> {
let mut doc = doc! {
"invites": {
"$elemMatch": {
"code": &code
}
}
};
if let Some(user_id) = user.into() {
doc.insert(
"bans",
doc! {
"$not": {
"$elemMatch": {
"id": user_id
}
}
},
);
}
if let Ok(result) = get_collection("guilds").find_one(
doc,
mongodb::options::FindOneOptions::builder()
.projection(doc! {
"_id": 1,
"name": 1,
"invites.$": 1,
})
.build(),
) {
if let Some(doc) = result {
let invite = doc
.get_array("invites")
.unwrap()
.iter()
.next()
.unwrap()
.as_document()
.unwrap();
Some((
doc.get_str("_id").unwrap().to_string(),
doc.get_str("name").unwrap().to_string(),
from_bson(bson::Bson::Document(invite.clone())).unwrap(),
))
} else {
None
}
} else {
None
}
}

View File

@@ -1,12 +1,15 @@
use super::get_collection;
use crate::notifications;
use crate::notifications::events::message::Create;
use crate::notifications::events::Notification;
use crate::database::channel::Channel;
use crate::routes::channel::ChannelType;
use crate::database::channel::Channel;
use super::get_collection;
use crate::notifications;
use bson::{doc, to_bson, UtcDateTime};
use serde::{Deserialize, Serialize};
use rocket::request::FromParam;
use bson::from_bson;
use rocket::http::RawStr;
#[derive(Serialize, Deserialize, Debug)]
pub struct PreviousEntry {
@@ -87,3 +90,20 @@ impl Message {
}
}
}
impl<'r> FromParam<'r> for Message {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let col = get_collection("messages");
let result = col
.find_one(doc! { "_id": param.to_string() }, None)
.unwrap();
if let Some(message) = result {
Ok(from_bson(bson::Bson::Document(message)).expect("Failed to unwrap message."))
} else {
Err(param)
}
}
}

View File

@@ -1,9 +1,8 @@
use super::mutual::has_mutual_connection;
use crate::database::channel::Channel;
use crate::database::guild::Member;
use crate::database::guild::{Guild, Member, get_member, fetch_guild};
use crate::database::user::UserRelationship;
use crate::guards::auth::UserRef;
use crate::guards::guild::{get_member, GuildRef};
use num_enum::TryFromPrimitive;
@@ -89,7 +88,7 @@ pub fn get_relationship(a: &UserRef, b: &UserRef) -> Relationship {
pub struct PermissionCalculator {
pub user: UserRef,
pub channel: Option<Channel>,
pub guild: Option<GuildRef>,
pub guild: Option<Guild>,
pub member: Option<Member>,
}
@@ -110,7 +109,7 @@ impl PermissionCalculator {
}
}
pub fn guild(self, guild: GuildRef) -> PermissionCalculator {
pub fn guild(self, guild: Guild) -> PermissionCalculator {
PermissionCalculator {
guild: Some(guild),
..self
@@ -125,7 +124,11 @@ impl PermissionCalculator {
0..=1 => None,
2 => {
if let Some(id) = &channel.guild {
GuildRef::from(id.clone())
if let Ok(result) = fetch_guild(id) {
result
} else {
None
}
} else {
None
}