forked from zond/unbolted
/
db.go
216 lines (200 loc) · 5.06 KB
/
db.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package unbolted
import (
"bytes"
"fmt"
"reflect"
"sync"
"github.com/boltdb/bolt"
)
/*
DB wraps a bolt database in an object API.
*/
type DB struct {
db *bolt.DB
lock sync.RWMutex
subscriptions map[string]map[string]*Subscription
afterTransaction []func(*DB) error
}
func (self *DB) String() string {
return fmt.Sprintf("&bobject.DB@%p{db:%v,subscriptions:%v}", self, self.db, self.subscriptions)
}
/*
MustDB returns a DB or panics.
*/
func MustDB(path string) (result *DB) {
result, err := NewDB(path)
if err != nil {
panic(err)
}
return
}
/*
NewDB returns a DB using (or reusing) path as persistent file.
*/
func NewDB(path string) (result *DB, err error) {
result = &DB{
subscriptions: make(map[string]map[string]*Subscription),
}
if result.db, err = bolt.Open(path, 0600, nil); err != nil {
return
}
return
}
/*
Close closes the database persistence file.
*/
func (self *DB) Close() (err error) {
return self.db.Close()
}
/*
AfterTransaction will append f to a list of functions that will run after the current transaction finishes.
If run outside a transaction it will wait until the next transaction finishes.
If f returns an error, the transaction call (Update or View) will return an error, but mutating transactions will still commit!
*/
func (self *DB) AfterTransaction(f func(*DB) error) (err error) {
self.lock.Lock()
defer self.lock.Unlock()
self.afterTransaction = append(self.afterTransaction, f)
return
}
func (self *DB) runAfterTransaction() (err error) {
self.lock.Lock()
defer self.lock.Unlock()
for len(self.afterTransaction) > 0 {
next := self.afterTransaction[0]
self.afterTransaction = self.afterTransaction[1:]
if err = func() (err error) {
self.lock.Unlock()
defer self.lock.Lock()
if err = next(self); err != nil {
return
}
return
}(); err != nil {
return
}
}
self.afterTransaction = nil
return
}
/*
View opens a read only transaction.
*/
func (self *DB) View(f func(tx *TX) error) (err error) {
if err = self.db.View(func(boltTx *bolt.Tx) error {
return f(&TX{
tx: boltTx,
db: self,
})
}); err != nil {
return
}
if err = self.runAfterTransaction(); err != nil {
return
}
return
}
/*
Update opens a read/write transaction.
*/
func (self *DB) Update(f func(tx *TX) error) (err error) {
if err = self.db.Update(func(boltTx *bolt.Tx) error {
return f(&TX{
tx: boltTx,
db: self,
})
}); err != nil {
return
}
if err = self.runAfterTransaction(); err != nil {
return
}
return
}
/*
Unsubscribe will remove the named subscription from this DB.
*/
func (self *DB) Unsubscribe(name string) {
self.lock.Lock()
defer self.lock.Unlock()
for _, typeSubs := range self.subscriptions {
delete(typeSubs, name)
}
}
/*
Subscription will return a subscription with name, watching changes to objects with the same type and id as obj.
It will watch for operations matching the ops, and send the events to the subscriber.
*/
func (self *DB) Subscription(name string, obj interface{}, ops Operation, subscriber Subscriber) (result *Subscription, err error) {
var wantedValue reflect.Value
var wantedId reflect.Value
if wantedValue, wantedId, err = identify(obj); err != nil {
return
}
wantedType := wantedValue.Type()
wantedBytes := make([]byte, len(wantedId.Bytes()))
copy(wantedBytes, wantedId.Bytes())
result = &Subscription{
name: name,
db: self,
matcher: func(tx *TX, typ reflect.Type, value reflect.Value) (result bool, err error) {
if typ.Name() != wantedType.Name() {
return
}
if bytes.Compare(value.FieldByName(idField).Bytes(), wantedBytes) != 0 {
return
}
result = true
return
},
subscriber: subscriber,
ops: ops,
typ: wantedType,
}
return
}
/*
EmitUpdate will artificially emit an update on obj, that will cause all subscriptions for update operations on matching objects get an update event.
*/
func (self *DB) EmitUpdate(obj interface{}) (err error) {
value := reflect.ValueOf(obj).Elem()
return self.emit(reflect.TypeOf(value.Interface()), &value, &value)
}
func (self *DB) emit(typ reflect.Type, oldValue, newValue *reflect.Value) (err error) {
if oldValue != nil && newValue != nil {
if chain := newValue.Addr().MethodByName("Updated"); chain.IsValid() {
if err = callErr(chain, []reflect.Value{reflect.ValueOf(self), oldValue.Addr()}); err != nil {
return
}
}
} else if newValue != nil {
if chain := newValue.Addr().MethodByName("Created"); chain.IsValid() {
if err = callErr(chain, []reflect.Value{reflect.ValueOf(self)}); err != nil {
return
}
}
} else if oldValue != nil {
if chain := oldValue.Addr().MethodByName("Deleted"); chain.IsValid() {
if err = callErr(chain, []reflect.Value{reflect.ValueOf(self)}); err != nil {
return
}
}
}
self.lock.RLock()
defer self.lock.RUnlock()
for _, subscription := range self.subscriptions[typ.Name()] {
go subscription.handle(typ, oldValue, newValue)
}
return
}
/*
Query returns a new query for DB.
*/
func (self *DB) Query() *Query {
return &Query{
db: self,
run: func(f func(*TX) error) error {
return self.View(f)
},
}
}