Skip to content
This repository has been archived by the owner on Dec 1, 2024. It is now read-only.

Serialize compact range options #517

Merged
merged 6 commits into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ LevelDOWN.prototype.approximateSize = function (start, end, callback) {
end == null ||
typeof start === 'function' ||
typeof end === 'function') {
throw new Error('approximateSize() requires valid `start`, `end` and `callback` arguments')
throw new Error('approximateSize() requires valid `start` and `end` arguments')
}

if (typeof callback !== 'function') {
Expand All @@ -76,6 +76,20 @@ LevelDOWN.prototype.approximateSize = function (start, end, callback) {
}

LevelDOWN.prototype.compactRange = function (start, end, callback) {
if (start == null ||
end == null ||
typeof start === 'function' ||
typeof end === 'function') {
throw new Error('compactRange() requires valid `start` and `end` arguments')
}

if (typeof callback !== 'function') {
throw new Error('compactRange() requires a callback argument')
}

start = this._serializeKey(start)
end = this._serializeKey(end)

this.binding.compactRange(start, end, callback)
}

Expand Down
50 changes: 36 additions & 14 deletions test/compact-range-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ test('test compactRange() frees disk space after key deletion', function (t) {
var key2 = '000001'
var val1 = Buffer.allocUnsafe(64).fill(1)
var val2 = Buffer.allocUnsafe(64).fill(1)
db.put(key1, val1, function () {
db.put(key2, val2, function () {
db.compactRange(key1, key2, function () {
db.approximateSize('0', 'z', function (err, sizeAfterPuts) {
t.error(err, 'no error')
db.del(key1, function () {
db.del(key2, function () {
db.compactRange(key1, key2, function () {
db.approximateSize('0', 'z', function (err, sizeAfterCompact) {
t.error(err, 'no error')
t.ok(sizeAfterCompact < sizeAfterPuts)
t.end()
})
})

db.batch().put(key1, val1).put(key2, val2).write(function (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

t.ifError(err, 'no batch put error')

db.compactRange(key1, key2, function (err) {
t.ifError(err, 'no compactRange1 error')

db.approximateSize('0', 'z', function (err, sizeAfterPuts) {
t.error(err, 'no approximateSize1 error')

db.batch().del(key1).del(key2).write(function (err) {
t.ifError(err, 'no batch del error')

db.compactRange(key1, key2, function (err) {
t.ifError(err, 'no compactRange2 error')

db.approximateSize('0', 'z', function (err, sizeAfterCompact) {
t.error(err, 'no approximateSize2 error')
t.ok(sizeAfterCompact < sizeAfterPuts)
t.end()
})
})
})
Expand All @@ -37,6 +43,22 @@ test('test compactRange() frees disk space after key deletion', function (t) {
})
})

test('test compactRange() serializes start and end', function (t) {
t.plan(3)

var clone = Object.create(db)
var count = 0

clone._serializeKey = function (key) {
t.is(key, count++)
return db._serializeKey(key)
}

clone.compactRange(0, 1, function (err) {
t.ifError(err, 'no compactRange error')
})
})

test('tearDown', function (t) {
db.close(testCommon.tearDown.bind(null, t))
})