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 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 return ds.Stop |
| 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 |