Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: service/datastore/checkfilter.go

Issue 1259593005: Add 'user friendly' datastore API. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: 100% coverage of new code Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 rawdatastore 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 » Interface 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 nil 24 » » return fmt.Errorf("datastore: RunInTransaction function is nil")
25 } 25 }
26 » return tcf.Interface.RunInTransaction(f, opts) 26 » return tcf.RawInterface.RunInTransaction(f, opts)
27 } 27 }
28 28
29 func (tcf *checkFilter) Run(q Query, cb RunCB) error { 29 func (tcf *checkFilter) Run(q Query, cb RawRunCB) error {
30 » if q == nil || cb == nil { 30 » if q == nil {
31 » » return nil 31 » » return fmt.Errorf("datastore: Run query is nil")
32 } 32 }
33 » return tcf.Interface.Run(q, cb) 33 » if cb == nil {
34 » » return fmt.Errorf("datastore: Run callback is nil")
35 » }
36 » return tcf.RawInterface.Run(q, cb)
34 } 37 }
35 38
36 func (tcf *checkFilter) GetMulti(keys []Key, cb GetMultiCB) error { 39 func (tcf *checkFilter) GetMulti(keys []Key, cb GetMultiCB) error {
37 » if len(keys) == 0 || cb == nil { 40 » if len(keys) == 0 {
38 return nil 41 return nil
39 } 42 }
43 if cb == nil {
44 return fmt.Errorf("datastore: GetMulti callback is nil")
45 }
40 lme := errors.LazyMultiError{Size: len(keys)} 46 lme := errors.LazyMultiError{Size: len(keys)}
41 for i, k := range keys { 47 for i, k := range keys {
42 if KeyIncomplete(k) || !KeyValid(k, true, tcf.aid, tcf.ns) { 48 if KeyIncomplete(k) || !KeyValid(k, true, tcf.aid, tcf.ns) {
43 lme.Assign(i, ErrInvalidKey) 49 lme.Assign(i, ErrInvalidKey)
44 } 50 }
45 } 51 }
46 if me := lme.Get(); me != nil { 52 if me := lme.Get(); me != nil {
47 for _, err := range me.(errors.MultiError) { 53 for _, err := range me.(errors.MultiError) {
48 cb(nil, err) 54 cb(nil, err)
49 } 55 }
50 return nil 56 return nil
51 } 57 }
52 » return tcf.Interface.GetMulti(keys, cb) 58 » return tcf.RawInterface.GetMulti(keys, cb)
53 } 59 }
54 60
55 func (tcf *checkFilter) PutMulti(keys []Key, vals []PropertyLoadSaver, cb PutMul tiCB) error { 61 func (tcf *checkFilter) PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error {
56 if len(keys) != len(vals) { 62 if len(keys) != len(vals) {
57 » » return fmt.Errorf("rawdatastore: GetMulti with mismatched keys/v als lengths (%d/%d)", len(keys), len(vals)) 63 » » return fmt.Errorf("datastore: GetMulti with mismatched keys/vals lengths (%d/%d)", len(keys), len(vals))
58 } 64 }
59 if len(keys) == 0 { 65 if len(keys) == 0 {
60 return nil 66 return nil
61 } 67 }
68 if cb == nil {
69 return fmt.Errorf("datastore: PutMulti callback is nil")
70 }
62 lme := errors.LazyMultiError{Size: len(keys)} 71 lme := errors.LazyMultiError{Size: len(keys)}
63 for i, k := range keys { 72 for i, k := range keys {
64 if KeyIncomplete(k) { 73 if KeyIncomplete(k) {
74 // use NewKey to avoid going all the way down the stack for this check.
65 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "", 1, k. Parent()) 75 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "", 1, k. Parent())
66 } 76 }
67 if !KeyValid(k, false, tcf.aid, tcf.ns) { 77 if !KeyValid(k, false, tcf.aid, tcf.ns) {
68 lme.Assign(i, ErrInvalidKey) 78 lme.Assign(i, ErrInvalidKey)
69 continue 79 continue
70 } 80 }
71 v := vals[i] 81 v := vals[i]
72 if v == nil { 82 if v == nil {
73 » » » lme.Assign(i, errors.New("rawdatastore: PutMulti got nil vals entry")) 83 » » » lme.Assign(i, errors.New("datastore: PutMulti got nil va ls entry"))
74 » » } else {
75 » » » lme.Assign(i, v.Problem())
76 } 84 }
77 } 85 }
78 if me := lme.Get(); me != nil { 86 if me := lme.Get(); me != nil {
79 for _, err := range me.(errors.MultiError) { 87 for _, err := range me.(errors.MultiError) {
80 cb(nil, err) 88 cb(nil, err)
81 } 89 }
82 return nil 90 return nil
83 } 91 }
84 92
85 » err := error(nil) 93 » return tcf.RawInterface.PutMulti(keys, vals, cb)
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 } 94 }
100 95
101 func (tcf *checkFilter) DeleteMulti(keys []Key, cb DeleteMultiCB) error { 96 func (tcf *checkFilter) DeleteMulti(keys []Key, cb DeleteMultiCB) error {
102 if len(keys) == 0 { 97 if len(keys) == 0 {
103 return nil 98 return nil
104 } 99 }
100 if cb == nil {
101 return fmt.Errorf("datastore: DeleteMulti callback is nil")
102 }
105 lme := errors.LazyMultiError{Size: len(keys)} 103 lme := errors.LazyMultiError{Size: len(keys)}
106 for i, k := range keys { 104 for i, k := range keys {
107 if KeyIncomplete(k) || !KeyValid(k, false, tcf.aid, tcf.ns) { 105 if KeyIncomplete(k) || !KeyValid(k, false, tcf.aid, tcf.ns) {
108 lme.Assign(i, ErrInvalidKey) 106 lme.Assign(i, ErrInvalidKey)
109 } 107 }
110 } 108 }
111 if me := lme.Get(); me != nil { 109 if me := lme.Get(); me != nil {
112 for _, err := range me.(errors.MultiError) { 110 for _, err := range me.(errors.MultiError) {
113 cb(err) 111 cb(err)
114 } 112 }
115 return nil 113 return nil
116 } 114 }
117 » return tcf.Interface.DeleteMulti(keys, cb) 115 » return tcf.RawInterface.DeleteMulti(keys, cb)
118 } 116 }
119 117
120 func applyCheckFilter(c context.Context, i Interface) Interface { 118 func applyCheckFilter(c context.Context, i RawInterface) RawInterface {
121 inf := info.Get(c) 119 inf := info.Get(c)
122 return &checkFilter{i, inf.AppID(), inf.GetNamespace()} 120 return &checkFilter{i, inf.AppID(), inf.GetNamespace()}
123 } 121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698