OLD | NEW |
---|---|
(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 return fmt.Errorf("failed to finalize internal query: %v ", err) | |
iannucci
2016/03/31 22:40:31
panic: this can't fail if we started with a Finali
dnj
2016/03/31 23:54:06
Done.
| |
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. | |
45 return fmt.Errorf("iterating past batch size") | |
iannucci
2016/03/31 22:40:30
yeah let's panic here too. maybe more comment expl
dnj
2016/03/31 23:54:06
Done.
| |
46 } | |
47 | |
48 // Call backing iterator. | |
iannucci
2016/03/31 22:40:30
doesn't need a comment
dnj
2016/03/31 23:54:06
Done.
| |
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 cursor, err = getCursor() | |
58 if err != nil { | |
iannucci
2016/03/31 22:40:31
if cursor, err = .......; .... {
| |
59 return fmt.Errorf("failed to get cursor: %v", err) | |
60 } | |
61 } | |
62 return nil | |
63 }) | |
64 if err != nil && err != ds.Stop { | |
65 return err | |
66 } | |
67 | |
68 // If we have no cursor, we're done. | |
69 if cursor == nil { | |
70 break | |
71 } | |
72 | |
73 // Reduce our limit for the next round. | |
74 if hasLimit { | |
75 limit -= count | |
76 if limit <= 0 { | |
77 break | |
78 } | |
79 } | |
80 } | |
81 return nil | |
82 } | |
OLD | NEW |