Skip to content

Commit 8b6f083

Browse files
Henrik Johanssonpsarna
authored andcommitted
iterx: paging iteration working
We used to rely upon NumRows to determine if there are new pages available. This is not correct since the server is allowed to return empty pages with has_more_data flag set and the needed data to do this is not exposed by the gocql driver. We simply remove these checks and let the driver decide when to stop reading. Co-authored-by: Henrik Johansson <[email protected]> Co-authored-by: Piotr Sarna <[email protected]>
1 parent eddedae commit 8b6f083

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

iterx.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ func (iter *Iterx) scanAny(dest interface{}, structOnly bool) bool {
8080
return false
8181
}
8282

83-
// no results or query error
84-
if iter.Iter.NumRows() == 0 {
85-
return false
86-
}
87-
8883
base := reflectx.Deref(value.Type())
8984
scannable := isScannable(base)
9085

@@ -132,11 +127,6 @@ func (iter *Iterx) scanAll(dest interface{}, structOnly bool) bool {
132127
return false
133128
}
134129

135-
// no results or query error
136-
if iter.Iter.NumRows() == 0 {
137-
return false
138-
}
139-
140130
slice, err := baseType(value.Type(), reflect.Slice)
141131
if err != nil {
142132
iter.err = err
@@ -180,7 +170,7 @@ func (iter *Iterx) scanAll(dest interface{}, structOnly bool) bool {
180170

181171
// allocate memory for the page data
182172
if !alloc {
183-
v = reflect.MakeSlice(slice, 0, iter.Iter.NumRows())
173+
v = reflect.MakeSlice(slice, 0, iter.NumRows())
184174
alloc = true
185175
}
186176

@@ -212,11 +202,6 @@ func (iter *Iterx) StructScan(dest interface{}) bool {
212202
return false
213203
}
214204

215-
// no results or query error
216-
if iter.Iter.NumRows() == 0 {
217-
return false
218-
}
219-
220205
if !iter.started {
221206
columns := columnNames(iter.Iter.Columns())
222207
m := iter.Mapper

iterx_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/gocql/gocql"
1717
"github.com/scylladb/gocqlx"
1818
. "github.com/scylladb/gocqlx/gocqlxtest"
19+
"github.com/scylladb/gocqlx/qb"
1920
"gopkg.in/inf.v0"
2021
)
2122

@@ -327,3 +328,46 @@ func TestNotFound(t *testing.T) {
327328
}
328329
})
329330
}
331+
332+
func TestPaging(t *testing.T) {
333+
session := CreateSession(t)
334+
defer session.Close()
335+
if err := ExecStmt(session, `CREATE TABLE gocqlx_test.paging_table (id int PRIMARY KEY, val int)`); err != nil {
336+
t.Fatal("create table:", err)
337+
}
338+
if err := ExecStmt(session, `CREATE INDEX id_val_index ON gocqlx_test.paging_table (val)`); err != nil {
339+
t.Fatal("create index:", err)
340+
}
341+
stmt, names := qb.Insert("gocqlx_test.paging_table").Columns("id", "val").ToCql()
342+
q := gocqlx.Query(session.Query(stmt), names)
343+
for i := 0; i < 5000; i++ {
344+
if err := q.Bind(i, i).Exec(); err != nil {
345+
t.Fatal(err)
346+
}
347+
}
348+
349+
type Paging struct {
350+
ID int
351+
Val int
352+
}
353+
354+
t.Run("iter", func(t *testing.T) {
355+
stmt, names := qb.Select("gocqlx_test.paging_table").
356+
Where(qb.Lt("val")).
357+
AllowFiltering().
358+
Columns("id", "val").ToCql()
359+
it := gocqlx.Query(session.Query(stmt, 100).PageSize(10), names).Iter()
360+
defer it.Close()
361+
var cnt int
362+
for {
363+
p := &Paging{}
364+
if !it.StructScan(p) {
365+
break
366+
}
367+
cnt++
368+
}
369+
if cnt != 100 {
370+
t.Fatal("expected 100", "got", cnt)
371+
}
372+
})
373+
}

0 commit comments

Comments
 (0)