-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelstore.go
174 lines (139 loc) · 4.06 KB
/
levelstore.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
package mqtt
import (
"git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git/packets"
"github.com/syndtr/goleveldb/leveldb"
"strconv"
"github.com/syndtr/goleveldb/leveldb/util"
"strings"
)
type LevelStore struct {
db *leveldb.DB
}
func newLevelStore() Store {
db, err := leveldb.OpenFile("store.db", nil)
// db, err := leveldb.Open(storage.NewMemStorage(), nil)
if err != nil {
log.Fatal(err)
}
store := &LevelStore{
db: db,
}
return store
}
func (this *LevelStore) StoreSubscription(filter, cid string, qos byte) {
key := "subscribe:" + cid + ":" + filter
this.db.Put([]byte(key), []byte{qos}, nil)
}
func (this *LevelStore) DeleteSubscription(filter, cid string) {
key := "subscribe:" + cid + ":" + filter
this.db.Delete([]byte(key), nil)
}
func (this *LevelStore) CleanSubscription(cid string) {
iter := this.db.NewIterator(util.BytesPrefix([]byte("subscribe:" + cid)), nil)
defer iter.Release()
b := new(leveldb.Batch)
for iter.Next() {
b.Delete(iter.Key())
}
this.db.Write(b, nil)
}
func (this *LevelStore) LookupSubscriptions(callback func(filter, cid string, qos byte)) {
iter := this.db.NewIterator(util.BytesPrefix([]byte("subscribe")), nil)
defer iter.Release()
for iter.Next() {
key := string(iter.Key())
value := iter.Value()
slice := strings.SplitN(key, ":", 3)
cid := slice[1]
filter := slice[2]
qos := value[0]
callback(filter, cid, qos)
}
}
func (this *LevelStore) StoreRetained(p *packets.PublishPacket) {
key := "retain:" + p.TopicName
if len(p.Payload) == 0 {
this.db.Delete([]byte(key), nil)
return
}
value := MarshalPacket(p)
this.db.Put([]byte(key), value, nil)
}
func (this *LevelStore) LookupRetained(callback func(*packets.PublishPacket)) {
iter := this.db.NewIterator(util.BytesPrefix([]byte("retain")), nil)
defer iter.Release()
for iter.Next() {
cp := UnmarshalPacket(iter.Value()).(*packets.PublishPacket)
callback(cp)
}
}
func (this *LevelStore) FindInboundPacket(cid string, mid uint16) packets.ControlPacket {
key := levelPacketKey(cid, mid, true)
if value, err := this.db.Get([]byte(key), nil); err == nil {
return UnmarshalPacket(value)
}
return nil
}
func (this *LevelStore) StoreInboundPacket(cid string, p packets.ControlPacket) error {
key := levelPacketKey(cid, p.Details().MessageID, true)
value := MarshalPacket(p)
return this.db.Put([]byte(key), value, nil)
}
func (this *LevelStore) StoreOutboundPacket(cid string, p packets.ControlPacket) error {
key := levelPacketKey(cid, p.Details().MessageID, false)
value := MarshalPacket(p)
return this.db.Put([]byte(key), value, nil)
}
func (this *LevelStore) StreamOfflinePackets(cid string, callback func(packets.ControlPacket)) {
iter := this.db.NewIterator(util.BytesPrefix([]byte("packets:out:" + cid)), nil)
defer iter.Release()
for iter.Next() {
cp := UnmarshalPacket(iter.Value())
callback(cp)
}
}
func (this *LevelStore) DeleteInboundPacket(cid string, mid uint16) {
this.db.Delete([]byte(levelPacketKey(cid, mid, true)), nil)
}
func (this *LevelStore) DeleteOutboundPacket(cid string, mid uint16) {
this.db.Delete([]byte(levelPacketKey(cid, mid, false)), nil)
}
func (this *LevelStore) CleanPackets(cid string) {
b := new(leveldb.Batch)
iter := this.db.NewIterator(util.BytesPrefix([]byte("packets:in:" + cid)), nil)
for iter.Next() {
b.Delete(iter.Key())
}
iter.Release()
iter = this.db.NewIterator(util.BytesPrefix([]byte("packets:out:" + cid)), nil)
for iter.Next() {
b.Delete(iter.Key())
}
iter.Release()
this.db.Write(b, nil)
}
func (this *LevelStore) InPacketsSize() int {
iter := this.db.NewIterator(util.BytesPrefix([]byte("packets:in")), nil)
count := 0
for iter.Next() {
count++
}
iter.Release()
return count
}
func (this *LevelStore) OutPacketsSize() int {
iter := this.db.NewIterator(util.BytesPrefix([]byte("packets:out")), nil)
count := 0
for iter.Next() {
count++
}
iter.Release()
return count
}
func levelPacketKey(cid string, mid uint16, in bool) string {
direction := "out"
if in {
direction = "in"
}
return "packets:" + direction + ":" + cid + ":" + strconv.Itoa(int(mid))
}