/
dbobject.go
81 lines (61 loc) · 1.21 KB
/
dbobject.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
package database
import (
"sync"
"github.com/Cristofori/kmud/types"
"github.com/Cristofori/kmud/utils"
"gopkg.in/mgo.v2/bson"
)
type DbObject struct {
Id types.Id `bson:"_id"`
mutex sync.RWMutex
destroyed bool
}
func (self *DbObject) SetId(id types.Id) {
self.Id = id
}
func (self *DbObject) GetId() types.Id {
return self.Id
}
func (self *DbObject) ReadLock() {
self.mutex.RLock()
}
func (self *DbObject) ReadUnlock() {
self.mutex.RUnlock()
}
func (self *DbObject) WriteLock() {
self.mutex.Lock()
}
func (self *DbObject) writeLock(worker func()) {
self.WriteLock()
defer self.WriteUnlock()
defer self.modified()
worker()
}
func (self *DbObject) WriteUnlock() {
self.mutex.Unlock()
}
func (self *DbObject) Destroy() {
self.WriteLock()
defer self.WriteUnlock()
self.destroyed = true
}
func (self *DbObject) IsDestroyed() bool {
self.ReadLock()
defer self.ReadUnlock()
return self.destroyed
}
func (self *DbObject) modified() {
modifiedObjectChannel <- self.Id
}
func (self *DbObject) syncModified() {
commitObject(self.Id)
}
func idSetToList(set utils.Set) []types.Id {
ids := make([]types.Id, len(set))
i := 0
for id := range set {
ids[i] = bson.ObjectIdHex(id)
i++
}
return ids
}