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

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

Issue 1358063003: Add Kind/StringID/IntID back to Key. (Closed) Base URL: https://github.com/luci/gae.git@add_allocate_ids
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
« no previous file with comments | « service/datastore/datastore_test.go ('k') | service/datastore/multiarg.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "bytes" 8 "bytes"
9 "encoding/base64" 9 "encoding/base64"
10 "encoding/json" 10 "encoding/json"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 for i, e := range r.Path.Element { 169 for i, e := range r.Path.Element {
170 ret.toks[i] = KeyTok{ 170 ret.toks[i] = KeyTok{
171 Kind: e.GetType(), 171 Kind: e.GetType(),
172 IntID: e.GetId(), 172 IntID: e.GetId(),
173 StringID: e.GetName(), 173 StringID: e.GetName(),
174 } 174 }
175 } 175 }
176 return 176 return
177 } 177 }
178 178
179 // Last returns the last KeyTok in this Key. Non-nil Keys are always guaranteed 179 // LastTok returns the last KeyTok in this Key. Non-nil Keys are always guarante ed
180 // to have at least one token. 180 // to have at least one token.
181 func (k *Key) Last() KeyTok { 181 func (k *Key) LastTok() KeyTok {
182 return k.toks[len(k.toks)-1] 182 return k.toks[len(k.toks)-1]
183 } 183 }
184 184
185 // AppID returns the application ID that this Key is for. 185 // AppID returns the application ID that this Key is for.
186 func (k *Key) AppID() string { return k.appID } 186 func (k *Key) AppID() string { return k.appID }
187 187
188 // Namespace returns the namespace that this Key is for. 188 // Namespace returns the namespace that this Key is for.
189 func (k *Key) Namespace() string { return k.namespace } 189 func (k *Key) Namespace() string { return k.namespace }
190 190
191 // Kind returns the Kind of the child KeyTok
192 func (k *Key) Kind() string { return k.toks[len(k.toks)-1].Kind }
dnj 2015/09/22 17:56:19 nit: Use "LastTok" here (and in StringID and IntID
iannucci 2015/09/23 23:51:47 nah, it creates an extra copy I don't want.
193
194 // StringID returns the StringID of the child KeyTok
195 func (k *Key) StringID() string { return k.toks[len(k.toks)-1].StringID }
196
197 // IntID returns the IntID of the child KeyTok
198 func (k *Key) IntID() int64 { return k.toks[len(k.toks)-1].IntID }
199
191 // String returns a human-readable representation of the key in the form of 200 // String returns a human-readable representation of the key in the form of
192 // AID:NS:/Kind,id/Kind,id/... 201 // AID:NS:/Kind,id/Kind,id/...
193 func (k *Key) String() string { 202 func (k *Key) String() string {
194 b := bytes.NewBuffer(make([]byte, 0, 512)) 203 b := bytes.NewBuffer(make([]byte, 0, 512))
195 fmt.Fprintf(b, "%s:%s:", k.appID, k.namespace) 204 fmt.Fprintf(b, "%s:%s:", k.appID, k.namespace)
196 for _, t := range k.toks { 205 for _, t := range k.toks {
197 if t.StringID != "" { 206 if t.StringID != "" {
198 fmt.Fprintf(b, "/%s,%q", t.Kind, t.StringID) 207 fmt.Fprintf(b, "/%s,%q", t.Kind, t.StringID)
199 } else { 208 } else {
200 fmt.Fprintf(b, "/%s,%d", t.Kind, t.IntID) 209 fmt.Fprintf(b, "/%s,%d", t.Kind, t.IntID)
201 } 210 }
202 } 211 }
203 return b.String() 212 return b.String()
204 } 213 }
205 214
206 // Incomplete returns true iff k doesn't have an id yet. 215 // Incomplete returns true iff k doesn't have an id yet.
207 func (k *Key) Incomplete() bool { 216 func (k *Key) Incomplete() bool {
208 » return k.Last().Incomplete() 217 » return k.LastTok().Incomplete()
209 } 218 }
210 219
211 // Valid determines if a key is valid, according to a couple rules: 220 // Valid determines if a key is valid, according to a couple rules:
212 // - k is not nil 221 // - k is not nil
213 // - every token of k: 222 // - every token of k:
214 // - (if !allowSpecial) token's kind doesn't start with '__' 223 // - (if !allowSpecial) token's kind doesn't start with '__'
215 // - token's kind and appid are non-blank 224 // - token's kind and appid are non-blank
216 // - token is not incomplete 225 // - token is not incomplete
217 // - all tokens have the same namespace and appid 226 // - all tokens have the same namespace and appid
218 func (k *Key) Valid(allowSpecial bool, aid, ns string) bool { 227 func (k *Key) Valid(allowSpecial bool, aid, ns string) bool {
(...skipping 15 matching lines...) Expand all
234 } 243 }
235 } 244 }
236 return true 245 return true
237 } 246 }
238 247
239 // PartialValid returns true iff this key is suitable for use in a Put 248 // PartialValid returns true iff this key is suitable for use in a Put
240 // operation. This is the same as Valid(k, false, ...), but also allowing k to 249 // operation. This is the same as Valid(k, false, ...), but also allowing k to
241 // be Incomplete(). 250 // be Incomplete().
242 func (k *Key) PartialValid(aid, ns string) bool { 251 func (k *Key) PartialValid(aid, ns string) bool {
243 if k.Incomplete() { 252 if k.Incomplete() {
244 » » k = NewKey(k.AppID(), k.Namespace(), k.Last().Kind, "", 1, k.Par ent()) 253 » » k = NewKey(k.AppID(), k.Namespace(), k.Kind(), "", 1, k.Parent() )
245 } 254 }
246 return k.Valid(false, aid, ns) 255 return k.Valid(false, aid, ns)
247 } 256 }
248 257
249 // Parent returns the parent Key of this *Key, or nil. The parent 258 // Parent returns the parent Key of this *Key, or nil. The parent
250 // will always have the concrete type of *Key. 259 // will always have the concrete type of *Key.
251 func (k *Key) Parent() *Key { 260 func (k *Key) Parent() *Key {
252 if len(k.toks) <= 1 { 261 if len(k.toks) <= 1 {
253 return nil 262 return nil
254 } 263 }
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 // 403 //
395 // toks is guaranteed to be empty if and only if k is nil. If k is non-nil then 404 // toks is guaranteed to be empty if and only if k is nil. If k is non-nil then
396 // it contains at least one token. 405 // it contains at least one token.
397 func (k *Key) Split() (appID, namespace string, toks []KeyTok) { 406 func (k *Key) Split() (appID, namespace string, toks []KeyTok) {
398 appID = k.appID 407 appID = k.appID
399 namespace = k.namespace 408 namespace = k.namespace
400 toks = make([]KeyTok, len(k.toks)) 409 toks = make([]KeyTok, len(k.toks))
401 copy(toks, k.toks) 410 copy(toks, k.toks)
402 return 411 return
403 } 412 }
OLDNEW
« no previous file with comments | « service/datastore/datastore_test.go ('k') | service/datastore/multiarg.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698