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 rawdatastore | |
6 | |
7 import ( | |
8 "fmt" | |
9 | |
10 "github.com/luci/gae/service/info" | |
11 "github.com/luci/luci-go/common/errors" | |
12 "golang.org/x/net/context" | |
13 ) | |
14 | |
15 type checkFilter struct { | |
16 Interface | |
17 | |
18 aid string | |
19 ns string | |
20 } | |
21 | |
22 func (tcf *checkFilter) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { | |
23 if f == nil { | |
24 return nil | |
25 } | |
26 return tcf.Interface.RunInTransaction(f, opts) | |
27 } | |
28 | |
29 func (tcf *checkFilter) Run(q Query, cb RunCB) error { | |
30 if q == nil || cb == nil { | |
31 return nil | |
32 } | |
33 return tcf.Interface.Run(q, cb) | |
34 } | |
35 | |
36 func (tcf *checkFilter) GetMulti(keys []Key, cb GetMultiCB) error { | |
37 if len(keys) == 0 || cb == nil { | |
38 return nil | |
39 } | |
40 lme := errors.LazyMultiError{Size: len(keys)} | |
41 for i, k := range keys { | |
42 if KeyIncomplete(k) || !KeyValid(k, true, tcf.aid, tcf.ns) { | |
43 lme.Assign(i, ErrInvalidKey) | |
44 } | |
45 } | |
46 if me := lme.Get(); me != nil { | |
47 for _, err := range me.(errors.MultiError) { | |
48 cb(nil, err) | |
49 } | |
50 return nil | |
51 } | |
52 return tcf.Interface.GetMulti(keys, cb) | |
53 } | |
54 | |
55 func (tcf *checkFilter) PutMulti(keys []Key, vals []PropertyLoadSaver, cb PutMul
tiCB) error { | |
56 if len(keys) != len(vals) { | |
57 return fmt.Errorf("rawdatastore: GetMulti with mismatched keys/v
als lengths (%d/%d)", len(keys), len(vals)) | |
58 } | |
59 if len(keys) == 0 { | |
60 return nil | |
61 } | |
62 lme := errors.LazyMultiError{Size: len(keys)} | |
63 for i, k := range keys { | |
64 if KeyIncomplete(k) { | |
65 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "", 1, k.
Parent()) | |
66 } | |
67 if !KeyValid(k, false, tcf.aid, tcf.ns) { | |
68 lme.Assign(i, ErrInvalidKey) | |
69 continue | |
70 } | |
71 v := vals[i] | |
72 if v == nil { | |
73 if !lme.Assign(i, errors.New("rawdatastore: PutMulti got
nil vals entry")) { | |
74 lme.Assign(i, v.Problem()) | |
75 } | |
76 } | |
77 } | |
78 if me := lme.Get(); me != nil { | |
79 for _, err := range me.(errors.MultiError) { | |
80 cb(nil, err) | |
81 } | |
82 return nil | |
83 } | |
84 | |
85 err := error(nil) | |
86 pmVals := make([]PropertyLoadSaver, len(vals)) | |
87 for i, val := range vals { | |
88 pmVals[i], err = val.Save(true) | |
89 lme.Assign(i, err) | |
90 } | |
91 if me := lme.Get(); me != nil { | |
92 for _, err := range me.(errors.MultiError) { | |
93 cb(nil, err) | |
94 } | |
95 return nil | |
96 } | |
97 | |
98 return tcf.Interface.PutMulti(keys, pmVals, cb) | |
99 } | |
100 | |
101 func (tcf *checkFilter) DeleteMulti(keys []Key, cb DeleteMultiCB) error { | |
102 if len(keys) == 0 { | |
103 return nil | |
104 } | |
105 lme := errors.LazyMultiError{Size: len(keys)} | |
106 for i, k := range keys { | |
107 if KeyIncomplete(k) || !KeyValid(k, false, tcf.aid, tcf.ns) { | |
108 lme.Assign(i, ErrInvalidKey) | |
109 } | |
110 } | |
111 if me := lme.Get(); me != nil { | |
112 for _, err := range me.(errors.MultiError) { | |
113 cb(err) | |
114 } | |
115 return nil | |
116 } | |
117 return tcf.Interface.DeleteMulti(keys, cb) | |
118 } | |
119 | |
120 func applyCheckFilter(c context.Context, i Interface) Interface { | |
121 inf := info.Get(c) | |
122 return &checkFilter{i, inf.AppID(), inf.GetNamespace()} | |
123 } | |
OLD | NEW |