Count items in database as much as possible (#344)
* Count items in database as much as possible * Fix the tests * Remove two useless queries * Run pragma directive before each sqlite connection * Pragma for tests too * Remove debug messages
This commit is contained in:
+4
-2
@@ -39,7 +39,7 @@ use diesel::r2d2::ConnectionManager;
|
||||
use rocket::State;
|
||||
use rocket_csrf::CsrfFairingBuilder;
|
||||
use plume_models::{DATABASE_URL, Connection,
|
||||
db_conn::DbPool, search::Searcher as UnmanagedSearcher};
|
||||
db_conn::{DbPool, PragmaForeignKey}, search::Searcher as UnmanagedSearcher};
|
||||
use scheduled_thread_pool::ScheduledThreadPool;
|
||||
use std::process::exit;
|
||||
use std::sync::Arc;
|
||||
@@ -59,7 +59,9 @@ fn init_pool() -> Option<DbPool> {
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let manager = ConnectionManager::<Connection>::new(DATABASE_URL.as_str());
|
||||
DbPool::new(manager).ok()
|
||||
DbPool::builder()
|
||||
.connection_customizer(Box::new(PragmaForeignKey))
|
||||
.build(manager).ok()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
+3
-3
@@ -29,7 +29,7 @@ pub fn details(intl: I18n, name: String, conn: DbConn, user: Option<User>, page:
|
||||
let blog = Blog::find_by_fqn(&*conn, &name)
|
||||
.ok_or_else(|| render!(errors::not_found(&(&*conn, &intl.catalog, user.clone()))))?;
|
||||
let posts = Post::blog_page(&*conn, &blog, page.limits());
|
||||
let articles = Post::get_for_blog(&*conn, &blog); // TODO only count them in DB
|
||||
let articles_count = Post::count_for_blog(&*conn, &blog);
|
||||
let authors = &blog.list_authors(&*conn);
|
||||
|
||||
Ok(render!(blogs::details(
|
||||
@@ -37,9 +37,9 @@ pub fn details(intl: I18n, name: String, conn: DbConn, user: Option<User>, page:
|
||||
blog.clone(),
|
||||
blog.get_fqn(&*conn),
|
||||
authors,
|
||||
articles.len(),
|
||||
articles_count,
|
||||
page.0,
|
||||
Page::total(articles.len() as i32),
|
||||
Page::total(articles_count as i32),
|
||||
user.map(|x| x.is_author_in(&*conn, &blog)).unwrap_or(false),
|
||||
posts
|
||||
)))
|
||||
|
||||
@@ -75,8 +75,8 @@ pub fn create(blog_name: String, slug: String, form: LenientForm<NewCommentForm>
|
||||
Tag::for_post(&*conn, post.id),
|
||||
comments.into_iter().filter(|c| c.in_response_to_id.is_none()).collect::<Vec<Comment>>(),
|
||||
previous,
|
||||
post.get_likes(&*conn).len(),
|
||||
post.get_reshares(&*conn).len(),
|
||||
post.count_likes(&*conn),
|
||||
post.count_reshares(&*conn),
|
||||
user.has_liked(&*conn, &post),
|
||||
user.has_reshared(&*conn, &post),
|
||||
user.is_following(&*conn, post.get_authors(&*conn)[0].id),
|
||||
|
||||
@@ -37,8 +37,8 @@ pub fn index(conn: DbConn, user: Option<User>, intl: I18n) -> Ructe {
|
||||
render!(instance::index(
|
||||
&(&*conn, &intl.catalog, user),
|
||||
inst,
|
||||
User::count_local(&*conn) as i32,
|
||||
Post::count_local(&*conn) as i32,
|
||||
User::count_local(&*conn),
|
||||
Post::count_local(&*conn),
|
||||
local,
|
||||
federated,
|
||||
user_feed
|
||||
|
||||
@@ -13,7 +13,7 @@ pub fn notifications(conn: DbConn, user: User, page: Option<Page>, intl: I18n) -
|
||||
&(&*conn, &intl.catalog, Some(user.clone())),
|
||||
Notification::page_for_user(&*conn, &user, page.limits()),
|
||||
page.0,
|
||||
Page::total(Notification::find_for_user(&*conn, &user).len() as i32)
|
||||
Page::total(Notification::count_for_user(&*conn, &user) as i32)
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -66,8 +66,8 @@ pub fn details(blog: String, slug: String, conn: DbConn, user: Option<User>, res
|
||||
Tag::for_post(&*conn, post.id),
|
||||
comments.into_iter().filter(|c| c.in_response_to_id.is_none()).collect::<Vec<Comment>>(),
|
||||
previous,
|
||||
post.get_likes(&*conn).len(),
|
||||
post.get_reshares(&*conn).len(),
|
||||
post.count_likes(&*conn),
|
||||
post.count_reshares(&*conn),
|
||||
user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
|
||||
user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false),
|
||||
user.map(|u| u.is_following(&*conn, post.get_authors(&*conn)[0].id)).unwrap_or(false),
|
||||
|
||||
+1
-1
@@ -168,7 +168,7 @@ pub fn follow_auth(name: String, i18n: I18n) -> Flash<Redirect> {
|
||||
pub fn followers(name: String, conn: DbConn, account: Option<User>, page: Option<Page>, intl: I18n) -> Result<Ructe, Ructe> {
|
||||
let page = page.unwrap_or_default();
|
||||
let user = User::find_by_fqn(&*conn, &name).ok_or_else(|| render!(errors::not_found(&(&*conn, &intl.catalog, account.clone()))))?;
|
||||
let followers_count = user.get_followers(&*conn).len(); // TODO: count in DB
|
||||
let followers_count = user.count_followers(&*conn);
|
||||
|
||||
Ok(render!(users::followers(
|
||||
&(&*conn, &intl.catalog, account.clone()),
|
||||
|
||||
Reference in New Issue
Block a user