-
Notifications
You must be signed in to change notification settings - Fork 109
feat: add chunk by query methods (#714) #1281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,6 +43,13 @@ type Query interface { | |||||||||||||
| Begin() (Query, error) | ||||||||||||||
| // BeginTransaction begins a new transaction | ||||||||||||||
| BeginTransaction() (Query, error) | ||||||||||||||
| // Chunk processes a given number of records in batches. | ||||||||||||||
| Chunk(count int, callback func([]any) error) error | ||||||||||||||
| // ChunkByID processes records in batches by comparing numeric IDs in ascending order. | ||||||||||||||
| // This avoids issues with offset-based pagination when records are added or deleted during processing. | ||||||||||||||
| ChunkByID(count int, callback func([]any) error) error | ||||||||||||||
| // ChunkByIDDesc processes a given number of records in batches, ordered by ID in descending order. | ||||||||||||||
| ChunkByIDDesc(count int, callback func([]any) error) error | ||||||||||||||
|
Comment on lines
+50
to
+52
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default column is
Suggested change
|
||||||||||||||
| // Commit commits the changes in a transaction. | ||||||||||||||
| Commit() error | ||||||||||||||
| // Count retrieve the "count" result of the query. | ||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,6 +189,179 @@ func (r *Query) Cursor() chan contractsdb.Row { | |
| return cursorChan | ||
| } | ||
|
|
||
| func (r *Query) Chunk(count int, callback func([]any) error) error { | ||
hwbrzzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if count <= 0 { | ||
| return errors.OrmQueryChunkZeroOrLess | ||
| } | ||
|
|
||
| if r.conditions.model == nil { | ||
| return errors.OrmQueryInvalidModel.Args("nil") | ||
| } | ||
|
|
||
| initialOffset := 0 | ||
| if r.conditions.offset != nil { | ||
| initialOffset = *r.conditions.offset | ||
| } | ||
| var remaining *int | ||
| if r.conditions.limit != nil { | ||
| limit := *r.conditions.limit | ||
| remaining = &limit | ||
| } | ||
|
|
||
| query := r.addGlobalScopes().buildConditions() | ||
|
|
||
| destType := reflect.TypeOf(r.conditions.model) | ||
hwbrzzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sliceType := reflect.SliceOf(reflect.PointerTo(destType)) | ||
| offset := initialOffset | ||
|
|
||
| for remaining == nil || *remaining > 0 { | ||
| chunkLimit := count | ||
| if remaining != nil && *remaining < count { | ||
| chunkLimit = *remaining | ||
| } | ||
|
|
||
| results := reflect.New(sliceType).Interface() | ||
|
|
||
| chunkQuery := query.Offset(offset).Limit(chunkLimit).(*Query) | ||
| err := chunkQuery.Find(results) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resultsValue := reflect.ValueOf(results).Elem() | ||
| length := resultsValue.Len() | ||
| if length == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| if remaining != nil { | ||
| *remaining = max(*remaining-length, 0) | ||
| } | ||
|
|
||
| values := make([]any, length) | ||
| for i := 0; i < length; i++ { | ||
| values[i] = resultsValue.Index(i).Interface() | ||
| } | ||
|
|
||
| if err = callback(values); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if length < chunkLimit { | ||
| return nil | ||
| } | ||
|
|
||
| offset += chunkLimit | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (r *Query) ChunkByID(count int, callback func([]any) error) error { | ||
| return r.orderedChunkByID(count, callback, false) | ||
| } | ||
|
|
||
| func (r *Query) ChunkByIDDesc(count int, callback func([]any) error) error { | ||
| return r.orderedChunkByID(count, callback, true) | ||
| } | ||
|
|
||
| func (r *Query) orderedChunkByID(count int, callback func([]any) error, descending bool) error { | ||
| if count <= 0 { | ||
| return errors.OrmQueryChunkZeroOrLess | ||
| } | ||
|
|
||
| if r.conditions.model == nil { | ||
| return errors.OrmQueryInvalidModel.Args("nil") | ||
| } | ||
|
|
||
| column := "id" | ||
|
||
| initialOffset := 0 | ||
| if r.conditions.offset != nil { | ||
| initialOffset = *r.conditions.offset | ||
| } | ||
| var remaining *int | ||
| if r.conditions.limit != nil { | ||
| limit := *r.conditions.limit | ||
| remaining = &limit | ||
| } | ||
|
|
||
| destType := reflect.TypeOf(r.conditions.model) | ||
hwbrzzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| sliceType := reflect.SliceOf(reflect.PointerTo(destType)) | ||
| var lastID any | ||
| page := 1 | ||
|
|
||
| for remaining == nil || *remaining > 0 { | ||
| chunkLimit := count | ||
| if remaining != nil && *remaining < count { | ||
| chunkLimit = *remaining | ||
| } | ||
|
|
||
| clone := r.addGlobalScopes() | ||
|
|
||
| if initialOffset > 0 { | ||
| if page > 1 { | ||
| clone = clone.Offset(0).(*Query) | ||
| } else { | ||
| clone = clone.Offset(initialOffset).(*Query) | ||
| } | ||
| } | ||
|
|
||
| if descending { | ||
| clone = clone.OrderByDesc(column).(*Query) | ||
| } else { | ||
| clone = clone.OrderBy(column).(*Query) | ||
| } | ||
|
|
||
| if lastID != nil { | ||
| if descending { | ||
| clone = clone.Where(column+" < ?", lastID).(*Query) | ||
| } else { | ||
| clone = clone.Where(column+" > ?", lastID).(*Query) | ||
| } | ||
| } | ||
| clone = clone.Limit(chunkLimit).(*Query) | ||
|
|
||
| query := clone.buildConditions() | ||
| results := reflect.New(sliceType).Interface() | ||
|
|
||
| err := query.Find(results) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| resultsValue := reflect.ValueOf(results).Elem() | ||
| length := resultsValue.Len() | ||
|
|
||
| if length == 0 { | ||
| break | ||
| } | ||
|
|
||
| if remaining != nil { | ||
| *remaining = max(*remaining-length, 0) | ||
| } | ||
|
|
||
| values := make([]any, length) | ||
| for i := 0; i < length; i++ { | ||
| values[i] = resultsValue.Index(i).Interface() | ||
| } | ||
|
|
||
| lastRecord := values[length-1] | ||
| lastID = database.GetID(lastRecord) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if err = callback(values); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if length < chunkLimit { | ||
| break | ||
| } | ||
|
|
||
| page++ | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (r *Query) DB() (*sql.DB, error) { | ||
| return r.instance.DB() | ||
| } | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add the same functions in db.go?