-
Notifications
You must be signed in to change notification settings - Fork 2
/
counter.go
66 lines (59 loc) · 1.5 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
package stored
import (
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
)
// Counter allow you to operate different counters inside your object
type Counter struct {
object *Object
fields []*Field
dir directory.DirectorySubspace
}
func counterNew(ob *ObjectBuilder, fields []*Field) *Counter {
ctr := Counter{
object: ob.object,
fields: fields,
}
ob.waitAll.Add(1)
go func() {
ob.waitInit.Wait()
dir, err := ob.object.dir.CreateOrOpen(ob.object.db, []string{"counter"}, nil)
if err != nil {
panic("Object " + ob.object.name + " could not add counter directory")
}
ctr.dir = dir
ob.mux.Lock()
ob.object.counters[fieldsKey(fields)] = &ctr
ob.mux.Unlock()
ob.waitAll.Done()
}()
return &ctr
}
func (c *Counter) increment(tr fdb.Transaction, input *Struct) {
t := input.getTuple(c.fields)
tr.Add(c.dir.Pack(t), countInc)
}
func (c *Counter) decrement(tr fdb.Transaction, input *Struct) {
t := input.getTuple(c.fields)
tr.Add(c.dir.Pack(t), countDec)
}
// Get will get counter data
func (c *Counter) Get(data interface{}) *Promise {
input := structAny(data)
p := c.object.promiseInt64()
p.doRead(func() Chain {
t := input.getTuple(c.fields)
incKey := c.dir.Pack(t)
bytes, err := p.readTr.Get(incKey).Get()
if err != nil {
return p.fail(err)
}
if len(bytes) == 0 {
// counter not created yet
return p.done(int64(0))
//return p.fail(ErrNotFound)
}
return p.done(ToInt64(bytes))
})
return p
}