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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
Index: service/datastore/meta/namespaces.go
diff --git a/service/datastore/meta/namespaces.go b/service/datastore/meta/namespaces.go
new file mode 100644
index 0000000000000000000000000000000000000000..369e646b302b2e1e791e162e7737952dee6d8f47
--- /dev/null
+++ b/service/datastore/meta/namespaces.go
@@ -0,0 +1,36 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package meta
+
+import (
+ "github.com/luci/gae/service/datastore"
+ "github.com/luci/luci-go/common/logging"
+ "golang.org/x/net/context"
+)
+
+// Namespaces returns a list of all of the namespaces in the datastore.
+//
+// This is done by issuing a datastore query for kind "__namespace__". The
+// resulting keys will have IDs for the namespaces, namely:
+// - The empty namespace will have integer ID 1. We ignore this.
+// - Other namespaces will have string IDs.
+func Namespaces(c context.Context) ([]string, error) {
dnj 2016/04/23 02:17:10 (This is also verbatim transplant from luci-go) w/
iannucci 2016/04/23 19:15:21 I'm not comfortable making the default API be GetA
dnj 2016/04/24 00:08:37 The problem with a query is that you still have to
+ q := datastore.NewQuery("__namespace__").KeysOnly(true)
+
+ // Query our datastore for the full set of namespaces.
+ var namespaceKeys []*datastore.Key
+ if err := datastore.Get(c).GetAll(q, &namespaceKeys); err != nil {
iannucci 2016/04/23 19:15:21 Note: this query will ONLY run in the default (emp
dnj 2016/04/24 00:08:37 So on live Datastore, I can execute a GQL query fo
+ logging.WithError(err).Errorf(c, "Failed to execute namespace query.")
+ return nil, err
+ }
+
+ namespaces := make([]string, 0, len(namespaceKeys))
+ for _, nk := range namespaceKeys {
+ if ns := nk.StringID(); ns != "" {
+ namespaces = append(namespaces, ns)
+ }
+ }
+ return namespaces, nil
+}

Powered by Google App Engine
This is Rietveld 408576698