@nxtedition/rocksdb 12.0.2 → 12.0.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/chained-batch.js CHANGED
@@ -4,6 +4,7 @@ const { AbstractChainedBatch } = require('abstract-level')
4
4
  const binding = require('./binding')
5
5
  const ModuleError = require('module-error')
6
6
  const { fromCallback } = require('catering')
7
+ const assert = require('node:assert')
7
8
 
8
9
  const kBatchContext = Symbol('batchContext')
9
10
  const kDbContext = Symbol('dbContext')
@@ -20,6 +21,8 @@ class ChainedBatch extends AbstractChainedBatch {
20
21
  }
21
22
 
22
23
  _put (key, value, options) {
24
+ assert(this[kBatchContext])
25
+
23
26
  if (key === null || key === undefined) {
24
27
  throw new ModuleError('Key cannot be null or undefined', {
25
28
  code: 'LEVEL_INVALID_KEY'
@@ -39,6 +42,8 @@ class ChainedBatch extends AbstractChainedBatch {
39
42
  }
40
43
 
41
44
  _del (key, options) {
45
+ assert(this[kBatchContext])
46
+
42
47
  if (key === null || key === undefined) {
43
48
  throw new ModuleError('Key cannot be null or undefined', {
44
49
  code: 'LEVEL_INVALID_KEY'
@@ -51,10 +56,14 @@ class ChainedBatch extends AbstractChainedBatch {
51
56
  }
52
57
 
53
58
  _clear () {
59
+ assert(this[kBatchContext])
60
+
54
61
  binding.batch_clear(this[kBatchContext])
55
62
  }
56
63
 
57
64
  _write (options, callback) {
65
+ assert(this[kBatchContext])
66
+
58
67
  callback = fromCallback(callback, kPromise)
59
68
 
60
69
  try {
@@ -68,19 +77,33 @@ class ChainedBatch extends AbstractChainedBatch {
68
77
  }
69
78
 
70
79
  _writeSync (options) {
80
+ assert(this[kBatchContext])
81
+
71
82
  binding.batch_write(this[kDbContext], this[kBatchContext], options ?? EMPTY)
72
83
  }
73
84
 
74
85
  _close (callback) {
75
- binding.batch_clear(this[kBatchContext])
86
+ this._closeSync()
87
+
76
88
  process.nextTick(callback)
77
89
  }
78
90
 
91
+ _closeSync () {
92
+ if (this[kBatchContext]) {
93
+ binding.batch_clear(this[kBatchContext])
94
+ this[kBatchContext] = null
95
+ }
96
+ }
97
+
79
98
  get length () {
99
+ assert(this[kBatchContext])
100
+
80
101
  return binding.batch_count(this[kBatchContext])
81
102
  }
82
103
 
83
104
  _merge (key, value, options) {
105
+ assert(this[kBatchContext])
106
+
84
107
  if (key === null || key === undefined) {
85
108
  throw new ModuleError('Key cannot be null or undefined', {
86
109
  code: 'LEVEL_INVALID_KEY'
@@ -111,6 +134,8 @@ class ChainedBatch extends AbstractChainedBatch {
111
134
  }
112
135
 
113
136
  toArray (options) {
137
+ assert(this[kBatchContext])
138
+
114
139
  return binding.batch_iterate(this[kDbContext], this[kBatchContext], {
115
140
  keys: true,
116
141
  values: true,
package/iterator.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const { fromCallback } = require('catering')
4
4
  const { AbstractIterator } = require('abstract-level')
5
+ const assert = require('node:assert')
5
6
 
6
7
  const binding = require('./binding')
7
8
 
@@ -26,6 +27,8 @@ class Iterator extends AbstractIterator {
26
27
  }
27
28
 
28
29
  _seek (target) {
30
+ assert(this[kContext])
31
+
29
32
  if (target.length === 0) {
30
33
  throw new Error('cannot seek() to an empty target')
31
34
  }
@@ -39,6 +42,8 @@ class Iterator extends AbstractIterator {
39
42
  }
40
43
 
41
44
  _next (callback) {
45
+ assert(this[kContext])
46
+
42
47
  if (this[kPosition] < this[kCache].length) {
43
48
  const key = this[kCache][this[kPosition]++]
44
49
  const val = this[kCache][this[kPosition]++]
@@ -65,6 +70,8 @@ class Iterator extends AbstractIterator {
65
70
  }
66
71
 
67
72
  _nextv (size, options, callback) {
73
+ assert(this[kContext])
74
+
68
75
  callback = fromCallback(callback, kPromise)
69
76
 
70
77
  if (this[kFinished]) {
@@ -72,28 +79,28 @@ class Iterator extends AbstractIterator {
72
79
  } else {
73
80
  this[kFirst] = false
74
81
 
75
- setImmediate(() => {
76
- try {
77
- const { rows, finished } = binding.iterator_nextv(this[kContext], size)
82
+ try {
83
+ const { rows, finished } = binding.iterator_nextv(this[kContext], size)
78
84
 
79
- const entries = []
80
- for (let n = 0; n < rows.length; n += 2) {
81
- entries.push([rows[n + 0], rows[n + 1]])
82
- }
85
+ const entries = []
86
+ for (let n = 0; n < rows.length; n += 2) {
87
+ entries.push([rows[n + 0], rows[n + 1]])
88
+ }
83
89
 
84
- this[kFinished] = finished
90
+ this[kFinished] = finished
85
91
 
86
- callback(null, entries, finished)
87
- } catch (err) {
88
- callback(err)
89
- }
90
- })
92
+ process.nextTick(callback, null, entries, finished)
93
+ } catch (err) {
94
+ process.nextTick(callback, err)
95
+ }
91
96
  }
92
97
 
93
98
  return callback[kPromise]
94
99
  }
95
100
 
96
101
  _nextvSync (size, options) {
102
+ assert(this[kContext])
103
+
97
104
  if (this[kFinished]) {
98
105
  return { rows: [], finished: true }
99
106
  }
@@ -117,7 +124,11 @@ class Iterator extends AbstractIterator {
117
124
 
118
125
  _closeSync () {
119
126
  this[kCache] = kEmpty
120
- binding.iterator_close(this[kContext])
127
+
128
+ if (this[kContext]) {
129
+ binding.iterator_close(this[kContext])
130
+ this[kContext] = null
131
+ }
121
132
  }
122
133
 
123
134
  _end (callback) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/rocksdb",
3
- "version": "12.0.2",
3
+ "version": "12.0.4",
4
4
  "description": "A low-level Node.js RocksDB binding",
5
5
  "license": "MIT",
6
6
  "main": "index.js",