OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be 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) { | |
23 if n <= 0 { | |
24 return 0, fmt.Errorf("datastore: invalid `n` parameter in Alloca
teIDs: %d", n) | |
25 } | |
26 if !incomplete.PartialValid(tcf.aid, tcf.ns) { | |
27 return 0, ErrInvalidKey | |
28 } | |
29 return tcf.RawInterface.AllocateIDs(incomplete, n) | |
30 } | |
31 | |
32 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 { |
33 if f == nil { | 23 if f == nil { |
34 return fmt.Errorf("datastore: RunInTransaction function is nil") | 24 return fmt.Errorf("datastore: RunInTransaction function is nil") |
35 } | 25 } |
36 return tcf.RawInterface.RunInTransaction(f, opts) | 26 return tcf.RawInterface.RunInTransaction(f, opts) |
37 } | 27 } |
38 | 28 |
39 func (tcf *checkFilter) Run(fq *FinalizedQuery, cb RawRunCB) error { | 29 func (tcf *checkFilter) Run(fq *FinalizedQuery, cb RawRunCB) error { |
40 if fq == nil { | 30 if fq == nil { |
41 return fmt.Errorf("datastore: Run query is nil") | 31 return fmt.Errorf("datastore: Run query is nil") |
42 } | 32 } |
43 if cb == nil { | 33 if cb == nil { |
44 return fmt.Errorf("datastore: Run callback is nil") | 34 return fmt.Errorf("datastore: Run callback is nil") |
45 } | 35 } |
46 return tcf.RawInterface.Run(fq, cb) | 36 return tcf.RawInterface.Run(fq, cb) |
47 } | 37 } |
48 | 38 |
49 func (tcf *checkFilter) GetMulti(keys []*Key, meta MultiMetaGetter, cb GetMultiC
B) error { | 39 func (tcf *checkFilter) GetMulti(keys []*Key, meta MultiMetaGetter, cb GetMultiC
B) error { |
50 if len(keys) == 0 { | 40 if len(keys) == 0 { |
51 return nil | 41 return nil |
52 } | 42 } |
53 if cb == nil { | 43 if cb == nil { |
54 return fmt.Errorf("datastore: GetMulti callback is nil") | 44 return fmt.Errorf("datastore: GetMulti callback is nil") |
55 } | 45 } |
56 lme := errors.NewLazyMultiError(len(keys)) | 46 lme := errors.NewLazyMultiError(len(keys)) |
57 for i, k := range keys { | 47 for i, k := range keys { |
58 » » if k.Incomplete() || !k.Valid(true, tcf.aid, tcf.ns) { | 48 » » if k.IsIncomplete() || !k.Valid(true, tcf.aid, tcf.ns) { |
59 lme.Assign(i, ErrInvalidKey) | 49 lme.Assign(i, ErrInvalidKey) |
60 } | 50 } |
61 } | 51 } |
62 if me := lme.Get(); me != nil { | 52 if me := lme.Get(); me != nil { |
63 for _, err := range me.(errors.MultiError) { | 53 for _, err := range me.(errors.MultiError) { |
64 cb(nil, err) | 54 cb(nil, err) |
65 } | 55 } |
66 return nil | 56 return nil |
67 } | 57 } |
68 return tcf.RawInterface.GetMulti(keys, meta, cb) | 58 return tcf.RawInterface.GetMulti(keys, meta, cb) |
69 } | 59 } |
70 | 60 |
71 func (tcf *checkFilter) PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB)
error { | 61 func (tcf *checkFilter) PutMulti(keys []*Key, vals []PropertyMap, cb NewKeyCB) e
rror { |
72 if len(keys) != len(vals) { | 62 if len(keys) != len(vals) { |
73 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)) |
74 } | 64 } |
75 if len(keys) == 0 { | 65 if len(keys) == 0 { |
76 return nil | 66 return nil |
77 } | 67 } |
78 if cb == nil { | 68 if cb == nil { |
79 return fmt.Errorf("datastore: PutMulti callback is nil") | 69 return fmt.Errorf("datastore: PutMulti callback is nil") |
80 } | 70 } |
81 lme := errors.NewLazyMultiError(len(keys)) | 71 lme := errors.NewLazyMultiError(len(keys)) |
(...skipping 19 matching lines...) Expand all Loading... |
101 | 91 |
102 func (tcf *checkFilter) DeleteMulti(keys []*Key, cb DeleteMultiCB) error { | 92 func (tcf *checkFilter) DeleteMulti(keys []*Key, cb DeleteMultiCB) error { |
103 if len(keys) == 0 { | 93 if len(keys) == 0 { |
104 return nil | 94 return nil |
105 } | 95 } |
106 if cb == nil { | 96 if cb == nil { |
107 return fmt.Errorf("datastore: DeleteMulti callback is nil") | 97 return fmt.Errorf("datastore: DeleteMulti callback is nil") |
108 } | 98 } |
109 lme := errors.NewLazyMultiError(len(keys)) | 99 lme := errors.NewLazyMultiError(len(keys)) |
110 for i, k := range keys { | 100 for i, k := range keys { |
111 » » if k.Incomplete() || !k.Valid(false, tcf.aid, tcf.ns) { | 101 » » if k.IsIncomplete() || !k.Valid(false, tcf.aid, tcf.ns) { |
112 lme.Assign(i, ErrInvalidKey) | 102 lme.Assign(i, ErrInvalidKey) |
113 } | 103 } |
114 } | 104 } |
115 if me := lme.Get(); me != nil { | 105 if me := lme.Get(); me != nil { |
116 for _, err := range me.(errors.MultiError) { | 106 for _, err := range me.(errors.MultiError) { |
117 cb(err) | 107 cb(err) |
118 } | 108 } |
119 return nil | 109 return nil |
120 } | 110 } |
121 return tcf.RawInterface.DeleteMulti(keys, cb) | 111 return tcf.RawInterface.DeleteMulti(keys, cb) |
122 } | 112 } |
123 | 113 |
124 func applyCheckFilter(c context.Context, i RawInterface) RawInterface { | 114 func applyCheckFilter(c context.Context, i RawInterface) RawInterface { |
125 inf := info.Get(c) | 115 inf := info.Get(c) |
126 ns, _ := inf.GetNamespace() | 116 ns, _ := inf.GetNamespace() |
127 return &checkFilter{i, inf.FullyQualifiedAppID(), ns} | 117 return &checkFilter{i, inf.FullyQualifiedAppID(), ns} |
128 } | 118 } |
OLD | NEW |