rocksdb-native 3.5.2 → 3.5.4

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
@@ -81,8 +81,9 @@ class RocksDB {
81
81
  if (this._index !== -1) this._state.removeSession(this)
82
82
 
83
83
  if (force) {
84
- while (this._state.sessions.length > 0)
84
+ while (this._state.sessions.length > 0) {
85
85
  await this._state.sessions[this._state.sessions.length - 1].close()
86
+ }
86
87
  }
87
88
 
88
89
  return this.isRoot() ? this._state.close() : Promise.resolve()
@@ -101,11 +102,11 @@ class RocksDB {
101
102
  }
102
103
 
103
104
  isIdle() {
104
- return this._state.activity.isIdle()
105
+ return this._state.handles.isIdle()
105
106
  }
106
107
 
107
108
  idle() {
108
- return this._state.activity.idle()
109
+ return this._state.handles.idle()
109
110
  }
110
111
 
111
112
  iterator(range, opts) {
@@ -165,26 +166,14 @@ class RocksDB {
165
166
  await batch.flush()
166
167
  }
167
168
 
168
- // used by iterators/batches to ensure no gc/close when active
169
-
170
169
  _ref() {
171
170
  if (this._snapshot) this._snapshot.ref()
172
- this._state.activity.inc()
173
- }
174
-
175
- _refBatch() {
176
- this._ref()
177
- this._state.activeBatches.inc()
171
+ this._state.handles.inc()
178
172
  }
179
173
 
180
174
  _unref() {
181
175
  if (this._snapshot) this._snapshot.unref()
182
- this._state.activity.dec()
183
- }
184
-
185
- _unrefBatch() {
186
- this._unref()
187
- this._state.activeBatches.dec()
176
+ this._state.handles.dec()
188
177
  }
189
178
  }
190
179
 
package/lib/batch.js CHANGED
@@ -8,7 +8,7 @@ class RocksDBBatch {
8
8
  constructor(db, opts = {}) {
9
9
  const { capacity = 8, autoDestroy = false } = opts
10
10
 
11
- db._refBatch()
11
+ db._ref()
12
12
 
13
13
  this._db = db
14
14
  this._destroyed = false
@@ -32,7 +32,7 @@ class RocksDBBatch {
32
32
  _reuse(db, opts = {}) {
33
33
  const { autoDestroy = false } = opts
34
34
 
35
- db._refBatch()
35
+ db._ref()
36
36
 
37
37
  this._db = db
38
38
  this._destroyed = false
@@ -48,6 +48,7 @@ class RocksDBBatch {
48
48
  this._request = null
49
49
  this._resolve = null
50
50
  this._reject = null
51
+ this._db._state.io.dec()
51
52
 
52
53
  if (this._autoDestroy === true) this.destroy()
53
54
 
@@ -81,7 +82,7 @@ class RocksDBBatch {
81
82
 
82
83
  if (this._promises.length) this._abort()
83
84
 
84
- this._db._unrefBatch()
85
+ this._db._unref()
85
86
  this._onfree()
86
87
  }
87
88
 
@@ -121,15 +122,19 @@ class RocksDBBatch {
121
122
  }
122
123
 
123
124
  async _flush() {
124
- const state = this._db._state
125
-
126
125
  if (this._handle === null) await this.ready()
127
- if (state.resumed !== null) await state.resumed.promise
128
126
 
129
- if ((state._suspending || state._suspended) && !this._destroyed) {
130
- this._destroyed = true
131
- this._abort()
132
- this._db._unrefBatch()
127
+ this._db._state.io.inc()
128
+
129
+ if (this._db._state.resumed !== null) {
130
+ const resumed = await this._db._state.resumed.promise
131
+
132
+ if (!resumed && !this._destroyed) {
133
+ this._destroyed = true
134
+ this._abort()
135
+ this._db._state.io.dec()
136
+ this._db._unref()
137
+ }
133
138
  }
134
139
  }
135
140
 
@@ -170,16 +175,22 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
170
175
 
171
176
  async _flush() {
172
177
  await super._flush()
178
+
173
179
  if (this._destroyed) return
174
180
 
175
- binding.read(
176
- this._db._state._handle,
177
- this._handle,
178
- this._operations,
179
- this._db._snapshot ? this._db._snapshot._handle : null,
180
- this,
181
- this._onread
182
- )
181
+ try {
182
+ binding.read(
183
+ this._db._state._handle,
184
+ this._handle,
185
+ this._operations,
186
+ this._db._snapshot ? this._db._snapshot._handle : null,
187
+ this,
188
+ this._onread
189
+ )
190
+ } catch (err) {
191
+ this._db._state.io.dec()
192
+ throw err
193
+ }
183
194
  }
184
195
 
185
196
  _onread(errs, values) {
@@ -238,15 +249,21 @@ exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
238
249
 
239
250
  async _flush() {
240
251
  await super._flush()
252
+
241
253
  if (this._destroyed) return
242
254
 
243
- binding.write(
244
- this._db._state._handle,
245
- this._handle,
246
- this._operations,
247
- this,
248
- this._onwrite
249
- )
255
+ try {
256
+ binding.write(
257
+ this._db._state._handle,
258
+ this._handle,
259
+ this._operations,
260
+ this,
261
+ this._onwrite
262
+ )
263
+ } catch (err) {
264
+ this._db._state.io.dec()
265
+ throw err
266
+ }
250
267
  }
251
268
 
252
269
  _onwrite(err) {
package/lib/iterator.js CHANGED
@@ -30,6 +30,7 @@ module.exports = class RocksDBIterator extends Readable {
30
30
  this._reverse = reverse
31
31
  this._limit = limit < 0 ? Infinity : limit
32
32
  this._capacity = capacity
33
+ this._opened = false
33
34
 
34
35
  this._pendingOpen = null
35
36
  this._pendingRead = null
@@ -44,12 +45,15 @@ module.exports = class RocksDBIterator extends Readable {
44
45
  _onopen(err) {
45
46
  const cb = this._pendingOpen
46
47
  this._pendingOpen = null
48
+ this._opened = true
49
+ this._db._state.io.dec()
47
50
  cb(err)
48
51
  }
49
52
 
50
53
  _onread(err, keys, values) {
51
54
  const cb = this._pendingRead
52
55
  this._pendingRead = null
56
+ this._db._state.io.dec()
53
57
  if (err) return cb(err)
54
58
 
55
59
  const n = keys.length
@@ -97,10 +101,16 @@ module.exports = class RocksDBIterator extends Readable {
97
101
  async _open(cb) {
98
102
  await this.ready()
99
103
 
100
- const state = this._db._state
104
+ this._db._state.io.inc()
101
105
 
102
- if (state._suspending || state._suspended) {
103
- return cb(new Error('Database is suspended'))
106
+ if (this._db._state.resumed !== null) {
107
+ const resumed = await this._db._state.resumed.promise
108
+
109
+ if (!resumed) {
110
+ this._db._state.io.dec()
111
+
112
+ return cb(new Error('RocksDB session is closed'))
113
+ }
104
114
  }
105
115
 
106
116
  this._pendingOpen = cb
@@ -122,16 +132,16 @@ module.exports = class RocksDBIterator extends Readable {
122
132
  )
123
133
  }
124
134
 
125
- _read(cb) {
126
- const state = this._db._state
127
-
128
- if (state._suspending || state._suspended) {
129
- return cb(new Error('Database is suspended'))
135
+ async _read(cb) {
136
+ if (this._db._state.resumed !== null) {
137
+ await this._db._state.resumed.promise
130
138
  }
131
139
 
132
140
  this._pendingRead = cb
133
141
 
134
142
  binding.iteratorRead(this._handle, Math.min(this._capacity, this._limit))
143
+
144
+ this._db._state.io.inc()
135
145
  }
136
146
 
137
147
  async _destroy(cb) {
@@ -139,6 +149,8 @@ module.exports = class RocksDBIterator extends Readable {
139
149
 
140
150
  this._pendingDestroy = cb
141
151
 
152
+ if (this._opened === false) return this._onclose(null)
153
+
142
154
  binding.iteratorClose(this._handle)
143
155
  }
144
156
 
package/lib/snapshot.js CHANGED
@@ -5,7 +5,7 @@ module.exports = class RocksDBSnapshot {
5
5
  this._state = state
6
6
 
7
7
  this._handle = null
8
- this._refs = 0 // starts unreffed, easier, must be reffed in first tick
8
+ this._refs = 0
9
9
 
10
10
  if (state.deferSnapshotInit === false) this._init()
11
11
  }
package/lib/state.js CHANGED
@@ -25,8 +25,8 @@ module.exports = class RocksDBState extends ReadyResource {
25
25
 
26
26
  this.path = path
27
27
  this.db = db
28
- this.activity = new RefCounter()
29
- this.activeBatches = new RefCounter()
28
+ this.handles = new RefCounter()
29
+ this.io = new RefCounter()
30
30
  this.sessions = []
31
31
  this.columnFamilies = [columnFamily]
32
32
  this.deferSnapshotInit = true
@@ -146,6 +146,7 @@ module.exports = class RocksDBState extends ReadyResource {
146
146
  await promise
147
147
 
148
148
  this.deferSnapshotInit = false
149
+
149
150
  for (const session of this.sessions) {
150
151
  if (session._snapshot) session._snapshot._init()
151
152
  }
@@ -158,10 +159,12 @@ module.exports = class RocksDBState extends ReadyResource {
158
159
 
159
160
  async _close() {
160
161
  if (this.resumed) this.resumed.resolve(false)
161
- while (!this.activity.isIdle()) await this.activity.idle()
162
162
 
163
- while (this.sessions.length > 0)
163
+ while (!this.handles.isIdle()) await this.handles.idle()
164
+
165
+ while (this.sessions.length > 0) {
164
166
  await this.sessions[this.sessions.length - 1].close()
167
+ }
165
168
 
166
169
  for (const columnFamily of this.columnFamilies) columnFamily.destroy()
167
170
 
@@ -185,6 +188,20 @@ module.exports = class RocksDBState extends ReadyResource {
185
188
  async flush(db, opts) {
186
189
  if (this.opened === false) await this.ready()
187
190
 
191
+ this.handles.inc()
192
+ this.io.inc()
193
+
194
+ if (this.resumed !== null) {
195
+ const resumed = await this.resumed.promise
196
+
197
+ if (!resumed) {
198
+ this.handles.dec()
199
+ this.io.dec()
200
+
201
+ throw new Error('RocksDB session is closed')
202
+ }
203
+ }
204
+
188
205
  const req = { resolve: null, reject: null, handle: null }
189
206
 
190
207
  const promise = new Promise((resolve, reject) => {
@@ -192,14 +209,19 @@ module.exports = class RocksDBState extends ReadyResource {
192
209
  req.reject = reject
193
210
  })
194
211
 
195
- req.handle = binding.flush(
196
- this._handle,
197
- db._columnFamily._handle,
198
- req,
199
- onflush
200
- )
212
+ try {
213
+ req.handle = binding.flush(
214
+ this._handle,
215
+ db._columnFamily._handle,
216
+ req,
217
+ onflush
218
+ )
201
219
 
202
- await promise
220
+ await promise
221
+ } finally {
222
+ this.handles.dec()
223
+ this.io.dec()
224
+ }
203
225
 
204
226
  function onflush(err) {
205
227
  if (err) req.reject(new Error(err))
@@ -208,16 +230,17 @@ module.exports = class RocksDBState extends ReadyResource {
208
230
  }
209
231
 
210
232
  async suspend() {
211
- if (this._suspended === true) return
212
233
  if (this._suspending === null) this._suspending = this._suspend()
213
234
  return this._suspending
214
235
  }
215
236
 
216
237
  async _suspend() {
217
- if (this._resuming !== null) await this._resuming
218
238
  if (this.opened === false) await this.ready()
239
+ if (this._resuming !== null) await this._resuming
240
+ if (this._suspended === true) return
241
+
242
+ while (!this.io.isIdle()) await this.io.idle()
219
243
 
220
- while (!this.activeBatches.isIdle()) await this.activeBatches.idle()
221
244
  this.resumed = rrp()
222
245
 
223
246
  const req = { resolve: null, reject: null, handle: null }
@@ -227,9 +250,9 @@ module.exports = class RocksDBState extends ReadyResource {
227
250
  req.reject = reject
228
251
  })
229
252
 
230
- req.handle = binding.suspend(this._handle, req, onsuspend)
231
-
232
253
  try {
254
+ req.handle = binding.suspend(this._handle, req, onsuspend)
255
+
233
256
  await promise
234
257
 
235
258
  this._suspended = true
@@ -244,14 +267,14 @@ module.exports = class RocksDBState extends ReadyResource {
244
267
  }
245
268
 
246
269
  resume() {
247
- if (this._suspended === false) return
248
270
  if (this._resuming === null) this._resuming = this._resume()
249
271
  return this._resuming
250
272
  }
251
273
 
252
274
  async _resume() {
253
- if (this._suspending !== null) await this._suspending
254
275
  if (this.opened === false) await this.ready()
276
+ if (this._suspending !== null) await this._suspending
277
+ if (this._suspended === false) return
255
278
 
256
279
  const req = { resolve: null, reject: null, handle: null }
257
280
 
@@ -260,9 +283,9 @@ module.exports = class RocksDBState extends ReadyResource {
260
283
  req.reject = reject
261
284
  })
262
285
 
263
- req.handle = binding.resume(this._handle, req, onresume)
264
-
265
286
  try {
287
+ req.handle = binding.resume(this._handle, req, onresume)
288
+
266
289
  await promise
267
290
 
268
291
  this._suspended = false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rocksdb-native",
3
- "version": "3.5.2",
3
+ "version": "3.5.4",
4
4
  "description": "librocksdb bindings for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -50,7 +50,6 @@
50
50
  "cmake-fetch": "^1.0.1",
51
51
  "cmake-napi": "^1.0.6",
52
52
  "prettier": "^3.4.1",
53
- "prettier-config-standard": "^7.0.0",
54
- "test-tmp": "^1.2.0"
53
+ "prettier-config-standard": "^7.0.0"
55
54
  }
56
55
  }