rocksdb-native 2.6.5 → 2.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -5,23 +5,26 @@ const { ReadBatch, WriteBatch } = require('./lib/batch')
5
5
  const Iterator = require('./lib/iterator')
6
6
  const Snapshot = require('./lib/snapshot')
7
7
 
8
- const RocksDB = module.exports = class RocksDB extends ReadyResource {
9
- constructor (path, {
10
- // default options, https://github.com/facebook/rocksdb/wiki/Setup-Options-and-Basic-Tuning
11
- readOnly = false,
12
- createIfMissing = true,
13
- maxBackgroundJobs = 6,
14
- bytesPerSync = 1048576,
15
- // blob options, https://github.com/facebook/rocksdb/wiki/BlobDB
16
- enableBlobFiles = false,
17
- minBlobSize = 0,
18
- blobFileSize = 0,
19
- enableBlobGarbageCollection = true,
20
- // (block) table options
21
- tableBlockSize = 16384,
22
- tableCacheIndexAndFilterBlocks = true,
23
- tableFormatVersion = 4
24
- } = {}) {
8
+ module.exports = class RocksDB extends ReadyResource {
9
+ constructor(
10
+ path,
11
+ {
12
+ // default options, https://github.com/facebook/rocksdb/wiki/Setup-Options-and-Basic-Tuning
13
+ readOnly = false,
14
+ createIfMissing = true,
15
+ maxBackgroundJobs = 6,
16
+ bytesPerSync = 1048576,
17
+ // blob options, https://github.com/facebook/rocksdb/wiki/BlobDB
18
+ enableBlobFiles = false,
19
+ minBlobSize = 0,
20
+ blobFileSize = 0,
21
+ enableBlobGarbageCollection = true,
22
+ // (block) table options
23
+ tableBlockSize = 16384,
24
+ tableCacheIndexAndFilterBlocks = true,
25
+ tableFormatVersion = 4
26
+ } = {}
27
+ ) {
25
28
  super()
26
29
 
27
30
  this.path = path
@@ -49,7 +52,7 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
49
52
  this._handle = binding.init()
50
53
  }
51
54
 
52
- async _open () {
55
+ async _open() {
53
56
  const opts = new Uint32Array(16)
54
57
 
55
58
  opts[0] = this.readOnly ? 1 : 0
@@ -78,19 +81,17 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
78
81
 
79
82
  req.handle = binding.open(this._handle, this.path, opts, req, onopen)
80
83
 
81
- RocksDB._instances.add(this)
82
-
83
84
  await promise
84
85
 
85
86
  for (const snapshot of this._snapshots) snapshot._init()
86
87
 
87
- function onopen (err) {
88
+ function onopen(err) {
88
89
  if (err) req.reject(new Error(err))
89
90
  else req.resolve()
90
91
  }
91
92
  }
92
93
 
93
- async _close () {
94
+ async _close() {
94
95
  if (this._refs > 0) {
95
96
  await new Promise((resolve) => {
96
97
  this._resolvePreclose = resolve
@@ -108,17 +109,15 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
108
109
 
109
110
  req.handle = binding.close(this._handle, req, onclose)
110
111
 
111
- RocksDB._instances.delete(this)
112
-
113
112
  await promise
114
113
 
115
- function onclose (err) {
114
+ function onclose(err) {
116
115
  if (err) req.reject(new Error(err))
117
116
  else req.resolve()
118
117
  }
119
118
  }
120
119
 
121
- _incRef () {
120
+ _incRef() {
122
121
  if (this.closing !== null) {
123
122
  throw new Error('Database closed')
124
123
  }
@@ -126,7 +125,7 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
126
125
  this._refs++
127
126
  }
128
127
 
129
- _decRef () {
128
+ _decRef() {
130
129
  if (--this._refs !== 0) return
131
130
 
132
131
  if (this._resolveOnIdle !== null) {
@@ -143,76 +142,69 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
143
142
  }
144
143
  }
145
144
 
146
- isIdle () {
145
+ isIdle() {
147
146
  return this._refs === 0
148
147
  }
149
148
 
150
- idle () {
149
+ idle() {
151
150
  if (this.isIdle()) return Promise.resolve()
152
151
 
153
152
  if (!this._idlePromise) {
154
- this._idlePromise = new Promise(resolve => { this._resolveOnIdle = resolve })
153
+ this._idlePromise = new Promise((resolve) => {
154
+ this._resolveOnIdle = resolve
155
+ })
155
156
  }
156
157
 
157
158
  return this._idlePromise
158
159
  }
159
160
 
160
- snapshot (opts) {
161
+ snapshot(opts) {
161
162
  return new Snapshot(this, opts)
162
163
  }
163
164
 
164
- iterator (range, opts) {
165
+ iterator(range, opts) {
165
166
  return new Iterator(this, { ...range, ...opts })
166
167
  }
167
168
 
168
- async peek (range, opts) {
169
- for await (const value of this.iterator({ ...range, ...opts, limit: 1 })) { // eslint-disable-line no-unreachable-loop
169
+ async peek(range, opts) {
170
+ for await (const value of this.iterator({ ...range, ...opts, limit: 1 })) {
171
+ // eslint-disable-line no-unreachable-loop
170
172
  return value
171
173
  }
172
174
 
173
175
  return null
174
176
  }
175
177
 
176
- read (opts) {
178
+ read(opts) {
177
179
  return new ReadBatch(this, opts)
178
180
  }
179
181
 
180
- write (opts) {
182
+ write(opts) {
181
183
  return new WriteBatch(this, opts)
182
184
  }
183
185
 
184
- async get (key, opts) {
186
+ async get(key, opts) {
185
187
  const batch = this.read(opts)
186
188
  const value = batch.get(key)
187
189
  await batch.flush()
188
190
  return value
189
191
  }
190
192
 
191
- async put (key, value, opts) {
193
+ async put(key, value, opts) {
192
194
  const batch = this.write(opts)
193
195
  batch.put(key, value)
194
196
  await batch.flush()
195
197
  }
196
198
 
197
- async delete (key, opts) {
199
+ async delete(key, opts) {
198
200
  const batch = this.write(opts)
199
201
  batch.delete(key)
200
202
  await batch.flush()
201
203
  }
202
204
 
203
- async deleteRange (start, end, opts) {
205
+ async deleteRange(start, end, opts) {
204
206
  const batch = this.write(opts)
205
207
  batch.deleteRange(start, end)
206
208
  await batch.flush()
207
209
  }
208
-
209
- static _instances = new Set()
210
- }
211
-
212
- if (typeof Bare !== 'undefined') {
213
- Bare.on('exit', async () => {
214
- for (const db of RocksDB._instances) {
215
- await db.close()
216
- }
217
- })
218
210
  }
package/lib/batch.js CHANGED
@@ -6,12 +6,13 @@ const empty = b4a.alloc(0)
6
6
  const emptyPromise = Promise.resolve()
7
7
 
8
8
  class RocksDBBatch {
9
- constructor (db, opts = {}) {
9
+ constructor(db, opts = {}) {
10
10
  const {
11
11
  capacity = 8,
12
12
  encoding = null,
13
13
  keyEncoding = encoding,
14
- valueEncoding = encoding
14
+ valueEncoding = encoding,
15
+ autoDestroy = true
15
16
  } = opts
16
17
 
17
18
  db._incRef()
@@ -24,6 +25,7 @@ class RocksDBBatch {
24
25
 
25
26
  this._keyEncoding = keyEncoding
26
27
  this._valueEncoding = valueEncoding
28
+ this._autoDestroy = autoDestroy !== false
27
29
 
28
30
  this._enqueuePromise = this._enqueuePromise.bind(this)
29
31
 
@@ -36,19 +38,19 @@ class RocksDBBatch {
36
38
  if (db.opened === true) this.ready()
37
39
  }
38
40
 
39
- _onfinished () {
41
+ _onfinished() {
40
42
  const resolve = this._resolveRequest
41
43
 
42
44
  this._operations = []
43
45
  this._promises = []
44
46
  this._request = null
45
47
  this._resolveRequest = null
46
- this.destroy()
48
+ if (this._autoDestroy) this.destroy()
47
49
 
48
50
  if (resolve !== null) resolve()
49
51
  }
50
52
 
51
- _resize () {
53
+ _resize() {
52
54
  if (this._operations.length <= this._capacity) return false
53
55
 
54
56
  while (this._operations.length > this._capacity) {
@@ -58,7 +60,7 @@ class RocksDBBatch {
58
60
  return true
59
61
  }
60
62
 
61
- async ready () {
63
+ async ready() {
62
64
  if (this._handle !== null) return
63
65
 
64
66
  if (this._db.opened === false) await this._db.ready()
@@ -66,85 +68,93 @@ class RocksDBBatch {
66
68
  this._init()
67
69
  }
68
70
 
69
- destroy () {
71
+ destroy() {
70
72
  if (this._request) throw new Error('Batch already flushed')
71
73
  if (this._destroyed) return
72
74
  this._destroyed = true
73
75
  this._db._decRef()
74
76
  }
75
77
 
76
- flush () {
78
+ flush() {
79
+ if (this._destroyed) throw new Error('Batch is destroyed')
77
80
  if (this._request) throw new Error('Request already in progress')
78
81
 
79
- this._request = new Promise((resolve) => { this._resolveRequest = resolve })
82
+ this._request = new Promise((resolve) => {
83
+ this._resolveRequest = resolve
84
+ })
80
85
  this._flush()
81
86
 
82
87
  return this._request
83
88
  }
84
89
 
85
- tryFlush () {
90
+ tryFlush() {
86
91
  if (this._request) throw new Error('Request already in progress')
87
92
 
88
93
  this._request = emptyPromise
89
94
  this._flush()
90
95
  }
91
96
 
92
- async _flush () {
97
+ async _flush() {
93
98
  if (this._handle === null) await this.ready()
94
99
  }
95
100
 
96
- _enqueuePromise (resolve, reject) {
101
+ _enqueuePromise(resolve, reject) {
97
102
  this._promises.push({ resolve, reject })
98
103
  }
99
104
 
100
- _encodeKey (k) {
105
+ _encodeKey(k) {
101
106
  if (this._keyEncoding) return c.encode(this._keyEncoding, k)
102
107
  if (typeof k === 'string') return b4a.from(k)
103
108
  return k
104
109
  }
105
110
 
106
- _encodeValue (v) {
111
+ _encodeValue(v) {
107
112
  if (this._valueEncoding) return c.encode(this._valueEncoding, v)
108
113
  if (v === null) return empty
109
114
  if (typeof v === 'string') return b4a.from(v)
110
115
  return v
111
116
  }
112
117
 
113
- _decodeValue (b) {
118
+ _decodeValue(b) {
114
119
  if (this._valueEncoding) return c.decode(this._valueEncoding, b)
115
120
  return b
116
121
  }
117
122
  }
118
123
 
119
124
  exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
120
- constructor (db, opts = {}) {
121
- const {
122
- snapshot = null
123
- } = opts
125
+ constructor(db, opts = {}) {
126
+ const { snapshot = null } = opts
124
127
 
125
128
  super(db, opts)
126
129
 
127
130
  this._snapshot = snapshot
128
131
  }
129
132
 
130
- _init () {
133
+ _init() {
131
134
  this._handle = binding.readInit()
132
135
  this._buffer = binding.readBuffer(this._handle, this._capacity)
133
136
  }
134
137
 
135
- _resize () {
138
+ _resize() {
136
139
  if (super._resize() && this._handle !== null) {
137
140
  this._buffer = binding.readBuffer(this._handle, this._capacity)
138
141
  }
139
142
  }
140
143
 
141
- async _flush () {
144
+ async _flush() {
142
145
  await super._flush()
143
146
 
144
- binding.read(this._db._handle, this._handle, this._operations, this._snapshot ? this._snapshot._handle : null, this, this._onread)
147
+ binding.read(
148
+ this._db._handle,
149
+ this._handle,
150
+ this._operations,
151
+ this._snapshot ? this._snapshot._handle : null,
152
+ this,
153
+ this._onread
154
+ )
145
155
  }
146
156
 
147
- _onread (errs, values) {
157
+ _onread(errs, values) {
148
158
  for (let i = 0, n = this._promises.length; i < n; i++) {
149
159
  const promise = this._promises[i]
150
160
  if (promise === null) continue
@@ -152,13 +162,18 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
152
162
  const err = errs[i]
153
163
 
154
164
  if (err) promise.reject(new Error(err))
155
- else promise.resolve(values[i].byteLength === 0 ? null : this._decodeValue(b4a.from(values[i])))
165
+ else
166
+ promise.resolve(
167
+ values[i].byteLength === 0
168
+ ? null
169
+ : this._decodeValue(b4a.from(values[i]))
170
+ )
156
171
  }
157
172
 
158
173
  this._onfinished()
159
174
  }
160
175
 
161
- get (key) {
176
+ get(key) {
162
177
  if (this._request) throw new Error('Request already in progress')
163
178
 
164
179
  const promise = new Promise(this._enqueuePromise)
@@ -171,24 +186,30 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
171
186
  }
172
187
 
173
188
  exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
174
- _init () {
189
+ _init() {
175
190
  this._handle = binding.writeInit()
176
191
  this._buffer = binding.writeBuffer(this._handle, this._capacity)
177
192
  }
178
193
 
179
- _resize () {
194
+ _resize() {
180
195
  if (super._resize() && this._handle !== null) {
181
196
  this._buffer = binding.writeBuffer(this._handle, this._capacity)
182
197
  }
183
198
  }
184
199
 
185
- async _flush () {
200
+ async _flush() {
186
201
  await super._flush()
187
202
 
188
- binding.write(this._db._handle, this._handle, this._operations, this, this._onwrite)
203
+ binding.write(
204
+ this._db._handle,
205
+ this._handle,
206
+ this._operations,
207
+ this,
208
+ this._onwrite
209
+ )
189
210
  }
190
211
 
191
- _onwrite (err) {
212
+ _onwrite(err) {
192
213
  for (let i = 0, n = this._promises.length; i < n; i++) {
193
214
  const promise = this._promises[i]
194
215
  if (promise === null) continue
@@ -200,26 +221,30 @@ exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
200
221
  this._onfinished()
201
222
  }
202
223
 
203
- put (key, value) {
224
+ put(key, value) {
204
225
  if (this._request) throw new Error('Request already in progress')
205
226
 
206
227
  const promise = new Promise(this._enqueuePromise)
207
228
 
208
- this._operations.push(new RocksDBPut(this._encodeKey(key), this._encodeValue(value)))
229
+ this._operations.push(
230
+ new RocksDBPut(this._encodeKey(key), this._encodeValue(value))
231
+ )
209
232
  this._resize()
210
233
 
211
234
  return promise
212
235
  }
213
236
 
214
- tryPut (key, value) {
237
+ tryPut(key, value) {
215
238
  if (this._request) throw new Error('Request already in progress')
216
239
 
217
- this._operations.push(new RocksDBPut(this._encodeKey(key), this._encodeValue(value)))
240
+ this._operations.push(
241
+ new RocksDBPut(this._encodeKey(key), this._encodeValue(value))
242
+ )
218
243
  this._promises.push(null)
219
244
  this._resize()
220
245
  }
221
246
 
222
- delete (key) {
247
+ delete(key) {
223
248
  if (this._request) throw new Error('Request already in progress')
224
249
 
225
250
  const promise = new Promise(this._enqueuePromise)
@@ -230,7 +255,7 @@ exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
230
255
  return promise
231
256
  }
232
257
 
233
- tryDelete (key) {
258
+ tryDelete(key) {
234
259
  if (this._request) throw new Error('Request already in progress')
235
260
 
236
261
  this._operations.push(new RocksDBDelete(this._encodeKey(key)))
@@ -238,64 +263,68 @@ exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
238
263
  this._resize()
239
264
  }
240
265
 
241
- deleteRange (start, end) {
266
+ deleteRange(start, end) {
242
267
  if (this._request) throw new Error('Request already in progress')
243
268
 
244
269
  const promise = new Promise(this._enqueuePromise)
245
270
 
246
- this._operations.push(new RocksDBDeleteRange(this._encodeKey(start), this._encodeKey(end)))
271
+ this._operations.push(
272
+ new RocksDBDeleteRange(this._encodeKey(start), this._encodeKey(end))
273
+ )
247
274
  this._resize()
248
275
 
249
276
  return promise
250
277
  }
251
278
 
252
- tryDeleteRange (start, end) {
279
+ tryDeleteRange(start, end) {
253
280
  if (this._request) throw new Error('Request already in progress')
254
281
 
255
- this._operations.push(new RocksDBDeleteRange(this._encodeKey(start), this._encodeKey(end)))
282
+ this._operations.push(
283
+ new RocksDBDeleteRange(this._encodeKey(start), this._encodeKey(end))
284
+ )
256
285
  this._promises.push(null)
257
286
  this._resize()
258
287
  }
259
288
  }
260
289
 
261
290
  class RocksDBGet {
262
- constructor (key) {
291
+ constructor(key) {
263
292
  this.key = key
264
293
  }
265
294
 
266
- get type () {
295
+ get type() {
267
296
  return binding.GET
268
297
  }
269
298
  }
270
299
 
271
300
  class RocksDBPut {
272
- constructor (key, value) {
301
+ constructor(key, value) {
273
302
  this.key = key
274
303
  this.value = value
275
304
  }
276
305
 
277
- get type () {
306
+ get type() {
278
307
  return binding.PUT
279
308
  }
280
309
  }
281
310
 
282
311
  class RocksDBDelete {
283
- constructor (key) {
312
+ constructor(key) {
284
313
  this.key = key
285
314
  }
286
315
 
287
- get type () {
316
+ get type() {
288
317
  return binding.DELETE
289
318
  }
290
319
  }
291
320
 
292
321
  class RocksDBDeleteRange {
293
- constructor (start, end) {
322
+ constructor(start, end) {
294
323
  this.start = start
295
324
  this.end = end
296
325
  }
297
326
 
298
- get type () {
327
+ get type() {
299
328
  return binding.DELETE_RANGE
300
329
  }
301
330
  }
package/lib/iterator.js CHANGED
@@ -6,7 +6,7 @@ const binding = require('../binding')
6
6
  const empty = b4a.alloc(0)
7
7
 
8
8
  module.exports = class RocksDBIterator extends Readable {
9
- constructor (db, opts = {}) {
9
+ constructor(db, opts = {}) {
10
10
  const {
11
11
  gt = null,
12
12
  gte = null,
@@ -50,13 +50,13 @@ module.exports = class RocksDBIterator extends Readable {
50
50
  if (db.opened === true) this.ready()
51
51
  }
52
52
 
53
- _onopen (err) {
53
+ _onopen(err) {
54
54
  const cb = this._pendingOpen
55
55
  this._pendingOpen = null
56
56
  cb(err)
57
57
  }
58
58
 
59
- _onread (err, keys, values) {
59
+ _onread(err, keys, values) {
60
60
  const cb = this._pendingRead
61
61
  this._pendingRead = null
62
62
  if (err) return cb(err)
@@ -66,7 +66,10 @@ module.exports = class RocksDBIterator extends Readable {
66
66
  this._limit -= n
67
67
 
68
68
  for (let i = 0; i < n; i++) {
69
- this.push({ key: this._decodeKey(b4a.from(keys[i])), value: this._decodeValue(b4a.from(values[i])) })
69
+ this.push({
70
+ key: this._decodeKey(b4a.from(keys[i])),
71
+ value: this._decodeValue(b4a.from(values[i]))
72
+ })
70
73
  }
71
74
 
72
75
  if (n < this._capacity) this.push(null)
@@ -74,20 +77,20 @@ module.exports = class RocksDBIterator extends Readable {
74
77
  cb(null)
75
78
  }
76
79
 
77
- _onclose (err) {
80
+ _onclose(err) {
78
81
  const cb = this._pendingDestroy
79
82
  this._pendingDestroy = null
80
83
  this._db._decRef()
81
84
  cb(err)
82
85
  }
83
86
 
84
- _resize () {
87
+ _resize() {
85
88
  if (this._handle !== null) {
86
89
  this._buffer = binding.iteratorBuffer(this._handle, this._capacity)
87
90
  }
88
91
  }
89
92
 
90
- async ready () {
93
+ async ready() {
91
94
  if (this._handle !== null) return
92
95
 
93
96
  if (this._db.opened === false) await this._db.ready()
@@ -95,26 +98,40 @@ module.exports = class RocksDBIterator extends Readable {
95
98
  this._init()
96
99
  }
97
100
 
98
- _init () {
99
- this._handle = binding.iteratorInit(this, this._onopen, this._onclose, this._onread)
101
+ _init() {
102
+ this._handle = binding.iteratorInit(
103
+ this,
104
+ this._onopen,
105
+ this._onclose,
106
+ this._onread
107
+ )
100
108
  this._buffer = binding.iteratorBuffer(this._handle, this._capacity)
101
109
  }
102
110
 
103
- async _open (cb) {
111
+ async _open(cb) {
104
112
  await this.ready()
105
113
 
106
114
  this._pendingOpen = cb
107
115
 
108
- binding.iteratorOpen(this._db._handle, this._handle, this._gt, this._gte, this._lt, this._lte, this._reverse, this._snapshot ? this._snapshot._handle : null)
116
+ binding.iteratorOpen(
117
+ this._db._handle,
118
+ this._handle,
119
+ this._gt,
120
+ this._gte,
121
+ this._lt,
122
+ this._lte,
123
+ this._reverse,
124
+ this._snapshot ? this._snapshot._handle : null
125
+ )
109
126
  }
110
127
 
111
- _read (cb) {
128
+ _read(cb) {
112
129
  this._pendingRead = cb
113
130
 
114
131
  binding.iteratorRead(this._handle, Math.min(this._capacity, this._limit))
115
132
  }
116
133
 
117
- async _destroy (cb) {
134
+ async _destroy(cb) {
118
135
  await this.ready()
119
136
 
120
137
  this._pendingDestroy = cb
@@ -122,18 +139,18 @@ module.exports = class RocksDBIterator extends Readable {
122
139
  binding.iteratorClose(this._handle)
123
140
  }
124
141
 
125
- _encodeKey (k) {
142
+ _encodeKey(k) {
126
143
  if (this._keyEncoding) return c.encode(this._keyEncoding, k)
127
144
  if (typeof k === 'string') return b4a.from(k)
128
145
  return k
129
146
  }
130
147
 
131
- _decodeKey (b) {
148
+ _decodeKey(b) {
132
149
  if (this._keyEncoding) return c.decode(this._keyEncoding, b)
133
150
  return b
134
151
  }
135
152
 
136
- _decodeValue (b) {
153
+ _decodeValue(b) {
137
154
  if (this._valueEncoding) return c.decode(this._valueEncoding, b)
138
155
  return b
139
156
  }
package/lib/snapshot.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const binding = require('../binding')
2
2
 
3
3
  module.exports = class RocksDBSnapshot {
4
- constructor (db) {
4
+ constructor(db) {
5
5
  this._db = db
6
6
  this._db._snapshots.add(this)
7
7
 
@@ -10,11 +10,11 @@ module.exports = class RocksDBSnapshot {
10
10
  if (db.opened === true) this._init()
11
11
  }
12
12
 
13
- _init () {
13
+ _init() {
14
14
  this._handle = binding.snapshotCreate(this._db._handle)
15
15
  }
16
16
 
17
- destroy () {
17
+ destroy() {
18
18
  this._db._snapshots.delete(this)
19
19
 
20
20
  if (this._handle === null) return