From 5d19d3c72f5af04c76cdb739fb9fbe8f10f721ac Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 21 Aug 2012 11:25:02 +0200 Subject: [PATCH] Optimice the partial queries --- admin.go | 10 +++++----- database.go | 30 ++++++++++++++++++++++++++---- search.go | 15 +++++---------- trantor.go | 2 +- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/admin.go b/admin.go index af9f325..10beea9 100644 --- a/admin.go +++ b/admin.go @@ -23,7 +23,7 @@ func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, * // cutre hack: /delete/ and /delnew/ have the same lenght: id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):]) - books, err := GetBook(coll, bson.M{"_id": id}) + books, _, err := GetBook(coll, bson.M{"_id": id}) if err != nil { http.NotFound(w, r) return @@ -47,7 +47,7 @@ func editHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) return } id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):]) - books, err := GetBook(coll, bson.M{"_id": id}) + books, _, err := GetBook(coll, bson.M{"_id": id}) if err != nil { http.NotFound(w, r) return @@ -122,10 +122,10 @@ func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) { return } - res, _ := GetBook(coll, bson.M{}) + res, num, _ := GetBook(coll, bson.M{}) var data newData data.S = GetStatus(w, r) - data.Found = len(res) + data.Found = num data.Books = res loadTemplate(w, "new", data) } @@ -140,7 +140,7 @@ func storeHandler(newColl, coll *mgo.Collection) func(http.ResponseWriter, *http } id := bson.ObjectIdHex(r.URL.Path[len("/store/"):]) - books, err := GetBook(newColl, bson.M{"_id": id}) + books, _, err := GetBook(newColl, bson.M{"_id": id}) if err != nil { http.NotFound(w, r) return diff --git a/database.go b/database.go index a956b6b..c840abc 100644 --- a/database.go +++ b/database.go @@ -28,12 +28,34 @@ type Book struct { Keywords []string } -func GetBook(coll *mgo.Collection, query bson.M) ([]Book, error) { - var books []Book - err := coll.Find(query).All(&books) +/* optional parameters: length and start index + * + * Returns: list of books, number found and err + */ +func GetBook(coll *mgo.Collection, query bson.M, r ...int) (books []Book, num int, err error) { + var start, length int + if len(r) > 0 { + length = r[0] + if len(r) > 1 { + start = r[1] + } + } + q := coll.Find(query) + num, err = q.Count() + if err != nil { + return + } + if start != 0 { + q = q.Skip(start) + } + if length != 0 { + q = q.Limit(length) + } + + err = q.All(&books) for i, b := range books { books[i].Id = bson.ObjectId(b.Id).Hex() } - return books, err + return } diff --git a/search.go b/search.go index 4f0a1af..f58e230 100644 --- a/search.go +++ b/search.go @@ -47,27 +47,22 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request return } req := strings.Join(r.Form["q"], " ") - res, _ := GetBook(coll, buildQuery(req)) - page := 0 if len(r.Form["p"]) != 0 { page, err = strconv.Atoi(r.Form["p"][0]) - if err != nil || len(res) < ITEMS_PAGE*page { + if err != nil { page = 0 } } + res, num, _ := GetBook(coll, buildQuery(req), ITEMS_PAGE, page) var data searchData data.S = GetStatus(w, r) data.S.Search = req - data.Found = len(res) - if len(res) > ITEMS_PAGE*(page+1) { - data.Books = res[ITEMS_PAGE*page : ITEMS_PAGE*(page+1)] - } else { - data.Books = res[ITEMS_PAGE*page:] - } + data.Books = res + data.Found = num data.Page = page + 1 - if len(res) > (page+1)*ITEMS_PAGE { + if num > (page+1)*ITEMS_PAGE { data.Next = "/search/?q=" + req + "&p=" + strconv.Itoa(page+1) } if page > 0 { diff --git a/trantor.go b/trantor.go index de46baa..3fdc081 100644 --- a/trantor.go +++ b/trantor.go @@ -66,7 +66,7 @@ func bookHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) return func(w http.ResponseWriter, r *http.Request) { var data bookData data.S = GetStatus(w, r) - books, err := GetBook(coll, bson.M{"title": r.URL.Path[len("/book/"):]}) + books, _, err := GetBook(coll, bson.M{"title": r.URL.Path[len("/book/"):]}) if err != nil || len(books) == 0 { http.NotFound(w, r) return