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