Skip to content

Commit

Permalink
fix bug when buffersize with iterate (go-xorm#941)
Browse files Browse the repository at this point in the history
Merge branch 'master' into lunny/fix_buffer_iterate

Exclude schema from index name (#1505)

Merge branch 'master' into fix-schema-idx

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Exclude schema from the index name

Co-authored-by: Guillermo Prandi <[email protected]>
Co-authored-by: Lunny Xiao <[email protected]>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/1505

fix test

fix bug

fix bug when buffersize with iterate

SetExpr support more go types (#1499)

Improve tests

SetExpr support more go types

fix vet

fix drone lint

remove go1.10 test on drone

Reviewed-on: https://gitea.com/xorm/xorm/pulls/1499

fix vet

fix drone lint

remove go1.10 test on drone

Fix update with Alias (go-xorm#1455)

Co-authored-by: Guillermo Prandi <[email protected]>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/941
  • Loading branch information
lunny and Guillermo Prandi committed Jan 20, 2020
1 parent bd20ffb commit 14a0c19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
27 changes: 15 additions & 12 deletions session_iterate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package xorm

import "reflect"
import (
"reflect"
)

// IterFunc only use by Iterate
type IterFunc func(idx int, bean interface{}) error
Expand Down Expand Up @@ -60,10 +62,6 @@ func (session *Session) BufferSize(size int) *Session {
}

func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
if session.isAutoClose {
defer session.Close()
}

var bufferSize = session.statement.bufferSize
var limit = session.statement.LimitN
if limit > 0 && bufferSize > limit {
Expand All @@ -73,9 +71,14 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
v := rValue(bean)
sliceType := reflect.SliceOf(v.Type())
var idx = 0
for {
session.autoResetStatement = false
defer func() {
session.autoResetStatement = true
}()

for bufferSize > 0 {
slice := reflect.New(sliceType)
if err := session.Limit(bufferSize, start).find(slice.Interface(), bean); err != nil {
if err := session.NoCache().Limit(bufferSize, start).find(slice.Interface(), bean); err != nil {
return err
}

Expand All @@ -86,13 +89,13 @@ func (session *Session) bufferIterate(bean interface{}, fun IterFunc) error {
idx++
}

start = start + slice.Elem().Len()
if limit > 0 && idx+bufferSize > limit {
bufferSize = limit - idx
if bufferSize > slice.Elem().Len() {
break
}

if bufferSize <= 0 || slice.Elem().Len() < bufferSize || idx == limit {
break
start = start + slice.Elem().Len()
if limit > 0 && start+bufferSize > limit {
bufferSize = limit - start
}
}

Expand Down
11 changes: 11 additions & 0 deletions session_iterate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,15 @@ func TestBufferIterate(t *testing.T) {
})
assert.NoError(t, err)
assert.EqualValues(t, 7, cnt)

cnt = 0
err = testEngine.Where("id <= 10").BufferSize(2).Iterate(new(UserBufferIterate), func(i int, bean interface{}) error {
user := bean.(*UserBufferIterate)
assert.EqualValues(t, cnt+1, user.Id)
assert.EqualValues(t, true, user.IsMan)
cnt++
return nil
})
assert.NoError(t, err)
assert.EqualValues(t, 10, cnt)
}

0 comments on commit 14a0c19

Please sign in to comment.