rocksdb-native 3.9.1 → 3.9.3

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
@@ -9,7 +9,7 @@ project(rocksdb_native C CXX)
9
9
 
10
10
  bare_target(target)
11
11
 
12
- fetch_package("github:holepunchto/librocksdb#3aaa9dc")
12
+ fetch_package("github:holepunchto/librocksdb#1fa9c8c")
13
13
  fetch_package("github:holepunchto/libjstl#098664c")
14
14
 
15
15
  add_bare_module(rocksdb_native_bare)
package/binding.cc CHANGED
@@ -6,26 +6,20 @@
6
6
  #include <stdlib.h>
7
7
  #include <string.h>
8
8
  #include <utf.h>
9
-
10
- namespace {
11
- using cb_on_open_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
12
- using cb_on_close_t = js_function_t<void, js_receiver_t>;
13
- using cb_on_suspend_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
14
- using cb_on_resume_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
15
- using cb_on_flush_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
16
- using cb_on_write_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
17
- using cb_on_read_t = js_function_t<void, js_receiver_t, js_array_t, js_array_t>;
18
- using cb_on_iterator_open_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
19
- using cb_on_iterator_close_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
20
- using cb_on_iterator_read_t = js_function_t<
21
- void,
22
- js_receiver_t,
23
- std::optional<js_string_t>,
24
- std::vector<js_arraybuffer_t>,
25
- std::vector<js_arraybuffer_t>>;
26
- using cb_on_compact_range_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
27
- using cb_on_approximate_size_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>, uint64_t>;
28
- }; // namespace
9
+ #include <uv.h>
10
+
11
+ using rocksdb_native_on_open_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
12
+ using rocksdb_native_on_close_t = js_function_t<void, js_receiver_t>;
13
+ using rocksdb_native_on_suspend_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
14
+ using rocksdb_native_on_resume_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
15
+ using rocksdb_native_on_flush_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
16
+ using rocksdb_native_on_write_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
17
+ using rocksdb_native_on_read_t = js_function_t<void, js_receiver_t, js_array_t, js_array_t>;
18
+ using rocksdb_native_on_iterator_open_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
19
+ using rocksdb_native_on_iterator_close_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
20
+ using rocksdb_native_on_iterator_read_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>, std::vector<js_arraybuffer_t>, std::vector<js_arraybuffer_t>>;
21
+ using rocksdb_native_on_compact_range_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>>;
22
+ using rocksdb_native_on_approximate_size_t = js_function_t<void, js_receiver_t, std::optional<js_string_t>, uint64_t>;
29
23
 
