forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_datastore.go
62 lines (51 loc) · 1.31 KB
/
namespace_datastore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sample
// [START using_namespaces_with_the_Datastore]
import (
"io"
"net/http"
"golang.org/x/net/context"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
type Counter struct {
Count int64
}
func incrementCounter(ctx context.Context, name string) error {
key := datastore.NewKey(ctx, "Counter", name, 0, nil)
return datastore.RunInTransaction(ctx, func(ctx context.Context) error {
var ctr Counter
err := datastore.Get(ctx, key, &ctr)
if err != nil && err != datastore.ErrNoSuchEntity {
return err
}
ctr.Count++
_, err = datastore.Put(ctx, key, &ctr)
return err
}, nil)
}
func someHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
err := incrementCounter(ctx, "SomeRequest")
if err != nil {
// ... handle err
}
// temporarily use a new namespace
{
ctx, err := appengine.Namespace(ctx, "-global-")
if err != nil {
// ... handle err
}
err = incrementCounter(ctx, "SomeRequest")
if err != nil {
// ... handle err
}
}
io.WriteString(w, "Updated counters.\n")
}
func init() {
http.HandleFunc("/some_url", someHandler)
}
// [END using_namespaces_with_the_Datastore]