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 prod | |
6 | |
7 import ( | |
8 "github.com/luci/gae" | |
9 "golang.org/x/net/context" | |
10 "google.golang.org/appengine/datastore" | |
11 ) | |
12 | |
13 // useRDS adds a gae.RawDatastore implementation to context, accessible | |
14 // by gae.GetDS(c) | |
15 func useRDS(c context.Context) context.Context { | |
16 return gae.SetRDSFactory(c, func(ci context.Context) gae.RawDatastore { | |
17 return rdsImpl{ci} | |
18 }) | |
19 } | |
20 | |
21 ////////// Query | |
22 | |
23 type queryImpl struct{ *datastore.Query } | |
24 | |
25 func (q queryImpl) Distinct() gae.DSQuery { | |
26 return queryImpl{q.Query.Distinct()} | |
27 } | |
28 func (q queryImpl) End(c gae.DSCursor) gae.DSQuery { | |
29 return queryImpl{q.Query.End(c.(datastore.Cursor))} | |
30 } | |
31 func (q queryImpl) EventualConsistency() gae.DSQuery { | |
32 return queryImpl{q.Query.EventualConsistency()} | |
33 } | |
34 func (q queryImpl) KeysOnly() gae.DSQuery { | |
35 return queryImpl{q.Query.KeysOnly()} | |
36 } | |
37 func (q queryImpl) Limit(limit int) gae.DSQuery { | |
38 return queryImpl{q.Query.Limit(limit)} | |
39 } | |
40 func (q queryImpl) Offset(offset int) gae.DSQuery { | |
41 return queryImpl{q.Query.Offset(offset)} | |
42 } | |
43 func (q queryImpl) Order(fieldName string) gae.DSQuery { | |
44 return queryImpl{q.Query.Order(fieldName)} | |
45 } | |
46 func (q queryImpl) Start(c gae.DSCursor) gae.DSQuery { | |
47 return queryImpl{q.Query.Start(c.(datastore.Cursor))} | |
48 } | |
49 func (q queryImpl) Ancestor(ancestor gae.DSKey) gae.DSQuery { | |
50 return queryImpl{q.Query.Ancestor(dsF2R(ancestor))} | |
51 } | |
52 func (q queryImpl) Project(fieldNames ...string) gae.DSQuery { | |
53 return queryImpl{q.Query.Project(fieldNames...)} | |
54 } | |
55 func (q queryImpl) Filter(filterStr string, value interface{}) gae.DSQuery { | |
56 return queryImpl{q.Query.Filter(filterStr, value)} | |
57 } | |
58 | |
59 ////////// Iterator | |
60 | |
61 type iteratorImpl struct{ *datastore.Iterator } | |
62 | |
63 var _ gae.RDSIterator = iteratorImpl{} | |
64 | |
65 func (i iteratorImpl) Cursor() (gae.DSCursor, error) { | |
66 return i.Iterator.Cursor() | |
67 } | |
68 | |
69 func (i iteratorImpl) Next(pls gae.DSPropertyLoadSaver) (gae.DSKey, error) { | |
70 return dsR2FErr(i.Iterator.Next(&typeFilter{pls})) | |
71 } | |
72 | |
73 ////////// Datastore | |
74 | |
75 type rdsImpl struct{ context.Context } | |
76 | |
77 // NewKeyer | |
78 func (d rdsImpl) NewKey(kind, stringID string, intID int64, parent gae.DSKey) ga
e.DSKey { | |
79 return dsR2F(datastore.NewKey(d, kind, stringID, intID, dsF2R(parent))) | |
80 } | |
81 | |
82 func (rdsImpl) DecodeKey(encoded string) (gae.DSKey, error) { | |
83 return dsR2FErr(datastore.DecodeKey(encoded)) | |
84 } | |
85 | |
86 func multiWrap(os []gae.DSPropertyLoadSaver) []datastore.PropertyLoadSaver { | |
87 ret := make([]datastore.PropertyLoadSaver, len(os)) | |
88 for i, pls := range os { | |
89 ret[i] = &typeFilter{pls} | |
90 } | |
91 return ret | |
92 } | |
93 | |
94 func (d rdsImpl) Delete(k gae.DSKey) error { return datastore.Delete(d, dsF2R(k)
) } | |
95 func (d rdsImpl) Get(key gae.DSKey, dst gae.DSPropertyLoadSaver) error { | |
96 return datastore.Get(d, dsF2R(key), &typeFilter{dst}) | |
97 } | |
98 func (d rdsImpl) Put(key gae.DSKey, src gae.DSPropertyLoadSaver) (gae.DSKey, err
or) { | |
99 return dsR2FErr(datastore.Put(d, dsF2R(key), &typeFilter{src})) | |
100 } | |
101 | |
102 func (d rdsImpl) DeleteMulti(ks []gae.DSKey) error { | |
103 return gae.FixError(datastore.DeleteMulti(d, dsMF2R(ks))) | |
104 } | |
105 | |
106 func (d rdsImpl) GetMulti(ks []gae.DSKey, plss []gae.DSPropertyLoadSaver) error
{ | |
107 return gae.FixError(datastore.GetMulti(d, dsMF2R(ks), multiWrap(plss))) | |
108 } | |
109 func (d rdsImpl) PutMulti(key []gae.DSKey, plss []gae.DSPropertyLoadSaver) ([]ga
e.DSKey, error) { | |
110 ks, err := datastore.PutMulti(d, dsMF2R(key), multiWrap(plss)) | |
111 return dsMR2F(ks), gae.FixError(err) | |
112 } | |
113 | |
114 // DSQueryer | |
115 func (d rdsImpl) NewQuery(kind string) gae.DSQuery { | |
116 return queryImpl{datastore.NewQuery(kind)} | |
117 } | |
118 func (d rdsImpl) Run(q gae.DSQuery) gae.RDSIterator { | |
119 return iteratorImpl{q.(queryImpl).Query.Run(d)} | |
120 } | |
121 func (d rdsImpl) Count(q gae.DSQuery) (int, error) { | |
122 return q.(queryImpl).Query.Count(d) | |
123 } | |
124 func (d rdsImpl) GetAll(q gae.DSQuery, dst *[]gae.DSPropertyMap) ([]gae.DSKey, e
rror) { | |
125 fakeDst := []datastore.PropertyList(nil) | |
126 ks, err := q.(queryImpl).GetAll(d, &fakeDst) | |
127 if err != nil { | |
128 return nil, err | |
129 } | |
130 *dst = make([]gae.DSPropertyMap, len(fakeDst)) | |
131 for i, pl := range fakeDst { | |
132 (*dst)[i] = gae.DSPropertyMap{} | |
133 if err := (&typeFilter{(*dst)[i]}).Load(pl); err != nil { | |
134 return nil, err | |
135 } | |
136 } | |
137 return dsMR2F(ks), err | |
138 } | |
139 | |
140 // Transactioner | |
141 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *gae.DST
ransactionOptions) error { | |
142 ropts := (*datastore.TransactionOptions)(opts) | |
143 return datastore.RunInTransaction(d, f, ropts) | |
144 } | |
OLD | NEW |