| 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" | |
| 11 "github.com/luci/luci-go/common/errors" | 10 "github.com/luci/luci-go/common/errors" |
| 11 |
| 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 » kc KeyContext |
| 19 » ns string | |
| 20 } | 19 } |
| 21 | 20 |
| 22 func (tcf *checkFilter) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { | 21 func (tcf *checkFilter) RunInTransaction(f func(c context.Context) error, opts *
TransactionOptions) error { |
| 22 |
| 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(fq *FinalizedQuery, cb RawRunCB) error { | 29 func (tcf *checkFilter) Run(fq *FinalizedQuery, cb RawRunCB) error { |
| 30 if fq == 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(fq, cb) | 36 return tcf.RawInterface.Run(fq, cb) |
| 37 } | 37 } |
| 38 | 38 |
| 39 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 { |
| 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.IsIncomplete() || !k.Valid(true, tcf.aid, tcf.ns) { | 48 » » if k.IsIncomplete() || !k.Valid(true, tcf.kc) { |
| 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 NewKeyCB) e
rror { | 61 func (tcf *checkFilter) PutMulti(keys []*Key, vals []PropertyMap, cb NewKeyCB) e
rror { |
| 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)) |
| 72 for i, k := range keys { | 72 for i, k := range keys { |
| 73 » » if !k.PartialValid(tcf.aid, tcf.ns) { | 73 » » if !k.PartialValid(tcf.kc) { |
| 74 lme.Assign(i, ErrInvalidKey) | 74 lme.Assign(i, ErrInvalidKey) |
| 75 continue | 75 continue |
| 76 } | 76 } |
| 77 v := vals[i] | 77 v := vals[i] |
| 78 if v == nil { | 78 if v == nil { |
| 79 lme.Assign(i, errors.New("datastore: PutMulti got nil va
ls entry")) | 79 lme.Assign(i, errors.New("datastore: PutMulti got nil va
ls entry")) |
| 80 } | 80 } |
| 81 } | 81 } |
| 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.IsIncomplete() || !k.Valid(false, tcf.aid, tcf.ns) { | 101 » » if k.IsIncomplete() || !k.Valid(false, tcf.kc) { |
| 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 » return &checkFilter{ |
| 116 » ns, _ := inf.GetNamespace() | 116 » » RawInterface: i, |
| 117 » return &checkFilter{i, inf.FullyQualifiedAppID(), ns} | 117 » » kc: GetKeyContext(c), |
| 118 » } |
| 118 } | 119 } |
| 120 |
| 121 func (tcf *checkFilter) WithTransaction(t Transaction) context.Context { |
| 122 // TODO(dnj): Currently only support for entering nil Transactions (i.e.
, |
| 123 // clearing the current Transaction and working against the raw |
| 124 // non-transactional datastore instance) is supported. This will change
when |
| 125 // support for arbitrary transactions is added to the `impl/prod` datast
ore. |
| 126 // Currently, support could be added to the `impl/memory` and `impl/clou
d` |
| 127 // implementations rather easily, but since `impl/prod` is the main |
| 128 // implementation, we will gate formally supporting fluid transactions o
n |
| 129 // adding support there. |
| 130 if t != nil { |
| 131 panic(errors.New("support for entering non-nil Transaction is no
t implenented")) |
| 132 } |
| 133 return tcf.RawInterface.WithTransaction(t) |
| 134 } |
| OLD | NEW |