OLD | NEW |
(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("DS") |
| 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 /////////////////////////////// Testable(dsImpl) /////////////////////////////// |
| 52 |
| 53 func (d *dsImpl) BreakFeatures(err error, features ...string) { |
| 54 d.data.BreakFeatures(err, features...) |
| 55 } |
| 56 func (d *dsImpl) UnbreakFeatures(features ...string) { |
| 57 d.data.UnbreakFeatures(features...) |
| 58 } |
| 59 |
| 60 /////////////////////////////// DSKinder(dsImpl) /////////////////////////////// |
| 61 |
| 62 func (d *dsImpl) Kind(src interface{}) string { |
| 63 return kind(d.ns, d.KindNameResolver(), src) |
| 64 } |
| 65 |
| 66 ///////////////////////////// DSKindSetter(dsImpl) ///////////////////////////// |
| 67 |
| 68 func (d *dsImpl) KindNameResolver() goon.KindNameResolver { |
| 69 return d.data.KindNameResolver() |
| 70 } |
| 71 func (d *dsImpl) SetKindNameResolver(knr goon.KindNameResolver) { |
| 72 d.data.SetKindNameResolver(knr) |
| 73 } |
| 74 |
| 75 ////////////////////////////// DSNewKeyer(dsImpl) ////////////////////////////// |
| 76 |
| 77 func (d *dsImpl) NewKey(kind, stringID string, intID int64, parent *datastore.Ke
y) *datastore.Key { |
| 78 return newKey(d.ns, kind, stringID, intID, parent) |
| 79 } |
| 80 func (d *dsImpl) NewKeyObj(src interface{}) *datastore.Key { |
| 81 return newKeyObj(d.ns, d.KindNameResolver(), src) |
| 82 } |
| 83 func (d *dsImpl) NewKeyObjError(src interface{}) (*datastore.Key, error) { |
| 84 return newKeyObjError(d.ns, d.KindNameResolver(), src) |
| 85 } |
| 86 |
| 87 ////////////////////// DSSingleReadWriter(*dataStoreData) ////////////////////// |
| 88 |
| 89 func (d *dsImpl) Put(src interface{}) (*datastore.Key, error) { |
| 90 if err := d.data.IsBroken(); err != nil { |
| 91 return nil, err |
| 92 } |
| 93 return d.data.put(d.ns, src) |
| 94 } |
| 95 |
| 96 func (d *dsImpl) Get(dst interface{}) error { |
| 97 if err := d.data.IsBroken(); err != nil { |
| 98 return err |
| 99 } |
| 100 return d.data.get(d.ns, dst) |
| 101 } |
| 102 |
| 103 func (d *dsImpl) Delete(key *datastore.Key) error { |
| 104 if err := d.data.IsBroken(); err != nil { |
| 105 return err |
| 106 } |
| 107 return d.data.del(d.ns, key) |
| 108 } |
| 109 |
| 110 ////////////////////////////////// txnDsImpl /////////////////////////////////// |
| 111 |
| 112 type txnDsImpl struct { |
| 113 wrapper.Datastore |
| 114 |
| 115 data *txnDataStoreData |
| 116 ns string |
| 117 } |
| 118 |
| 119 ///////////////////////////// Testable(txnDsImpl) ////////////////////////////// |
| 120 |
| 121 func (d *txnDsImpl) BreakFeatures(err error, features ...string) { |
| 122 d.data.BreakFeatures(err, features...) |
| 123 } |
| 124 func (d *txnDsImpl) UnbreakFeatures(features ...string) { |
| 125 d.data.UnbreakFeatures(features...) |
| 126 } |
| 127 |
| 128 ///////////////////////////// DSKinder(txnDsImpl) ////////////////////////////// |
| 129 |
| 130 func (d *txnDsImpl) Kind(src interface{}) string { |
| 131 return kind(d.ns, d.KindNameResolver(), src) |
| 132 } |
| 133 |
| 134 /////////////////////////// DSKindSetter(txnDsImpl) //////////////////////////// |
| 135 |
| 136 func (d *txnDsImpl) KindNameResolver() goon.KindNameResolver { |
| 137 return d.data.KindNameResolver() |
| 138 } |
| 139 func (d *txnDsImpl) SetKindNameResolver(knr goon.KindNameResolver) { |
| 140 d.data.SetKindNameResolver(knr) |
| 141 } |
| 142 |
| 143 //////////////////////////// DSNewKeyer(txnDsImpl) ///////////////////////////// |
| 144 |
| 145 func (d *txnDsImpl) NewKey(kind, stringID string, intID int64, parent *datastore
.Key) *datastore.Key { |
| 146 return newKey(d.ns, kind, stringID, intID, parent) |
| 147 } |
| 148 func (d *txnDsImpl) NewKeyObj(src interface{}) *datastore.Key { |
| 149 return newKeyObj(d.ns, d.KindNameResolver(), src) |
| 150 } |
| 151 func (d *txnDsImpl) NewKeyObjError(src interface{}) (*datastore.Key, error) { |
| 152 return newKeyObjError(d.ns, d.KindNameResolver(), src) |
| 153 } |
| 154 |
| 155 //////////////////////// DSSingleReadWriter(*txnDsImpl) //////////////////////// |
| 156 |
| 157 func (d *txnDsImpl) Put(src interface{}) (*datastore.Key, error) { |
| 158 if err := d.data.IsBroken(); err != nil { |
| 159 return nil, err |
| 160 } |
| 161 return d.data.put(d.ns, src) |
| 162 } |
| 163 |
| 164 func (d *txnDsImpl) Get(dst interface{}) error { |
| 165 if err := d.data.IsBroken(); err != nil { |
| 166 return err |
| 167 } |
| 168 return d.data.get(d.ns, dst) |
| 169 } |
| 170 |
| 171 func (d *txnDsImpl) Delete(key *datastore.Key) error { |
| 172 if err := d.data.IsBroken(); err != nil { |
| 173 return err |
| 174 } |
| 175 return d.data.del(d.ns, key) |
| 176 } |
| 177 |
| 178 ///////////////////////// DSTransactioner(*txnDsImpl) ////////////////////////// |
| 179 |
| 180 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *datastore.Tra
nsactionOptions) error { |
| 181 return errors.New("datastore: nested transactions are not supported") |
| 182 } |
| 183 |
| 184 ////////////////////////////// private functions /////////////////////////////// |
| 185 |
| 186 func newDSError(code pb.Error_ErrorCode, message ...string) *appengine_internal.
APIError { |
| 187 return &appengine_internal.APIError{ |
| 188 Detail: strings.Join(message, ""), |
| 189 Service: "datastore_v3", |
| 190 Code: int32(code), |
| 191 } |
| 192 } |
OLD | NEW |