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 "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 Loading... |
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 Loading... |
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 } |
OLD | NEW |