-
Notifications
You must be signed in to change notification settings - Fork 2
/
promise.go
201 lines (177 loc) · 4.56 KB
/
promise.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package stored
import (
"errors"
"github.com/apple/foundationdb/bindings/go/src/fdb"
)
// Chain is the recursive functions chain
type Chain func() Chain
// Promise is an basic promise object
type Promise struct {
db *fdb.Database
readTr fdb.ReadTransaction
tr fdb.Transaction
chain Chain
after func() PromiseAny
err error
readOnly bool
resp interface{}
confirmed bool
}
// PromiseAny describes any type of promise
type PromiseAny interface {
self() *Promise
}
func (p *Promise) do(chain Chain) {
p.chain = chain
}
func (p *Promise) doRead(chain Chain) {
p.readOnly = true
p.chain = chain
}
func (p *Promise) fail(err error) Chain {
p.err = err
return nil
}
func (p *Promise) done(resp interface{}) Chain {
p.resp = resp
return nil
}
func (p *Promise) ok() Chain {
return nil
}
func (p *Promise) getValueField(o *Object, field *Field, bytes []byte) *Value {
raw := valueRaw{}
//data := map[string]interface{}{}
raw[field.Name] = bytes
val := Value{
object: o,
raw: raw,
}
return &val
}
func (p *Promise) execute() (interface{}, error) {
next := p.chain()
for next != nil {
next = next()
}
if p.after != nil {
after := p.after().self()
after.tr = p.tr
after.readTr = p.readTr
after.execute()
}
return p.resp, p.err
}
func (p *Promise) clear() {
p.err = nil
p.resp = nil
}
func (p *Promise) transact() (resp interface{}, err error) {
if p.readTr != nil {
p.clear()
resp, err = p.execute()
p.confirmed = true
return
}
if p.readOnly {
resp, err = p.db.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) {
p.clear() // since transaction could be repeated - should clear everything
p.readTr = tr.Snapshot()
return p.execute()
})
p.confirmed = true
return
}
resp, err = p.db.Transact(func(tr fdb.Transaction) (ret interface{}, err error) {
p.clear() // clear tmp data in case if transaction resended
p.tr = tr
p.readTr = tr
return p.execute()
})
p.confirmed = true
return
}
// Err will execute the promise and return error
func (p *Promise) Err() error {
_, err := p.transact()
return err
}
// Bool return bool value if promise contins true or false
func (p *Promise) Bool() (bool, error) {
data, err := p.transact()
var res bool
if err != nil {
return res, err
}
if data == nil {
panic("promise does not contain any value, use Scan")
}
res, ok := data.(bool)
if !ok {
return res, errors.New("promise value is not bool")
}
return res, nil
}
// Int64 return Int64 value if promise contin int64 data
func (p *Promise) Int64() (int64, error) {
data, err := p.transact()
var res int64
if err != nil {
return res, err
}
if data == nil {
panic("promise does not contain any value, use Scan")
}
res, ok := data.(int64)
if !ok {
return res, errors.New("promise value is not int64")
}
return res, nil
}
// After will perform an additional promise right after current one will be finised
// This works in transactions as well as in standalone promises, child promise will
// be executed in same transaction as parent
func (p *Promise) After(do func() PromiseAny) *Promise {
p.after = do
return p
}
// Submit will submit promise to transaction. Once promise is completed callback will be called. If callback function will return error – the transaction will be cancelled.
func (p *Promise) Submit(t *Transaction, onDone func(err error) error) {
t.tasks = append(t.tasks, transactionTask{
promise: p,
check: false,
onDone: onDone,
})
}
// Check will perform promise in parallel with other promises whithin transaction
// without returning the result. But if Promise will return error full transaction
// will be cancelled and error will be returned
func (p *Promise) Check(t *Transaction) {
t.tasks = append(t.tasks, transactionTask{
promise: p,
check: true,
})
}
// Try will perform promise in parallel with other promises within transaction
// without returning the result. But if Promise will return error, transaction will
// be performed as everythig is ok, error will be ignored
func (p *Promise) Try(t *Transaction) {
t.tasks = append(t.tasks, transactionTask{
promise: p,
check: false,
})
}
// Do will attach promise to transaction, so promise will be called within passed transaction
// Promise should be inside an transaction callback, because transaction could be resent
func (p *Promise) Do(t *Transaction) *Promise {
if !t.started {
panic("transaction not started, could not use in Promise")
}
p.tr = t.tr
p.readTr = t.readTr
return p
}
// Promise will return promise from any type of promise
func (p *Promise) self() *Promise {
return p
}