From 18d6ad39920580866c3d6485d5117b92c444db25 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 9 May 2017 11:07:42 -0400 Subject: [PATCH] make rss fast again :^) --- main.go | 1 + router/rssHandler.go | 2 +- service/torrent/torrent.go | 19 +++++++++++++++---- util/search/search.go | 16 +++++++++++++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 3dc344ba..a8510886 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bufio" "flag" + "github.com/nicksnyder/go-i18n/i18n" "github.com/ewhal/nyaa/config" diff --git a/router/rssHandler.go b/router/rssHandler.go index 102ffc89..381b30a7 100644 --- a/router/rssHandler.go +++ b/router/rssHandler.go @@ -13,7 +13,7 @@ import ( func RssHandler(w http.ResponseWriter, r *http.Request) { - _, torrents, _, err := search.SearchByQuery(r, 1) + _, torrents, err := search.SearchByQueryNoCount(r, 1) if err != nil { util.SendError(w, err, 400) return diff --git a/service/torrent/torrent.go b/service/torrent/torrent.go index 72b83e7a..7426a4f7 100644 --- a/service/torrent/torrent.go +++ b/service/torrent/torrent.go @@ -67,7 +67,17 @@ func GetTorrentById(id string) (model.Torrents, error) { return torrent, nil } +func GetTorrentsOrderByNoCount(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, err error) { + torrents, _, err = getTorrentsOrderBy(parameters, orderBy, limit, offset, false) + return +} + func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int) (torrents []model.Torrents, count int, err error) { + torrents, count, err = getTorrentsOrderBy(parameters, orderBy, limit, offset, true) + return +} + +func getTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offset int, countAll bool) (torrents []model.Torrents, count int, err error) { var conditionArray []string if strings.HasPrefix(orderBy, "filesize") { // torrents w/ NULL filesize fuck up the sorting on postgres @@ -81,11 +91,12 @@ func GetTorrentsOrderBy(parameters *WhereParams, orderBy string, limit int, offs params = parameters.Params } conditions := strings.Join(conditionArray, " AND ") - err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error - if err != nil { - return + if countAll { + err = db.ORM.Model(&torrents).Where(conditions, params...).Count(&count).Error + if err != nil { + return + } } - // TODO: Vulnerable to injections. Use query builder. // build custom db query for performance reasons diff --git a/util/search/search.go b/util/search/search.go index 415ee4e8..a4a848d7 100644 --- a/util/search/search.go +++ b/util/search/search.go @@ -57,6 +57,16 @@ type SearchParam struct { } func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, count int, err error) { + search, tor, count, err = searchByQuery(r, pagenum, true) + return +} + +func SearchByQueryNoCount(r *http.Request, pagenum int) (search SearchParam, tor []model.Torrents, err error) { + search, tor, _, err = searchByQuery(r, pagenum, false) + return +} + +func searchByQuery(r *http.Request, pagenum int, countAll bool) (search SearchParam, tor []model.Torrents, count int, err error) { max, err := strconv.ParseUint(r.URL.Query().Get("max"), 10, 32) if err != nil { err = nil @@ -180,6 +190,10 @@ func SearchByQuery(r *http.Request, pagenum int) (search SearchParam, tor []mode parameters.Conditions = strings.Join(conditions[:], " AND ") log.Infof("SQL query is :: %s\n", parameters.Conditions) - tor, count, err = torrentService.GetTorrentsOrderBy(¶meters, order_by, int(search.Max), int(search.Max)*(pagenum-1)) + if countAll { + tor, count, err = torrentService.GetTorrentsOrderBy(¶meters, order_by, int(search.Max), int(search.Max)*(pagenum-1)) + } else { + tor, err = torrentService.GetTorrentsOrderByNoCount(¶meters, order_by, int(search.Max), int(search.Max)*(pagenum-1)) + } return }