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

Commit

Permalink
deprecate .approximateSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed May 6, 2015
1 parent 681c6ce commit e19ec96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 55 deletions.
50 changes: 25 additions & 25 deletions lib/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var EventEmitter = require('events').EventEmitter
, inherits = require('util').inherits
, deprecate = require('util').deprecate
, extend = require('xtend')
, prr = require('prr')
, DeferredLevelDOWN = require('deferred-leveldown')
Expand Down Expand Up @@ -317,31 +318,30 @@ LevelUP.prototype.batch = function (arr_, options, callback) {
})
}

// DEPRECATED: prefer accessing LevelDOWN for this: db.db.approximateSize()
LevelUP.prototype.approximateSize = function (start_, end_, options, callback) {
var self = this
, start
, end

callback = getCallback(options, callback)

options = getOptions(options)

if (start_ === null || start_ === undefined
|| end_ === null || end_ === undefined || 'function' !== typeof callback)
return readError(this, 'approximateSize() requires start, end and callback arguments', callback)

start = this._codec.encodeKey(start_, options)
end = this._codec.encodeKey(end_, options)

this.db.approximateSize(start, end, function (err, size) {
if (err) {
return dispatchError(self, new OpenError(err), callback)
} else if (callback) {
callback(null, size)
}
})
}
LevelUP.prototype.approximateSize = deprecate(function (start_, end_, options, callback) {
var self = this
, start
, end

callback = getCallback(options, callback)

options = getOptions(options)

if (start_ === null || start_ === undefined
|| end_ === null || end_ === undefined || 'function' !== typeof callback)
return readError(this, 'approximateSize() requires start, end and callback arguments', callback)

start = this._codec.encodeKey(start_, options)
end = this._codec.encodeKey(end_, options)

this.db.approximateSize(start, end, function (err, size) {
if (err) {
return dispatchError(self, new OpenError(err), callback)
} else if (callback) {
callback(null, size)
}
})
}, 'db.approximateSize() is deprecated. Use db.db.approximateSize() instead')

LevelUP.prototype.readStream =
LevelUP.prototype.createReadStream = function (options) {
Expand Down
12 changes: 12 additions & 0 deletions test/approximate-size-test.js → test/appromixate-size-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ buster.testCase('approximateSize()', {
'setUp': common.commonSetUp
, 'tearDown': common.commonTearDown

, 'approximateSize() is deprecated': function (done) {
this.openTestDatabase(function (db) {
var error = console.error
console.error = function(str){
console.error = error
assert.equals(str, 'db.approximateSize() is deprecated. Use db.db.approximateSize() instead')
done()
}
db.approximateSize('a', 'z', function(){})
})
}

, 'approximateSize() works on empty database': function (done) {
this.openTestDatabase(function (db) {
db.approximateSize('a', 'z', function(err, size) {
Expand Down
30 changes: 0 additions & 30 deletions test/get-put-del-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,4 @@ buster.testCase('get() / put() / del()', {
})
}

, 'test approximateSize() throwables': function (done) {
this.openTestDatabase(function (db) {

assert.exception(
db.approximateSize.bind(db)
, { name: 'ReadError', message: 'approximateSize() requires start, end and callback arguments' }
, 'no-arg approximateSize() throws'
)

assert.exception(
db.approximateSize.bind(db, 'foo')
, { name: 'ReadError', message: 'approximateSize() requires start, end and callback arguments' }
, 'callback-less, 1-arg approximateSize() throws'
)

assert.exception(
db.approximateSize.bind(db, 'foo', 'bar')
, { name: 'ReadError', message: 'approximateSize() requires start, end and callback arguments' }
, 'callback-less, 2-arg approximateSize() throws'
)

assert.exception(
db.approximateSize.bind(db, 'foo', 'bar', {})
, { name: 'ReadError', message: 'approximateSize() requires start, end and callback arguments' }
, 'callback-less, 3-arg approximateSize(), no cb throws'
)

done()
})
}
})

0 comments on commit e19ec96

Please sign in to comment.