rocksdb-native 2.4.2 → 2.4.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/binding.c CHANGED
@@ -773,10 +773,10 @@ rocksdb_native_read (js_env_t *env, js_callback_info_t *info) {
773
773
  err = js_get_array_length(env, argv[2], &len);
774
774
  assert(err == 0);
775
775
 
776
- err = js_create_reference(env, argv[4], 0, &batch->ctx);
776
+ err = js_create_reference(env, argv[4], 1, &batch->ctx);
777
777
  assert(err == 0);
778
778
 
779
- err = js_create_reference(env, argv[5], 0, &batch->on_status);
779
+ err = js_create_reference(env, argv[5], 1, &batch->on_status);
780
780
  assert(err == 0);
781
781
 
782
782
  for (uint32_t i = 0; i < len; i++) {
package/index.js CHANGED
@@ -41,6 +41,8 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
41
41
  this.tableFormatVersion = tableFormatVersion
42
42
 
43
43
  this._snapshots = new Set()
44
+ this._refs = 0
45
+ this._resolvePreclose = null
44
46
 
45
47
  this._handle = binding.init()
46
48
  }
@@ -87,6 +89,12 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
87
89
  }
88
90
 
89
91
  async _close () {
92
+ if (this._refs > 0) {
93
+ await new Promise((resolve) => {
94
+ this._resolvePreclose = resolve
95
+ })
96
+ }
97
+
90
98
  for (const snapshot of this._snapshots) snapshot.destroy()
91
99
 
92
100
  const req = { resolve: null, reject: null, handle: null }
@@ -108,6 +116,18 @@ const RocksDB = module.exports = class RocksDB extends ReadyResource {
108
116
  }
109
117
  }
110
118
 
119
+ _incRef () {
120
+ this._refs++
121
+ }
122
+
123
+ _decRef () {
124
+ if (--this._refs === 0 && this._resolvePreclose !== null) {
125
+ const resolve = this._resolvePreclose
126
+ this._resolvePreclose = null
127
+ resolve()
128
+ }
129
+ }
130
+
111
131
  snapshot (opts) {
112
132
  return new Snapshot(this, opts)
113
133
  }
package/lib/batch.js CHANGED
@@ -3,6 +3,7 @@ const b4a = require('b4a')
3
3
  const binding = require('../binding')
4
4
 
5
5
  const empty = b4a.alloc(0)
6
+ const emptyPromise = Promise.resolve()
6
7
 
7
8
  class RocksDBBatch {
8
9
  constructor (db, opts = {}) {
@@ -13,6 +14,8 @@ class RocksDBBatch {
13
14
  valueEncoding = encoding
14
15
  } = opts
15
16
 
17
+ db._incRef()
18
+
16
19
  this._db = db
17
20
  this._capacity = capacity
18
21
  this._operations = []
@@ -39,6 +42,7 @@ class RocksDBBatch {
39
42
  this._promises = []
40
43
  this._request = null
41
44
  this._resolveRequest = null
45
+ this._db._decRef()
42
46
 
43
47
  if (resolve !== null) resolve()
44
48
  }
@@ -73,7 +77,7 @@ class RocksDBBatch {
73
77
  tryFlush () {
74
78
  if (this._request) throw new Error('Request already in progress')
75
79
 
76
- this._request = true
80
+ this._request = emptyPromise
77
81
  this._flush()
78
82
  }
79
83
 
package/lib/iterator.js CHANGED
@@ -23,6 +23,8 @@ module.exports = class RocksDBIterator extends Readable {
23
23
 
24
24
  super()
25
25
 
26
+ db._incRef()
27
+
26
28
  this._db = db
27
29
 
28
30
  this._keyEncoding = keyEncoding
@@ -75,6 +77,7 @@ module.exports = class RocksDBIterator extends Readable {
75
77
  _onclose (err) {
76
78
  const cb = this._pendingDestroy
77
79
  this._pendingDestroy = null
80
+ this._db._decRef()
78
81
  cb(err)
79
82
  }
80
83
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rocksdb-native",
3
- "version": "2.4.2",
3
+ "version": "2.4.4",
4
4
  "description": "librocksdb bindings for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",