rocksdb-native 2.4.3 → 2.5.0

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
@@ -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,7 +14,10 @@ class RocksDBBatch {
13
14
  valueEncoding = encoding
14
15
  } = opts
15
16
 
17
+ db._incRef()
18
+
16
19
  this._db = db
20
+ this._destroyed = false
17
21
  this._capacity = capacity
18
22
  this._operations = []
19
23
  this._promises = []
@@ -39,6 +43,7 @@ class RocksDBBatch {
39
43
  this._promises = []
40
44
  this._request = null
41
45
  this._resolveRequest = null
46
+ this.destroy()
42
47
 
43
48
  if (resolve !== null) resolve()
44
49
  }
@@ -61,6 +66,13 @@ class RocksDBBatch {
61
66
  this._init()
62
67
  }
63
68
 
69
+ destroy () {
70
+ if (this._request) throw new Error('Batch already flushed')
71
+ if (this._destroyed) return
72
+ this._destroyed = true
73
+ this._db._decRef()
74
+ }
75
+
64
76
  flush () {
65
77
  if (this._request) throw new Error('Request already in progress')
66
78
 
@@ -73,7 +85,7 @@ class RocksDBBatch {
73
85
  tryFlush () {
74
86
  if (this._request) throw new Error('Request already in progress')
75
87
 
76
- this._request = true
88
+ this._request = emptyPromise
77
89
  this._flush()
78
90
  }
79
91
 
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.3",
3
+ "version": "2.5.0",
4
4
  "description": "librocksdb bindings for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",