OLD | NEW |
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 prod | 5 package prod |
6 | 6 |
7 import ( | 7 import ( |
8 » rds "github.com/luci/gae/service/rawdatastore" | 8 » ds "github.com/luci/gae/service/datastore" |
9 "golang.org/x/net/context" | 9 "golang.org/x/net/context" |
10 "google.golang.org/appengine" | 10 "google.golang.org/appengine" |
11 "google.golang.org/appengine/datastore" | 11 "google.golang.org/appengine/datastore" |
12 ) | 12 ) |
13 | 13 |
14 // useRDS adds a gae.RawDatastore implementation to context, accessible | 14 // useRDS adds a gae.RawDatastore implementation to context, accessible |
15 // by gae.GetDS(c) | 15 // by gae.GetDS(c) |
16 func useRDS(c context.Context) context.Context { | 16 func useRDS(c context.Context) context.Context { |
17 » return rds.SetFactory(c, func(ci context.Context) rds.Interface { | 17 » return ds.SetFactory(c, func(ci context.Context) ds.Interface { |
18 // TODO(riannucci): Track namespace in a better way | 18 // TODO(riannucci): Track namespace in a better way |
19 k := datastore.NewKey(ci, "kind", "", 1, nil) // get current nam
espace. | 19 k := datastore.NewKey(ci, "kind", "", 1, nil) // get current nam
espace. |
20 return rdsImpl{ci, k.Namespace()} | 20 return rdsImpl{ci, k.Namespace()} |
21 }) | 21 }) |
22 } | 22 } |
23 | 23 |
24 ////////// Query | 24 ////////// Query |
25 | 25 |
26 type queryImpl struct{ *datastore.Query } | 26 type queryImpl struct{ *datastore.Query } |
27 | 27 |
28 func (q queryImpl) Distinct() rds.Query { | 28 func (q queryImpl) Distinct() ds.Query { |
29 return queryImpl{q.Query.Distinct()} | 29 return queryImpl{q.Query.Distinct()} |
30 } | 30 } |
31 func (q queryImpl) End(c rds.Cursor) rds.Query { | 31 func (q queryImpl) End(c ds.Cursor) ds.Query { |
32 return queryImpl{q.Query.End(c.(datastore.Cursor))} | 32 return queryImpl{q.Query.End(c.(datastore.Cursor))} |
33 } | 33 } |
34 func (q queryImpl) EventualConsistency() rds.Query { | 34 func (q queryImpl) EventualConsistency() ds.Query { |
35 return queryImpl{q.Query.EventualConsistency()} | 35 return queryImpl{q.Query.EventualConsistency()} |
36 } | 36 } |
37 func (q queryImpl) KeysOnly() rds.Query { | 37 func (q queryImpl) KeysOnly() ds.Query { |
38 return queryImpl{q.Query.KeysOnly()} | 38 return queryImpl{q.Query.KeysOnly()} |
39 } | 39 } |
40 func (q queryImpl) Limit(limit int) rds.Query { | 40 func (q queryImpl) Limit(limit int) ds.Query { |
41 return queryImpl{q.Query.Limit(limit)} | 41 return queryImpl{q.Query.Limit(limit)} |
42 } | 42 } |
43 func (q queryImpl) Offset(offset int) rds.Query { | 43 func (q queryImpl) Offset(offset int) ds.Query { |
44 return queryImpl{q.Query.Offset(offset)} | 44 return queryImpl{q.Query.Offset(offset)} |
45 } | 45 } |
46 func (q queryImpl) Order(fieldName string) rds.Query { | 46 func (q queryImpl) Order(fieldName string) ds.Query { |
47 return queryImpl{q.Query.Order(fieldName)} | 47 return queryImpl{q.Query.Order(fieldName)} |
48 } | 48 } |
49 func (q queryImpl) Start(c rds.Cursor) rds.Query { | 49 func (q queryImpl) Start(c ds.Cursor) ds.Query { |
50 return queryImpl{q.Query.Start(c.(datastore.Cursor))} | 50 return queryImpl{q.Query.Start(c.(datastore.Cursor))} |
51 } | 51 } |
52 func (q queryImpl) Ancestor(ancestor rds.Key) rds.Query { | 52 func (q queryImpl) Ancestor(ancestor ds.Key) ds.Query { |
53 return queryImpl{q.Query.Ancestor(dsF2R(ancestor))} | 53 return queryImpl{q.Query.Ancestor(dsF2R(ancestor))} |
54 } | 54 } |
55 func (q queryImpl) Project(fieldNames ...string) rds.Query { | 55 func (q queryImpl) Project(fieldNames ...string) ds.Query { |
56 return queryImpl{q.Query.Project(fieldNames...)} | 56 return queryImpl{q.Query.Project(fieldNames...)} |
57 } | 57 } |
58 func (q queryImpl) Filter(filterStr string, value interface{}) rds.Query { | 58 func (q queryImpl) Filter(filterStr string, value interface{}) ds.Query { |
59 return queryImpl{q.Query.Filter(filterStr, value)} | 59 return queryImpl{q.Query.Filter(filterStr, value)} |
60 } | 60 } |
61 | 61 |
62 ////////// Datastore | 62 ////////// Datastore |
63 | 63 |
64 type rdsImpl struct { | 64 type rdsImpl struct { |
65 context.Context | 65 context.Context |
66 | 66 |
67 ns string | 67 ns string |
68 } | 68 } |
69 | 69 |
70 func (d rdsImpl) NewKey(kind, stringID string, intID int64, parent rds.Key) rds.
Key { | 70 func (d rdsImpl) NewKey(kind, stringID string, intID int64, parent ds.Key) ds.Ke
y { |
71 return dsR2F(datastore.NewKey(d, kind, stringID, intID, dsF2R(parent))) | 71 return dsR2F(datastore.NewKey(d, kind, stringID, intID, dsF2R(parent))) |
72 } | 72 } |
73 | 73 |
74 func (rdsImpl) DecodeKey(encoded string) (rds.Key, error) { | 74 func (rdsImpl) DecodeKey(encoded string) (ds.Key, error) { |
75 k, err := datastore.DecodeKey(encoded) | 75 k, err := datastore.DecodeKey(encoded) |
76 return dsR2F(k), err | 76 return dsR2F(k), err |
77 } | 77 } |
78 | 78 |
79 func idxCallbacker(err error, amt int, cb func(idx int, err error)) error { | 79 func idxCallbacker(err error, amt int, cb func(idx int, err error)) error { |
80 if err == nil { | 80 if err == nil { |
81 for i := 0; i < amt; i++ { | 81 for i := 0; i < amt; i++ { |
82 cb(i, nil) | 82 cb(i, nil) |
83 } | 83 } |
84 return nil | 84 return nil |
85 } | 85 } |
86 me, ok := err.(appengine.MultiError) | 86 me, ok := err.(appengine.MultiError) |
87 if ok { | 87 if ok { |
88 for i, err := range me { | 88 for i, err := range me { |
89 cb(i, err) | 89 cb(i, err) |
90 } | 90 } |
91 return nil | 91 return nil |
92 } | 92 } |
93 return err | 93 return err |
94 } | 94 } |
95 | 95 |
96 func (d rdsImpl) DeleteMulti(ks []rds.Key, cb rds.DeleteMultiCB) error { | 96 func (d rdsImpl) DeleteMulti(ks []ds.Key, cb ds.DeleteMultiCB) error { |
97 err := datastore.DeleteMulti(d, dsMF2R(ks)) | 97 err := datastore.DeleteMulti(d, dsMF2R(ks)) |
98 return idxCallbacker(err, len(ks), func(_ int, err error) { | 98 return idxCallbacker(err, len(ks), func(_ int, err error) { |
99 cb(err) | 99 cb(err) |
100 }) | 100 }) |
101 } | 101 } |
102 | 102 |
103 func (d rdsImpl) GetMulti(keys []rds.Key, cb rds.GetMultiCB) error { | 103 func (d rdsImpl) GetMulti(keys []ds.Key, cb ds.GetMultiCB) error { |
104 rkeys := dsMF2R(keys) | 104 rkeys := dsMF2R(keys) |
105 vals := make([]datastore.PropertyLoadSaver, len(keys)) | 105 vals := make([]datastore.PropertyLoadSaver, len(keys)) |
106 for i := range keys { | 106 for i := range keys { |
107 » » vals[i] = &typeFilter{rds.PropertyMap{}} | 107 » » vals[i] = &typeFilter{ds.PropertyMap{}} |
108 } | 108 } |
109 err := datastore.GetMulti(d, rkeys, vals) | 109 err := datastore.GetMulti(d, rkeys, vals) |
110 return idxCallbacker(err, len(keys), func(idx int, err error) { | 110 return idxCallbacker(err, len(keys), func(idx int, err error) { |
111 cb(vals[idx].(*typeFilter).pm, err) | 111 cb(vals[idx].(*typeFilter).pm, err) |
112 }) | 112 }) |
113 } | 113 } |
114 | 114 |
115 func (d rdsImpl) PutMulti(keys []rds.Key, vals []rds.PropertyLoadSaver, cb rds.P
utMultiCB) error { | 115 func (d rdsImpl) PutMulti(keys []ds.Key, vals []ds.PropertyLoadSaver, cb ds.PutM
ultiCB) error { |
116 rkeys := dsMF2R(keys) | 116 rkeys := dsMF2R(keys) |
117 rvals := make([]datastore.PropertyLoadSaver, len(vals)) | 117 rvals := make([]datastore.PropertyLoadSaver, len(vals)) |
118 for i, val := range vals { | 118 for i, val := range vals { |
119 » » rvals[i] = &typeFilter{val.(rds.PropertyMap)} | 119 » » rvals[i] = &typeFilter{val.(ds.PropertyMap)} |
120 } | 120 } |
121 rkeys, err := datastore.PutMulti(d, rkeys, vals) | 121 rkeys, err := datastore.PutMulti(d, rkeys, vals) |
122 return idxCallbacker(err, len(keys), func(idx int, err error) { | 122 return idxCallbacker(err, len(keys), func(idx int, err error) { |
123 » » k := rds.Key(nil) | 123 » » k := ds.Key(nil) |
124 if err == nil { | 124 if err == nil { |
125 k = dsR2F(rkeys[idx]) | 125 k = dsR2F(rkeys[idx]) |
126 } | 126 } |
127 cb(k, err) | 127 cb(k, err) |
128 }) | 128 }) |
129 } | 129 } |
130 | 130 |
131 func (d rdsImpl) NewQuery(kind string) rds.Query { | 131 func (d rdsImpl) NewQuery(kind string) ds.Query { |
132 return queryImpl{datastore.NewQuery(kind)} | 132 return queryImpl{datastore.NewQuery(kind)} |
133 } | 133 } |
134 | 134 |
135 func (d rdsImpl) Run(q rds.Query, cb rds.RunCB) error { | 135 func (d rdsImpl) Run(q ds.Query, cb ds.RunCB) error { |
136 tf := typeFilter{} | 136 tf := typeFilter{} |
137 t := q.(queryImpl).Query.Run(d) | 137 t := q.(queryImpl).Query.Run(d) |
138 » cfunc := func() (rds.Cursor, error) { | 138 » cfunc := func() (ds.Cursor, error) { |
139 return t.Cursor() | 139 return t.Cursor() |
140 } | 140 } |
141 for { | 141 for { |
142 k, err := t.Next(&tf) | 142 k, err := t.Next(&tf) |
143 if err == datastore.Done { | 143 if err == datastore.Done { |
144 return nil | 144 return nil |
145 } | 145 } |
146 if err != nil { | 146 if err != nil { |
147 return err | 147 return err |
148 } | 148 } |
149 if !cb(dsR2F(k), tf.pm, cfunc) { | 149 if !cb(dsR2F(k), tf.pm, cfunc) { |
150 return nil | 150 return nil |
151 } | 151 } |
152 } | 152 } |
153 } | 153 } |
154 | 154 |
155 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *rds.Tra
nsactionOptions) error { | 155 func (d rdsImpl) RunInTransaction(f func(c context.Context) error, opts *ds.Tran
sactionOptions) error { |
156 ropts := (*datastore.TransactionOptions)(opts) | 156 ropts := (*datastore.TransactionOptions)(opts) |
157 return datastore.RunInTransaction(d, f, ropts) | 157 return datastore.RunInTransaction(d, f, ropts) |
158 } | 158 } |
OLD | NEW |