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

Side by Side Diff: go/src/infra/gae/epservice/example/service_cas.go

Issue 1227183003: Change RawDatastore to do less reflection. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@move_dummy
Patch Set: fix No/ShouldIndex Created 5 years, 5 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
OLDNEW
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 example 5 package example
6 6
7 import ( 7 import (
8 "golang.org/x/net/context" 8 "golang.org/x/net/context"
9
9 "infra/gae/libs/gae" 10 "infra/gae/libs/gae"
11 "infra/gae/libs/gae/helper"
10 "infra/gae/libs/gae/prod" 12 "infra/gae/libs/gae/prod"
11 13
12 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" 14 "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
13 ) 15 )
14 16
15 // CASReq is the input for the CAS RPC 17 // CASReq is the input for the CAS RPC
16 type CASReq struct { 18 type CASReq struct {
17 Name string `endpoints:"required"` 19 Name string `endpoints:"required"`
18 20
19 OldVal int64 `json:",string"` 21 OldVal int64 `json:",string"`
20 NewVal int64 `json:",string"` 22 NewVal int64 `json:",string"`
21 } 23 }
22 24
23 // CAS does an atomic compare-and-swap on a counter. 25 // CAS does an atomic compare-and-swap on a counter.
24 func (Example) CAS(c context.Context, r *CASReq) (err error) { 26 func (Example) CAS(c context.Context, r *CASReq) (err error) {
25 success := false 27 success := false
26 c = prod.Use(c) 28 c = prod.Use(c)
27 err = gae.GetRDS(c).RunInTransaction(func(c context.Context) error { 29 err = gae.GetRDS(c).RunInTransaction(func(c context.Context) error {
28 rds := gae.GetRDS(c) 30 rds := gae.GetRDS(c)
29 key := rds.NewKey("Counter", r.Name, 0, nil) 31 key := rds.NewKey("Counter", r.Name, 0, nil)
30 ctr := &Counter{} 32 ctr := &Counter{}
31 » » if err := rds.Get(key, ctr); err != nil { 33 » » pls := helper.GetPLS(ctr)
34 » » if err := rds.Get(key, pls); err != nil {
32 return err 35 return err
33 } 36 }
34 if ctr.Val == r.OldVal { 37 if ctr.Val == r.OldVal {
35 success = true 38 success = true
36 ctr.Val = r.NewVal 39 ctr.Val = r.NewVal
37 » » » _, err := rds.Put(key, ctr) 40 » » » _, err := rds.Put(key, pls)
38 return err 41 return err
39 } 42 }
40 success = false 43 success = false
41 return nil 44 return nil
42 }, nil) 45 }, nil)
43 if err == nil && !success { 46 if err == nil && !success {
44 err = endpoints.ConflictError 47 err = endpoints.ConflictError
45 } 48 }
46 return 49 return
47 } 50 }
48 51
49 func init() { 52 func init() {
50 mi["CAS"] = &endpoints.MethodInfo{ 53 mi["CAS"] = &endpoints.MethodInfo{
51 Path: "counter/{Name}/cas", 54 Path: "counter/{Name}/cas",
52 Desc: "Compare and swap a counter value", 55 Desc: "Compare and swap a counter value",
53 } 56 }
54 } 57 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/epservice/example/service_add.go ('k') | go/src/infra/gae/epservice/example/service_currentvalue.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698