| 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" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // Key is the equivalent of *datastore.Key from the original SDK, except that | |
| 14 // it can have multiple implementations. See helper.Key* methods for missing | |
| 15 // methods like KeyIncomplete (and some new ones like KeyValid). | |
| 16 type Key interface { | |
| 17 Kind() string | |
| 18 StringID() string | |
| 19 IntID() int64 | |
| 20 Parent() Key | |
| 21 AppID() string | |
| 22 Namespace() string | |
| 23 | |
| 24 // Incomplete returns true iff k doesn't have an id yet. | |
| 25 Incomplete() bool | |
| 26 | |
| 27 // Valid determines if a key is valid, according to a couple rules: | |
| 28 // - k is not nil | |
| 29 // - every token of k: | |
| 30 // - (if !allowSpecial) token's kind doesn't start with '__' | |
| 31 // - token's kind and appid are non-blank | |
| 32 // - token is not incomplete | |
| 33 // - all tokens have the same namespace and appid | |
| 34 Valid(allowSpecial bool, aid, ns string) bool | |
| 35 | |
| 36 // PartialValid determines if a key is valid for a Put operation. This i
s | |
| 37 // like Valid(false, aid, ns), except that the childmost key is allowed
to | |
| 38 // be Incomplete(). | |
| 39 PartialValid(aid, ns string) bool | |
| 40 | |
| 41 String() string | |
| 42 } | |
| 43 | |
| 44 // KeyTok is a single token from a multi-part Key. | |
| 45 type KeyTok struct { | |
| 46 Kind string | |
| 47 IntID int64 | |
| 48 StringID string | |
| 49 } | |
| 50 | |
| 51 // Cursor wraps datastore.Cursor. | 13 // Cursor wraps datastore.Cursor. |
| 52 type Cursor interface { | 14 type Cursor interface { |
| 53 fmt.Stringer | 15 fmt.Stringer |
| 54 } | 16 } |
| 55 | 17 |
| 56 // Query wraps datastore.Query. | |
| 57 type Query interface { | |
| 58 Ancestor(ancestor Key) Query | |
| 59 Distinct() Query | |
| 60 End(c Cursor) Query | |
| 61 EventualConsistency() Query | |
| 62 Filter(filterStr string, value interface{}) Query | |
| 63 KeysOnly() Query | |
| 64 Limit(limit int) Query | |
| 65 Offset(offset int) Query | |
| 66 Order(fieldName string) Query | |
| 67 Project(fieldNames ...string) Query | |
| 68 Start(c Cursor) Query | |
| 69 } | |
| 70 | |
| 71 // CursorCB is used to obtain a Cursor while Run'ing a query on either | 18 // CursorCB is used to obtain a Cursor while Run'ing a query on either |
| 72 // Interface or RawInterface. | 19 // Interface or RawInterface. |
| 73 // | 20 // |
| 74 // it can be invoked to obtain the current cursor. | 21 // it can be invoked to obtain the current cursor. |
| 75 type CursorCB func() (Cursor, error) | 22 type CursorCB func() (Cursor, error) |
| 76 | 23 |
| 77 // RawRunCB is the callback signature provided to RawInterface.Run | 24 // RawRunCB is the callback signature provided to RawInterface.Run |
| 78 // | 25 // |
| 79 // - key is the Key of the entity | 26 // - key is the Key of the entity |
| 80 // - val is the data of the entity (or nil, if the query was keys-only) | 27 // - val is the data of the entity (or nil, if the query was keys-only) |
| 81 // | 28 // |
| 82 // Return true to continue iterating through the query results, or false to stop
. | 29 // Return true to continue iterating through the query results, or false to stop
. |
| 83 type RawRunCB func(key Key, val PropertyMap, getCursor CursorCB) bool | 30 type RawRunCB func(key *Key, val PropertyMap, getCursor CursorCB) bool |
| 84 | 31 |
| 85 // GetMultiCB is the callback signature provided to RawInterface.GetMulti | 32 // GetMultiCB is the callback signature provided to RawInterface.GetMulti |
| 86 // | 33 // |
| 87 // - val is the data of the entity | 34 // - val is the data of the entity |
| 88 // * It may be nil if some of the keys to the GetMulti were bad, since all | 35 // * It may be nil if some of the keys to the GetMulti were bad, since all |
| 89 // keys are validated before the RPC occurs! | 36 // keys are validated before the RPC occurs! |
| 90 // - err is an error associated with this entity (e.g. ErrNoSuchEntity). | 37 // - err is an error associated with this entity (e.g. ErrNoSuchEntity). |
| 91 type GetMultiCB func(val PropertyMap, err error) | 38 type GetMultiCB func(val PropertyMap, err error) |
| 92 | 39 |
| 93 // PutMultiCB is the callback signature provided to RawInterface.PutMulti | 40 // PutMultiCB is the callback signature provided to RawInterface.PutMulti |
| 94 // | 41 // |
| 95 // - key is the new key for the entity (if the original was incomplete) | 42 // - key is the new key for the entity (if the original was incomplete) |
| 96 // * It may be nil if some of the keys/vals to the PutMulti were bad, since | 43 // * It may be nil if some of the keys/vals to the PutMulti were bad, since |
| 97 // all keys are validated before the RPC occurs! | 44 // all keys are validated before the RPC occurs! |
| 98 // - err is an error associated with putting this entity. | 45 // - err is an error associated with putting this entity. |
| 99 type PutMultiCB func(key Key, err error) | 46 type PutMultiCB func(key *Key, err error) |
| 100 | 47 |
| 101 // DeleteMultiCB is the callback signature provided to RawInterface.DeleteMulti | 48 // DeleteMultiCB is the callback signature provided to RawInterface.DeleteMulti |
| 102 // | 49 // |
| 103 // - err is an error associated with deleting this entity. | 50 // - err is an error associated with deleting this entity. |
| 104 type DeleteMultiCB func(err error) | 51 type DeleteMultiCB func(err error) |
| 105 | 52 |
| 106 type nullMetaGetterType struct{} | 53 type nullMetaGetterType struct{} |
| 107 | 54 |
| 108 func (nullMetaGetterType) GetMeta(string) (interface{}, error)
{ return nil, ErrMetaFieldUnset } | 55 func (nullMetaGetterType) GetMeta(string) (interface{}, error)
{ return nil, ErrMetaFieldUnset } |
| 109 func (nullMetaGetterType) GetMetaDefault(_ string, dflt interface{}) interface{}
{ return dflt } | 56 func (nullMetaGetterType) GetMetaDefault(_ string, dflt interface{}) interface{}
{ return dflt } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 135 return m.GetSingle(idx).GetMeta(key) | 82 return m.GetSingle(idx).GetMeta(key) |
| 136 } | 83 } |
| 137 | 84 |
| 138 // GetMetaDefault is like PropertyLoadSaver.GetMetaDefault, but it also takes an | 85 // GetMetaDefault is like PropertyLoadSaver.GetMetaDefault, but it also takes an |
| 139 // index indicating which slot you want metadata for. If idx isn't there, this | 86 // index indicating which slot you want metadata for. If idx isn't there, this |
| 140 // returns dflt. | 87 // returns dflt. |
| 141 func (m MultiMetaGetter) GetMetaDefault(idx int, key string, dflt interface{}) i
nterface{} { | 88 func (m MultiMetaGetter) GetMetaDefault(idx int, key string, dflt interface{}) i
nterface{} { |
| 142 return m.GetSingle(idx).GetMetaDefault(key, dflt) | 89 return m.GetSingle(idx).GetMetaDefault(key, dflt) |
| 143 } | 90 } |
| 144 | 91 |
| 92 // GetSingle gets a single MetaGetter at the specified index. |
| 145 func (m MultiMetaGetter) GetSingle(idx int) MetaGetter { | 93 func (m MultiMetaGetter) GetSingle(idx int) MetaGetter { |
| 146 if idx >= len(m) || m[idx] == nil { | 94 if idx >= len(m) || m[idx] == nil { |
| 147 return nullMetaGetter | 95 return nullMetaGetter |
| 148 } | 96 } |
| 149 return m[idx] | 97 return m[idx] |
| 150 } | 98 } |
| 151 | 99 |
| 152 // RawInterface implements the datastore functionality without any of the fancy | 100 // RawInterface implements the datastore functionality without any of the fancy |
| 153 // reflection stuff. This is so that Filters can avoid doing lots of redundant | 101 // reflection stuff. This is so that Filters can avoid doing lots of redundant |
| 154 // reflection work. See datastore.RawInterface for a more user-friendly interfac
e. | 102 // reflection work. See datastore.RawInterface for a more user-friendly interfac
e. |
| 155 type RawInterface interface { | 103 type RawInterface interface { |
| 156 NewKey(kind, stringID string, intID int64, parent Key) Key | |
| 157 DecodeKey(encoded string) (Key, error) | |
| 158 NewQuery(kind string) Query | |
| 159 | |
| 160 // RunInTransaction runs f in a transaction. | 104 // RunInTransaction runs f in a transaction. |
| 161 // | 105 // |
| 162 // opts may be nil. | 106 // opts may be nil. |
| 163 // | 107 // |
| 164 // NOTE: Implementations and filters are guaranteed that: | 108 // NOTE: Implementations and filters are guaranteed that: |
| 165 // - f is not nil | 109 // - f is not nil |
| 166 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio
ns) error | 110 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio
ns) error |
| 167 | 111 |
| 168 // DecodeCursor converts a string returned by a Cursor into a Cursor ins
tance. | 112 // DecodeCursor converts a string returned by a Cursor into a Cursor ins
tance. |
| 169 // It will return an error if the supplied string is not valid, or could
not | 113 // It will return an error if the supplied string is not valid, or could
not |
| 170 // be decoded by the implementation. | 114 // be decoded by the implementation. |
| 171 DecodeCursor(s string) (Cursor, error) | 115 DecodeCursor(s string) (Cursor, error) |
| 172 | 116 |
| 173 // Run executes the given query, and calls `cb` for each successfully it
em. | 117 // Run executes the given query, and calls `cb` for each successfully it
em. |
| 174 // | 118 // |
| 175 // NOTE: Implementations and filters are guaranteed that: | 119 // NOTE: Implementations and filters are guaranteed that: |
| 176 // - query is not nil | 120 // - query is not nil |
| 177 // - cb is not nil | 121 // - cb is not nil |
| 178 » Run(q Query, cb RawRunCB) error | 122 » Run(q *FinalizedQuery, cb RawRunCB) error |
| 179 | 123 |
| 180 // GetMulti retrieves items from the datastore. | 124 // GetMulti retrieves items from the datastore. |
| 181 // | 125 // |
| 182 // Callback execues once per key, in the order of keys. Callback may not | 126 // Callback execues once per key, in the order of keys. Callback may not |
| 183 // execute at all if there's a server error. If callback is nil, this | 127 // execute at all if there's a server error. If callback is nil, this |
| 184 // method does nothing. | 128 // method does nothing. |
| 185 // | 129 // |
| 186 // meta is used to propagate metadata from higher levels. | 130 // meta is used to propagate metadata from higher levels. |
| 187 // | 131 // |
| 188 // NOTE: Implementations and filters are guaranteed that: | 132 // NOTE: Implementations and filters are guaranteed that: |
| 189 // - len(keys) > 0 | 133 // - len(keys) > 0 |
| 190 // - all keys are Valid, !Incomplete, and in the current namespace | 134 // - all keys are Valid, !Incomplete, and in the current namespace |
| 191 // - cb is not nil | 135 // - cb is not nil |
| 192 » GetMulti(keys []Key, meta MultiMetaGetter, cb GetMultiCB) error | 136 » GetMulti(keys []*Key, meta MultiMetaGetter, cb GetMultiCB) error |
| 193 | 137 |
| 194 // PutMulti writes items to the datastore. | 138 // PutMulti writes items to the datastore. |
| 195 // | 139 // |
| 196 // Callback execues once per key/value pair, in the passed-in order. Cal
lback | 140 // Callback execues once per key/value pair, in the passed-in order. Cal
lback |
| 197 // may not execute at all if there was a server error. | 141 // may not execute at all if there was a server error. |
| 198 // | 142 // |
| 199 // NOTE: Implementations and filters are guaranteed that: | 143 // NOTE: Implementations and filters are guaranteed that: |
| 200 // - len(keys) > 0 | 144 // - len(keys) > 0 |
| 201 // - len(keys) == len(vals) | 145 // - len(keys) == len(vals) |
| 202 // - all keys are Valid and in the current namespace | 146 // - all keys are Valid and in the current namespace |
| 203 // - cb is not nil | 147 // - cb is not nil |
| 204 » PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error | 148 » PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB) error |
| 205 | 149 |
| 206 // DeleteMulti removes items from the datastore. | 150 // DeleteMulti removes items from the datastore. |
| 207 // | 151 // |
| 208 // Callback execues once per key, in the order of keys. Callback may not | 152 // Callback execues once per key, in the order of keys. Callback may not |
| 209 // execute at all if there's a server error. | 153 // execute at all if there's a server error. |
| 210 // | 154 // |
| 211 // NOTE: Implementations and filters are guaranteed that | 155 // NOTE: Implementations and filters are guaranteed that |
| 212 // - len(keys) > 0 | 156 // - len(keys) > 0 |
| 213 // - all keys are Valid, !Incomplete, and in the current namespace | 157 // - all keys are Valid, !Incomplete, and in the current namespace |
| 214 // - none keys of the keys are 'special' (use a kind prefixed with '__
') | 158 // - none keys of the keys are 'special' (use a kind prefixed with '__
') |
| 215 // - cb is not nil | 159 // - cb is not nil |
| 216 » DeleteMulti(keys []Key, cb DeleteMultiCB) error | 160 » DeleteMulti(keys []*Key, cb DeleteMultiCB) error |
| 217 | 161 |
| 218 // Testable returns the Testable interface for the implementation, or ni
l if | 162 // Testable returns the Testable interface for the implementation, or ni
l if |
| 219 // there is none. | 163 // there is none. |
| 220 Testable() Testable | 164 Testable() Testable |
| 221 } | 165 } |
| OLD | NEW |