-
Notifications
You must be signed in to change notification settings - Fork 7
/
counter.go
77 lines (69 loc) · 1.82 KB
/
counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package riaken_core
import (
"errors"
"github.com/golang/protobuf/proto"
"github.com/riaken/riaken-core/rpb"
)
type Counter struct {
bucket *Bucket // bucket this object is associated with
key string // key this object is associated with
opts interface{}
}
func (c *Counter) reset() {
c.opts = nil
}
func (c *Counter) GetOpts() interface{} {
return c.opts
}
// Do allows opts to be passed to a method. This call should be chained.
func (c *Counter) Do(opts interface{}) *Counter {
c.opts = opts
return c
}
// Update a counter.
func (c *Counter) Update(count int64) (*rpb.RpbCounterUpdateResp, error) {
defer c.reset()
opts := new(rpb.RpbCounterUpdateReq)
if c.opts != nil {
if _, ok := c.opts.(*rpb.RpbCounterUpdateReq); !ok {
return nil, errors.New("Called Do() with wrong opts. Should be RpbCounterUpdateReq")
} else {
opts = c.opts.(*rpb.RpbCounterUpdateReq)
}
}
opts.Bucket = []byte(c.bucket.name)
opts.Key = []byte(c.key)
opts.Amount = proto.Int64(count)
in, err := proto.Marshal(opts)
if err != nil {
return nil, err
}
out, err := c.bucket.session.execute(Messages["CounterUpdateReq"], in)
if err != nil {
return nil, err
}
return out.(*rpb.RpbCounterUpdateResp), nil
}
// Get a counter.
func (c *Counter) Get() (*rpb.RpbCounterGetResp, error) {
defer c.reset()
opts := new(rpb.RpbCounterGetReq)
if c.opts != nil {
if _, ok := c.opts.(*rpb.RpbCounterGetReq); !ok {
return nil, errors.New("Called Do() with wrong opts. Should be RpbCounterGetReq")
} else {
opts = c.opts.(*rpb.RpbCounterGetReq)
}
}
opts.Bucket = []byte(c.bucket.name)
opts.Key = []byte(c.key)
in, err := proto.Marshal(opts)
if err != nil {
return nil, err
}
out, err := c.bucket.session.execute(Messages["CounterGetReq"], in)
if err != nil {
return nil, err
}
return out.(*rpb.RpbCounterGetResp), nil
}