30
24
  struct rocksdb_native_column_family_t {
31
25
  rocksdb_column_family_t *handle;
@@ -55,7 +49,7 @@ struct rocksdb_native_open_t {
55
49
 
56
50
  js_env_t *env;
57
51
  js_persistent_t<js_receiver_t> ctx;
58
- js_persistent_t<cb_on_open_t> on_open;
52
+ js_persistent_t<rocksdb_native_on_open_t> on_open;
59
53
 
60
54
  js_persistent_t<js_array_t> column_families;
61
55
  };
@@ -65,7 +59,7 @@ struct rocksdb_native_close_t {
65
59
 
66
60
  js_env_t *env;
67
61
  js_persistent_t<js_receiver_t> ctx;
68
- js_persistent_t<cb_on_close_t> on_close;
62
+ js_persistent_t<rocksdb_native_on_close_t> on_close;
69
63
  };
70
64
 
71
65
  struct rocksdb_native_suspend_t {
@@ -73,7 +67,7 @@ struct rocksdb_native_suspend_t {
73
67
 
74
68
  js_env_t *env;
75
69
  js_persistent_t<js_receiver_t> ctx;
76
- js_persistent_t<cb_on_suspend_t> on_suspend;
70
+ js_persistent_t<rocksdb_native_on_suspend_t> on_suspend;
77
71
  };
78
72
 
79
73
  struct rocksdb_native_resume_t {
@@ -81,7 +75,7 @@ struct rocksdb_native_resume_t {
81
75
 
82
76
  js_env_t *env;
83
77
  js_persistent_t<js_receiver_t> ctx;
84
- js_persistent_t<cb_on_resume_t> on_resume;
78
+ js_persistent_t<rocksdb_native_on_resume_t> on_resume;
85
79
  };
86
80
 
87
81
  struct rocksdb_native_iterator_t {
@@ -92,9 +86,9 @@ struct rocksdb_native_iterator_t {
92
86
 
93
87
  js_env_t *env;
94
88
  js_persistent_t<js_receiver_t> ctx;
95
- js_persistent_t<cb_on_iterator_open_t> on_open;
96
- js_persistent_t<cb_on_iterator_close_t> on_close;
97
- js_persistent_t<cb_on_iterator_read_t> on_read;
89
+ js_persistent_t<rocksdb_native_on_iterator_open_t> on_open;
90
+ js_persistent_t<rocksdb_native_on_iterator_close_t> on_close;
91
+ js_persistent_t<rocksdb_native_on_iterator_read_t> on_read;
98
92
 
99
93
  bool active;
100
94
  bool exiting;
@@ -111,7 +105,7 @@ struct rocksdb_native_read_batch_t {
111
105
 
112
106
  js_env_t *env;
113
107
  js_persistent_t<js_receiver_t> ctx;
114
- js_persistent_t<cb_on_read_t> on_read;
108
+ js_persistent_t<rocksdb_native_on_read_t> on_read;
115
109
  };
116
110
 
117
111
  struct rocksdb_native_write_batch_t {
@@ -123,7 +117,7 @@ struct rocksdb_native_write_batch_t {
123
117
 
124
118
  js_env_t *env;
125
119
  js_persistent_t<js_receiver_t> ctx;
126
- js_persistent_t<cb_on_write_t> on_write;
120
+ js_persistent_t<rocksdb_native_on_write_t> on_write;
127
121
  };
128
122
 
129
123
  struct rocksdb_native_flush_t {
@@ -131,7 +125,7 @@ struct rocksdb_native_flush_t {
131
125
 
132
126
  js_env_t *env;
133
127
  js_persistent_t<js_receiver_t> ctx;
134
- js_persistent_t<cb_on_flush_t> on_flush;
128
+ js_persistent_t<rocksdb_native_on_flush_t> on_flush;
135
129
 
136
130
  js_persistent_t<rocksdb_native_column_family_t> column_family;
137
131
  };
@@ -145,7 +139,7 @@ struct rocksdb_native_compact_range_t {
145
139
 
146
140
  js_env_t *env;
147
141
  js_persistent_t<js_receiver_t> ctx;
148
- js_persistent_t<cb_on_compact_range_t> on_compact_range;
142
+ js_persistent_t<rocksdb_native_on_compact_range_t> on_compact_range;
149
143
  };
150
144
 
151
145
  struct rocksdb_native_approximate_size_t {
@@ -153,7 +147,7 @@ struct rocksdb_native_approximate_size_t {
153
147
 
154
148
  js_env_t *env;
155
149
  js_persistent_t<js_receiver_t> ctx;
156
- js_persistent_t<cb_on_approximate_size_t> on_approximate_size;
150
+ js_persistent_t<rocksdb_native_on_approximate_size_t> on_approximate_size;
157
151
  };
158
152
 
159
153
  static void
@@ -161,6 +155,21 @@ rocksdb_native__on_free(js_env_t *env, char *data) {
161
155
  free(data);
162
156
  }
163
157
 
158
+ static int
159
+ rocksdb_native__try_create_external_arraybuffer(js_env_t *env, char *data, size_t len, js_arraybuffer_t &result) {
160
+ int err;
161
+
162
+ err = js_create_external_arraybuffer<rocksdb_native__on_free>(env, data, len, result);
163
+ if (err == 0) return 0;
164
+
165
+ err = js_create_arraybuffer(env, data, len, result);
166
+ assert(err == 0);
167
+
168
+ free(data);
169
+
170
+ return 0;
171
+ }
172
+
164
173
  static void
165
174
  rocksdb_native__on_column_family_teardown(void *data);
166
175
 
@@ -174,11 +183,11 @@ rocksdb_native__on_open(rocksdb_open_t *handle, int status) {
174
183
 
175
184
  auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
176
185
 
177
- js_env_t *env = req->env;
186
+ auto env = req->env;
178
187
 
179
- const rocksdb_column_family_descriptor_t *descriptors = handle->column_families;
188
+ auto descriptors = handle->column_families;
180
189
 
181
- rocksdb_column_family_t **handles = handle->handles;
190
+ auto handles = handle->handles;
182
191
 
183
192
  if (db->exiting) {
184
193
  req->on_open.reset();
@@ -192,7 +201,7 @@ rocksdb_native__on_open(rocksdb_open_t *handle, int status) {
192
201
  err = js_get_reference_value(env, req->ctx, ctx);
193
202
  assert(err == 0);
194
203
 
195
- cb_on_open_t cb;
204
+ rocksdb_native_on_open_t cb;
196
205
  err = js_get_reference_value(env, req->on_open, cb);
197
206
  assert(err == 0);
198
207
 
@@ -213,9 +222,8 @@ rocksdb_native__on_open(rocksdb_open_t *handle, int status) {
213
222
 
214
223
  rocksdb_column_family_t **handles = handle->handles;
215
224
 
216
- if (req->handle.error == NULL) {
225
+ if (req->handle.error == nullptr) {
217
226
  std::vector<js_arraybuffer_t> elements;
218
-
219
227
  err = js_get_array_elements(env, column_families, elements);
220
228
  assert(err == 0);
221
229
 
@@ -233,7 +241,7 @@ rocksdb_native__on_open(rocksdb_open_t *handle, int status) {
233
241
  err = js_create_reference(env, ctx, column_family->ctx);
234
242
  assert(err == 0);
235
243
 
236
- err = js_add_teardown_callback(env, rocksdb_native__on_column_family_teardown, (void *) column_family);
244
+ err = js_add_teardown_callback(env, rocksdb_native__on_column_family_teardown, column_family);
237
245
  assert(err == 0);
238
246
  }
239
247
  }
@@ -254,13 +262,13 @@ rocksdb_native__on_close(rocksdb_close_t *handle, int status) {
254
262
 
255
263
  assert(status == 0);
256
264
 
257
- rocksdb_native_close_t *req = (rocksdb_native_close_t *) handle->data;
265
+ auto req = reinterpret_cast<rocksdb_native_close_t *>(handle->data);
258
266
 
259
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
267
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
260
268
 
261
- js_env_t *env = req->env;
269
+ auto env = req->env;
262
270
 
263
- js_deferred_teardown_t *teardown = db->teardown;
271
+ auto teardown = db->teardown;
264
272
 
265
273
  if (db->exiting) {
266
274
  db->ctx.reset();
@@ -280,7 +288,7 @@ rocksdb_native__on_close(rocksdb_close_t *handle, int status) {
280
288
  err = js_get_reference_value(env, req->ctx, ctx);
281
289
  assert(err == 0);
282
290
 
283
- cb_on_close_t cb;
291
+ rocksdb_native_on_close_t cb;
284
292
  err = js_get_reference_value(env, req->on_close, cb);
285
293
  assert(err == 0);
286
294
 
@@ -302,9 +310,9 @@ static void
302
310
  rocksdb_native__on_teardown(js_deferred_teardown_t *handle, void *data) {
303
311
  int err;
304
312
 
305
- rocksdb_native_t *db = (rocksdb_native_t *) data;
313
+ auto db = reinterpret_cast<rocksdb_native_t *>(data);
306
314
 
307
- js_env_t *env = db->env;
315
+ auto env = db->env;
308
316
 
309
317
  db->exiting = true;
310
318
 
@@ -313,7 +321,7 @@ rocksdb_native__on_teardown(js_deferred_teardown_t *handle, void *data) {
313
321
  auto req = reinterpret_cast<rocksdb_native_close_t *>(malloc(sizeof(rocksdb_native_close_t)));
314
322
 
315
323
  req->env = env;
316
- req->handle.data = (void *) req;
324
+ req->handle.data = req;
317
325
 
318
326
  err = rocksdb_close(&db->handle, &req->handle, rocksdb_native__on_close);
319
327
  assert(err == 0);
@@ -366,7 +374,13 @@ rocksdb_native_init(
366
374
  };
367
375
 
368
376
  err = rocksdb_init(loop, &db->handle);
369
- assert(err == 0);
377
+
378
+ if (err < 0) {
379
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
380
+ assert(err == 0);
381
+
382
+ throw js_pending_exception;
383
+ }
370
384
 
371
385
  return handle;
372
386
  }
@@ -379,12 +393,11 @@ rocksdb_native_open(
379
393
  char *path,
380
394
  js_array_t column_families_array,
381
395
  js_receiver_t ctx,
382
- cb_on_open_t on_open
396
+ rocksdb_native_on_open_t on_open
383
397
  ) {
384
398
  int err;
385
399
 
386
400
  std::vector<js_arraybuffer_t> elements;
387
-
388
401
  err = js_get_array_elements(env, column_families_array, elements);
389
402
  assert(err == 0);
390
403
 
@@ -415,6 +428,15 @@ rocksdb_native_open(
415
428
  req->env = env;
416
429
  req->handle.data = req;
417
430
 
431
+ err = rocksdb_open(&db->handle, &req->handle, path, &db->options, column_families, handles, len, rocksdb_native__on_open);
432
+
433
+ if (err < 0) {
434
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
435
+ assert(err == 0);
436
+
437
+ throw js_pending_exception;
438
+ }
439
+
418
440
  err = js_create_reference(env, self, db->ctx);
419
441
  assert(err == 0);
420
442
 
@@ -427,10 +449,7 @@ rocksdb_native_open(
427
449
  err = js_create_reference(env, column_families_array, req->column_families);
428
450
  assert(err == 0);
429
451
 
430
- err = rocksdb_open(&db->handle, &req->handle, path, &db->options, column_families, handles, len, rocksdb_native__on_open);
431
- assert(err == 0);
432
-
433
- err = js_add_deferred_teardown_callback(env, rocksdb_native__on_teardown, (void *) db, &db->teardown);
452
+ err = js_add_deferred_teardown_callback(env, rocksdb_native__on_teardown, db, &db->teardown);
434
453
  assert(err == 0);
435
454
 
436
455
  return handle;
@@ -441,7 +460,7 @@ rocksdb_native_close(
441
460
  js_env_t *env,
442
461
  js_arraybuffer_span_of_t<rocksdb_native_t, 1> db,
443
462
  js_receiver_t ctx,
444
- cb_on_close_t on_close
463
+ rocksdb_native_on_close_t on_close
445
464
  ) {
446
465
  int err;
447
466
 
@@ -452,7 +471,16 @@ rocksdb_native_close(
452
471
  assert(err == 0);
453
472
 
454
473
  req->env = env;
455
- req->handle.data = (void *) req;
474
+ req->handle.data = req;
475
+
476
+ err = rocksdb_close(&db->handle, &req->handle, rocksdb_native__on_close);
477
+
478
+ if (err < 0) {
479
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
480
+ assert(err == 0);
481
+
482
+ throw js_pending_exception;
483
+ }
456
484
 
457
485
  err = js_create_reference(env, ctx, req->ctx);
458
486
  assert(err == 0);
@@ -462,9 +490,6 @@ rocksdb_native_close(
462
490
 
463
491
  db->closing = true;
464
492
 
465
- err = rocksdb_close(&db->handle, &req->handle, rocksdb_native__on_close);
466
- assert(err == 0);
467
-
468
493
  return handle;
469
494
  }
470
495
 
@@ -474,13 +499,13 @@ rocksdb_native__on_suspend(rocksdb_suspend_t *handle, int status) {
474
499
 
475
500
  assert(status == 0);
476
501
 
477
- rocksdb_native_suspend_t *req = (rocksdb_native_suspend_t *) handle->data;
502
+ auto req = reinterpret_cast<rocksdb_native_suspend_t *>(handle->data);
478
503
 
479
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
504
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
480
505
 
481
- js_env_t *env = req->env;
506
+ auto env = req->env;
482
507
 
483
- js_deferred_teardown_t *teardown = db->teardown;
508
+ auto teardown = db->teardown;
484
509
 
485
510
  if (db->exiting) {
486
511
  req->on_suspend.reset();
@@ -494,7 +519,7 @@ rocksdb_native__on_suspend(rocksdb_suspend_t *handle, int status) {
494
519
  err = js_get_reference_value(env, req->ctx, ctx);
495
520
  assert(err == 0);
496
521
 
497
- cb_on_suspend_t cb;
522
+ rocksdb_native_on_suspend_t cb;
498
523
  err = js_get_reference_value(env, req->on_suspend, cb);
499
524
  assert(err == 0);
500
525
 
@@ -517,7 +542,7 @@ rocksdb_native_suspend(
517
542
  js_env_t *env,
518
543
  js_arraybuffer_span_of_t<rocksdb_native_t, 1> db,
519
544
  js_receiver_t ctx,
520
- cb_on_suspend_t on_suspend
545
+ rocksdb_native_on_suspend_t on_suspend
521
546
  ) {
522
547
  int err;
523
548
 
@@ -528,7 +553,16 @@ rocksdb_native_suspend(
528
553
  assert(err == 0);
529
554
 
530
555
  req->env = env;
531
- req->handle.data = (void *) req;
556
+ req->handle.data = req;
557
+
558
+ err = rocksdb_suspend(&db->handle, &req->handle, rocksdb_native__on_suspend);
559
+
560
+ if (err < 0) {
561
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
562
+ assert(err == 0);
563
+
564
+ throw js_pending_exception;
565
+ }
532
566
 
533
567
  err = js_create_reference(env, ctx, req->ctx);
534
568
  assert(err == 0);
@@ -536,9 +570,6 @@ rocksdb_native_suspend(
536
570
  err = js_create_reference(env, on_suspend, req->on_suspend);
537
571
  assert(err == 0);
538
572
 
539
- err = rocksdb_suspend(&db->handle, &req->handle, rocksdb_native__on_suspend);
540
- assert(err == 0);
541
-
542
573
  return handle;
543
574
  }
544
575
 
@@ -548,13 +579,13 @@ rocksdb_native__on_resume(rocksdb_resume_t *handle, int status) {
548
579
 
549
580
  assert(status == 0);
550
581
 
551
- rocksdb_native_resume_t *req = (rocksdb_native_resume_t *) handle->data;
582
+ auto req = reinterpret_cast<rocksdb_native_resume_t *>(handle->data);
552
583
 
553
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
584
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
554
585
 
555
- js_env_t *env = req->env;
586
+ auto env = req->env;
556
587
 
557
- js_deferred_teardown_t *teardown = db->teardown;
588
+ auto teardown = db->teardown;
558
589
 
559
590
  if (db->exiting) {
560
591
  req->on_resume.reset();
@@ -568,7 +599,7 @@ rocksdb_native__on_resume(rocksdb_resume_t *handle, int status) {
568
599
  err = js_get_reference_value(env, req->ctx, ctx);
569
600
  assert(err == 0);
570
601
 
571
- cb_on_resume_t cb;
602
+ rocksdb_native_on_resume_t cb;
572
603
  err = js_get_reference_value(env, req->on_resume, cb);
573
604
  assert(err == 0);
574
605
 
@@ -594,7 +625,7 @@ rocksdb_native_resume(
594
625
  js_env_t *env,
595
626
  js_arraybuffer_span_of_t<rocksdb_native_t, 1> db,
596
627
  js_receiver_t ctx,
597
- cb_on_resume_t on_resume
628
+ rocksdb_native_on_resume_t on_resume
598
629
  ) {
599
630
  int err;
600
631
 
@@ -605,7 +636,16 @@ rocksdb_native_resume(
605
636
  assert(err == 0);
606
637
 
607
638
  req->env = env;
608
- req->handle.data = (void *) req;
639
+ req->handle.data = req;
640
+
641
+ err = rocksdb_resume(&db->handle, &req->handle, rocksdb_native__on_resume);
642
+
643
+ if (err < 0) {
644
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
645
+ assert(err == 0);
646
+
647
+ throw js_pending_exception;
648
+ }
609
649
 
610
650
  err = js_create_reference(env, ctx, req->ctx);
611
651
  assert(err == 0);
@@ -613,9 +653,6 @@ rocksdb_native_resume(
613
653
  err = js_create_reference(env, on_resume, req->on_resume);
614
654
  assert(err == 0);
615
655
 
616
- err = rocksdb_resume(&db->handle, &req->handle, rocksdb_native__on_resume);
617
- assert(err == 0);
618
-
619
656
  return handle;
620
657
  }
621
658
 
@@ -623,9 +660,9 @@ static void
623
660
  rocksdb_native__on_column_family_teardown(void *data) {
624
661
  int err;
625
662
 
626
- rocksdb_native_column_family_t *column_family = (rocksdb_native_column_family_t *) data;
663
+ auto column_family = reinterpret_cast<rocksdb_native_column_family_t *>(data);
627
664
 
628
- js_env_t *env = column_family->env;
665
+ auto env = column_family->env;
629
666
 
630
667
  err = rocksdb_column_family_destroy(column_family->db, column_family->handle);
631
668
  assert(err == 0);
@@ -688,8 +725,8 @@ rocksdb_native_column_family_init(
688
725
  assert(err == 0);
689
726
 
690
727
  column_family->env = env;
691
- column_family->db = NULL;
692
- column_family->handle = NULL;
728
+ column_family->db = nullptr;
729
+ column_family->handle = nullptr;
693
730
 
694
731
  column_family->descriptor = (rocksdb_column_family_descriptor_t) {
695
732
  name,
@@ -711,7 +748,7 @@ rocksdb_native_column_family_init(
711
748
  rocksdb_pinning_tier_t(unpartitioned_pinning_tier),
712
749
  optimize_filters_for_hits,
713
750
  num_levels,
714
- max_write_buffer_number
751
+ max_write_buffer_number,
715
752
  }
716
753
  };
717
754
 
@@ -725,7 +762,7 @@ rocksdb_native_column_family_destroy(
725
762
  ) {
726
763
  int err;
727
764
 
728
- if (column_family->handle == NULL) return;
765
+ if (column_family->handle == nullptr) return;
729
766
 
730
767
  err = rocksdb_column_family_destroy(column_family->db, column_family->handle);
731
768
  assert(err == 0);
@@ -735,7 +772,7 @@ rocksdb_native_column_family_destroy(
735
772
 
736
773
  column_family->ctx.reset();
737
774
 
738
- column_family->handle = NULL;
775
+ column_family->handle = nullptr;
739
776
  }
740
777
 
741
778
  static js_arraybuffer_t
@@ -772,11 +809,11 @@ rocksdb_native_iterator_buffer(
772
809
 
773
810
  size_t offset = 0;
774
811
 
775
- req->keys = (rocksdb_slice_t *) &data[offset];
812
+ req->keys = reinterpret_cast<rocksdb_slice_t *>(&data[offset]);
776
813
 
777
814
  offset += capacity * sizeof(rocksdb_slice_t);
778
815
 
779
- req->values = (rocksdb_slice_t *) &data[offset];
816
+ req->values = reinterpret_cast<rocksdb_slice_t *>(&data[offset]);
780
817
 
781
818
  return handle;
782
819
  }
@@ -787,13 +824,13 @@ rocksdb_native__on_iterator_close(rocksdb_iterator_t *handle, int status) {
787
824
 
788
825
  assert(status == 0);
789
826
 
790
- rocksdb_native_iterator_t *req = (rocksdb_native_iterator_t *) handle->data;
827
+ auto req = reinterpret_cast<rocksdb_native_iterator_t *>(handle->data);
791
828
 
792
829
  req->active = false;
793
830
 
794
- js_env_t *env = req->env;
831
+ auto env = req->env;
795
832
 
796
- js_deferred_teardown_t *teardown = req->teardown;
833
+ auto teardown = req->teardown;
797
834
 
798
835
  if (req->exiting) {
799
836
  req->on_open.reset();
@@ -809,7 +846,7 @@ rocksdb_native__on_iterator_close(rocksdb_iterator_t *handle, int status) {
809
846
  err = js_get_reference_value(env, req->ctx, ctx);
810
847
  assert(err == 0);
811
848
 
812
- cb_on_iterator_close_t cb;
849
+ rocksdb_native_on_iterator_close_t cb;
813
850
  err = js_get_reference_value(env, req->on_close, cb);
814
851
  assert(err == 0);
815
852
 
@@ -841,7 +878,7 @@ rocksdb_native__on_iterator_open(rocksdb_iterator_t *handle, int status) {
841
878
 
842
879
  assert(status == 0);
843
880
 
844
- rocksdb_native_iterator_t *req = (rocksdb_native_iterator_t *) handle->data;
881
+ auto req = reinterpret_cast<rocksdb_native_iterator_t *>(handle->data);
845
882
 
846
883
  req->active = false;
847
884
 
@@ -849,7 +886,7 @@ rocksdb_native__on_iterator_open(rocksdb_iterator_t *handle, int status) {
849
886
  err = rocksdb_iterator_close(&req->handle, rocksdb_native__on_iterator_close);
850
887
  assert(err == 0);
851
888
  } else {
852
- js_env_t *env = req->env;
889
+ auto env = req->env;
853
890
 
854
891
  js_handle_scope_t *scope;
855
892
  err = js_open_handle_scope(env, &scope);
@@ -859,7 +896,7 @@ rocksdb_native__on_iterator_open(rocksdb_iterator_t *handle, int status) {
859
896
  err = js_get_reference_value(env, req->ctx, ctx);
860
897
  assert(err == 0);
861
898
 
862
- cb_on_iterator_open_t cb;
899
+ rocksdb_native_on_iterator_open_t cb;
863
900
  err = js_get_reference_value(env, req->on_open, cb);
864
901
  assert(err == 0);
865
902
 
@@ -881,7 +918,7 @@ static void
881
918
  rocksdb_native__on_iterator_teardown(js_deferred_teardown_t *handle, void *data) {
882
919
  int err;
883
920
 
884
- rocksdb_native_iterator_t *req = (rocksdb_native_iterator_t *) data;
921
+ auto req = reinterpret_cast<rocksdb_native_iterator_t *>(data);
885
922
 
886
923
  req->exiting = true;
887
924
 
@@ -905,14 +942,12 @@ rocksdb_native_iterator_open(
905
942
  bool keys_only,
906
943
  std::optional<js_arraybuffer_t> snapshot,
907
944
  js_receiver_t ctx,
908
- cb_on_iterator_open_t on_open,
909
- cb_on_iterator_close_t on_close,
910
- cb_on_iterator_read_t on_read
945
+ rocksdb_native_on_iterator_open_t on_open,
946
+ rocksdb_native_on_iterator_close_t on_close,
947
+ rocksdb_native_on_iterator_read_t on_read
911
948
  ) {
912
949
  int err;
913
950
 
914
- req->active = true;
915
-
916
951
  rocksdb_range_t range;
917
952
 
918
953
  err = js_get_typedarray_info(env, gt, range.gt.data, range.gt.len);
@@ -938,6 +973,17 @@ rocksdb_native_iterator_open(
938
973
  assert(err == 0);
939
974
  }
940
975
 
976
+ err = rocksdb_iterator_open(&db->handle, &req->handle, column_family->handle, range, &options, rocksdb_native__on_iterator_open);
977
+
978
+ if (err < 0) {
979
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
980
+ assert(err == 0);
981
+
982
+ throw js_pending_exception;
983
+ }
984
+
985
+ req->active = true;
986
+
941
987
  err = js_create_reference(env, ctx, req->ctx);
942
988
  assert(err == 0);
943
989
 
@@ -950,10 +996,7 @@ rocksdb_native_iterator_open(
950
996
  err = js_create_reference(env, on_read, req->on_read);
951
997
  assert(err == 0);
952
998
 
953
- err = rocksdb_iterator_open(&db->handle, &req->handle, column_family->handle, range, &options, rocksdb_native__on_iterator_open);
954
- assert(err == 0);
955
-
956
- err = js_add_deferred_teardown_callback(env, rocksdb_native__on_iterator_teardown, (void *) req, &req->teardown);
999
+ err = js_add_deferred_teardown_callback(env, rocksdb_native__on_iterator_teardown, req, &req->teardown);
957
1000
  assert(err == 0);
958
1001
  }
959
1002
 
@@ -961,20 +1004,16 @@ static void
961
1004
  rocksdb_native_iterator_close(js_env_t *env, js_arraybuffer_span_of_t<rocksdb_native_iterator_t, 1> req) {
962
1005
  int err;
963
1006
 
964
- req->active = true;
965
-
966
1007
  err = rocksdb_iterator_close(&req->handle, rocksdb_native__on_iterator_close);
967
- assert(err == 0);
968
- }
969
1008
 
970
- static int
971
- rocksdb_native_try_create_external_arraybuffer(js_env_t *env, char *data, size_t len, js_arraybuffer_t &result) {
972
- int err;
1009
+ if (err < 0) {
1010
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1011
+ assert(err == 0);
973
1012
 
974
- err = js_create_external_arraybuffer<rocksdb_native__on_free>(env, data, len, result);
975
- if (err == 0) return 0;
1013
+ throw js_pending_exception;
1014
+ }
976
1015
 
977
- return js_create_arraybuffer(env, data, len, result);
1016
+ req->active = true;
978
1017
  }
979
1018
 
980
1019
  static void
@@ -983,16 +1022,16 @@ rocksdb_native__on_iterator_read(rocksdb_iterator_t *handle, int status) {
983
1022
 
984
1023
  assert(status == 0);
985
1024
 
986
- rocksdb_native_iterator_t *req = (rocksdb_native_iterator_t *) handle->data;
1025
+ auto req = reinterpret_cast<rocksdb_native_iterator_t *>(handle->data);
987
1026
 
988
1027
  req->active = false;
989
1028
 
990
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
1029
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
991
1030
 
992
1031
  size_t len = req->handle.len;
993
1032
 
994
1033
  if (db->exiting) {
995
- if (status == 0 && req->handle.error == NULL) {
1034
+ if (status == 0 && req->handle.error == nullptr) {
996
1035
  for (size_t i = 0; i < len; i++) {
997
1036
  js_value_t *result;
998
1037
 
@@ -1005,7 +1044,7 @@ rocksdb_native__on_iterator_read(rocksdb_iterator_t *handle, int status) {
1005
1044
  err = rocksdb_iterator_close(&req->handle, rocksdb_native__on_iterator_close);
1006
1045
  assert(err == 0);
1007
1046
  } else {
1008
- js_env_t *env = req->env;
1047
+ auto env = req->env;
1009
1048
 
1010
1049
  js_handle_scope_t *scope;
1011
1050
  err = js_open_handle_scope(env, &scope);
@@ -1015,7 +1054,7 @@ rocksdb_native__on_iterator_read(rocksdb_iterator_t *handle, int status) {
1015
1054
  err = js_get_reference_value(env, req->ctx, ctx);
1016
1055
  assert(err == 0);
1017
1056
 
1018
- cb_on_iterator_read_t cb;
1057
+ rocksdb_native_on_iterator_read_t cb;
1019
1058
  err = js_get_reference_value(env, req->on_read, cb);
1020
1059
  assert(err == 0);
1021
1060
 
@@ -1036,14 +1075,14 @@ rocksdb_native__on_iterator_read(rocksdb_iterator_t *handle, int status) {
1036
1075
 
1037
1076
  rocksdb_slice_t *key = &req->keys[i];
1038
1077
 
1039
- err = rocksdb_native_try_create_external_arraybuffer(env, const_cast<char *>(key->data), key->len, result);
1078
+ err = rocksdb_native__try_create_external_arraybuffer(env, const_cast<char *>(key->data), key->len, result);
1040
1079
  assert(err == 0);
1041
1080
 
1042
1081
  keys.push_back(result);
1043
1082
 
1044
1083
  rocksdb_slice_t *value = &req->values[i];
1045
1084
 
1046
- err = rocksdb_native_try_create_external_arraybuffer(env, const_cast<char *>(value->data), value->len, result);
1085
+ err = rocksdb_native__try_create_external_arraybuffer(env, const_cast<char *>(value->data), value->len, result);
1047
1086
  assert(err == 0);
1048
1087
 
1049
1088
  values.push_back(result);
@@ -1065,10 +1104,16 @@ rocksdb_native_iterator_read(
1065
1104
  ) {
1066
1105
  int err;
1067
1106
 
1068
- req->active = true;
1069
-
1070
1107
  err = rocksdb_iterator_read(&req->handle, req->keys, req->values, capacity, rocksdb_native__on_iterator_read);
1071
- assert(err == 0);
1108
+
1109
+ if (err < 0) {
1110
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1111
+ assert(err == 0);
1112
+
1113
+ throw js_pending_exception;
1114
+ }
1115
+
1116
+ req->active = true;
1072
1117
  }
1073
1118
 
1074
1119
  static js_arraybuffer_t
@@ -1113,11 +1158,11 @@ rocksdb_native__on_read(rocksdb_read_batch_t *handle, int status) {
1113
1158
 
1114
1159
  assert(status == 0);
1115
1160
 
1116
- rocksdb_native_read_batch_t *req = (rocksdb_native_read_batch_t *) handle->data;
1161
+ auto req = reinterpret_cast<rocksdb_native_read_batch_t *>(handle->data);
1117
1162
 
1118
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
1163
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
1119
1164
 
1120
- js_env_t *env = req->env;
1165
+ auto env = req->env;
1121
1166
 
1122
1167
  size_t len = req->handle.len;
1123
1168
 
@@ -1163,11 +1208,11 @@ rocksdb_native__on_read(rocksdb_read_batch_t *handle, int status) {
1163
1208
 
1164
1209
  rocksdb_slice_t *slice = &req->reads[i].value;
1165
1210
 
1166
- if (slice->data == NULL && slice->len == (size_t) -1) {
1167
- err = js_get_null(env, (js_value_t **) result);
1211
+ if (slice->data == nullptr && slice->len == size_t(-1)) {
1212
+ err = js_get_null(env, static_cast<js_value_t **>(result));
1168
1213
  assert(err == 0);
1169
1214
  } else {
1170
- err = rocksdb_native_try_create_external_arraybuffer(env, const_cast<char *>(slice->data), slice->len, result);
1215
+ err = rocksdb_native__try_create_external_arraybuffer(env, const_cast<char *>(slice->data), slice->len, result);
1171
1216
  assert(err == 0);
1172
1217
  }
1173
1218
 
@@ -1180,7 +1225,7 @@ rocksdb_native__on_read(rocksdb_read_batch_t *handle, int status) {
1180
1225
  err = js_get_reference_value(env, req->ctx, ctx);
1181
1226
  assert(err == 0);
1182
1227
 
1183
- cb_on_read_t cb;
1228
+ rocksdb_native_on_read_t cb;
1184
1229
  err = js_get_reference_value(env, req->on_read, cb);
1185
1230
  assert(err == 0);
1186
1231
 
@@ -1204,18 +1249,11 @@ rocksdb_native_read(
1204
1249
  bool async_io,
1205
1250
  bool fill_cache,
1206
1251
  js_receiver_t ctx,
1207
- cb_on_read_t on_read
1252
+ rocksdb_native_on_read_t on_read
1208
1253
  ) {
1209
1254
  int err;
1210
1255
 
1211
- err = js_create_reference(env, ctx, req->ctx);
1212
- assert(err == 0);
1213
-
1214
- err = js_create_reference(env, on_read, req->on_read);
1215
- assert(err == 0);
1216
-
1217
1256
  std::vector<js_object_t> elements;
1218
-
1219
1257
  err = js_get_array_elements(env, operations, elements);
1220
1258
  assert(err == 0);
1221
1259
 
@@ -1268,6 +1306,18 @@ rocksdb_native_read(
1268
1306
  }
1269
1307
 
1270
1308
  err = rocksdb_read(&db->handle, &req->handle, req->reads, len, &options, rocksdb_native__on_read);
1309
+
1310
+ if (err < 0) {
1311
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1312
+ assert(err == 0);
1313
+
1314
+ throw js_pending_exception;
1315
+ }
1316
+
1317
+ err = js_create_reference(env, ctx, req->ctx);
1318
+ assert(err == 0);
1319
+
1320
+ err = js_create_reference(env, on_read, req->on_read);
1271
1321
  assert(err == 0);
1272
1322
  }
1273
1323
 
@@ -1313,11 +1363,11 @@ rocksdb_native__on_write(rocksdb_write_batch_t *handle, int status) {
1313
1363
 
1314
1364
  assert(status == 0);
1315
1365
 
1316
- rocksdb_native_write_batch_t *req = (rocksdb_native_write_batch_t *) handle->data;
1366
+ auto req = reinterpret_cast<rocksdb_native_write_batch_t *>(handle->data);
1317
1367
 
1318
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
1368
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
1319
1369
 
1320
- js_env_t *env = req->env;
1370
+ auto env = req->env;
1321
1371
 
1322
1372
  if (db->exiting) {
1323
1373
  req->on_write.reset();
@@ -1338,7 +1388,7 @@ rocksdb_native__on_write(rocksdb_write_batch_t *handle, int status) {
1338
1388
  err = js_get_reference_value(env, req->ctx, ctx);
1339
1389
  assert(err == 0);
1340
1390
 
1341
- cb_on_write_t cb;
1391
+ rocksdb_native_on_write_t cb;
1342
1392
  err = js_get_reference_value(env, req->on_write, cb);
1343
1393
  assert(err == 0);
1344
1394
 
@@ -1359,18 +1409,11 @@ rocksdb_native_write(
1359
1409
  js_arraybuffer_span_of_t<rocksdb_native_write_batch_t, 1> req,
1360
1410
  js_array_t operations,
1361
1411
  js_receiver_t ctx,
1362
- cb_on_write_t on_write
1412
+ rocksdb_native_on_write_t on_write
1363
1413
  ) {
1364
1414
  int err;
1365
1415
 
1366
- err = js_create_reference(env, ctx, req->ctx);
1367
- assert(err == 0);
1368
-
1369
- err = js_create_reference(env, on_write, req->on_write);
1370
- assert(err == 0);
1371
-
1372
1416
  std::vector<js_object_t> elements;
1373
-
1374
1417
  err = js_get_array_elements(env, operations, elements);
1375
1418
  assert(err == 0);
1376
1419
 
@@ -1449,7 +1492,19 @@ rocksdb_native_write(
1449
1492
  }
1450
1493
  }
1451
1494
 
1452
- err = rocksdb_write(&db->handle, &req->handle, req->writes, len, NULL, rocksdb_native__on_write);
1495
+ err = rocksdb_write(&db->handle, &req->handle, req->writes, len, nullptr, rocksdb_native__on_write);
1496
+
1497
+ if (err < 0) {
1498
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1499
+ assert(err == 0);
1500
+
1501
+ throw js_pending_exception;
1502
+ }
1503
+
1504
+ err = js_create_reference(env, ctx, req->ctx);
1505
+ assert(err == 0);
1506
+
1507
+ err = js_create_reference(env, on_write, req->on_write);
1453
1508
  assert(err == 0);
1454
1509
  }
1455
1510
 
@@ -1459,11 +1514,11 @@ rocksdb_native__on_flush(rocksdb_flush_t *handle, int status) {
1459
1514
 
1460
1515
  assert(status == 0);
1461
1516
 
1462
- rocksdb_native_flush_t *req = (rocksdb_native_flush_t *) handle->data;
1517
+ auto req = reinterpret_cast<rocksdb_native_flush_t *>(handle->data);
1463
1518
 
1464
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
1519
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
1465
1520
 
1466
- js_env_t *env = req->env;
1521
+ auto env = req->env;
1467
1522
 
1468
1523
  if (db->exiting) {
1469
1524
  req->on_flush.reset();
@@ -1484,7 +1539,7 @@ rocksdb_native__on_flush(rocksdb_flush_t *handle, int status) {
1484
1539
  err = js_get_reference_value(env, req->ctx, ctx);
1485
1540
  assert(err == 0);
1486
1541
 
1487
- cb_on_flush_t cb;
1542
+ rocksdb_native_on_flush_t cb;
1488
1543
  err = js_get_reference_value(env, req->on_flush, cb);
1489
1544
  assert(err == 0);
1490
1545
 
@@ -1504,7 +1559,7 @@ rocksdb_native_flush(
1504
1559
  js_arraybuffer_span_of_t<rocksdb_native_t, 1> db,
1505
1560
  js_arraybuffer_span_of_t<rocksdb_native_column_family_t, 1> column_family,
1506
1561
  js_receiver_t ctx,
1507
- cb_on_flush_t on_flush
1562
+ rocksdb_native_on_flush_t on_flush
1508
1563
  ) {
1509
1564
  int err;
1510
1565
 
@@ -1515,52 +1570,37 @@ rocksdb_native_flush(
1515
1570
  assert(err == 0);
1516
1571
 
1517
1572
  req->env = env;
1518
- req->handle.data = (void *) req;
1519
-
1520
- err = js_create_reference(env, ctx, req->ctx);
1521
- assert(err == 0);
1522
-
1523
- err = js_create_reference(env, on_flush, req->on_flush);
1524
- assert(err == 0);
1525
-
1526
- err = rocksdb_flush(&db->handle, &req->handle, column_family->handle, NULL, rocksdb_native__on_flush);
1527
- assert(err == 0);
1573
+ req->handle.data = req;
1528
1574
 
1529
- return handle;
1530
- }
1575
+ err = rocksdb_flush(&db->handle, &req->handle, column_family->handle, nullptr, rocksdb_native__on_flush);
1531
1576
 
1532
- static js_arraybuffer_t
1533
- rocksdb_native_snapshot_create(js_env_t *env, js_arraybuffer_span_of_t<rocksdb_native_t, 1> db) {
1534
- int err;
1577
+ if (err < 0) {
1578
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1579
+ assert(err == 0);
1535
1580
 
1536
- js_arraybuffer_t handle;
1581
+ throw js_pending_exception;
1582
+ }
1537
1583
 
1538
- rocksdb_native_snapshot_t *snapshot;
1539
- err = js_create_arraybuffer(env, snapshot, handle);
1584
+ err = js_create_reference(env, ctx, req->ctx);
1540
1585
  assert(err == 0);
1541
1586
 
1542
- err = rocksdb_snapshot_create(&db->handle, &snapshot->handle);
1587
+ err = js_create_reference(env, on_flush, req->on_flush);
1543
1588
  assert(err == 0);
1544
1589
 
1545
1590
  return handle;
1546
1591
  }
1547
1592
 
1548
- static void
1549
- rocksdb_native_snapshot_destroy(js_env_t *env, js_arraybuffer_span_of_t<rocksdb_native_snapshot_t, 1> snapshot) {
1550
- rocksdb_snapshot_destroy(&snapshot->handle);
1551
- }
1552
-
1553
1593
  static void
1554
1594
  rocksdb_native__on_compact_range(rocksdb_compact_range_t *handle, int status) {
1555
1595
  int err;
1556
1596
 
1557
1597
  assert(status == 0);
1558
1598
 
1559
- rocksdb_native_compact_range_t *req = (rocksdb_native_compact_range_t *) handle->data;
1599
+ auto req = reinterpret_cast<rocksdb_native_compact_range_t *>(handle->data);
1560
1600
 
1561
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
1601
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
1562
1602
 
1563
- js_env_t *env = req->env;
1603
+ auto env = req->env;
1564
1604
 
1565
1605
  if (db->exiting) {
1566
1606
  req->on_compact_range.reset();
@@ -1581,7 +1621,7 @@ rocksdb_native__on_compact_range(rocksdb_compact_range_t *handle, int status) {
1581
1621
  err = js_get_reference_value(env, req->ctx, ctx);
1582
1622
  assert(err == 0);
1583
1623
 
1584
- cb_on_compact_range_t cb;
1624
+ rocksdb_native_on_compact_range_t cb;
1585
1625
  err = js_get_reference_value(env, req->on_compact_range, cb);
1586
1626
  assert(err == 0);
1587
1627
 
@@ -1604,10 +1644,18 @@ rocksdb_native_compact_range(
1604
1644
  js_typedarray_t<> end,
1605
1645
  bool exclusive,
1606
1646
  js_receiver_t ctx,
1607
- cb_on_compact_range_t on_compact_range
1647
+ rocksdb_native_on_compact_range_t on_compact_range
1608
1648
  ) {
1609
1649
  int err;
1610
1650
 
1651
+ rocksdb_slice_t start_slice;
1652
+ err = js_get_typedarray_info(env, start, start_slice.data, start_slice.len);
1653
+ assert(err == 0);
1654
+
1655
+ rocksdb_slice_t end_slice;
1656
+ err = js_get_typedarray_info(env, end, end_slice.data, end_slice.len);
1657
+ assert(err == 0);
1658
+
1611
1659
  js_arraybuffer_t handle;
1612
1660
 
1613
1661
  rocksdb_native_compact_range_t *req;
@@ -1615,28 +1663,26 @@ rocksdb_native_compact_range(
1615
1663
  assert(err == 0);
1616
1664
 
1617
1665
  req->env = env;
1618
- req->handle.data = (void *) req;
1619
-
1620
- err = js_create_reference(env, ctx, req->ctx);
1621
- assert(err == 0);
1622
-
1623
- err = js_create_reference(env, on_compact_range, req->on_compact_range);
1624
- assert(err == 0);
1666
+ req->handle.data = req;
1625
1667
 
1626
1668
  rocksdb_compact_range_options_t options = {
1627
1669
  .version = 0,
1628
1670
  .exclusive_manual_compaction = exclusive
1629
1671
  };
1630
1672
 
1631
- rocksdb_slice_t start_slice;
1632
- err = js_get_typedarray_info(env, start, start_slice.data, start_slice.len);
1633
- assert(err == 0);
1673
+ err = rocksdb_compact_range(&db->handle, &req->handle, column_family->handle, start_slice, end_slice, &options, rocksdb_native__on_compact_range);
1634
1674
 
1635
- rocksdb_slice_t end_slice;
1636
- err = js_get_typedarray_info(env, end, end_slice.data, end_slice.len);
1675
+ if (err < 0) {
1676
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1677
+ assert(err == 0);
1678
+
1679
+ throw js_pending_exception;
1680
+ }
1681
+
1682
+ err = js_create_reference(env, ctx, req->ctx);
1637
1683
  assert(err == 0);
1638
1684
 
1639
- err = rocksdb_compact_range(&db->handle, &req->handle, column_family->handle, start_slice, end_slice, &options, rocksdb_native__on_compact_range);
1685
+ err = js_create_reference(env, on_compact_range, req->on_compact_range);
1640
1686
  assert(err == 0);
1641
1687
 
1642
1688
  return handle;
@@ -1648,11 +1694,11 @@ rocksdb_native__on_approximate_size(rocksdb_approximate_size_t *handle, int stat
1648
1694
 
1649
1695
  assert(status == 0);
1650
1696
 
1651
- rocksdb_native_approximate_size_t *req = (rocksdb_native_approximate_size_t *) handle->data;
1697
+ auto req = reinterpret_cast<rocksdb_native_approximate_size_t *>(handle->data);
1652
1698
 
1653
- rocksdb_native_t *db = (rocksdb_native_t *) req->handle.req.db;
1699
+ auto db = reinterpret_cast<rocksdb_native_t *>(req->handle.req.db);
1654
1700
 
1655
- js_env_t *env = req->env;
1701
+ auto env = req->env;
1656
1702
 
1657
1703
  if (db->exiting) {
1658
1704
  req->on_approximate_size.reset();
@@ -1673,7 +1719,7 @@ rocksdb_native__on_approximate_size(rocksdb_approximate_size_t *handle, int stat
1673
1719
  err = js_get_reference_value(env, req->ctx, ctx);
1674
1720
  assert(err == 0);
1675
1721
 
1676
- cb_on_approximate_size_t cb;
1722
+ rocksdb_native_on_approximate_size_t cb;
1677
1723
  err = js_get_reference_value(env, req->on_approximate_size, cb);
1678
1724
  assert(err == 0);
1679
1725
 
@@ -1698,10 +1744,18 @@ rocksdb_native_approximate_size(
1698
1744
  bool include_files,
1699
1745
  double error_margin,
1700
1746
  js_receiver_t ctx,
1701
- cb_on_approximate_size_t on_approximate_size
1747
+ rocksdb_native_on_approximate_size_t on_approximate_size
1702
1748
  ) {
1703
1749
  int err;
1704
1750
 
1751
+ rocksdb_slice_t start_slice;
1752
+ err = js_get_typedarray_info(env, start, start_slice.data, start_slice.len);
1753
+ assert(err == 0);
1754
+
1755
+ rocksdb_slice_t end_slice;
1756
+ err = js_get_typedarray_info(env, end, end_slice.data, end_slice.len);
1757
+ assert(err == 0);
1758
+
1705
1759
  js_arraybuffer_t handle;
1706
1760
 
1707
1761
  rocksdb_native_approximate_size_t *req;
@@ -1709,13 +1763,7 @@ rocksdb_native_approximate_size(
1709
1763
  assert(err == 0);
1710
1764
 
1711
1765
  req->env = env;
1712
- req->handle.data = (void *) req;
1713
-
1714
- err = js_create_reference(env, ctx, req->ctx);
1715
- assert(err == 0);
1716
-
1717
- err = js_create_reference(env, on_approximate_size, req->on_approximate_size);
1718
- assert(err == 0);
1766
+ req->handle.data = req;
1719
1767
 
1720
1768
  rocksdb_approximate_size_options_t options = {
1721
1769
  .version = 0,
@@ -1724,20 +1772,51 @@ rocksdb_native_approximate_size(
1724
1772
  .files_size_error_margin = error_margin,
1725
1773
  };
1726
1774
 
1727
- rocksdb_slice_t start_slice;
1728
- err = js_get_typedarray_info(env, start, start_slice.data, start_slice.len);
1775
+ err = rocksdb_approximate_size(&db->handle, &req->handle, column_family->handle, start_slice, end_slice, &options, rocksdb_native__on_approximate_size);
1776
+
1777
+ if (err < 0) {
1778
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1779
+ assert(err == 0);
1780
+
1781
+ throw js_pending_exception;
1782
+ }
1783
+
1784
+ err = js_create_reference(env, ctx, req->ctx);
1729
1785
  assert(err == 0);
1730
1786
 
1731
- rocksdb_slice_t end_slice;
1732
- err = js_get_typedarray_info(env, end, end_slice.data, end_slice.len);
1787
+ err = js_create_reference(env, on_approximate_size, req->on_approximate_size);
1733
1788
  assert(err == 0);
1734
1789
 
1735
- err = rocksdb_approximate_size(&db->handle, &req->handle, column_family->handle, start_slice, end_slice, &options, rocksdb_native__on_approximate_size);
1790
+ return handle;
1791
+ }
1792
+
1793
+ static js_arraybuffer_t
1794
+ rocksdb_native_snapshot_create(js_env_t *env, js_arraybuffer_span_of_t<rocksdb_native_t, 1> db) {
1795
+ int err;
1796
+
1797
+ js_arraybuffer_t handle;
1798
+
1799
+ rocksdb_native_snapshot_t *snapshot;
1800
+ err = js_create_arraybuffer(env, snapshot, handle);
1736
1801
  assert(err == 0);
1737
1802
 
1803
+ err = rocksdb_snapshot_create(&db->handle, &snapshot->handle);
1804
+
1805
+ if (err < 0) {
1806
+ err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
1807
+ assert(err == 0);
1808
+
1809
+ throw js_pending_exception;
1810
+ }
1811
+
1738
1812
  return handle;
1739
1813
  }
1740
1814
 
1815
+ static void
1816
+ rocksdb_native_snapshot_destroy(js_env_t *env, js_arraybuffer_span_of_t<rocksdb_native_snapshot_t, 1> snapshot) {
1817
+ rocksdb_snapshot_destroy(&snapshot->handle);
1818
+ }
1819
+
1741
1820
  static js_value_t *
1742
1821
  rocksdb_native_exports(js_env_t *env, js_value_t *exports) {
1743
1822
  int err;
@@ -1770,12 +1849,11 @@ rocksdb_native_exports(js_env_t *env, js_value_t *exports) {
1770
1849
  V("iteratorRead", rocksdb_native_iterator_read)
1771
1850
 
1772
1851
  V("flush", rocksdb_native_flush)
1852
+ V("compactRange", rocksdb_native_compact_range)
1853
+ V("approximateSize", rocksdb_native_approximate_size)
1773
1854
 
1774
1855
  V("snapshotCreate", rocksdb_native_snapshot_create)
1775
1856
  V("snapshotDestroy", rocksdb_native_snapshot_destroy)
1776
-
1777
- V("compactRange", rocksdb_native_compact_range)
1778
- V("approximateSize", rocksdb_native_approximate_size)
1779
1857
  #undef V
1780
1858
 
1781
1859
  #define V(name, n) \
package/lib/iterator.js CHANGED
@@ -137,7 +137,8 @@ module.exports = class RocksDBIterator extends Readable {
137
137
  )
138
138
  } catch (err) {
139
139
  this._db._state.io.dec()
140
- throw err
140
+
141
+ cb(err)
141
142
  }
142
143
  }
143
144
 
@@ -160,7 +161,8 @@ module.exports = class RocksDBIterator extends Readable {
160
161
  binding.iteratorRead(this._handle, Math.min(this._capacity, this._limit))
161
162
  } catch (err) {
162
163
  this._db._state.io.dec()
163
- throw err
164
+
165
+ cb(err)
164
166
  }
165
167
  }
166
168
 
@@ -169,19 +171,22 @@ module.exports = class RocksDBIterator extends Readable {
169
171
 
170
172
  this._db._state.io.inc()
171
173
 
172
- this._pendingDestroy = cb
173
-
174
174
  if (this._opened === false) {
175
175
  this._db._state.io.dec()
176
+ this._db._unref()
176
177
 
177
- return this._onclose(null)
178
+ return cb(null)
178
179
  }
179
180
 
181
+ this._pendingDestroy = cb
182
+
180
183
  try {
181
184
  binding.iteratorClose(this._handle)
182
185
  } catch (err) {
183
186
  this._db._state.io.dec()
184
- throw err
187
+ this._db._unref()
188
+
189
+ cb(err)
185
190
  }
186
191
  }
187
192
 
package/lib/state.js CHANGED
@@ -83,6 +83,7 @@ module.exports = class RocksDBState extends ReadyResource {
83
83
  }
84
84
 
85
85
  freeBatch(batch, writable) {
86
+ if (batch._capacity > 16) return
86
87
  const queue = writable ? this._writeBatches : this._readBatches
87
88
  if (queue.length >= MAX_BATCH_REUSE) return
88
89
  queue.push(batch)
@@ -320,7 +321,10 @@ module.exports = class RocksDBState extends ReadyResource {
320
321
 
321
322
  const { exclusive = false } = opts
322
323
 
323
- const req = { resolve: null, reject: null, handle: null }
324
+ start = this._encodeKey(start)
325
+ end = this._encodeKey(end)
326
+
327
+ const req = { resolve: null, reject: null, handle: null, start, end }
324
328
 
325
329
  const promise = new Promise((resolve, reject) => {
326
330
  req.resolve = resolve
@@ -331,8 +335,8 @@ module.exports = class RocksDBState extends ReadyResource {
331
335
  req.handle = binding.compactRange(
332
336
  this._handle,
333
337
  db._columnFamily._handle,
334
- this._encodeKey(start),
335
- this._encodeKey(end),
338
+ start,
339
+ end,
336
340
  exclusive,
337
341
  req,
338
342
  oncompactrange
@@ -356,7 +360,10 @@ module.exports = class RocksDBState extends ReadyResource {
356
360
 
357
361
  const { includeMemtables = false, includeFiles = true, filesSizeErrorMargin = -1 } = opts
358
362
 
359
- const req = { resolve: null, reject: null, handle: null }
363
+ start = this._encodeKey(start)
364
+ end = this._encodeKey(end)
365
+
366
+ const req = { resolve: null, reject: null, handle: null, start, end }
360
367
 
361
368
  const promise = new Promise((resolve, reject) => {
362
369
  req.resolve = resolve
@@ -367,8 +374,8 @@ module.exports = class RocksDBState extends ReadyResource {
367
374
  req.handle = binding.approximateSize(
368
375
  this._handle,
369
376
  db._columnFamily._handle,
370
- this._encodeKey(start),
371
- this._encodeKey(end),
377
+ start,
378
+ end,
372
379
  includeMemtables,
373
380
  includeFiles,
374
381
  filesSizeErrorMargin,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rocksdb-native",
3
- "version": "3.9.1",
3
+ "version": "3.9.3",
4
4
  "description": "librocksdb bindings for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",