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

Side by Side Diff: service/datastore/meta/namespaces.go

Issue 1917513002: Add meta package to datastore. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/gae@master
Patch Set: Use callback interface, add namespace collector. Created 4 years, 8 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/meta/eg_test.go ('k') | service/datastore/meta/namespaces_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
(Empty)
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
3 // found in the LICENSE file.
4
5 package meta
6
7 import (
8 "strings"
9
10 "github.com/luci/gae/service/datastore"
11 "golang.org/x/net/context"
12 )
13
14 // NamespacesCallback is the callback type used with Namespaces. The callback
15 // will be invoked with each identified namespace.
16 //
17 // If the callback returns an error, iteration will stop. If the error is
18 // datastore.Stop, Namespaces will stop iterating and return nil. Otherwise,
19 // the error will be forwarded.
20 type NamespacesCallback func(string) error
21
22 // Namespaces returns a list of all of the namespaces in the datastore.
23 //
24 // This is done by issuing a datastore query for kind "__namespace__". The
25 // resulting keys will have IDs for the namespaces, namely:
26 // - The empty namespace will have integer ID 1. We ignore this.
iannucci 2016/04/25 08:05:27 Out of curiosity... why? Why not call cb("")?
dnj 2016/04/25 15:24:17 The thought was that someone enumerating namespace
27 // - Other namespaces will have string IDs.
28 func Namespaces(c context.Context, cb NamespacesCallback) error {
29 q := datastore.NewQuery("__namespace__").KeysOnly(true)
30
31 // Query our datastore for the full set of namespaces.
32 return datastore.Get(c).Run(q, func(k *datastore.Key) error {
33 ns := k.StringID()
34 if ns == "" {
35 return nil
36 }
37
38 return cb(ns)
39 })
40 }
41
42 // NamespacesWithPrefix runs Namespaces, returning only namespaces beginning
43 // with the supplied prefix string.
44 func NamespacesWithPrefix(c context.Context, p string, cb NamespacesCallback) er ror {
iannucci 2016/04/25 08:05:27 TODO: https://github.com/luci/gae/issues/49 : Actu
dnj 2016/04/25 15:24:17 Done.
45 any := false
46 return Namespaces(c, func(ns string) error {
47 if !strings.HasPrefix(ns, p) {
48 if any {
49 return datastore.Stop
50 }
51 return nil
52 }
53
54 any = true
55 return cb(ns)
56 })
57 }
58
59 // NamespacesCollector exposes a NamespacesCallback function that aggregates
60 // resulting namespaces into the collector slice.
61 type NamespacesCollector []string
62
63 // Callback is a NamespacesCallback which adds each namespace to the collector.
64 func (c *NamespacesCollector) Callback(v string) error {
65 *c = append(*c, v)
66 return nil
67 }
OLDNEW
« no previous file with comments | « service/datastore/meta/eg_test.go ('k') | service/datastore/meta/namespaces_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698