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

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

Issue 1355783002: Refactor keys and queries in datastore service and implementation. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Created 5 years, 3 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 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 if idx >= len(m) || m[idx] == nil { 93 if idx >= len(m) || m[idx] == nil {
147 return nullMetaGetter 94 return nullMetaGetter
148 } 95 }
149 return m[idx] 96 return m[idx]
150 } 97 }
151 98
152 // RawInterface implements the datastore functionality without any of the fancy 99 // RawInterface implements the datastore functionality without any of the fancy
153 // reflection stuff. This is so that Filters can avoid doing lots of redundant 100 // 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. 101 // reflection work. See datastore.RawInterface for a more user-friendly interfac e.
155 type RawInterface interface { 102 type RawInterface interface {
156 NewKey(kind, stringID string, intID int64, parent Key) Key
157 DecodeKey(encoded string) (Key, error)
158 NewQuery(kind string) Query
iannucci 2015/09/18 04:31:53 bye!!!!
159
160 // RunInTransaction runs f in a transaction. 103 // RunInTransaction runs f in a transaction.
161 // 104 //
162 // opts may be nil. 105 // opts may be nil.
163 // 106 //
164 // NOTE: Implementations and filters are guaranteed that: 107 // NOTE: Implementations and filters are guaranteed that:
165 // - f is not nil 108 // - f is not nil
166 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio ns) error 109 RunInTransaction(f func(c context.Context) error, opts *TransactionOptio ns) error
167 110
168 // DecodeCursor converts a string returned by a Cursor into a Cursor ins tance. 111 // 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 112 // It will return an error if the supplied string is not valid, or could not
170 // be decoded by the implementation. 113 // be decoded by the implementation.
171 DecodeCursor(s string) (Cursor, error) 114 DecodeCursor(s string) (Cursor, error)
172 115
173 // Run executes the given query, and calls `cb` for each successfully it em. 116 // Run executes the given query, and calls `cb` for each successfully it em.
174 // 117 //
175 // NOTE: Implementations and filters are guaranteed that: 118 // NOTE: Implementations and filters are guaranteed that:
176 // - query is not nil 119 // - query is not nil
177 // - cb is not nil 120 // - cb is not nil
178 » Run(q Query, cb RawRunCB) error 121 » Run(q *FinalizedQuery, cb RawRunCB) error
179 122
180 // GetMulti retrieves items from the datastore. 123 // GetMulti retrieves items from the datastore.
181 // 124 //
182 // Callback execues once per key, in the order of keys. Callback may not 125 // 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 126 // execute at all if there's a server error. If callback is nil, this
184 // method does nothing. 127 // method does nothing.
185 // 128 //
186 // meta is used to propagate metadata from higher levels. 129 // meta is used to propagate metadata from higher levels.
187 // 130 //
188 // NOTE: Implementations and filters are guaranteed that: 131 // NOTE: Implementations and filters are guaranteed that:
189 // - len(keys) > 0 132 // - len(keys) > 0
190 // - all keys are Valid, !Incomplete, and in the current namespace 133 // - all keys are Valid, !Incomplete, and in the current namespace
191 // - cb is not nil 134 // - cb is not nil
192 » GetMulti(keys []Key, meta MultiMetaGetter, cb GetMultiCB) error 135 » GetMulti(keys []*Key, meta MultiMetaGetter, cb GetMultiCB) error
193 136
194 // PutMulti writes items to the datastore. 137 // PutMulti writes items to the datastore.
195 // 138 //
196 // Callback execues once per key/value pair, in the passed-in order. Cal lback 139 // 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. 140 // may not execute at all if there was a server error.
198 // 141 //
199 // NOTE: Implementations and filters are guaranteed that: 142 // NOTE: Implementations and filters are guaranteed that:
200 // - len(keys) > 0 143 // - len(keys) > 0
201 // - len(keys) == len(vals) 144 // - len(keys) == len(vals)
202 // - all keys are Valid and in the current namespace 145 // - all keys are Valid and in the current namespace
203 // - cb is not nil 146 // - cb is not nil
204 » PutMulti(keys []Key, vals []PropertyMap, cb PutMultiCB) error 147 » PutMulti(keys []*Key, vals []PropertyMap, cb PutMultiCB) error
205 148
206 // DeleteMulti removes items from the datastore. 149 // DeleteMulti removes items from the datastore.
207 // 150 //
208 // Callback execues once per key, in the order of keys. Callback may not 151 // Callback execues once per key, in the order of keys. Callback may not
209 // execute at all if there's a server error. 152 // execute at all if there's a server error.
210 // 153 //
211 // NOTE: Implementations and filters are guaranteed that 154 // NOTE: Implementations and filters are guaranteed that
212 // - len(keys) > 0 155 // - len(keys) > 0
213 // - all keys are Valid, !Incomplete, and in the current namespace 156 // - all keys are Valid, !Incomplete, and in the current namespace
214 // - none keys of the keys are 'special' (use a kind prefixed with '__ ') 157 // - none keys of the keys are 'special' (use a kind prefixed with '__ ')
215 // - cb is not nil 158 // - cb is not nil
216 » DeleteMulti(keys []Key, cb DeleteMultiCB) error 159 » DeleteMulti(keys []*Key, cb DeleteMultiCB) error
217 160
218 // Testable returns the Testable interface for the implementation, or ni l if 161 // Testable returns the Testable interface for the implementation, or ni l if
219 // there is none. 162 // there is none.
220 Testable() Testable 163 Testable() Testable
221 } 164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698