Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: filter/dsQueryBatch/filter.go

Issue 1843313004: Add datastore iterating query filter. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gae@no-stop-in-raw
Patch Set: Comments, test edges. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « filter/dsQueryBatch/context.go ('k') | filter/dsQueryBatch/filter_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package dsQueryBatch
6
7 import (
8 "fmt"
9
10 ds "github.com/luci/gae/service/datastore"
11 )
12
13 type iterQueryFilter struct {
14 ds.RawInterface
15
16 batchSize int32
17 }
18
19 func (f *iterQueryFilter) Run(fq *ds.FinalizedQuery, cb ds.RawRunCB) error {
20 limit, hasLimit := fq.Limit()
21
22 var cursor ds.Cursor
23 for {
24 iterQuery := fq.Original()
25 if cursor != nil {
26 iterQuery = iterQuery.Start(cursor)
27 cursor = nil
28 }
29 iterLimit := f.batchSize
30 if hasLimit && limit < iterLimit {
31 iterLimit = limit
32 }
33 iterQuery = iterQuery.Limit(iterLimit)
34
35 iterFinalizedQuery, err := iterQuery.Finalize()
36 if err != nil {
37 panic(fmt.Errorf("failed to finalize internal query: %v" , err))
38 }
39
40 count := int32(0)
41 err = f.RawInterface.Run(iterFinalizedQuery, func(key *ds.Key, v al ds.PropertyMap, getCursor ds.CursorCB) error {
42 if cursor != nil {
43 // We're iterating past our batch size, which sh ould never happen, since
44 // we set a limit. This will only happen when ou r inner RawInterface
45 // fails to honor the limit that we set.
46 panic(fmt.Errorf("iterating past batch size"))
47 }
48
49 if err := cb(key, val, getCursor); err != nil {
50 return err
51 }
52
53 // If this is the last entry in our batch, get the curso r and stop this
54 // query round.
55 count++
56 if count >= f.batchSize {
57 if cursor, err = getCursor(); err != nil {
58 return fmt.Errorf("failed to get cursor: %v", err)
59 }
60 }
61 return nil
62 })
63 if err != nil && err != ds.Stop {
64 return err
65 }
66
67 // If we have no cursor, we're done.
68 if cursor == nil {
69 break
70 }
71
72 // Reduce our limit for the next round.
73 if hasLimit {
74 limit -= count
75 if limit <= 0 {
76 break
77 }
78 }
79 }
80 return nil
81 }
OLDNEW
« no previous file with comments | « filter/dsQueryBatch/context.go ('k') | filter/dsQueryBatch/filter_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698