Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Merge pull request #395 from sfan5/searchperf

(Search) performance
Cette révision appartient à :
PantsuDev 2017-05-13 12:24:03 +10:00 révisé par GitHub
révision 44b327e1a3
5 fichiers modifiés avec 35 ajouts et 16 suppressions

Voir le fichier

@ -38,8 +38,8 @@ func RunServer(conf *config.Config) {
// Set up server,
srv := &http.Server{
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 24 * time.Second,
ReadTimeout: 8 * time.Second,
}
l, err := network.CreateHTTPListener(conf)
log.CheckError(err)

Voir le fichier

@ -6,7 +6,6 @@ import (
"github.com/ewhal/nyaa/util/search"
"github.com/gorilla/feeds"
"net/http"
"strconv"
"time"
)
@ -31,7 +30,7 @@ func RSSHandler(w http.ResponseWriter, r *http.Request) {
for i, torrent := range torrents {
torrentJSON := torrent.ToJSON()
feed.Items[i] = &feeds.Item{
Id: "https://" + config.WebAddress + "/view/" + strconv.FormatUint(uint64(torrents[i].ID), 10),
Id: "https://" + config.WebAddress + "/view/" + torrentJSON.ID,
Title: torrent.Name,
Link: &feeds.Link{Href: string(torrentJSON.Magnet)},
Description: string(torrentJSON.Description),

Voir le fichier

@ -46,4 +46,4 @@ func (wh *wrappedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func wrapHandler(handler http.Handler) http.Handler {
return &wrappedHandler{handler}
}
}

Voir le fichier

@ -129,9 +129,10 @@ func getTorrentsOrderBy(parameters *serviceBase.WhereParams, orderBy string, lim
if conditions != "" {
dbQuery = dbQuery + " WHERE " + conditions
}
if strings.Contains(conditions, "torrent_name") {
/* This makes all queries take roughly the same amount of time (lots)...
if strings.Contains(conditions, "torrent_name") && offset > 0 {
dbQuery = "WITH t AS (SELECT * FROM torrents WHERE " + conditions + ") SELECT * FROM t"
}
}*/
if orderBy == "" { // default OrderBy
orderBy = "torrent_id DESC"

Voir le fichier

@ -18,17 +18,31 @@ import (
)
var searchOperator string
var useTSQuery bool
func Configure(conf *config.SearchConfig) (err error) {
// SQLite has case-insensitive LIKE, but no ILIKE
if db.ORM.Dialect().GetName() == "sqlite3" {
searchOperator = "LIKE ?"
} else {
useTSQuery = false
// Postgres needs ILIKE for case-insensitivity
if db.ORM.Dialect().GetName() == "postgres" {
searchOperator = "ILIKE ?"
//useTSQuery = true
// !!DISABLED!! because this makes search a lot stricter
// (only matches at word borders)
} else {
searchOperator = "LIKE ?"
}
return
}
func stringIsAscii(input string) bool {
for _, char := range input {
if char > 127 {
return false
}
}
return true
}
func SearchByQuery(r *http.Request, pagenum int) (search common.SearchParam, tor []model.Torrent, count int, err error) {
search, tor, count, err = searchByQuery(r, pagenum, true)
return
@ -164,7 +178,7 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
}
searchQuerySplit := strings.Fields(search.Query)
for i, word := range searchQuerySplit {
for _, word := range searchQuerySplit {
firstRune, _ := utf8.DecodeRuneInString(word)
if len(word) == 1 && unicode.IsPunct(firstRune) {
// some queries have a single punctuation character
@ -175,9 +189,14 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
continue
}
// TODO: make this faster ?
conditions = append(conditions, "torrent_name "+searchOperator)
parameters.Params = append(parameters.Params, "%"+searchQuerySplit[i]+"%")
if useTSQuery && stringIsAscii(word) {
conditions = append(conditions, "torrent_name @@ plainto_tsquery(?)")
parameters.Params = append(parameters.Params, word)
} else {
// TODO: possible to make this faster?
conditions = append(conditions, "torrent_name "+searchOperator)
parameters.Params = append(parameters.Params, "%"+word+"%")
}
}
parameters.Conditions = strings.Join(conditions[:], " AND ")
@ -194,4 +213,4 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
return
})
return
}
}