Skip to content

Commit

Permalink
Merge pull request diegogub#37 from jurrian/fix-limit-bug
Browse files Browse the repository at this point in the history
Fix FetchOne returning not enough elements
  • Loading branch information
diegogub authored Sep 25, 2016
2 parents 2775feb + 30e9cc1 commit a49d222
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,41 +83,31 @@ func (c *Cursor) FetchBatch(r interface{}) error {
return nil
}

// Iterates over cursor, returns false when no more values into batch, fetch next batch if necesary.
// FetchOne iterates over cursor, returns false when no more values into batch, fetch next batch if necesary.
func (c *Cursor) FetchOne(r interface{}) bool {
var max int = len(c.Result) - 1
if c.Index >= max {
b, err := json.Marshal(c.Result[c.Index])
err = json.Unmarshal(b, r)
if err != nil {
return false
} else {
if c.More {
//fetch rest from server
res, _ := c.db.send("cursor", c.Id, "PUT", nil, c, c)

if err != nil {
return false
}
if c.Index > c.max {
if c.More {
//fetch rest from server
res, err := c.db.send("cursor", c.Id, "PUT", nil, c, c)

if res.Status() == 200 {
c.Index = 0
return true
} else {
return false
}
if err != nil {
return false
}

if res.Status() == 200 {
c.Index = 0
return true
} else {
// last doc
return false
}
} else {
// last doc
return false
}
} else {
b, err := json.Marshal(c.Result[c.Index])
err = json.Unmarshal(b, r)
c.Index++ // move to next value into result
if c.Index == max {
}
if err != nil {
return false
} else {
Expand Down

0 comments on commit a49d222

Please sign in to comment.