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

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: 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 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 || cb == nil {
31 return nil 31 return nil
32 } 32 }
33 » return tcf.Interface.Run(q, cb) 33 » return tcf.RawInterface.Run(q, cb)
34 } 34 }
35 35
36 func (tcf *checkFilter) GetMulti(keys []Key, cb GetMultiCB) error { 36 func (tcf *checkFilter) GetMulti(keys []Key, cb GetMultiCB) error {
37 if len(keys) == 0 || cb == nil { 37 if len(keys) == 0 || cb == nil {
38 return nil 38 return nil
39 } 39 }
40 lme := errors.LazyMultiError{Size: len(keys)} 40 lme := errors.LazyMultiError{Size: len(keys)}
41 for i, k := range keys { 41 for i, k := range keys {
42 if KeyIncomplete(k) || !KeyValid(k, true, tcf.aid, tcf.ns) { 42 if KeyIncomplete(k) || !KeyValid(k, true, tcf.aid, tcf.ns) {
43 lme.Assign(i, ErrInvalidKey) 43 lme.Assign(i, ErrInvalidKey)
44 } 44 }
45 } 45 }
46 if me := lme.Get(); me != nil { 46 if me := lme.Get(); me != nil {
47 for _, err := range me.(errors.MultiError) { 47 for _, err := range me.(errors.MultiError) {
48 cb(nil, err) 48 cb(nil, err)
49 } 49 }
50 return nil 50 return nil
51 } 51 }
52 » return tcf.Interface.GetMulti(keys, cb) 52 » return tcf.RawInterface.GetMulti(keys, cb)
53 } 53 }
54 54
55 func (tcf *checkFilter) PutMulti(keys []Key, vals []PropertyLoadSaver, cb PutMul tiCB) error { 55 func (tcf *checkFilter) PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error {
56 if len(keys) != len(vals) { 56 if len(keys) != len(vals) {
57 » » return fmt.Errorf("rawdatastore: GetMulti with mismatched keys/v als lengths (%d/%d)", len(keys), len(vals)) 57 » » return fmt.Errorf("datastore: GetMulti with mismatched keys/vals lengths (%d/%d)", len(keys), len(vals))
58 } 58 }
59 if len(keys) == 0 { 59 if len(keys) == 0 {
60 return nil 60 return nil
61 } 61 }
62 lme := errors.LazyMultiError{Size: len(keys)} 62 lme := errors.LazyMultiError{Size: len(keys)}
63 for i, k := range keys { 63 for i, k := range keys {
64 if KeyIncomplete(k) { 64 if KeyIncomplete(k) {
65 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "", 1, k. Parent()) 65 k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "", 1, k. Parent())
66 } 66 }
67 if !KeyValid(k, false, tcf.aid, tcf.ns) { 67 if !KeyValid(k, false, tcf.aid, tcf.ns) {
68 lme.Assign(i, ErrInvalidKey) 68 lme.Assign(i, ErrInvalidKey)
69 continue 69 continue
70 } 70 }
71 v := vals[i] 71 v := vals[i]
72 if v == nil { 72 if v == nil {
73 » » » if !lme.Assign(i, errors.New("rawdatastore: PutMulti got nil vals entry")) { 73 » » » if !lme.Assign(i, errors.New("datastore: PutMulti got ni l vals entry")) {
74 lme.Assign(i, v.Problem()) 74 lme.Assign(i, v.Problem())
75 } 75 }
76 } 76 }
77 } 77 }
78 if me := lme.Get(); me != nil { 78 if me := lme.Get(); me != nil {
79 for _, err := range me.(errors.MultiError) { 79 for _, err := range me.(errors.MultiError) {
80 cb(nil, err) 80 cb(nil, err)
81 } 81 }
82 return nil 82 return nil
83 } 83 }
84 84
85 » err := error(nil) 85 » 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 } 86 }
100 87
101 func (tcf *checkFilter) DeleteMulti(keys []Key, cb DeleteMultiCB) error { 88 func (tcf *checkFilter) DeleteMulti(keys []Key, cb DeleteMultiCB) error {
102 if len(keys) == 0 { 89 if len(keys) == 0 {
103 return nil 90 return nil
104 } 91 }
105 lme := errors.LazyMultiError{Size: len(keys)} 92 lme := errors.LazyMultiError{Size: len(keys)}
106 for i, k := range keys { 93 for i, k := range keys {
107 if KeyIncomplete(k) || !KeyValid(k, false, tcf.aid, tcf.ns) { 94 if KeyIncomplete(k) || !KeyValid(k, false, tcf.aid, tcf.ns) {
108 lme.Assign(i, ErrInvalidKey) 95 lme.Assign(i, ErrInvalidKey)
109 } 96 }
110 } 97 }
111 if me := lme.Get(); me != nil { 98 if me := lme.Get(); me != nil {
112 for _, err := range me.(errors.MultiError) { 99 for _, err := range me.(errors.MultiError) {
113 cb(err) 100 cb(err)
114 } 101 }
115 return nil 102 return nil
116 } 103 }
117 » return tcf.Interface.DeleteMulti(keys, cb) 104 » return tcf.RawInterface.DeleteMulti(keys, cb)
118 } 105 }
119 106
120 func applyCheckFilter(c context.Context, i Interface) Interface { 107 func applyCheckFilter(c context.Context, i RawInterface) RawInterface {
121 inf := info.Get(c) 108 inf := info.Get(c)
122 return &checkFilter{i, inf.AppID(), inf.GetNamespace()} 109 return &checkFilter{i, inf.AppID(), inf.GetNamespace()}
123 } 110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698