-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mqtt-level-store.js
185 lines (165 loc) · 4.58 KB
/
mqtt-level-store.js
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
'use strict'
var level = require('level')
var sublevel = require('level-sublevel')
var msgpack = require('msgpack5')
function Store (options) {
if (!(this instanceof Store)) {
return new Store(options)
}
if (!options.level) {
throw new Error('missing level')
}
this._level = options.level
this._levelOpts = {
valueEncoding: msgpack()
}
this._sameDateCount = 0
this._prevDate = null
}
const zeroPadding = (function () {
// width and padding calculates only once
const width = parseInt(Number.MAX_SAFE_INTEGER).toString(10).length
const padding = new Array(width + 1).join('0')
return function (num) {
return (padding + num).slice(-width)
}
})()
Store.prototype.put = function (packet, cb) {
var date = new Date().toISOString()
if (this._prevDate === date) {
++this._sameDateCount
} else {
this._sameDateCount = 0
this._prevDate = date
}
date += zeroPadding(this._sameDateCount)
var that = this
this._level.get(
'packets~' + packet.messageId,
this._levelOpts,
function (err, _date) {
if (err) {
if (!err.notFound) return cb(err)
var cmd = [
{ type: 'put', key: 'packets~' + packet.messageId, value: date },
{ type: 'put', key: 'packet-by-date~' + date + '~' + packet.messageId, value: packet }
]
that._level.batch(cmd, that._levelOpts, cb)
} else {
that._level.get(
'packet-by-date~' + _date + '~' + packet.messageId,
that._levelOpts,
function (err, _packet) {
if (err) return cb(err)
that._level.put('packet-by-date~' + _date + '~' + packet.messageId, packet, that._levelOpts, cb)
})
}
})
return this
}
Store.prototype.get = function (packet, cb) {
var that = this
this._level.get(
'packets~' + packet.messageId,
this._levelOpts,
function (err, date) {
if (err) return cb(err)
that._level.get(
'packet-by-date~' + date + '~' + packet.messageId,
that._levelOpts,
cb)
})
return this
}
Store.prototype.del = function (packet, cb) {
var that = this
this._level.get(
'packets~' + packet.messageId,
this._levelOpts,
function (err, date) {
if (err) return cb(err)
that._level.get(
'packet-by-date~' + date + '~' + packet.messageId,
that._levelOpts,
function (err, _packet) {
if (err) return cb(err)
var cmd = [
{ type: 'del', key: 'packets~' + packet.messageId },
{ type: 'del', key: 'packet-by-date~' + date + '~' + packet.messageId }
]
that._level.batch(
cmd,
function (err) {
if (err) return cb(err)
cb(null, _packet)
})
})
})
return this
}
Store.prototype.createStream = function () {
var opts = this._levelOpts
opts.keys = false
opts.lt = 'packet-by-date~\xff'
opts.gt = 'packet-by-date~'
return this._level.createReadStream(opts)
}
Store.prototype.close = function (cb) {
this._level.close(cb)
return this
}
function Manager (path, options) {
if (!(this instanceof Manager)) {
return new Manager(path, options)
}
if (typeof path === 'object') {
options = path
path = null
}
if (options && options.level) {
this._level = options.level
} else {
this._level = level(path, options)
}
this._sublevel = sublevel(this._level)
this.incoming = new Store({ level: this._sublevel.sublevel('incoming') })
this.outgoing = new Store({ level: this._sublevel.sublevel('outgoing') })
}
Manager.single = Store
Manager.prototype.close = function (done) {
var incomingCloseCalled = false
var outgoingCloseCalled = false
var subLevelCloseCalled = false
var levelCloseCalled = false
var errors = {}
function tryAllClosed () {
if (incomingCloseCalled && outgoingCloseCalled && subLevelCloseCalled && levelCloseCalled) {
if (Object.keys(errors).length > 0) {
done(new Error(JSON.stringify(errors)))
} else {
done()
}
}
}
this.incoming.close(function (err) {
incomingCloseCalled = true
if (err) errors['incoming'] = err.message
tryAllClosed()
})
this.outgoing.close(function (err) {
outgoingCloseCalled = true
if (err) errors['outgoing'] = err.message
tryAllClosed()
})
this._sublevel.close(function (err) {
subLevelCloseCalled = true
if (err) errors['sublevel'] = err.message
tryAllClosed()
})
this._level.close(function (err) {
levelCloseCalled = true
if (err) errors['level'] = err.message
tryAllClosed()
})
}
module.exports = Manager