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) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { | 22 func (tcf *checkFilter) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { |
23 if f == nil { | 23 if f == nil { |
24 return fmt.Errorf("datastore: RunInTransaction function is nil") | 24 return fmt.Errorf("datastore: RunInTransaction function is nil") |
25 } | 25 } |
26 return tcf.RawInterface.RunInTransaction(f, opts) | 26 return tcf.RawInterface.RunInTransaction(f, opts) |
27 } | 27 } |
28 | 28 |
29 func (tcf *checkFilter) Run(q Query, cb RawRunCB) error { | 29 func (tcf *checkFilter) Run(fq *FinalizedQuery, cb RawRunCB) error { |
30 » if q == nil { | 30 » if fq == nil { |
31 return fmt.Errorf("datastore: Run query is nil") | 31 return fmt.Errorf("datastore: Run query is nil") |
32 } | 32 } |
33 if cb == nil { | 33 if cb == nil { |
34 return fmt.Errorf("datastore: Run callback is nil") | 34 return fmt.Errorf("datastore: Run callback is nil") |
35 } | 35 } |
36 » return tcf.RawInterface.Run(q, cb) | 36 » return tcf.RawInterface.Run(fq, cb) |
37 } | 37 } |
38 | 38 |
39 func (tcf *checkFilter) GetMulti(keys []Key, meta MultiMetaGetter, cb GetMultiCB
) error { | 39 func (tcf *checkFilter) GetMulti(keys []*Key, meta MultiMetaGetter, cb GetMultiC
B) error { |
40 if len(keys) == 0 { | 40 if len(keys) == 0 { |
41 return nil | 41 return nil |
42 } | 42 } |
43 if cb == nil { | 43 if cb == nil { |
44 return fmt.Errorf("datastore: GetMulti callback is nil") | 44 return fmt.Errorf("datastore: GetMulti callback is nil") |
45 } | 45 } |
46 lme := errors.NewLazyMultiError(len(keys)) | 46 lme := errors.NewLazyMultiError(len(keys)) |
47 for i, k := range keys { | 47 for i, k := range keys { |
48 if k.Incomplete() || !k.Valid(true, tcf.aid, tcf.ns) { | 48 if k.Incomplete() || !k.Valid(true, tcf.aid, tcf.ns) { |
49 lme.Assign(i, ErrInvalidKey) | 49 lme.Assign(i, ErrInvalidKey) |
50 } | 50 } |
51 } | 51 } |
52 if me := lme.Get(); me != nil { | 52 if me := lme.Get(); me != nil { |
53 for _, err := range me.(errors.MultiError) { | 53 for _, err := range me.(errors.MultiError) { |
54 cb(nil, err) | 54 cb(nil, err) |
55 } | 55 } |
56 return nil | 56 return nil |
57 } | 57 } |
58 return tcf.RawInterface.GetMulti(keys, meta, cb) | 58 return tcf.RawInterface.GetMulti(keys, meta, cb) |
59 } | 59 } |
60 | 60 |
61 func (tcf *checkFilter) PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB)
error { | 61 func (tcf *checkFilter) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { |
62 if len(keys) != len(vals) { | 62 if len(keys) != len(vals) { |
63 return fmt.Errorf("datastore: PutMulti with mismatched keys/vals
lengths (%d/%d)", len(keys), len(vals)) | 63 return fmt.Errorf("datastore: PutMulti with mismatched keys/vals
lengths (%d/%d)", len(keys), len(vals)) |
64 } | 64 } |
65 if len(keys) == 0 { | 65 if len(keys) == 0 { |
66 return nil | 66 return nil |
67 } | 67 } |
68 if cb == nil { | 68 if cb == nil { |
69 return fmt.Errorf("datastore: PutMulti callback is nil") | 69 return fmt.Errorf("datastore: PutMulti callback is nil") |
70 } | 70 } |
71 lme := errors.NewLazyMultiError(len(keys)) | 71 lme := errors.NewLazyMultiError(len(keys)) |
(...skipping 10 matching lines...) Expand all Loading... |
82 if me := lme.Get(); me != nil { | 82 if me := lme.Get(); me != nil { |
83 for _, err := range me.(errors.MultiError) { | 83 for _, err := range me.(errors.MultiError) { |
84 cb(nil, err) | 84 cb(nil, err) |
85 } | 85 } |
86 return nil | 86 return nil |
87 } | 87 } |
88 | 88 |
89 return tcf.RawInterface.PutMulti(keys, vals, cb) | 89 return tcf.RawInterface.PutMulti(keys, vals, cb) |
90 } | 90 } |
91 | 91 |
92 func (tcf *checkFilter) DeleteMulti(keys []Key, cb DeleteMultiCB) error { | 92 func (tcf *checkFilter) DeleteMulti(keys []*Key, cb DeleteMultiCB) error { |
93 if len(keys) == 0 { | 93 if len(keys) == 0 { |
94 return nil | 94 return nil |
95 } | 95 } |
96 if cb == nil { | 96 if cb == nil { |
97 return fmt.Errorf("datastore: DeleteMulti callback is nil") | 97 return fmt.Errorf("datastore: DeleteMulti callback is nil") |
98 } | 98 } |
99 lme := errors.NewLazyMultiError(len(keys)) | 99 lme := errors.NewLazyMultiError(len(keys)) |
100 for i, k := range keys { | 100 for i, k := range keys { |
101 if k.Incomplete() || !k.Valid(false, tcf.aid, tcf.ns) { | 101 if k.Incomplete() || !k.Valid(false, tcf.aid, tcf.ns) { |
102 lme.Assign(i, ErrInvalidKey) | 102 lme.Assign(i, ErrInvalidKey) |
103 } | 103 } |
104 } | 104 } |
105 if me := lme.Get(); me != nil { | 105 if me := lme.Get(); me != nil { |
106 for _, err := range me.(errors.MultiError) { | 106 for _, err := range me.(errors.MultiError) { |
107 cb(err) | 107 cb(err) |
108 } | 108 } |
109 return nil | 109 return nil |
110 } | 110 } |
111 return tcf.RawInterface.DeleteMulti(keys, cb) | 111 return tcf.RawInterface.DeleteMulti(keys, cb) |
112 } | 112 } |
113 | 113 |
114 func applyCheckFilter(c context.Context, i RawInterface) RawInterface { | 114 func applyCheckFilter(c context.Context, i RawInterface) RawInterface { |
115 inf := info.Get(c) | 115 inf := info.Get(c) |
116 return &checkFilter{i, inf.FullyQualifiedAppID(), inf.GetNamespace()} | 116 return &checkFilter{i, inf.FullyQualifiedAppID(), inf.GetNamespace()} |
117 } | 117 } |
OLD | NEW |