OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package datastore | 5 package datastore |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 | 9 |
10 "github.com/luci/gae/service/info" | 10 "github.com/luci/gae/service/info" |
11 "github.com/luci/luci-go/common/errors" | 11 "github.com/luci/luci-go/common/errors" |
12 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
13 ) | 13 ) |
14 | 14 |
15 type checkFilter struct { | 15 type checkFilter struct { |
16 RawInterface | 16 RawInterface |
17 | 17 |
18 aid string | 18 aid string |
19 ns string | 19 ns string |
20 } | 20 } |
21 | 21 |
22 func (tcf *checkFilter) AllocateIDs(incomplete *Key, n int) (start int64, err er
ror) { | 22 func (tcf *checkFilter) AllocateIDs(keys []*Key) error { |
23 » if n <= 0 { | 23 » if len(keys) == 0 { |
24 » » return 0, fmt.Errorf("datastore: invalid `n` parameter in Alloca
teIDs: %d", n) | 24 » » return nil |
25 } | 25 } |
26 » if !incomplete.PartialValid(tcf.aid, tcf.ns) { | 26 |
27 » » return 0, ErrInvalidKey | 27 » // Assert that all of the supplied keys are PartialValid, and that they
all |
| 28 » // share the same entity. |
| 29 » // |
| 30 » // TODO: If GAE protobufs and/or API ever relaxes the same-kind requirem
ent, |
| 31 » // we can drop that here and in the raw interface API too. |
| 32 » lme := errors.NewLazyMultiError(len(keys)) |
| 33 » var exemplar *Key |
| 34 » for i, k := range keys { |
| 35 » » if !k.PartialValid(tcf.aid, tcf.ns) { |
| 36 » » » lme.Assign(i, ErrInvalidKey) |
| 37 » » » continue |
| 38 » » } |
| 39 |
| 40 » » if exemplar == nil { |
| 41 » » » exemplar = k |
| 42 » » } else { |
| 43 » » » if !k.SameKind(exemplar) { |
| 44 » » » » lme.Assign(i, ErrInvalidKey) |
| 45 » » » » continue |
| 46 » » » } |
| 47 » » } |
28 } | 48 } |
29 » return tcf.RawInterface.AllocateIDs(incomplete, n) | 49 » if me := lme.Get(); me != nil { |
| 50 » » return me |
| 51 » } |
| 52 |
| 53 » return tcf.RawInterface.AllocateIDs(keys) |
30 } | 54 } |
31 | 55 |
32 func (tcf *checkFilter) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { | 56 func (tcf *checkFilter) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { |
33 if f == nil { | 57 if f == nil { |
34 return fmt.Errorf("datastore: RunInTransaction function is nil") | 58 return fmt.Errorf("datastore: RunInTransaction function is nil") |
35 } | 59 } |
36 return tcf.RawInterface.RunInTransaction(f, opts) | 60 return tcf.RawInterface.RunInTransaction(f, opts) |
37 } | 61 } |
38 | 62 |
39 func (tcf *checkFilter) Run(fq *FinalizedQuery, cb RawRunCB) error { | 63 func (tcf *checkFilter) Run(fq *FinalizedQuery, cb RawRunCB) error { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 return nil | 143 return nil |
120 } | 144 } |
121 return tcf.RawInterface.DeleteMulti(keys, cb) | 145 return tcf.RawInterface.DeleteMulti(keys, cb) |
122 } | 146 } |
123 | 147 |
124 func applyCheckFilter(c context.Context, i RawInterface) RawInterface { | 148 func applyCheckFilter(c context.Context, i RawInterface) RawInterface { |
125 inf := info.Get(c) | 149 inf := info.Get(c) |
126 ns, _ := inf.GetNamespace() | 150 ns, _ := inf.GetNamespace() |
127 return &checkFilter{i, inf.FullyQualifiedAppID(), ns} | 151 return &checkFilter{i, inf.FullyQualifiedAppID(), ns} |
128 } | 152 } |
OLD | NEW |