[Pkg-javascript-commits] [node-leveldown] 182/492: put LevelUP() into closure
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:57 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit 70c33e4be4b922f5852ed00d912800d7af21670a
Author: Lars-Magnus Skog <lars.magnus.skog at gmail.com>
Date: Thu Jan 24 01:34:04 2013 +0100
put LevelUP() into closure
refactor this._status to variable in closure for cleaner code
---
lib/levelup.js | 677 ++++++++++++++++++++++++++++-----------------------------
1 file changed, 333 insertions(+), 344 deletions(-)
diff --git a/lib/levelup.js b/lib/levelup.js
index 5216386..669c375 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -40,386 +40,375 @@ var bridge = require('bindings')('levelup.node')
return typeof options_ == 'function' ? options_ : callback_
}
-// Possible this._status values:
-// - 'new' - newly created, not opened or closed
-// - 'opening' - waiting for the database to be opened, post open()
-// - 'open' - successfully opened the database, available for use
-// - 'closing' - waiting for the database to be closed, post close()
-// - 'closed' - database has been successfully closed, should not be used
-// except for another open() operation
-
-function LevelUP (location, options) {
- EventEmitter.call(this)
- this.setMaxListeners(Infinity)
-
- this._options = extend(extend({}, defaultOptions), options)
- this._location = location
- this._status = 'new'
-}
-
-inherits(LevelUP, EventEmitter)
-
-LevelUP.prototype.open = function (callback) {
- if (this.isOpen()) {
- if (callback)
- process.nextTick(callback.bind(null, null, this))
- return this
- }
+ , createLevelUP = function (location, options, callback) {
+ // Possible status values:
+ // - 'new' - newly created, not opened or closed
+ // - 'opening' - waiting for the database to be opened, post open()
+ // - 'open' - successfully opened the database, available for use
+ // - 'closing' - waiting for the database to be closed, post close()
+ // - 'closed' - database has been successfully closed, should not be used
+ // except for another open() operation
+ var status = 'new'
+ , message
+ , error
+ , levelup
+
+ function isOpen () { return status == 'open' }
+ function isClosed () { return (/^clos/).test(status) }
+ function inLimbo () { return !isOpen() && !isClosed() }
+
+ if (typeof options == 'function') {
+ callback = options
+ options = {}
+ }
+
+ if (typeof location != 'string') {
+ message = 'Must provide a location for the database'
+ error = new errors.InitializationError(message)
+
+ if (callback)
+ return callback(error)
+
+ throw error
+ }
- if (this._status == 'opening')
- return callback && this.once('open', callback.bind(null, null, this))
+ function LevelUP (location, options) {
+ EventEmitter.call(this)
+ this.setMaxListeners(Infinity)
- this._status = 'opening'
+ this._options = extend(extend({}, defaultOptions), options)
+ this._location = location
+ }
- var execute = function () {
- var db = bridge.createDatabase()
+ inherits(LevelUP, EventEmitter)
- db.open(this._location, this._options, function (err) {
- if (err) {
- err = new errors.OpenError(err)
+ LevelUP.prototype.open = function (callback) {
+ if (isOpen()) {
if (callback)
- return callback(err)
- this.emit('error', err)
+ process.nextTick(callback.bind(null, null, this))
+ return this
+ }
+
+ if (status == 'opening')
+ return callback && this.once('open', callback.bind(null, null, this))
+
+ status = 'opening'
+
+ var execute = function () {
+ var db = bridge.createDatabase()
+
+ db.open(this._location, this._options, function (err) {
+ if (err) {
+ err = new errors.OpenError(err)
+ if (callback)
+ return callback(err)
+ this.emit('error', err)
+ } else {
+ this._db = db
+ status = 'open'
+ if (callback)
+ callback(null, this)
+ this.emit('open')
+ this.emit('ready')
+ }
+ }.bind(this))
+ }.bind(this)
+
+ execute()
+ this.emit('opening')
+ }
+
+ //TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
+ LevelUP.prototype.close = function (callback) {
+ var err
+
+ if (isOpen()) {
+ status = 'closing'
+ this._db.close(function () {
+ status = 'closed'
+ this.emit('closed')
+ if (callback)
+ callback.apply(null, arguments)
+ }.bind(this))
+ this.emit('closing')
+ this._db = null
+ } else if (status == 'closed') {
+ if (callback)
+ callback()
+ } else if (status == 'closing') {
+ if (callback)
+ this.once('closed', callback)
+ } else if (status == 'opening') {
+ this.once('open', function () {
+ this.close(callback)
+ })
} else {
- this._db = db
- this._status = 'open'
+ err = new errors.CloseError('Cannot close unopened database')
if (callback)
- callback(null, this)
- this.emit('open')
- this.emit('ready')
+ return callback(err)
+ this.emit('error', err)
}
- }.bind(this))
- }.bind(this)
-
- execute()
- this.emit('opening')
-}
-
-//TODO: we can crash Node by submitting an operation between close() and the actual closing of the database
-LevelUP.prototype.close = function (callback) {
- var err
-
- if (this.isOpen()) {
- this._status = 'closing'
- this._db.close(function () {
- this._status = 'closed'
- this.emit('closed')
- if (callback)
- callback.apply(null, arguments)
- }.bind(this))
- this.emit('closing')
- this._db = null
- } else if (this._status == 'closed') {
- if (callback)
- callback()
- } else if (this._status == 'closing') {
- if (callback)
- this.once('closed', callback)
- } else if (this._status == 'opening') {
- this.once('open', function () {
- this.close(callback)
- })
- } else {
- err = new errors.CloseError('Cannot close unopened database')
- if (callback)
- return callback(err)
- this.emit('error', err)
- }
-}
-
-LevelUP.prototype.isOpen = function () {
- return this._status == 'open'
-}
-
-// in between these two there is 'new' and 'opening'
-
-LevelUP.prototype.isClosed = function () {
- // covers 'closing' and 'closed'
- return (/^clos/).test(this._status)
-}
-
-LevelUP.prototype.get = function (key_, options_, callback_) {
- var open = this.isOpen()
- , callback
- , options
- , key
- , keyEnc
- , valueEnc
- , err
-
- if (!open && !this.isClosed()) {
- // limbo, defer the operation
- return this.once('ready', function () {
- this.get(key_, options_, callback_)
- })
- }
-
- callback = getCallback(options_, callback_)
-
- if (open) {
- 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_ + ']')
+ }
+
+ LevelUP.prototype.isOpen = function () { return isOpen() }
+ LevelUP.prototype.isClosed = function () { return isClosed() }
+
+ LevelUP.prototype.get = function (key_, options_, callback_) {
+ var callback
+ , options
+ , key
+ , keyEnc
+ , valueEnc
+ , err
+
+ if (inLimbo()) {
+ return this.once('ready', function () {
+ this.get(key_, options_, callback_)
+ })
+ }
+
+ 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_ + ']')
+ if (callback)
+ return callback(err)
+ throw err
+ }
+ if (callback)
+ callback(null, toEncoding[valueEnc](value), key_)
+ })
+ } else {
+ err = new errors.ReadError('Database is not open')
if (callback)
return callback(err)
throw err
}
- if (callback)
- callback(null, toEncoding[valueEnc](value), key_)
- })
- } else {
- err = new errors.ReadError('Database is not open')
- if (callback)
- return callback(err)
- throw err
- }
-}
-
-LevelUP.prototype.put = function (key_, value_, options_, callback_) {
- var open = this.isOpen()
- , callback
- , options
- , err
- , key
- , value
-
- if (!open && !this.isClosed()) {
- // limbo, defer the operation
- return this.once('ready', function () {
- this.put(key_, value_, options_, callback_)
- })
- }
-
- callback = getCallback(options_, callback_)
-
- if (open) {
- 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)
+ }
+
+ LevelUP.prototype.put = function (key_, value_, options_, callback_) {
+ var callback
+ , options
+ , err
+ , key
+ , value
+
+ if (inLimbo()) {
+ return this.once('ready', function () {
+ this.put(key_, value_, options_, callback_)
+ })
+ }
+
+ 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)
+ if (callback)
+ return callback(err)
+ this.emit('error', err)
+ } else {
+ this.emit('put', key_, value_)
+ if (callback)
+ callback(null, key, value)
+ }
+ }.bind(this))
+ } else {
+ err = new errors.WriteError('Database is not open')
if (callback)
return callback(err)
- this.emit('error', err)
+ throw err
+ }
+ }
+
+ LevelUP.prototype.del = function (key_, options_, callback_) {
+ var callback
+ , options
+ , err
+ , key
+
+ if (inLimbo()) {
+ return this.once('ready', function () {
+ this.del(key_, options_, callback_)
+ })
+ }
+
+ 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)
+ if (callback)
+ return callback(err)
+ this.emit('error', err)
+ } else {
+ this.emit('del', key_)
+ if (callback)
+ callback(null, key)
+ }
+ }.bind(this))
} else {
- this.emit('put', key_, value_)
+ err = new errors.WriteError('Database is not open')
if (callback)
- callback(null, key, value)
+ return callback(err)
+ throw err
}
- }.bind(this))
- } else {
- err = new errors.WriteError('Database is not open')
- if (callback)
- return callback(err)
- throw err
- }
-}
-
-LevelUP.prototype.del = function (key_, options_, callback_) {
- var open = this.isOpen()
- , callback
- , options
- , err
- , key
-
- if (!open && !this.isClosed()) {
- // limbo, defer the operation
- return this.once('ready', function () {
- this.del(key_, options_, callback_)
- })
- }
-
- callback = getCallback(options_, callback_)
-
- if (open) {
- 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)
+ }
+
+ LevelUP.prototype.batch = function (arr_, options_, callback_) {
+ var callback
+ , options
+ , keyEncoding
+ , valueEncoding
+ , err
+ , arr
+
+ if (inLimbo()) {
+ return this.once('ready', function () {
+ this.batch(arr_, options_, callback_)
+ })
+ }
+
+ callback = getCallback(options_, callback_)
+
+ if (isClosed()) {
+ err = new errors.WriteError('Database is not open')
if (callback)
return callback(err)
- this.emit('error', err)
- } else {
- this.emit('del', key_)
- if (callback)
- callback(null, key)
+ throw err
}
- }.bind(this))
- } else {
- err = new errors.WriteError('Database is not open')
- if (callback)
- return callback(err)
- throw err
- }
-}
-
-LevelUP.prototype.batch = function (arr_, options_, callback_) {
- var callback
- , options
- , keyEncoding
- , valueEncoding
- , err
- , arr
-
- if (!this.isOpen() && !this.isClosed()) {
- return this.once('ready', function () {
- this.batch(arr_, options_, callback_)
- })
- }
-
- callback = getCallback(options_, callback_)
-
- if (this.isClosed()) {
- err = new errors.WriteError('Database is not open')
- if (callback)
- return callback(err)
- throw err
- }
-
- options = getOptions(options_, this._options)
- keyEncoding = options.keyEncoding || options.encoding
- valueEncoding = options.valueEncoding || options.encoding
-
- // If we're not dealing with plain utf8 strings or plain
- // Buffers then we have to do some work on the array to
- // encode the keys and/or values. This includes JSON types.
- if ((keyEncoding != 'utf8' && keyEncoding != 'binary')
- || (valueEncoding != 'utf8' && valueEncoding != 'binary')) {
-
- arr = arr_.map(function (e) {
- if (e.type !== undefined && e.key !== undefined) {
- var o = { type: e.type, key: toSlice[keyEncoding](e.key) }
-
- if (e.value !== undefined)
- o.value = toSlice[valueEncoding](e.value)
-
- return o
+
+ options = getOptions(options_, this._options)
+ keyEncoding = options.keyEncoding || options.encoding
+ valueEncoding = options.valueEncoding || options.encoding
+
+ // If we're not dealing with plain utf8 strings or plain
+ // Buffers then we have to do some work on the array to
+ // encode the keys and/or values. This includes JSON types.
+ if ((keyEncoding != 'utf8' && keyEncoding != 'binary')
+ || (valueEncoding != 'utf8' && valueEncoding != 'binary')) {
+
+ arr = arr_.map(function (e) {
+ if (e.type !== undefined && e.key !== undefined) {
+ var o = { type: e.type, key: toSlice[keyEncoding](e.key) }
+
+ if (e.value !== undefined)
+ o.value = toSlice[valueEncoding](e.value)
+
+ return o
+ }
+ return {}
+ })
+ } else {
+ arr = arr_
}
- return {}
- })
- } else {
- arr = arr_
- }
- this._db.batch(arr, options, function (err) {
- if (err) {
- err = new errors.WriteError(err)
- if (callback)
- return callback(err)
- this.emit('error', err)
- } else {
- this.emit('batch', arr_)
- if (callback)
- callback(null, arr)
+ this._db.batch(arr, options, function (err) {
+ if (err) {
+ err = new errors.WriteError(err)
+ if (callback)
+ return callback(err)
+ this.emit('error', err)
+ } else {
+ this.emit('batch', arr_)
+ if (callback)
+ callback(null, arr)
+ }
+ }.bind(this))
}
- }.bind(this))
-}
-LevelUP.prototype.approximateSize = function(start, end, callback) {
- var err
+ LevelUP.prototype.approximateSize = function(start, end, callback) {
+ var err
- if (!this.isOpen() && !this.isClosed()) {
- return this.once('ready', function () {
- this.approximateSize(start, end, callback)
- })
- }
+ if (inLimbo()) {
+ return this.once('ready', function () {
+ this.approximateSize(start, end, callback)
+ })
+ }
- if (this.isClosed()) {
- err = new errors.WriteError('Database is not open')
- if (callback)
- return callback(err)
- throw err
- }
+ if (isClosed()) {
+ err = new errors.WriteError('Database is not open')
+ if (callback)
+ return callback(err)
+ throw err
+ }
- this._db.approximateSize(start, end, function(err, size) {
- if (err) {
- err = new errors.OpenError(err)
- if (callback)
- return callback(err)
- this.emit('error', err)
- } else if (callback)
- callback(null, size)
- }.bind(this))
-}
-
-LevelUP.prototype.readStream = function (options) {
- options = extend(
- extend({}, this._options)
- , typeof options == 'object' ? options : {}
- )
-
- return readStream.create(
- options
- , this
- , function (options) {
- return bridge.createIterator(this._db, options)
- }.bind(this)
- )
-}
+ this._db.approximateSize(start, end, function(err, size) {
+ if (err) {
+ err = new errors.OpenError(err)
+ if (callback)
+ return callback(err)
+ this.emit('error', err)
+ } else if (callback)
+ callback(null, size)
+ }.bind(this))
+ }
-LevelUP.prototype.keyStream = function (options) {
- return this.readStream(
- extend(
+ LevelUP.prototype.readStream = function (options) {
+ options = extend(
+ extend({}, this._options)
+ , typeof options == 'object' ? options : {}
+ )
+
+ return readStream.create(
+ options
+ , this
+ , function (options) {
+ return bridge.createIterator(this._db, options)
+ }.bind(this)
+ )
+ }
+
+ LevelUP.prototype.keyStream = function (options) {
+ return this.readStream(
+ extend(
options ? extend({}, options) : {}
- , { keys: true, values: false }
+ , { keys: true, values: false }
+ )
)
- )
-}
+ }
-LevelUP.prototype.valueStream = function (options) {
- return this.readStream(
- extend(
+ LevelUP.prototype.valueStream = function (options) {
+ return this.readStream(
+ extend(
options ? extend({}, options) : {}
- , { keys: false, values: true }
+ , { keys: false, values: true }
+ )
)
- )
-}
-
-LevelUP.prototype.writeStream = function (options) {
- return writeStream.create(
- options || {}
- , this
- )
-}
-
-LevelUP.prototype.toString = function () {
- return 'LevelUP'
-}
-
-module.exports = function (location, options, callback) {
- var message
- , error
- , levelup
-
- if (typeof options == 'function') {
- callback = options
- options = {}
- }
+ }
- if (typeof location != 'string') {
- message = 'Must provide a location for the database'
- error = new errors.InitializationError(message)
+ LevelUP.prototype.writeStream = function (options) {
+ return writeStream.create(
+ options || {}
+ , this
+ )
+ }
- if (callback) {
- return callback(error)
+ LevelUP.prototype.toString = function () {
+ return 'LevelUP'
}
- throw error
+ levelup = new LevelUP(location, options)
+ levelup.open(callback)
+ return levelup
}
- levelup = new LevelUP(location, options)
- levelup.open(callback)
- return levelup
-}
-
-module.exports.copy = require('./util').copy
\ No newline at end of file
+module.exports = createLevelUP
+module.exports.copy = require('./util').copy
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list