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:
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

View file

@ -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
}

View file

@ -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 {

View file

@ -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