| 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 count | 5 package count |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "golang.org/x/net/context" | 8 "golang.org/x/net/context" |
| 9 | 9 |
| 10 ds "github.com/luci/gae/service/datastore" | 10 ds "github.com/luci/gae/service/datastore" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 // DSCounter is the counter object for the datastore service. | 13 // DSCounter is the counter object for the datastore service. |
| 14 type DSCounter struct { | 14 type DSCounter struct { |
| 15 AllocateIDs Entry | 15 AllocateIDs Entry |
| 16 DecodeCursor Entry | 16 DecodeCursor Entry |
| 17 RunInTransaction Entry | 17 RunInTransaction Entry |
| 18 Run Entry | 18 Run Entry |
| 19 Count Entry |
| 19 DeleteMulti Entry | 20 DeleteMulti Entry |
| 20 GetMulti Entry | 21 GetMulti Entry |
| 21 PutMulti Entry | 22 PutMulti Entry |
| 22 } | 23 } |
| 23 | 24 |
| 24 type dsCounter struct { | 25 type dsCounter struct { |
| 25 c *DSCounter | 26 c *DSCounter |
| 26 | 27 |
| 27 ds ds.RawInterface | 28 ds ds.RawInterface |
| 28 } | 29 } |
| 29 | 30 |
| 30 var _ ds.RawInterface = (*dsCounter)(nil) | 31 var _ ds.RawInterface = (*dsCounter)(nil) |
| 31 | 32 |
| 32 func (r *dsCounter) AllocateIDs(incomplete *ds.Key, n int) (int64, error) { | 33 func (r *dsCounter) AllocateIDs(incomplete *ds.Key, n int) (int64, error) { |
| 33 start, err := r.ds.AllocateIDs(incomplete, n) | 34 start, err := r.ds.AllocateIDs(incomplete, n) |
| 34 return start, r.c.AllocateIDs.up(err) | 35 return start, r.c.AllocateIDs.up(err) |
| 35 } | 36 } |
| 36 | 37 |
| 37 func (r *dsCounter) DecodeCursor(s string) (ds.Cursor, error) { | 38 func (r *dsCounter) DecodeCursor(s string) (ds.Cursor, error) { |
| 38 cursor, err := r.ds.DecodeCursor(s) | 39 cursor, err := r.ds.DecodeCursor(s) |
| 39 return cursor, r.c.DecodeCursor.up(err) | 40 return cursor, r.c.DecodeCursor.up(err) |
| 40 } | 41 } |
| 41 | 42 |
| 42 func (r *dsCounter) Run(q *ds.FinalizedQuery, cb ds.RawRunCB) error { | 43 func (r *dsCounter) Run(q *ds.FinalizedQuery, cb ds.RawRunCB) error { |
| 43 return r.c.Run.up(r.ds.Run(q, cb)) | 44 return r.c.Run.up(r.ds.Run(q, cb)) |
| 44 } | 45 } |
| 45 | 46 |
| 47 func (r *dsCounter) Count(q *ds.FinalizedQuery) (int64, error) { |
| 48 count, err := r.ds.Count(q) |
| 49 return count, r.c.Count.up(err) |
| 50 } |
| 51 |
| 46 func (r *dsCounter) RunInTransaction(f func(context.Context) error, opts *ds.Tra
nsactionOptions) error { | 52 func (r *dsCounter) RunInTransaction(f func(context.Context) error, opts *ds.Tra
nsactionOptions) error { |
| 47 return r.c.RunInTransaction.up(r.ds.RunInTransaction(f, opts)) | 53 return r.c.RunInTransaction.up(r.ds.RunInTransaction(f, opts)) |
| 48 } | 54 } |
| 49 | 55 |
| 50 func (r *dsCounter) DeleteMulti(keys []*ds.Key, cb ds.DeleteMultiCB) error { | 56 func (r *dsCounter) DeleteMulti(keys []*ds.Key, cb ds.DeleteMultiCB) error { |
| 51 return r.c.DeleteMulti.up(r.ds.DeleteMulti(keys, cb)) | 57 return r.c.DeleteMulti.up(r.ds.DeleteMulti(keys, cb)) |
| 52 } | 58 } |
| 53 | 59 |
| 54 func (r *dsCounter) GetMulti(keys []*ds.Key, meta ds.MultiMetaGetter, cb ds.GetM
ultiCB) error { | 60 func (r *dsCounter) GetMulti(keys []*ds.Key, meta ds.MultiMetaGetter, cb ds.GetM
ultiCB) error { |
| 55 return r.c.GetMulti.up(r.ds.GetMulti(keys, meta, cb)) | 61 return r.c.GetMulti.up(r.ds.GetMulti(keys, meta, cb)) |
| 56 } | 62 } |
| 57 | 63 |
| 58 func (r *dsCounter) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMul
tiCB) error { | 64 func (r *dsCounter) PutMulti(keys []*ds.Key, vals []ds.PropertyMap, cb ds.PutMul
tiCB) error { |
| 59 return r.c.PutMulti.up(r.ds.PutMulti(keys, vals, cb)) | 65 return r.c.PutMulti.up(r.ds.PutMulti(keys, vals, cb)) |
| 60 } | 66 } |
| 61 | 67 |
| 62 func (r *dsCounter) Testable() ds.Testable { | 68 func (r *dsCounter) Testable() ds.Testable { |
| 63 return r.ds.Testable() | 69 return r.ds.Testable() |
| 64 } | 70 } |
| 65 | 71 |
| 66 // FilterRDS installs a counter datastore filter in the context. | 72 // FilterRDS installs a counter datastore filter in the context. |
| 67 func FilterRDS(c context.Context) (context.Context, *DSCounter) { | 73 func FilterRDS(c context.Context) (context.Context, *DSCounter) { |
| 68 state := &DSCounter{} | 74 state := &DSCounter{} |
| 69 return ds.AddRawFilters(c, func(ic context.Context, ds ds.RawInterface)
ds.RawInterface { | 75 return ds.AddRawFilters(c, func(ic context.Context, ds ds.RawInterface)
ds.RawInterface { |
| 70 return &dsCounter{state, ds} | 76 return &dsCounter{state, ds} |
| 71 }), state | 77 }), state |
| 72 } | 78 } |
| OLD | NEW |