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

Side by Side Diff: impl/memory/datastore.go

Issue 1379673002: Add a NewDatastore helper function. (Closed) Base URL: https://github.com/luci/gae.git@fix_queries
Patch Set: Created 5 years, 2 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
« no previous file with comments | « no previous file | impl/memory/datastore_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 memory 5 package memory
6 6
7 import ( 7 import (
8 "errors" 8 "errors"
9 "fmt" 9 "fmt"
10 10
11 "golang.org/x/net/context" 11 "golang.org/x/net/context"
12 12
13 ds "github.com/luci/gae/service/datastore" 13 ds "github.com/luci/gae/service/datastore"
14 "github.com/luci/gae/service/info"
14 ) 15 )
15 16
16 //////////////////////////////////// public //////////////////////////////////// 17 //////////////////////////////////// public ////////////////////////////////////
17 18
18 // useRDS adds a gae.Datastore implementation to context, accessible 19 // useRDS adds a gae.Datastore implementation to context, accessible
19 // by gae.GetDS(c) 20 // by gae.GetDS(c)
20 func useRDS(c context.Context) context.Context { 21 func useRDS(c context.Context) context.Context {
21 return ds.SetRawFactory(c, func(ic context.Context) ds.RawInterface { 22 return ds.SetRawFactory(c, func(ic context.Context) ds.RawInterface {
22 dsd := cur(ic).Get(memContextDSIdx) 23 dsd := cur(ic).Get(memContextDSIdx)
23 24
24 ns := curGID(ic).namespace 25 ns := curGID(ic).namespace
25 if x, ok := dsd.(*dataStoreData); ok { 26 if x, ok := dsd.(*dataStoreData); ok {
26 return &dsImpl{x, ns, ic} 27 return &dsImpl{x, ns, ic}
27 } 28 }
28 return &txnDsImpl{dsd.(*txnDataStoreData), ns} 29 return &txnDsImpl{dsd.(*txnDataStoreData), ns}
29 }) 30 })
30 } 31 }
31 32
33 // NewDatastore creates a new standalone memory implementation of the datastore,
34 // suitable for embedding for doing in-memory data organization.
35 //
36 // It's configured by default with the following settings:
37 // * AutoIndex(true)
38 // * Consistent(true)
39 // * DisableSpecialEntities(true)
40 //
41 // These settings can of course be changed by using the Testable() interface.
42 func NewDatastore(aid, ns string) (ds.Interface, error) {
43 ctx := UseWithAppID(context.Background(), aid)
44 ctx, err := info.Get(ctx).Namespace(ns)
45 if err != nil {
46 return nil, err
47 }
48 ret := ds.Get(ctx)
49 t := ret.Testable()
Vadim Sh. 2015/09/29 20:08:36 name 'Testable' is unfortunate, considering it is
iannucci 2015/09/29 20:09:45 Yeah that occurred to me. AlterSemantics?
Vadim Sh. 2015/09/29 20:12:03 "Tweaks"? "Hacks"? :)
50 t.AutoIndex(true)
51 t.Consistent(true)
52 t.DisableSpecialEntities(true)
53 return ret, nil
54 }
55
32 //////////////////////////////////// dsImpl //////////////////////////////////// 56 //////////////////////////////////// dsImpl ////////////////////////////////////
33 57
34 // dsImpl exists solely to bind the current c to the datastore data. 58 // dsImpl exists solely to bind the current c to the datastore data.
35 type dsImpl struct { 59 type dsImpl struct {
36 data *dataStoreData 60 data *dataStoreData
37 ns string 61 ns string
38 c context.Context 62 c context.Context
39 } 63 }
40 64
41 var _ ds.RawInterface = (*dsImpl)(nil) 65 var _ ds.RawInterface = (*dsImpl)(nil)
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 return countQuery(fq, d.data.parent.aid, d.ns, true, d.data.snap, d.data .snap) 205 return countQuery(fq, d.data.parent.aid, d.ns, true, d.data.snap, d.data .snap)
182 } 206 }
183 207
184 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *ds.Transactio nOptions) error { 208 func (*txnDsImpl) RunInTransaction(func(c context.Context) error, *ds.Transactio nOptions) error {
185 return errors.New("datastore: nested transactions are not supported") 209 return errors.New("datastore: nested transactions are not supported")
186 } 210 }
187 211
188 func (*txnDsImpl) Testable() ds.Testable { 212 func (*txnDsImpl) Testable() ds.Testable {
189 return nil 213 return nil
190 } 214 }
OLDNEW
« no previous file with comments | « no previous file | impl/memory/datastore_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698