This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clear() method to delete all entries or a range (#669)
* Add clear() method to delete all entries or a range * Upgrade deferred-leveldown from ~5.1.0 to ~5.2.0 * Upgrade encoding-down devDependency from ^6.0.0 to ^6.2.0 * Add link to Level/community#79
- Loading branch information
Showing
5 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
var test = require('tape') | ||
var memdown = require('memdown') | ||
var encode = require('encoding-down') | ||
var concat = require('level-concat-iterator') | ||
var levelup = require('../lib/levelup') | ||
|
||
test('clear()', function (t) { | ||
function makeTest (name, fn) { | ||
t.test(name, function (t) { | ||
var mem = memdown() | ||
|
||
mem.open(function (err) { | ||
t.ifError(err, 'no open error') | ||
|
||
mem.batch([ | ||
{ type: 'put', key: '"a"', value: 'a' }, | ||
{ type: 'put', key: '"b"', value: 'b' } | ||
], function (err) { | ||
t.ifError(err, 'no batch error') | ||
|
||
mem.close(function (err) { | ||
t.ifError(err, 'no close error') | ||
fn(t, mem) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
function verify (t, db, expectedKey) { | ||
concat(db.iterator({ keyAsBuffer: false }), function (err, entries) { | ||
t.ifError(err, 'no concat error') | ||
t.same(entries.map(function (e) { return e.key }), [expectedKey], 'got expected keys') | ||
db.close(t.end.bind(t)) | ||
}) | ||
} | ||
|
||
makeTest('clear() without encoding, without deferred-open', function (t, mem) { | ||
var db = levelup(mem) | ||
|
||
db.open(function (err) { | ||
t.ifError(err) | ||
|
||
db.clear({ gte: '"b"' }, function (err) { | ||
t.ifError(err, 'no clear error') | ||
verify(t, db, '"a"') | ||
}) | ||
}) | ||
}) | ||
|
||
makeTest('clear() without encoding, with deferred-open', function (t, mem) { | ||
var db = levelup(mem) | ||
|
||
db.clear({ gte: '"b"' }, function (err) { | ||
t.ifError(err, 'no clear error') | ||
verify(t, db, '"a"') | ||
}) | ||
}) | ||
|
||
makeTest('clear() with encoding, with deferred-open', function (t, mem) { | ||
var db = levelup(encode(mem, { keyEncoding: 'json' })) | ||
|
||
db.clear({ gte: 'b' }, function (err) { | ||
t.ifError(err, 'no clear error') | ||
verify(t, db, 'a') | ||
}) | ||
}) | ||
|
||
makeTest('clear() with encoding, without deferred-open', function (t, mem) { | ||
var db = levelup(encode(mem, { keyEncoding: 'json' })) | ||
|
||
db.open(function (err) { | ||
t.ifError(err) | ||
|
||
db.clear({ gte: 'b' }, function (err) { | ||
t.ifError(err, 'no clear error') | ||
verify(t, db, 'a') | ||
}) | ||
}) | ||
}) | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters