Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

abstract-leveldown

Package Overview
Dependencies
Maintainers
3
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abstract-leveldown - npm Package Compare versions

Comparing version 6.0.3 to 6.1.0

test/clear-range-test.js

46

abstract-leveldown.js

@@ -186,2 +186,48 @@ var xtend = require('xtend')

AbstractLevelDOWN.prototype.clear = function (options, callback) {
if (typeof options === 'function') {
callback = options
} else if (typeof callback !== 'function') {
throw new Error('clear() requires a callback argument')
}
options = cleanRangeOptions(this, options)
options.reverse = !!options.reverse
options.limit = 'limit' in options ? options.limit : -1
this._clear(options, callback)
}
AbstractLevelDOWN.prototype._clear = function (options, callback) {
// Avoid setupIteratorOptions, would serialize range options a second time.
options.keys = true
options.values = false
options.keyAsBuffer = true
options.valueAsBuffer = true
var iterator = this._iterator(options)
var emptyOptions = {}
var self = this
var next = function (err) {
if (err) {
return iterator.end(function () {
callback(err)
})
}
iterator.next(function (err, key) {
if (err) return next(err)
if (key === undefined) return iterator.end(callback)
// This could be optimized by using a batch, but the default _clear
// is not meant to be fast. Implementations have more room to optimize
// if they override _clear. Note: using _del bypasses key serialization.
self._del(key, emptyOptions, next)
})
}
next()
}
AbstractLevelDOWN.prototype._setupIteratorOptions = function (options) {

@@ -188,0 +234,0 @@ options = cleanRangeOptions(this, options)

17

CHANGELOG.md

@@ -5,2 +5,15 @@ # Changelog

## [6.1.0] - 2019-08-18
### Changed
- Upgrade `hallmark` devDependency from `^0.1.0` to `^1.0.0` ([#343](https://github.com/Level/abstract-leveldown/issues/343)) ([**@vweevers**](https://github.com/vweevers))
- Upgrade `standard` devDependency from `^12.0.0` to `^13.0.1` ([#341](https://github.com/Level/abstract-leveldown/issues/341)) ([**@vweevers**](https://github.com/vweevers))
### Added
- Add `clear()` method to delete all entries or a range ([#310](https://github.com/Level/abstract-leveldown/issues/310)) ([**@vweevers**](https://github.com/vweevers)).
**Historical Note** The `clear()` method is experimental and optional for the time being. Please see the [README](https://github.com/Level/abstract-leveldown) for details.
## [6.0.3] - 2019-04-26

@@ -760,4 +773,6 @@

[unreleased]: https://github.com/level/abstract-leveldown/compare/v6.0.3...HEAD
[unreleased]: https://github.com/level/abstract-leveldown/compare/v6.1.0...HEAD
[6.1.0]: https://github.com/level/abstract-leveldown/compare/v6.0.3...v6.1.0
[6.0.3]: https://github.com/level/abstract-leveldown/compare/v6.0.2...v6.0.3

@@ -764,0 +779,0 @@

6

CONTRIBUTORS.md

@@ -21,9 +21,9 @@ # Contributors

| **Huan LI** | [**@zixia**](https://github.com/zixia) | [**@zixia@twitter**](https://twitter.com/zixia) |
| **Nathan Shively-Sanders** | [**@sandersn**](https://github.com/sandersn) | |
| **Nolan Lawson** | [**@nolanlawson**](https://github.com/nolanlawson) | [**@nolan@toot.cafe**](https://toot.cafe/@nolan) |
| **Tim Kuijsten** | [**@timkuijsten**](https://github.com/timkuijsten) | [**@timkuijsten@mastodon.social**](https://mastodon.social/@timkuijsten) |
| **Nolan Lawson** | [**@nolanlawson**](https://github.com/nolanlawson) | [**@nolan@toot.cafe**](https://toot.cafe/@nolan) |
| **Hao-kang Den** | | |
| **Raynos (Jake Verbaten)** | | |
| **Nathan Shively-Sanders** | [**@sandersn**](https://github.com/sandersn) | |
| **Kyle Robinson Young** | [**@shama**](https://github.com/shama) | [**@shamakry@twitter**](https://twitter.com/shamakry) |
| **Tim Oxley** | [**@timoxley**](https://github.com/timoxley) | [**@secoif@twitter**](https://twitter.com/secoif) |
| **Dominic Tarr** | [**@dominictarr**](https://github.com/dominictarr) | [**@dominictarr@twitter**](https://twitter.com/dominictarr) |
| **Hao-kang Den** | | |
{
"name": "abstract-leveldown",
"version": "6.0.3",
"version": "6.1.0",
"description": "An abstract prototype matching the LevelDOWN API",

@@ -24,7 +24,7 @@ "license": "MIT",

"dependency-check": "^3.3.0",
"hallmark": "^0.1.0",
"hallmark": "^1.0.0",
"level-community": "^3.0.0",
"nyc": "^14.0.0",
"sinon": "^7.2.4",
"standard": "^12.0.0",
"standard": "^13.0.1",
"tape": "^4.10.0"

@@ -31,0 +31,0 @@ },

@@ -208,2 +208,15 @@ # abstract-leveldown

### `db.clear([options, ]callback)`
**This method is experimental. Not all implementations support it yet.**
Delete all entries or a range. Not guaranteed to be atomic. Accepts the following range options (with the same rules as on iterators):
- `gt` (greater than), `gte` (greater than or equal) define the lower bound of the range to be deleted. Only entries where the key is greater than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the entries deleted will be the same.
- `lt` (less than), `lte` (less than or equal) define the higher bound of the range to be deleted. Only entries where the key is less than (or equal to) this option will be included in the range. When `reverse=true` the order will be reversed, but the entries deleted will be the same.
- `reverse` _(boolean, default: `false`)_: delete entries in reverse order. Only effective in combination with `limit`, to remove the last N records.
- `limit` _(number, default: `-1`)_: limit the number of entries to be deleted. This number represents a _maximum_ number of entries and may not be reached if you get to the end of the range first. A value of `-1` means there is no limit. When `reverse=true` the entries with the highest keys will be deleted instead of the lowest keys.
If no options are provided, all entries will be deleted. The `callback` function will be called with no arguments if the operation was successful or with an `Error` if it failed for any reason.
### `chainedBatch`

@@ -360,2 +373,14 @@

### `db._clear(options, callback)`
**This method is experimental and optional for the time being. To enable its tests, set the [`clear` option of the test suite](#excluding-tests) to `true`.**
Delete all entries or a range. Does not have to be atomic. It is recommended (and possibly mandatory in the future) to operate on a snapshot so that writes scheduled after a call to `clear()` will not be affected.
The default `_clear()` uses `_iterator()` and `_del()` to provide a reasonable fallback, but requires binary key support. It is _recommended_ to implement `_clear()` with more performant primitives than `_iterator()` and `_del()` if the underlying storage has such primitives. Implementations that don't support binary keys _must_ implement their own `_clear()`.
Implementations that wrap another `db` can typically forward the `_clear()` call to that `db`, having transformed range options if necessary.
The `options` object will always have the following properties: `reverse` and `limit`.
### `iterator = AbstractIterator(db)`

@@ -447,2 +472,3 @@

- `seek`: set to `false` if your `iterator` does not implement `_seek`
- `clear`: defaults to `false` until a next major release. Set to `true` if your implementation either implements `_clear()` itself or is suitable to use the default implementation of `_clear()` (which requires binary key support).
- `snapshots`: set to `false` if any of the following is true:

@@ -449,0 +475,0 @@ - Reads don't operate on a [snapshot](#iterator)

@@ -0,4 +1,7 @@

var warned = false
function testCommon (options) {
var factory = options.factory
var test = options.test
var clear = !!options.clear

@@ -13,2 +16,11 @@ if (typeof factory !== 'function') {

if (!clear && !warned) {
warned = true
warn(
'A next major release of abstract-leveldown will make support of ' +
'clear() mandatory. Prepare by enabling the tests and implementing a ' +
'custom _clear() if necessary. See the README for details.'
)
}
return {

@@ -23,6 +35,15 @@ test: test,

snapshots: options.snapshots !== false,
seek: options.seek !== false
seek: options.seek !== false,
clear: clear
}
}
function warn (msg) {
if (typeof process !== 'undefined' && process && process.emitWarning) {
process.emitWarning(msg)
} else if (typeof console !== 'undefined' && console && console.warn) {
console.warn('Warning: ' + msg)
}
}
function noopTest () {

@@ -29,0 +50,0 @@ return function (t) {

@@ -41,2 +41,7 @@ var common = require('./common')

}
if (testCommon.clear) {
require('./clear-test').all(test, testCommon)
require('./clear-range-test').all(test, testCommon)
}
}

@@ -43,0 +48,0 @@

@@ -12,2 +12,3 @@ 'use strict'

test: test,
clear: true,
factory: function () {

@@ -18,2 +19,5 @@ return new AbstractLevelDOWN()

var rangeOptions = ['gt', 'gte', 'lt', 'lte']
var legacyRangeOptions = ['start', 'end']
// Test the suite itself as well as the default implementation,

@@ -72,2 +76,9 @@ // excluding noop operations that can't pass the test suite.

require('./clear-test').setUp(test, testCommon)
require('./clear-test').args(test, testCommon)
require('./clear-test').tearDown(test, testCommon)
require('./clear-range-test').setUp(test, testCommon)
require('./clear-range-test').tearDown(test, testCommon)
function implement (ctor, methods) {

@@ -380,3 +391,3 @@ function Test () {

test('test write() extensibility', function (t) {
test('test AbstractChainedBatch#write() extensibility', function (t) {
var spy = sinon.spy()

@@ -401,3 +412,3 @@ var spycb = sinon.spy()

test('test write() extensibility with null options', function (t) {
test('test AbstractChainedBatch#write() extensibility with null options', function (t) {
var spy = sinon.spy()

@@ -414,3 +425,3 @@ var Test = implement(AbstractChainedBatch, { _write: spy })

test('test write() extensibility with options', function (t) {
test('test AbstractChainedBatch#write() extensibility with options', function (t) {
var spy = sinon.spy()

@@ -427,3 +438,3 @@ var Test = implement(AbstractChainedBatch, { _write: spy })

test('test put() extensibility', function (t) {
test('test AbstractChainedBatch#put() extensibility', function (t) {
var spy = sinon.spy()

@@ -445,3 +456,3 @@ var expectedKey = 'key'

test('test del() extensibility', function (t) {
test('test AbstractChainedBatch#del() extensibility', function (t) {
var spy = sinon.spy()

@@ -461,3 +472,3 @@ var expectedKey = 'key'

test('test clear() extensibility', function (t) {
test('test AbstractChainedBatch#clear() extensibility', function (t) {
var spy = sinon.spy()

@@ -491,4 +502,4 @@ var Test = implement(AbstractChainedBatch, { _clear: spy })

t.equal(spy.callCount, 1, 'got _close() call')
t.equal(spy.getCall(0).thisValue, test, '`this` on _close() was correct')
t.equal(spy.callCount, 1, 'got _iterator() call')
t.equal(spy.getCall(0).thisValue, test, '`this` on _iterator() was correct')
t.equal(spy.getCall(0).args.length, 1, 'got one arguments')

@@ -507,3 +518,3 @@ t.deepEqual(spy.getCall(0).args[0], expectedOptions, 'got expected options argument')

test('test next() extensibility', function (t) {
test('test AbstractIterator#next() extensibility', function (t) {
var spy = sinon.spy()

@@ -527,3 +538,3 @@ var spycb = sinon.spy()

test('test end() extensibility', function (t) {
test('test AbstractIterator#end() extensibility', function (t) {
var spy = sinon.spy()

@@ -543,2 +554,31 @@ var expectedCb = function () {}

test('test clear() extensibility', function (t) {
var spy = sinon.spy()
var Test = implement(AbstractLevelDOWN, { _clear: spy })
var db = new Test()
var callback = function () {}
call([callback], { reverse: false, limit: -1 })
call([null, callback], { reverse: false, limit: -1 })
call([undefined, callback], { reverse: false, limit: -1 })
call([{ custom: 1 }, callback], { custom: 1, reverse: false, limit: -1 })
call([{ reverse: true, limit: 0 }, callback], { reverse: true, limit: 0 })
call([{ reverse: 1 }, callback], { reverse: true, limit: -1 })
call([{ reverse: null }, callback], { reverse: false, limit: -1 })
function call (args, expectedOptions) {
db.clear.apply(db, args)
t.is(spy.callCount, 1, 'got _clear() call')
t.is(spy.getCall(0).thisValue, db, '`this` on _clear() was correct')
t.is(spy.getCall(0).args.length, 2, 'got two arguments')
t.same(spy.getCall(0).args[0], expectedOptions, 'got expected options argument')
t.is(spy.getCall(0).args[1], callback, 'got expected callback argument')
spy.resetHistory()
}
t.end()
})
test('test serialization extensibility (put)', function (t) {

@@ -610,3 +650,3 @@ t.plan(5)

var test = new Test('foobar')
test.batch([ { type: 'put', key: 'no', value: 'nope' } ], function () {})
test.batch([{ type: 'put', key: 'no', value: 'nope' }], function () {})

@@ -658,3 +698,3 @@ t.equal(spy.callCount, 1, 'got _batch() call')

var test = new Test('foobar')
test.batch([ { type: 'del', key: 'no' } ], function () {})
test.batch([{ type: 'del', key: 'no' }], function () {})

@@ -770,2 +810,49 @@ t.equal(spy.callCount, 1, 'got _batch() call')

test('test serialization extensibility (clear range options)', function (t) {
t.plan(rangeOptions.length * 2)
rangeOptions.forEach(function (key) {
var Test = implement(AbstractLevelDOWN, {
_serializeKey: function (key) {
t.is(key, 'input')
return 'output'
},
_clear: function (options, callback) {
t.is(options[key], 'output')
}
})
var db = new Test()
var options = {}
options[key] = 'input'
db.clear(options, function () {})
})
})
test('clear() does not delete empty or nullish range options', function (t) {
var rangeValues = [Buffer.alloc(0), '', null, undefined]
t.plan(rangeOptions.length * rangeValues.length)
rangeValues.forEach(function (value) {
var Test = implement(AbstractLevelDOWN, {
_clear: function (options, callback) {
rangeOptions.forEach(function (key) {
t.ok(key in options, key + ' option should not be deleted')
})
}
})
var db = new Test()
var options = {}
rangeOptions.forEach(function (key) {
options[key] = value
})
db.clear(options, function () {})
})
})
test('.status', function (t) {

@@ -862,3 +949,3 @@ t.plan(5)

test('_setupIteratorOptions', function (t) {
var keys = 'start end gt gte lt lte'.split(' ')
var keys = legacyRangeOptions.concat(rangeOptions)
var db = new AbstractLevelDOWN()

@@ -876,3 +963,3 @@

keys.forEach(function (key) {
t.ok(key in options, 'property should not be deleted')
t.ok(key in options, key + ' option should not be deleted')
})

@@ -879,0 +966,0 @@ t.end()

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc