Optimice the partial queries

This commit is contained in:
Las Zenow 2012-08-21 11:25:02 +02:00
parent 8401826db0
commit 5d19d3c72f
4 changed files with 37 additions and 20 deletions

View file

@ -23,7 +23,7 @@ func deleteHandler(coll *mgo.Collection, url string) func(http.ResponseWriter, *
// cutre hack: /delete/ and /delnew/ have the same lenght: // cutre hack: /delete/ and /delnew/ have the same lenght:
id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):]) 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 { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -47,7 +47,7 @@ func editHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
return return
} }
id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):]) 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 { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -122,10 +122,10 @@ func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return return
} }
res, _ := GetBook(coll, bson.M{}) res, num, _ := GetBook(coll, bson.M{})
var data newData var data newData
data.S = GetStatus(w, r) data.S = GetStatus(w, r)
data.Found = len(res) data.Found = num
data.Books = res data.Books = res
loadTemplate(w, "new", data) 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/"):]) 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 { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
return return

View file

@ -28,12 +28,34 @@ type Book struct {
Keywords []string Keywords []string
} }
func GetBook(coll *mgo.Collection, query bson.M) ([]Book, error) { /* optional parameters: length and start index
var books []Book *
err := coll.Find(query).All(&books) * 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 { for i, b := range books {
books[i].Id = bson.ObjectId(b.Id).Hex() books[i].Id = bson.ObjectId(b.Id).Hex()
} }
return books, err return
} }

View file

@ -47,27 +47,22 @@ func searchHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request
return return
} }
req := strings.Join(r.Form["q"], " ") req := strings.Join(r.Form["q"], " ")
res, _ := GetBook(coll, buildQuery(req))
page := 0 page := 0
if len(r.Form["p"]) != 0 { if len(r.Form["p"]) != 0 {
page, err = strconv.Atoi(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 page = 0
} }
} }
res, num, _ := GetBook(coll, buildQuery(req), ITEMS_PAGE, page)
var data searchData var data searchData
data.S = GetStatus(w, r) data.S = GetStatus(w, r)
data.S.Search = req data.S.Search = req
data.Found = len(res) data.Books = res
if len(res) > ITEMS_PAGE*(page+1) { data.Found = num
data.Books = res[ITEMS_PAGE*page : ITEMS_PAGE*(page+1)]
} else {
data.Books = res[ITEMS_PAGE*page:]
}
data.Page = page + 1 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) data.Next = "/search/?q=" + req + "&p=" + strconv.Itoa(page+1)
} }
if page > 0 { if page > 0 {

View file

@ -66,7 +66,7 @@ func bookHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request)
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var data bookData var data bookData
data.S = GetStatus(w, r) 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 { if err != nil || len(books) == 0 {
http.NotFound(w, r) http.NotFound(w, r)
return return