Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
fixes #18 Added pagination option to Search()
Browse files Browse the repository at this point in the history
  • Loading branch information
nylar committed Dec 28, 2014
1 parent b93154c commit 7070a87
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cli/wally/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func SearchCommand() cli.Command {
func SearchFunc(c *cli.Context) {
query := c.String("query")

results, err := wally.Search(query, session)
results, err := wally.Search(query, session, 1)
if err != nil {
logError(err)
}
Expand Down
16 changes: 14 additions & 2 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ type Results struct {
Time float64
}

func parsePageNumber(page int) uint {
if page < 1 {
page = 1
}
return uint(page)
}

// Search returns a list of results along with the time taken to run and the
// number of results found.
func Search(query string, session *rdb.Session) (*Results, error) {
func Search(query string, session *rdb.Session, currentPage int) (*Results, error) {
start := time.Now()
res := []Query{}
keys := strings.Split(query, " ")

r := new(Results)

results, err := rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).GetAllByIndex("word", rdb.Args(keys)).EqJoin("document_id", rdb.Db(Conf.Database.Name).Table(Conf.Tables.DocumentTable)).Zip().OrderBy(rdb.Desc("count")).Run(session)
page := parsePageNumber(currentPage)

lower := (page - 1) * ItemsPerPage
upper := page * ItemsPerPage

results, err := rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).GetAllByIndex("word", rdb.Args(keys)).EqJoin("document_id", rdb.Db(Conf.Database.Name).Table(Conf.Tables.DocumentTable)).Zip().OrderBy(rdb.Desc("count")).Slice(lower, upper).Run(session)
if err != nil {
return nil, err
}
Expand Down
50 changes: 46 additions & 4 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
"github.com/stretchr/testify/assert"
)

var originalItemsPerPage = ItemsPerPage

func setUp(pages uint) {
ItemsPerPage = pages
}

func tearDown() {
ItemsPerPage = originalItemsPerPage
}

func SearchSetup() error {
DatabaseRebuild(session)

Expand Down Expand Up @@ -49,20 +59,52 @@ func SearchSetup() error {
}

func TestSearch_Search(t *testing.T) {
setUp(1)
if err := SearchSetup(); err != nil {
t.Errorf(err.Error())
}

results, err := Search("example", session)
assert.NoError(t, err)
for i := 1; i < 3; i++ {
results, err := Search("example", session, i)
assert.NoError(t, err)

assert.Equal(t, len(results.Results), 2)
assert.Equal(t, len(results.Results), 1)
}
tearDown()
}

func TestSearch_SearchWithNoIndex(t *testing.T) {
DatabaseRebuild(session)
rdb.Db(Conf.Database.Name).Table(Conf.Tables.IndexTable).IndexDrop("word").Exec(session)

_, err := Search("hello", session)
_, err := Search("hello", session, 1)
assert.Error(t, err)
}

func TestSearch_parsePageNumber(t *testing.T) {
tests := []struct {
input int
output uint
}{
{
0,
1,
},
{
1,
1,
},
{
2,
2,
},
{
192983,
192983,
},
}

for _, test := range tests {
assert.Equal(t, parsePageNumber(test.input), test.output)
}
}
5 changes: 4 additions & 1 deletion wally.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package wally

var Conf *Config
var (
Conf *Config
ItemsPerPage uint = 2
)

0 comments on commit 7070a87

Please sign in to comment.