Skip to content

Commit

Permalink
use !isOpen() and bail out early in all methods
Browse files Browse the repository at this point in the history
There were different ways of checking the state
of the db before actually doing something:

1. Check if open and do something, else bail

if (isOpen()) {
  // Do stuff
} else {
  // Bail
}

2. Check if closed and bail, else do something

if (isClosed()) {
  // Bail
}

// Do stuff

I chose number 2 for all methods, but replaced isClosed()
with !isOpen(). The reasons for this are:

- Consistency
- Get rid of a level of indentation
- No longer internally dependent of the isClosed() method ->
  Easier to remove if we so choose.
  • Loading branch information
Lars-Magnus Skog committed Feb 18, 2013
1 parent 0905aee commit 6f248ca
Showing 1 changed file with 59 additions and 60 deletions.
119 changes: 59 additions & 60 deletions lib/levelup.js
Expand Up @@ -53,7 +53,6 @@ var bridge = require('bindings')('levelup.node')
, levelup

, isOpen = function () { return status == 'open' }
, isClosed = function () { return (/^clos/).test(status) }
, isOpening = function () { return status == 'opening' }
, dispatchError = function (error, callback) {
return callback ? callback(error) : levelup.emit('error', error)
Expand Down Expand Up @@ -145,7 +144,7 @@ var bridge = require('bindings')('levelup.node')

LevelUP.prototype.isOpen = function () { return isOpen() }

LevelUP.prototype.isClosed = function () { return isClosed() }
LevelUP.prototype.isClosed = function () { return (/^clos/).test(status) }

LevelUP.prototype.get = function (key_, options_, callback_) {
var callback
Expand All @@ -163,26 +162,26 @@ var bridge = require('bindings')('levelup.node')

callback = getCallback(options_, callback_)

if (isOpen()) {
options = getOptions(options_, this._options)
keyEnc = options.keyEncoding || options.encoding
valueEnc = options.valueEncoding || options.encoding
key = toSlice[keyEnc](key_)
options.asBuffer = valueEnc != 'utf8' && valueEnc != 'json'

this._db.get(key, options, function (err, value) {
if (err) {
err = new errors.NotFoundError(
'Key not found in database [' + key_ + ']')
return dispatchError(err, callback)
}
if (callback)
callback(null, toEncoding[valueEnc](value), key_)
})
} else {
if (!isOpen()) {
err = new errors.ReadError('Database is not open')
return dispatchError(err, callback)
}

options = getOptions(options_, this._options)
keyEnc = options.keyEncoding || options.encoding
valueEnc = options.valueEncoding || options.encoding
key = toSlice[keyEnc](key_)
options.asBuffer = valueEnc != 'utf8' && valueEnc != 'json'

this._db.get(key, options, function (err, value) {
if (err) {
err = new errors.NotFoundError(
'Key not found in database [' + key_ + ']')
return dispatchError(err, callback)
}
if (callback)
callback(null, toEncoding[valueEnc](value), key_)
})
}

LevelUP.prototype.put = function (key_, value_, options_, callback_) {
Expand All @@ -200,25 +199,25 @@ var bridge = require('bindings')('levelup.node')

callback = getCallback(options_, callback_)

if (isOpen()) {
options = getOptions(options_, this._options)
key = toSlice[options.keyEncoding || options.encoding](key_)
value = toSlice[options.valueEncoding || options.encoding](value_)

this._db.put(key, value, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('put', key_, value_)
if (callback)
callback(null, key, value)
}
}.bind(this))
} else {
if (!isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}

options = getOptions(options_, this._options)
key = toSlice[options.keyEncoding || options.encoding](key_)
value = toSlice[options.valueEncoding || options.encoding](value_)

this._db.put(key, value, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('put', key_, value_)
if (callback)
callback(null, key, value)
}
}.bind(this))
}

LevelUP.prototype.del = function (key_, options_, callback_) {
Expand All @@ -235,24 +234,24 @@ var bridge = require('bindings')('levelup.node')

callback = getCallback(options_, callback_)

if (isOpen()) {
options = getOptions(options_, this._options)
key = toSlice[options.keyEncoding || options.encoding](key_)

this._db.del(key, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('del', key_)
if (callback)
callback(null, key)
}
}.bind(this))
} else {
if (!isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}

options = getOptions(options_, this._options)
key = toSlice[options.keyEncoding || options.encoding](key_)

this._db.del(key, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('del', key_)
if (callback)
callback(null, key)
}
}.bind(this))
}

LevelUP.prototype.batch = function (arr_, options_, callback_) {
Expand All @@ -271,7 +270,7 @@ var bridge = require('bindings')('levelup.node')

callback = getCallback(options_, callback_)

if (isClosed()) {
if (!isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}
Expand Down Expand Up @@ -322,7 +321,7 @@ var bridge = require('bindings')('levelup.node')
})
}

if (isClosed()) {
if (!isOpen()) {
err = new errors.WriteError('Database is not open')
return dispatchError(err, callback)
}
Expand Down Expand Up @@ -353,19 +352,19 @@ var bridge = require('bindings')('levelup.node')

LevelUP.prototype.keyStream = function (options) {
return this.readStream(
extend(
options ? extend({}, options) : {}
, { keys: true, values: false }
)
extend(

This comment has been minimized.

Copy link
@ralphtheninja

ralphtheninja Feb 18, 2013

Member

Cosmetic change. Should have same indentation as the parameters to the readStream.create() call in LevelUP.prototype.readStream()

options ? extend({}, options) : {}
, { keys: true, values: false }
)
)
}

LevelUP.prototype.valueStream = function (options) {
return this.readStream(
extend(
options ? extend({}, options) : {}
, { keys: false, values: true }
)
extend(
options ? extend({}, options) : {}
, { keys: false, values: true }
)
)
}

Expand Down

0 comments on commit 6f248ca

Please sign in to comment.