rocksdb-native 3.5.0 → 3.5.2

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/CMakeLists.txt CHANGED
@@ -13,7 +13,7 @@ if(target MATCHES "win32")
13
13
  add_compile_options(/MT$<$<CONFIG:Debug>:d>)
14
14
  endif()
15
15
 
16
- fetch_package("github:holepunchto/librocksdb#c254e48")
16
+ fetch_package("github:holepunchto/librocksdb#d2b6760")
17
17
 
18
18
  add_bare_module(rocksdb_native_bare)
19
19
 
package/binding.c CHANGED
@@ -190,10 +190,15 @@ rocksdb_native__on_open(rocksdb_open_t *handle, int status) {
190
190
  err = js_get_array_length(env, column_families, &len);
191
191
  assert(err == 0);
192
192
 
193
+ js_value_t **elements = malloc(len * sizeof(js_value_t *));
194
+
195
+ uint32_t fetched;
196
+ err = js_get_array_elements(env, column_families, elements, len, 0, &fetched);
197
+ assert(err == 0);
198
+ assert(fetched == len);
199
+
193
200
  for (uint32_t i = 0; i < len; i++) {
194
- js_value_t *handle;
195
- err = js_get_element(env, column_families, i, &handle);
196
- assert(err == 0);
201
+ js_value_t *handle = elements[i];
197
202
 
198
203
  rocksdb_native_column_family_t *column_family;
199
204
  err = js_get_arraybuffer_info(env, handle, (void **) &column_family, NULL);
@@ -207,6 +212,8 @@ rocksdb_native__on_open(rocksdb_open_t *handle, int status) {
207
212
  err = js_add_teardown_callback(env, rocksdb_native__on_column_family_teardown, (void *) column_family);
208
213
  assert(err == 0);
209
214
  }
215
+
216
+ free(elements);
210
217
  }
211
218
 
212
219
  js_call_function_with_checkpoint(env, ctx, cb, 1, (js_value_t *[]) {error}, NULL);
@@ -388,10 +395,15 @@ rocksdb_native_open(js_env_t *env, js_callback_info_t *info) {
388
395
 
389
396
  rocksdb_column_family_descriptor_t *column_families = calloc(len, sizeof(rocksdb_column_family_descriptor_t));
390
397
 
398
+ js_value_t **elements = malloc(len * sizeof(js_value_t *));
399
+
400
+ uint32_t fetched;
401
+ err = js_get_array_elements(env, argv[3], elements, len, 0, &fetched);
402
+ assert(err == 0);
403
+ assert(fetched == len);
404
+
391
405
  for (uint32_t i = 0; i < len; i++) {
392
- js_value_t *handle;
393
- err = js_get_element(env, argv[3], i, &handle);
394
- assert(err == 0);
406
+ js_value_t *handle = elements[i];
395
407
 
396
408
  rocksdb_native_column_family_t *column_family;
397
409
  err = js_get_arraybuffer_info(env, handle, (void **) &column_family, NULL);
@@ -402,6 +414,8 @@ rocksdb_native_open(js_env_t *env, js_callback_info_t *info) {
402
414
  column_family->db = &db->handle;
403
415
  }
404
416
 
417
+ free(elements);
418
+
405
419
  rocksdb_column_family_t **handles = calloc(len, sizeof(rocksdb_column_family_t *));
406
420
 
407
421
  js_value_t *handle;
@@ -1394,10 +1408,15 @@ rocksdb_native_read(js_env_t *env, js_callback_info_t *info) {
1394
1408
  err = js_create_reference(env, argv[5], 1, &req->on_status);
1395
1409
  assert(err == 0);
1396
1410
 
1411
+ js_value_t **elements = malloc(len * sizeof(js_value_t *));
1412
+
1413
+ uint32_t fetched;
1414
+ err = js_get_array_elements(env, argv[2], elements, len, 0, &fetched);
1415
+ assert(err == 0);
1416
+ assert(fetched == len);
1417
+
1397
1418
  for (uint32_t i = 0; i < len; i++) {
1398
- js_value_t *read;
1399
- err = js_get_element(env, argv[2], i, &read);
1400
- assert(err == 0);
1419
+ js_value_t *read = elements[i];
1401
1420
 
1402
1421
  js_value_t *property;
1403
1422
 
@@ -1433,6 +1452,8 @@ rocksdb_native_read(js_env_t *env, js_callback_info_t *info) {
1433
1452
  }
1434
1453
  }
1435
1454
 
1455
+ free(elements);
1456
+
1436
1457
  rocksdb_read_options_t options = {
1437
1458
  .version = 0,
1438
1459
  };
@@ -1591,10 +1612,15 @@ rocksdb_native_write(js_env_t *env, js_callback_info_t *info) {
1591
1612
  err = js_create_reference(env, argv[4], 1, &req->on_status);
1592
1613
  assert(err == 0);
1593
1614
 
1615
+ js_value_t **elements = malloc(len * sizeof(js_value_t *));
1616
+
1617
+ uint32_t fetched;
1618
+ err = js_get_array_elements(env, argv[2], elements, len, 0, &fetched);
1619
+ assert(err == 0);
1620
+ assert(fetched == len);
1621
+
1594
1622
  for (uint32_t i = 0; i < len; i++) {
1595
- js_value_t *write;
1596
- err = js_get_element(env, argv[2], i, &write);
1597
- assert(err == 0);
1623
+ js_value_t *write = elements[i];
1598
1624
 
1599
1625
  js_value_t *property;
1600
1626
 
@@ -1668,6 +1694,8 @@ rocksdb_native_write(js_env_t *env, js_callback_info_t *info) {
1668
1694
  }
1669
1695
  }
1670
1696
 
1697
+ free(elements);
1698
+
1671
1699
  err = rocksdb_write(&db->handle, &req->handle, req->writes, len, NULL, rocksdb_native__on_write);
1672
1700
  assert(err == 0);
1673
1701
 
package/index.js CHANGED
@@ -172,10 +172,20 @@ class RocksDB {
172
172
  this._state.activity.inc()
173
173
  }
174
174
 
175
+ _refBatch() {
176
+ this._ref()
177
+ this._state.activeBatches.inc()
178
+ }
179
+
175
180
  _unref() {
176
181
  if (this._snapshot) this._snapshot.unref()
177
182
  this._state.activity.dec()
178
183
  }
184
+
185
+ _unrefBatch() {
186
+ this._unref()
187
+ this._state.activeBatches.dec()
188
+ }
179
189
  }
180
190
 
181
191
  module.exports = exports = RocksDB
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._ref()
11
+ db._refBatch()
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._ref()
35
+ db._refBatch()
36
36
 
37
37
  this._db = db
38
38
  this._destroyed = false
@@ -81,7 +81,7 @@ class RocksDBBatch {
81
81
 
82
82
  if (this._promises.length) this._abort()
83
83
 
84
- this._db._unref()
84
+ this._db._unrefBatch()
85
85
  this._onfree()
86
86
  }
87
87
 
@@ -101,12 +101,6 @@ class RocksDBBatch {
101
101
  if (this._request) throw new Error('Request in progress')
102
102
  if (this._destroyed) throw new Error('Batch is destroyed')
103
103
 
104
- const state = this._db._state
105
-
106
- if (state._suspending || state._suspended) {
107
- throw new Error('Database is suspended')
108
- }
109
-
110
104
  this._request = new Promise((resolve, reject) => {
111
105
  this._resolve = resolve
112
106
  this._reject = reject
@@ -121,19 +115,22 @@ class RocksDBBatch {
121
115
  if (this._request) throw new Error('Request in progress')
122
116
  if (this._destroyed) throw new Error('Batch is destroyed')
123
117
 
124
- const state = this._db._state
125
-
126
- if (state._suspending || state._suspended) {
127
- throw new Error('Database is suspended')
128
- }
129
-
130
118
  this._request = resolved
131
119
 
132
120
  this._flush()
133
121
  }
134
122
 
135
123
  async _flush() {
124
+ const state = this._db._state
125
+
136
126
  if (this._handle === null) await this.ready()
127
+ if (state.resumed !== null) await state.resumed.promise
128
+
129
+ if ((state._suspending || state._suspended) && !this._destroyed) {
130
+ this._destroyed = true
131
+ this._abort()
132
+ this._db._unrefBatch()
133
+ }
137
134
  }
138
135
 
139
136
  _enqueuePromise(resolve, reject) {
@@ -173,6 +170,7 @@ exports.ReadBatch = class RocksDBReadBatch extends RocksDBBatch {
173
170
 
174
171
  async _flush() {
175
172
  await super._flush()
173
+ if (this._destroyed) return
176
174
 
177
175
  binding.read(
178
176
  this._db._state._handle,
@@ -240,6 +238,7 @@ exports.WriteBatch = class RocksDBWriteBatch extends RocksDBBatch {
240
238
 
241
239
  async _flush() {
242
240
  await super._flush()
241
+ if (this._destroyed) return
243
242
 
244
243
  binding.write(
245
244
  this._db._state._handle,
package/lib/state.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const ReadyResource = require('ready-resource')
2
2
  const RefCounter = require('refcounter')
3
+ const rrp = require('resolve-reject-promise')
3
4
  const { ReadBatch, WriteBatch } = require('./batch')
4
5
  const ColumnFamily = require('./column-family')
5
6
  const binding = require('../binding')
@@ -25,9 +26,11 @@ module.exports = class RocksDBState extends ReadyResource {
25
26
  this.path = path
26
27
  this.db = db
27
28
  this.activity = new RefCounter()
29
+ this.activeBatches = new RefCounter()
28
30
  this.sessions = []
29
31
  this.columnFamilies = [columnFamily]
30
32
  this.deferSnapshotInit = true
33
+ this.resumed = null
31
34
 
32
35
  this._suspended = false
33
36
  this._suspending = null
@@ -154,6 +157,7 @@ module.exports = class RocksDBState extends ReadyResource {
154
157
  }
155
158
 
156
159
  async _close() {
160
+ if (this.resumed) this.resumed.resolve(false)
157
161
  while (!this.activity.isIdle()) await this.activity.idle()
158
162
 
159
163
  while (this.sessions.length > 0)
@@ -204,14 +208,18 @@ module.exports = class RocksDBState extends ReadyResource {
204
208
  }
205
209
 
206
210
  async suspend() {
211
+ if (this._suspended === true) return
207
212
  if (this._suspending === null) this._suspending = this._suspend()
208
213
  return this._suspending
209
214
  }
210
215
 
211
216
  async _suspend() {
212
- if (this._resuming) await this._resuming
217
+ if (this._resuming !== null) await this._resuming
213
218
  if (this.opened === false) await this.ready()
214
219
 
220
+ while (!this.activeBatches.isIdle()) await this.activeBatches.idle()
221
+ this.resumed = rrp()
222
+
215
223
  const req = { resolve: null, reject: null, handle: null }
216
224
 
217
225
  const promise = new Promise((resolve, reject) => {
@@ -236,12 +244,13 @@ module.exports = class RocksDBState extends ReadyResource {
236
244
  }
237
245
 
238
246
  resume() {
247
+ if (this._suspended === false) return
239
248
  if (this._resuming === null) this._resuming = this._resume()
240
249
  return this._resuming
241
250
  }
242
251
 
243
252
  async _resume() {
244
- if (this._suspending) await this._suspending
253
+ if (this._suspending !== null) await this._suspending
245
254
  if (this.opened === false) await this.ready()
246
255
 
247
256
  const req = { resolve: null, reject: null, handle: null }
@@ -261,6 +270,10 @@ module.exports = class RocksDBState extends ReadyResource {
261
270
  this._resuming = null
262
271
  }
263
272
 
273
+ const resumed = this.resumed
274
+ this.resumed = null
275
+ resumed.resolve(true)
276
+
264
277
  function onresume(err) {
265
278
  if (err) req.reject(new Error(err))
266
279
  else req.resolve()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rocksdb-native",
3
- "version": "3.5.0",
3
+ "version": "3.5.2",
4
4
  "description": "librocksdb bindings for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -33,17 +33,18 @@
33
33
  },
34
34
  "homepage": "https://github.com/holepunchto/rocksdb-native",
35
35
  "engines": {
36
- "bare": ">=1.7.0"
36
+ "bare": ">=1.16.0"
37
37
  },
38
38
  "dependencies": {
39
39
  "compact-encoding": "^2.15.0",
40
40
  "ready-resource": "^1.0.0",
41
41
  "refcounter": "^1.0.0",
42
42
  "require-addon": "^1.0.2",
43
+ "resolve-reject-promise": "^1.1.0",
43
44
  "streamx": "^2.16.1"
44
45
  },
45
46
  "devDependencies": {
46
- "bare-compat-napi": "^1.1.0",
47
+ "bare-compat-napi": "^1.3.0",
47
48
  "brittle": "^3.5.0",
48
49
  "cmake-bare": "^1.1.14",
49
50
  "cmake-fetch": "^1.0.1",