Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 datastore | 5 package datastore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 "golang.org/x/net/context" | 10 "golang.org/x/net/context" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 // * It may be nil if some of the keys/vals to the PutMulti were bad, since | 74 // * It may be nil if some of the keys/vals to the PutMulti were bad, since |
| 75 // all keys are validated before the RPC occurs! | 75 // all keys are validated before the RPC occurs! |
| 76 // - err is an error associated with putting this entity. | 76 // - err is an error associated with putting this entity. |
| 77 type PutMultiCB func(key Key, err error) | 77 type PutMultiCB func(key Key, err error) |
| 78 | 78 |
| 79 // DeleteMultiCB is the callback signature provided to RawInterface.DeleteMulti | 79 // DeleteMultiCB is the callback signature provided to RawInterface.DeleteMulti |
| 80 // | 80 // |
| 81 // - err is an error associated with deleting this entity. | 81 // - err is an error associated with deleting this entity. |
| 82 type DeleteMultiCB func(err error) | 82 type DeleteMultiCB func(err error) |
| 83 | 83 |
| 84 type nullMetaGetterType struct{} | |
| 85 | |
| 86 func (nullMetaGetterType) GetMeta(string) (interface{}, error) { return nil, ErrMetaFieldUnset } | |
| 87 func (nullMetaGetterType) GetMetaDefault(_ string, dflt interface{}) interface{} { return dflt } | |
| 88 | |
| 89 var nullMetaGetter MetaGetter = nullMetaGetterType{} | |
| 90 | |
| 91 // MultiMetaGetter is a carrier for metadata, used with RawInterface.GetMulti | |
| 92 // | |
| 93 // It's OK to default-construct this. GetMeta will just return | |
| 94 // (nil, ErrMetaFieldUnset) for every index. | |
| 95 type MultiMetaGetter []MetaGetter | |
| 96 | |
| 97 // NewMultiMeta returns a new MultiMetaGetter object. data may be nil. | |
| 98 func NewMultiMeta(data []PropertyMap) MultiMetaGetter { | |
| 99 if len(data) == 0 { | |
| 100 return nil | |
| 101 } | |
| 102 inner := make(MultiMetaGetter, len(data)) | |
| 103 for i, pm := range data { | |
| 104 inner[i] = pm | |
| 105 } | |
| 106 return inner | |
| 107 } | |
| 108 | |
| 109 // GetMeta is like PropertyLoadSaver.GetMeta, but it also takes an index | |
| 110 // indicating which slot you want metadata for. If idx isn't there, this | |
| 111 // returns (nil, ErrMetaFieldUnset). | |
| 112 func (m MultiMetaGetter) GetMeta(idx int, key string) (interface{}, error) { | |
| 113 if idx >= len(m) { | |
|
dnj
2015/08/04 22:16:06
What's the use case for tolerating anything other
| |
| 114 return nil, ErrMetaFieldUnset | |
| 115 } | |
| 116 return m[idx].GetMeta(key) | |
| 117 } | |
| 118 | |
| 119 // GetMetaDefault is like PropertyLoadSaver.GetMetaDefault, but it also takes an | |
| 120 // index indicating which slot you want metadata for. If idx isn't there, this | |
| 121 // returns dflt. | |
| 122 func (m MultiMetaGetter) GetMetaDefault(idx int, key string, dflt interface{}) i nterface{} { | |
| 123 if idx >= len(m) { | |
| 124 return dflt | |
| 125 } | |
| 126 return m[idx].GetMetaDefault(key, dflt) | |
| 127 } | |
| 128 | |
| 129 func (m MultiMetaGetter) GetSingle(idx int) MetaGetter { | |
| 130 if idx >= len(m) { | |
| 131 return nullMetaGetter | |
| 132 } | |
| 133 return m[idx] | |
| 134 } | |
| 135 | |
| 84 // RawInterface implements the datastore functionality without any of the fancy | 136 // RawInterface implements the datastore functionality without any of the fancy |
| 85 // reflection stuff. This is so that Filters can avoid doing lots of redundant | 137 // reflection stuff. This is so that Filters can avoid doing lots of redundant |
| 86 // reflection work. See datastore.RawInterface for a more user-friendly interfac e. | 138 // reflection work. See datastore.RawInterface for a more user-friendly interfac e. |
| 87 type RawInterface interface { | 139 type RawInterface interface { |
| 88 NewKey(kind, stringID string, intID int64, parent Key) Key | 140 NewKey(kind, stringID string, intID int64, parent Key) Key |
| 89 DecodeKey(encoded string) (Key, error) | 141 DecodeKey(encoded string) (Key, error) |
| 90 NewQuery(kind string) Query | 142 NewQuery(kind string) Query |
| 91 | 143 |
| 92 // RunInTransaction runs f in a transaction. | 144 // RunInTransaction runs f in a transaction. |
| 93 // | 145 // |
| 94 // opts may be nil. | 146 // opts may be nil. |
| 95 // | 147 // |
| 96 // NOTE: Implementations and filters are guaranteed that: | 148 // NOTE: Implementations and filters are guaranteed that: |
| 97 // - f is not nil | 149 // - f is not nil |
| 98 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio ns) error | 150 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio ns) error |
| 99 | 151 |
| 100 // Run executes the given query, and calls `cb` for each successfully it em. | 152 // Run executes the given query, and calls `cb` for each successfully it em. |
| 101 // | 153 // |
| 102 // NOTE: Implementations and filters are guaranteed that: | 154 // NOTE: Implementations and filters are guaranteed that: |
| 103 // - query is not nil | 155 // - query is not nil |
| 104 // - cb is not nil | 156 // - cb is not nil |
| 105 Run(q Query, cb RawRunCB) error | 157 Run(q Query, cb RawRunCB) error |
| 106 | 158 |
| 107 // GetMulti retrieves items from the datastore. | 159 // GetMulti retrieves items from the datastore. |
| 108 // | 160 // |
| 109 // Callback execues once per key, in the order of keys. Callback may not | 161 // Callback execues once per key, in the order of keys. Callback may not |
| 110 // execute at all if there's a server error. If callback is nil, this | 162 // execute at all if there's a server error. If callback is nil, this |
| 111 // method does nothing. | 163 // method does nothing. |
| 112 // | 164 // |
| 165 // meta is used to propagate metadata from higher levels. | |
| 166 // | |
| 113 // NOTE: Implementations and filters are guaranteed that: | 167 // NOTE: Implementations and filters are guaranteed that: |
| 114 // - len(keys) > 0 | 168 // - len(keys) > 0 |
| 115 // - all keys are Valid, !Incomplete, and in the current namespace | 169 // - all keys are Valid, !Incomplete, and in the current namespace |
| 116 // - cb is not nil | 170 // - cb is not nil |
| 117 » GetMulti(keys []Key, cb GetMultiCB) error | 171 » GetMulti(keys []Key, meta MultiMetaGetter, cb GetMultiCB) error |
| 118 | 172 |
| 119 // PutMulti writes items to the datastore. | 173 // PutMulti writes items to the datastore. |
| 120 // | 174 // |
| 121 // Callback execues once per key/value pair, in the passed-in order. Cal lback | 175 // Callback execues once per key/value pair, in the passed-in order. Cal lback |
| 122 // may not execute at all if there was a server error. | 176 // may not execute at all if there was a server error. |
| 123 // | 177 // |
| 124 // NOTE: Implementations and filters are guaranteed that: | 178 // NOTE: Implementations and filters are guaranteed that: |
| 125 // - len(keys) > 0 | 179 // - len(keys) > 0 |
| 126 // - len(keys) == len(vals) | 180 // - len(keys) == len(vals) |
| 127 // - all keys are Valid and in the current namespace | 181 // - all keys are Valid and in the current namespace |
| 128 // - cb is not nil | 182 // - cb is not nil |
| 129 PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error | 183 PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error |
| 130 | 184 |
| 131 // DeleteMulti removes items from the datastore. | 185 // DeleteMulti removes items from the datastore. |
| 132 // | 186 // |
| 133 // Callback execues once per key, in the order of keys. Callback may not | 187 // Callback execues once per key, in the order of keys. Callback may not |
| 134 // execute at all if there's a server error. | 188 // execute at all if there's a server error. |
| 135 // | 189 // |
| 136 // NOTE: Implementations and filters are guaranteed that | 190 // NOTE: Implementations and filters are guaranteed that |
| 137 // - len(keys) > 0 | 191 // - len(keys) > 0 |
| 138 // - all keys are Valid, !Incomplete, and in the current namespace | 192 // - all keys are Valid, !Incomplete, and in the current namespace |
| 139 // - none keys of the keys are 'special' (use a kind prefixed with '__ ') | 193 // - none keys of the keys are 'special' (use a kind prefixed with '__ ') |
| 140 // - cb is not nil | 194 // - cb is not nil |
| 141 DeleteMulti(keys []Key, cb DeleteMultiCB) error | 195 DeleteMulti(keys []Key, cb DeleteMultiCB) error |
| 142 } | 196 } |
| OLD | NEW |