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

Side by Side Diff: go/src/infra/gae/libs/wrapper/memory/datastore.go

Issue 1152383003: Simple memory testing for gae/wrapper (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@better_context_lite
Patch Set: add go-slab dependency Created 5 years, 6 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
(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 memory
6
7 import (
8 "errors"
9 "fmt"
10 "infra/gae/libs/wrapper"
11 "strings"
12
13 "github.com/mjibson/goon"
14 "golang.org/x/net/context"
15
16 "appengine/datastore"
17 "appengine_internal"
18 pb "appengine_internal/datastore"
19 )
20
21 //////////////////////////////////// public ////////////////////////////////////
22
23 // UseDS adds a wrapper.Datastore implementation to context, accessible
24 // by wrapper.GetDS(c)
25 func UseDS(c context.Context) context.Context {
26 return wrapper.SetDSFactory(c, func(ic context.Context) wrapper.Datastor e {
27 dsd := cur(ic).Get(memContextDSIdx)
28
29 switch x := dsd.(type) {
30 case *dataStoreData:
31 return &dsImpl{wrapper.DummyDS(), x, curGID(ic).namespac e, ic}
32 case *txnDataStoreData:
33 return &txnDsImpl{wrapper.DummyDS(), x, curGID(ic).names pace}
34 default:
35 panic(fmt.Errorf("DS: bad type: %v in context %v", dsd, ic))
36 }
37 })
38 }
39
40 //////////////////////////////////// dsImpl ////////////////////////////////////
41
42 // dsImpl exists solely to bind the current c to the datastore data.
43 type dsImpl struct {
44 wrapper.Datastore
45
46 data *dataStoreData
47 ns string
48 c context.Context
49 }
50
51 var (
52 _ = wrapper.Datastore((*dsImpl)(nil))
53 _ = wrapper.Testable((*dsImpl)(nil))
54 )
55
56 func (d *dsImpl) BreakFeatures(err error, features ...string) {
57 d.data.BreakFeatures(err, features...)
58 }
59 func (d *dsImpl) UnbreakFeatures(features ...string) {
60 d.data.UnbreakFeatures(features...)
61 }
62
63 func (d *dsImpl) Kind(src interface{}) string {
64 return kind(d.ns, d.KindNameResolver(), src)
65 }
66
67 func (d *dsImpl) KindNameResolver() goon.KindNameResolver {
68 return d.data.KindNameResolver()
69 }
70 func (d *dsImpl) SetKindNameResolver(knr goon.KindNameResolver) {
71 d.data.SetKindNameResolver(knr)
72 }
73
74 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent *datastore.Ke y) *datastore.Key {
75 return newKey(d.ns, kind, stringID, intID, parent)
76 }
77 func (d *dsImpl) NewKeyObj(src interface{}) *datastore.Key {
78 return newKeyObj(d.ns, d.KindNameResolver(), src)
79 }
80 func (d *dsImpl) NewKeyObjError(src interface{}) (*datastore.Key, error) {
81 return newKeyObjError(d.ns, d.KindNameResolver(), src)
82 }
83
84 func (d *dsImpl) Put(src interface{}) (*datastore.Key, error) {
85 if err := d.data.IsBroken(); err != nil {
86 return nil, err
87 }
88 return d.data.put(d.ns, src)
89 }
90
91 func (d *dsImpl) Get(dst interface{}) error {
92 if err := d.data.IsBroken(); err != nil {
93 return err
94 }
95 return d.data.get(d.ns, dst)
96 }
97
98 func (d *dsImpl) Delete(key *datastore.Key) error {
99 if err := d.data.IsBroken(); err != nil {
100 return err
101 }
102 return d.data.del(d.ns, key)
103 }
104
105 ////////////////////////////////// txnDsImpl ///////////////////////////////////
106
107 type txnDsImpl struct {
108 wrapper.Datastore
109
110 data *txnDataStoreData
111 ns string
112 }
113
114 var (
115 _ = wrapper.Datastore((*txnDsImpl)(nil))
116 _ = wrapper.Testable((*txnDsImpl)(nil))
117 )
118
119 func (d *txnDsImpl) BreakFeatures(err error, features ...string) {
120 d.data.BreakFeatures(err, features...)
121 }
122 func (d *txnDsImpl) UnbreakFeatures(features ...string) {
123 d.data.UnbreakFeatures(features...)
124 }
125
126 func (d *txnDsImpl) Kind(src interface{}) string {
127 return kind(d.ns, d.KindNameResolver(), src)
128 }
129
130 func (d *txnDsImpl) KindNameResolver() goon.KindNameResolver {
131 return d.data.KindNameResolver()
132 }
133 func (d *txnDsImpl) SetKindNameResolver(knr goon.KindNameResolver) {
134 d.data.SetKindNameResolver(knr)
135 }
136
137 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent *datastore .Key) *datastore.Key {
138 return newKey(d.ns, kind, stringID, intID, parent)
139 }
140 func (d *txnDsImpl) NewKeyObj(src interface{}) *datastore.Key {
141 return newKeyObj(d.ns, d.KindNameResolver(), src)
142 }
143 func (d *txnDsImpl) NewKeyObjError(src interface{}) (*datastore.Key, error) {
144 return newKeyObjError(d.ns, d.KindNameResolver(), src)
145 }
146
147 func (d *txnDsImpl) Put(src interface{}) (*datastore.Key, error) {
148 if err := d.data.IsBroken(); err != nil {
149 return nil, err
150 }
151 return d.data.put(d.ns, src)
152 }
153
154 func (d *txnDsImpl) Get(dst interface{}) error {
155 if err := d.data.IsBroken(); err != nil {
156 return err
157 }
158 return d.data.get(d.ns, dst)
159 }
160
161 func (d *txnDsImpl) Delete(key *datastore.Key) error {
162 if err := d.data.IsBroken(); err != nil {
163 return err
164 }
165 return d.data.del(d.ns, key)
166 }
167
168 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *datastore.Tra nsactionOptions) error {
169 return errors.New("datastore: nested transactions are not supported")
170 }
171
172 ////////////////////////////// private functions ///////////////////////////////
173
174 func newDSError(code pb.Error_ErrorCode, message ...string) *appengine_internal. APIError {
175 return &appengine_internal.APIError{
176 Detail: strings.Join(message, ""),
177 Service: "datastore_v3",
178 Code: int32(code),
179 }
180 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/libs/wrapper/memory/context.go ('k') | go/src/infra/gae/libs/wrapper/memory/datastore_data.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698