@nxtedition/rocksdb 7.0.46 → 7.0.49

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
@@ -10,7 +10,6 @@ const kBatchContext = Symbol('batchContext')
10
10
  const kDbContext = Symbol('dbContext')
11
11
  const kPromise = Symbol('promise')
12
12
 
13
- const NOOP = () => {}
14
13
  const EMPTY = {}
15
14
 
16
15
  class ChainedBatch extends AbstractChainedBatch {
package/common.js ADDED
@@ -0,0 +1,7 @@
1
+ module.exports.AbortError = class AbortError extends Error {
2
+ constructor () {
3
+ super('The operation was aborted')
4
+ this.code = 'ABORT_ERR'
5
+ this.name = 'AbortError'
6
+ }
7
+ }
package/index.js CHANGED
@@ -1,13 +1,16 @@
1
1
  'use strict'
2
2
 
3
3
  const { fromCallback } = require('catering')
4
+ const { AbortError } = require('./common')
4
5
  const { AbstractLevel } = require('abstract-level')
5
6
  const ModuleError = require('module-error')
6
7
  const fs = require('fs')
7
8
  const binding = require('./binding')
8
9
  const { ChainedBatch } = require('./chained-batch')
9
10
  const { Iterator } = require('./iterator')
11
+ const { Readable } = require('readable-stream')
10
12
  const os = require('os')
13
+ const AbortController = require('abort-controller')
11
14
 
12
15
  const kContext = Symbol('context')
13
16
  const kColumns = Symbol('columns')
@@ -108,7 +111,7 @@ class RocksLevel extends AbstractLevel {
108
111
  } catch (err) {
109
112
  process.nextTick(callback, err)
110
113
  }
111
-
114
+
112
115
  return callback[kPromise]
113
116
  }
114
117
 
@@ -281,6 +284,11 @@ class RocksLevel extends AbstractLevel {
281
284
  }
282
285
 
283
286
  async * updates (options) {
287
+ // TODO (fix): Ensure open status etc...?
288
+ if (this.status !== 'open') {
289
+ throw new Error('Database is not open')
290
+ }
291
+
284
292
  if (this.status !== 'open') {
285
293
  throw new ModuleError('Database is not open', {
286
294
  code: 'LEVEL_DATABASE_NOT_OPEN'
@@ -292,12 +300,14 @@ class RocksLevel extends AbstractLevel {
292
300
  keys: options?.keys ?? true,
293
301
  values: options?.values ?? true,
294
302
  data: options?.data ?? true,
295
- column: options?.column ?? null
303
+ live: options?.live ?? false,
304
+ column: options?.column ?? null,
305
+ signal: options?.signal ?? null
296
306
  }
297
307
 
298
308
  // HACK: We don't properly check for nully column in binding.
299
- if (!options.column) {
300
- delete options.column
309
+ if (options.column) {
310
+ throw new TypeError("'column' not supported")
301
311
  }
302
312
 
303
313
  if (typeof options.since !== 'number') {
@@ -320,6 +330,85 @@ class RocksLevel extends AbstractLevel {
320
330
  throw new TypeError("'column' must be nully or a object")
321
331
  }
322
332
 
333
+ if (typeof options.live !== 'boolean') {
334
+ throw new TypeError("'live' must be nully or a boolean")
335
+ }
336
+
337
+ const ac = new AbortController()
338
+ const onAbort = () => {
339
+ ac.abort()
340
+ }
341
+
342
+ options.signal?.addEventListener('abort', onAbort)
343
+
344
+ try {
345
+ let since = options.since
346
+
347
+ const db = this
348
+ while (true) {
349
+ const buffer = new Readable({
350
+ signal: ac.signal,
351
+ objectMode: true,
352
+ readableHighWaterMark: 1024,
353
+ construct (callback) {
354
+ this._next = (batch, sequence) => {
355
+ const rows = []
356
+ for (const { type, key, value } of batch) {
357
+ rows.push(type, key, value, null)
358
+ }
359
+ if (!this.push({ rows, sequence, count: batch.length })) {
360
+ this.push(null)
361
+ db.off('write', this._next)
362
+ }
363
+ }
364
+ db.on('write', this._next)
365
+ callback()
366
+ },
367
+ read () {},
368
+ destroy (err, callback) {
369
+ db.off('write', this._next)
370
+ callback(err)
371
+ }
372
+ })
373
+
374
+ if (ac.signal.aborted) {
375
+ throw new AbortError()
376
+ }
377
+
378
+ try {
379
+ if (since <= this.sequence) {
380
+ for await (const update of this._updates(options)) {
381
+ if (ac.signal.aborted) {
382
+ throw new AbortError()
383
+ }
384
+
385
+ yield update
386
+ since = update.sequence + update.count
387
+ }
388
+ }
389
+ } catch (err) {
390
+ if (err.code !== 'LEVEL_TRYAGAIN') {
391
+ throw err
392
+ }
393
+ }
394
+
395
+ if (!options.live) {
396
+ return
397
+ }
398
+
399
+ for await (const update of buffer) {
400
+ if (update.sequence >= since) {
401
+ yield update
402
+ since = update.sequence + update.count
403
+ }
404
+ }
405
+ }
406
+ } finally {
407
+ options.signal?.removeventListener('abort', onAbort)
408
+ }
409
+ }
410
+
411
+ async * _updates (options) {
323
412
  class Updates {
324
413
  constructor (db, options) {
325
414
  this.context = binding.updates_init(db[kContext], options)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/rocksdb",
3
- "version": "7.0.46",
3
+ "version": "7.0.49",
4
4
  "description": "A low-level Node.js RocksDB binding",
5
5
  "license": "MIT",
6
6
  "main": "index.js",
@@ -11,11 +11,13 @@
11
11
  "rebuild": "npm run install --build-from-source"
12
12
  },
13
13
  "dependencies": {
14
+ "abort-controller": "^3.0.0",
14
15
  "abstract-level": "^1.0.2",
15
16
  "catering": "^2.1.1",
16
17
  "module-error": "^1.0.2",
17
18
  "napi-macros": "~2.0.0",
18
- "node-gyp-build": "^4.3.0"
19
+ "node-gyp-build": "^4.3.0",
20
+ "readable-stream": "^4.1.0"
19
21
  },
20
22
  "devDependencies": {
21
23
  "@types/node": "^17.0.16",
Binary file
Binary file