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

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

Issue 1377863004: Add Key.HasAncestor() (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: Created 5 years, 2 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 | « no previous file | service/datastore/key_test.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 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 a, b := k.toks[i], other.toks[i] 352 a, b := k.toks[i], other.toks[i]
353 if a.Less(b) { 353 if a.Less(b) {
354 return true 354 return true
355 } else if b.Less(a) { 355 } else if b.Less(a) {
356 return false 356 return false
357 } 357 }
358 } 358 }
359 return len(k.toks) < len(other.toks) 359 return len(k.toks) < len(other.toks)
360 } 360 }
361 361
362 // HasAncestor returns true iff other is an ancestor of k (or if other == k).
363 func (k *Key) HasAncestor(other *Key) bool {
364 if k.appID != other.appID || k.namespace != other.namespace {
365 return false
366 }
367 if len(k.toks) < len(other.toks) {
368 return false
369 }
370 for i, tok := range other.toks {
371 if tok != k.toks[i] {
372 return false
373 }
374 }
375 return true
376 }
377
362 // GQL returns a correctly formatted Cloud Datastore GQL key literal. 378 // GQL returns a correctly formatted Cloud Datastore GQL key literal.
363 // 379 //
364 // The flavor of GQL that this emits is defined here: 380 // The flavor of GQL that this emits is defined here:
365 // https://cloud.google.com/datastore/docs/apis/gql/gql_reference 381 // https://cloud.google.com/datastore/docs/apis/gql/gql_reference
366 func (k *Key) GQL() string { 382 func (k *Key) GQL() string {
367 ret := &bytes.Buffer{} 383 ret := &bytes.Buffer{}
368 fmt.Fprintf(ret, "KEY(DATASET(%s)", gqlQuoteString(k.appID)) 384 fmt.Fprintf(ret, "KEY(DATASET(%s)", gqlQuoteString(k.appID))
369 if k.namespace != "" { 385 if k.namespace != "" {
370 fmt.Fprintf(ret, ", NAMESPACE(%s)", gqlQuoteString(k.namespace)) 386 fmt.Fprintf(ret, ", NAMESPACE(%s)", gqlQuoteString(k.namespace))
371 } 387 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 for _, t := range k.toks { 437 for _, t := range k.toks {
422 ret += int64(len(t.Kind)) 438 ret += int64(len(t.Kind))
423 if t.StringID != "" { 439 if t.StringID != "" {
424 ret += int64(len(t.StringID)) 440 ret += int64(len(t.StringID))
425 } else { 441 } else {
426 ret += 8 442 ret += 8
427 } 443 }
428 } 444 }
429 return ret 445 return ret
430 } 446 }
OLDNEW
« no previous file with comments | « no previous file | service/datastore/key_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698