| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package meta | 5 package meta |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "strings" | 9 "strings" |
| 10 | 10 |
| 11 » "github.com/luci/gae/service/datastore" | 11 » ds "github.com/luci/gae/service/datastore" |
| 12 |
| 12 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 13 ) | 14 ) |
| 14 | 15 |
| 15 // NamespacesCallback is the callback type used with Namespaces. The callback | 16 // NamespacesCallback is the callback type used with Namespaces. The callback |
| 16 // will be invoked with each identified namespace. | 17 // will be invoked with each identified namespace. |
| 17 // | 18 // |
| 18 // If the callback returns an error, iteration will stop. If the error is | 19 // If the callback returns an error, iteration will stop. If the error is |
| 19 // datastore.Stop, Namespaces will stop iterating and return nil. Otherwise, | 20 // datastore.Stop, Namespaces will stop iterating and return nil. Otherwise, |
| 20 // the error will be forwarded. | 21 // the error will be forwarded. |
| 21 type NamespacesCallback func(string) error | 22 type NamespacesCallback func(string) error |
| 22 | 23 |
| 23 // Namespaces returns a list of all of the namespaces in the datastore. | 24 // Namespaces returns a list of all of the namespaces in the datastore. |
| 24 // | 25 // |
| 25 // This is done by issuing a datastore query for kind "__namespace__". The | 26 // This is done by issuing a datastore query for kind "__namespace__". The |
| 26 // resulting keys will have IDs for the namespaces, namely: | 27 // resulting keys will have IDs for the namespaces, namely: |
| 27 // - The empty namespace will have integer ID 1. This will be forwarded to
the | 28 // - The empty namespace will have integer ID 1. This will be forwarded to
the |
| 28 // callback as "". | 29 // callback as "". |
| 29 // - Other namespaces will have non-zero string IDs. | 30 // - Other namespaces will have non-zero string IDs. |
| 30 func Namespaces(c context.Context, cb NamespacesCallback) error { | 31 func Namespaces(c context.Context, cb NamespacesCallback) error { |
| 31 » q := datastore.NewQuery("__namespace__").KeysOnly(true) | 32 » q := ds.NewQuery("__namespace__").KeysOnly(true) |
| 32 | 33 |
| 33 // Query our datastore for the full set of namespaces. | 34 // Query our datastore for the full set of namespaces. |
| 34 » return datastore.Get(c).Run(q, func(k *datastore.Key) error { | 35 » return ds.Run(c, q, func(k *ds.Key) error { |
| 35 switch { | 36 switch { |
| 36 case k.IntID() == 1: | 37 case k.IntID() == 1: |
| 37 return cb("") | 38 return cb("") |
| 38 case k.IntID() != 0: | 39 case k.IntID() != 0: |
| 39 return fmt.Errorf("unexpected namepsace integer key (%d)
", k.IntID()) | 40 return fmt.Errorf("unexpected namepsace integer key (%d)
", k.IntID()) |
| 40 default: | 41 default: |
| 41 return cb(k.StringID()) | 42 return cb(k.StringID()) |
| 42 } | 43 } |
| 43 }) | 44 }) |
| 44 } | 45 } |
| 45 | 46 |
| 46 // NamespacesWithPrefix runs Namespaces, returning only namespaces beginning | 47 // NamespacesWithPrefix runs Namespaces, returning only namespaces beginning |
| 47 // with the supplied prefix string. | 48 // with the supplied prefix string. |
| 48 func NamespacesWithPrefix(c context.Context, p string, cb NamespacesCallback) er
ror { | 49 func NamespacesWithPrefix(c context.Context, p string, cb NamespacesCallback) er
ror { |
| 49 // TODO: https://github.com/luci/gae/issues/49 : When inequality filters
are | 50 // TODO: https://github.com/luci/gae/issues/49 : When inequality filters
are |
| 50 // supported, implement this using a "Gte" filter. | 51 // supported, implement this using a "Gte" filter. |
| 51 any := false | 52 any := false |
| 52 return Namespaces(c, func(ns string) error { | 53 return Namespaces(c, func(ns string) error { |
| 53 if !strings.HasPrefix(ns, p) { | 54 if !strings.HasPrefix(ns, p) { |
| 54 if any { | 55 if any { |
| 55 » » » » return datastore.Stop | 56 » » » » return ds.Stop |
| 56 } | 57 } |
| 57 return nil | 58 return nil |
| 58 } | 59 } |
| 59 | 60 |
| 60 any = true | 61 any = true |
| 61 return cb(ns) | 62 return cb(ns) |
| 62 }) | 63 }) |
| 63 } | 64 } |
| 64 | 65 |
| 65 // NamespacesCollector exposes a NamespacesCallback function that aggregates | 66 // NamespacesCollector exposes a NamespacesCallback function that aggregates |
| 66 // resulting namespaces into the collector slice. | 67 // resulting namespaces into the collector slice. |
| 67 type NamespacesCollector []string | 68 type NamespacesCollector []string |
| 68 | 69 |
| 69 // Callback is a NamespacesCallback which adds each namespace to the collector. | 70 // Callback is a NamespacesCallback which adds each namespace to the collector. |
| 70 func (c *NamespacesCollector) Callback(v string) error { | 71 func (c *NamespacesCollector) Callback(v string) error { |
| 71 *c = append(*c, v) | 72 *c = append(*c, v) |
| 72 return nil | 73 return nil |
| 73 } | 74 } |
| OLD | NEW |