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

Commit

Permalink
🔥 remove .createIfMissing
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphtheninja committed Jul 31, 2017
1 parent 7c16fe9 commit 26e2e05
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 106 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ var db = levelup(memdown)

`levelup()` takes an optional options object as its second argument; the following properties are accepted:

* `'createIfMissing'` *(boolean, default: `true`)*: If `true`, will initialise an empty database at the specified location if one doesn't already exist. If `false` and a database doesn't exist you will receive an error in your `open()` callback and your database won't open.

* `'compression'` *(boolean, default: `true`)*: If `true`, all *compressible* data will be run through the Snappy compression algorithm before being stored. Snappy is very fast and shouldn't gain much speed by disabling so leave this on unless you have good reason to turn it off.

* `'cacheSize'` *(number, default: `8 * 1024 * 1024`)*: The size (in bytes) of the in-memory [LRU](http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) cache with frequently used uncompressed block contents.
Expand Down
1 change: 0 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

var defaultOptions = {
createIfMissing: true,
keyEncoding: 'utf8',
valueEncoding: 'utf8',
compression: true
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/engines/levelup-nosnappy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var levelup = require('../../../')

var createDb = function (location, callback) {
levelup(location, { createIfMissing: true, compression: false }, function (err, db) {
levelup(location, { compression: false }, function (err, db) {
setTimeout(callback.bind(null, err, db), 50)
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/engines/levelup-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var levelup = require('levelup')

var createDb = function (location, callback) {
levelup(location, { createIfMissing: true }, function (err, db) {
levelup(location, function (err, db) {
setTimeout(callback.bind(null, err, db), 50)
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/engines/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var levelup = require('../../../')

var createDb = function (location, callback) {
levelup(location, { createIfMissing: true }, function (err, db) {
levelup(location, function (err, db) {
setTimeout(callback.bind(null, err, db), 50)
})
}
Expand Down
4 changes: 2 additions & 2 deletions test/binary-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ buster.testCase('Binary API', {
},

'test put() and get() with binary value {valueEncoding:binary} on createDatabase()': function (done) {
this.openTestDatabase({ createIfMissing: true, valueEncoding: 'binary' }, function (db) {
this.openTestDatabase({ valueEncoding: 'binary' }, function (db) {
db.put('binarydata', this.testData, function (err) {
refute(err)
db.get('binarydata', function (err, value) {
Expand Down Expand Up @@ -81,7 +81,7 @@ buster.testCase('Binary API', {
},

'test put() and get() with binary value {keyEncoding:utf8,valueEncoding:binary} on createDatabase()': function (done) {
this.openTestDatabase({ createIfMissing: true, keyEncoding: 'utf8', valueEncoding: 'binary' }, function (db) {
this.openTestDatabase({ keyEncoding: 'utf8', valueEncoding: 'binary' }, function (db) {
db.put('binarydata', this.testData, function (err) {
refute(err)
db.get('binarydata', function (err, value) {
Expand Down
2 changes: 1 addition & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports.cleanup = function (callback) {
}

module.exports.openTestDatabase = function () {
var options = typeof arguments[0] === 'object' ? arguments[0] : { createIfMissing: true }
var options = typeof arguments[0] === 'object' ? arguments[0] : {}
var callback = typeof arguments[0] === 'function' ? arguments[0] : arguments[1]
var location = typeof arguments[0] === 'string' ? arguments[0] : module.exports.nextLocation()

Expand Down
10 changes: 5 additions & 5 deletions test/deferred-open-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buster.testCase('Deferred open()', {
'put() and get() on pre-opened database': function (done) {
var location = common.nextLocation()
// 1) open database without callback, opens in worker thread
var db = levelup(location, { createIfMissing: true, valueEncoding: 'utf8' })
var db = levelup(location, { valueEncoding: 'utf8' })

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
Expand Down Expand Up @@ -55,7 +55,7 @@ buster.testCase('Deferred open()', {
'batch() on pre-opened database': function (done) {
var location = common.nextLocation()
// 1) open database without callback, opens in worker thread
var db = levelup(location, { createIfMissing: true, valueEncoding: 'utf8' })
var db = levelup(location, { valueEncoding: 'utf8' })

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
Expand Down Expand Up @@ -93,7 +93,7 @@ buster.testCase('Deferred open()', {
'chained batch() on pre-opened database': function (done) {
var location = common.nextLocation()
// 1) open database without callback, opens in worker thread
var db = levelup(location, { createIfMissing: true, valueEncoding: 'utf8' })
var db = levelup(location, { valueEncoding: 'utf8' })

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
Expand Down Expand Up @@ -138,7 +138,7 @@ buster.testCase('Deferred open()', {
refute(err)
db.close(function (err) {
refute(err, 'no error')
db = levelup(location, { createIfMissing: false })
db = levelup(location)
var rs = db.createReadStream()
rs.on('data', this.dataSpy)
rs.on('end', this.endSpy)
Expand All @@ -152,7 +152,7 @@ buster.testCase('Deferred open()', {
'maxListeners warning': function (done) {
var location = common.nextLocation()
// 1) open database without callback, opens in worker thread
var db = levelup(location, { createIfMissing: true, valueEncoding: 'utf8' })
var db = levelup(location, { valueEncoding: 'utf8' })
var stderrMock = this.mock(console)

this.closeableDatabases.push(db)
Expand Down
8 changes: 4 additions & 4 deletions test/encoding-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ buster.testCase('Encoding', {
'tearDown': common.commonTearDown,

'test safe decode in get()': function (done) {
this.openTestDatabase({ createIfMissing: true, valueEncoding: 'utf8' }, function (db) {
this.openTestDatabase({ valueEncoding: 'utf8' }, function (db) {
db.put('foo', 'this {} is [] not : json', function (err) {
refute(err)
db.close(function (err) {
refute(err)
db = levelup(db.location, { createIfMissing: false, valueEncoding: 'json' })
db = levelup(db.location, { valueEncoding: 'json' })
db.get('foo', function (err, value) {
assert(err)
assert.equals('EncodingError', err.name)
Expand All @@ -34,7 +34,7 @@ buster.testCase('Encoding', {
},

'test safe decode in readStream()': function (done) {
this.openTestDatabase({ createIfMissing: true, valueEncoding: 'utf8' }, function (db) {
this.openTestDatabase({ valueEncoding: 'utf8' }, function (db) {
db.put('foo', 'this {} is [] not : json', function (err) {
refute(err)
db.close(function (err) {
Expand All @@ -43,7 +43,7 @@ buster.testCase('Encoding', {
var dataSpy = this.spy()
var errorSpy = this.spy()

db = levelup(db.location, { createIfMissing: false, valueEncoding: 'json' })
db = levelup(db.location, { valueEncoding: 'json' })
db.readStream()
.on('data', dataSpy)
.on('error', errorSpy)
Expand Down
12 changes: 4 additions & 8 deletions test/idempotent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ buster.testCase('Idempotent open & close', {

this.cleanupDirs.push(location)

db = levelup(
location,
{ createIfMissing: true },
function () {
assert.equals(n++, 0, 'callback should fire only once')
if (n && m) { close() }
}
)
db = levelup(location, function () {
assert.equals(n++, 0, 'callback should fire only once')
if (n && m) { close() }
})

db.on('open', function () {
assert.equals(m++, 0, 'callback should fire only once')
Expand Down
103 changes: 33 additions & 70 deletions test/init-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/

var levelup = require('../lib/levelup.js')
var errors = levelup.errors
var fs = require('fs')
var common = require('./common')
var assert = require('referee').assert
var refute = require('referee').refute
Expand All @@ -24,7 +22,7 @@ buster.testCase('Init & open()', {

'default options': function (done) {
var location = common.nextLocation()
levelup(location, { createIfMissing: true }, function (err, db) {
levelup(location, function (err, db) {
refute(err, 'no error')
assert.isTrue(db.isOpen())
this.closeableDatabases.push(db)
Expand All @@ -37,12 +35,11 @@ buster.testCase('Init & open()', {
levelup(location, function (err, db) { // no options object
refute(err)
assert.isObject(db)
assert.isTrue(db.options.createIfMissing)
assert.equals(db.options.keyEncoding, 'utf8')
assert.equals(db.options.valueEncoding, 'utf8')
assert.equals(db.location, location)

// read-only properties
// read-only properties
db.location = 'foo'
assert.equals(db.location, location)

Expand All @@ -54,60 +51,51 @@ buster.testCase('Init & open()', {

'basic options': function (done) {
var location = common.nextLocation()
levelup(
location,
{ createIfMissing: true, valueEncoding: 'binary' }, function (err, db) {
refute(err)
levelup(location, { valueEncoding: 'binary' }, function (err, db) {
refute(err)

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
assert.isObject(db)
assert.isTrue(db.options.createIfMissing)
assert.equals(db.options.keyEncoding, 'utf8')
assert.equals(db.options.valueEncoding, 'binary')
assert.equals(db.location, location)

// read-only properties
db.location = 'bar'
assert.equals(db.location, location)

done()
}.bind(this)
)
this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
assert.isObject(db)
assert.equals(db.options.keyEncoding, 'utf8')
assert.equals(db.options.valueEncoding, 'binary')
assert.equals(db.location, location)

// read-only properties
db.location = 'bar'
assert.equals(db.location, location)

done()
}.bind(this))
},

'options with encoding': function (done) {
var location = common.nextLocation()
levelup(
location,
{ createIfMissing: true, keyEncoding: 'ascii', valueEncoding: 'json' }, function (err, db) {
refute(err)
levelup(location, { keyEncoding: 'ascii', valueEncoding: 'json' }, function (err, db) {
refute(err)

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
assert.isObject(db)
assert.equals(db.options.keyEncoding, 'ascii')
assert.equals(db.options.valueEncoding, 'json')
assert.equals(db.location, location)

// read-only properties
db.location = 'bar'
assert.equals(db.location, location)

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
assert.isObject(db)
assert.isTrue(db.options.createIfMissing)
assert.equals(db.options.keyEncoding, 'ascii')
assert.equals(db.options.valueEncoding, 'json')
assert.equals(db.location, location)

// read-only properties
db.location = 'bar'
assert.equals(db.location, location)

done()
}.bind(this)
)
done()
}.bind(this))
},

'without callback': function (done) {
var location = common.nextLocation()
var db = levelup(location, { createIfMissing: true })
var db = levelup(location)

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
assert.isObject(db)
assert.isTrue(db.options.createIfMissing)
assert.equals(db.location, location)

db.on('ready', function () {
Expand All @@ -116,31 +104,6 @@ buster.testCase('Init & open()', {
})
},

'open() with !createIfMissing expects error': function (done) {
levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: false }, function (err, db) {
assert(err)
refute(db)
assert.isInstanceOf(err, Error)
assert.isInstanceOf(err, errors.LevelUPError)
assert.isInstanceOf(err, errors.OpenError)
assert(err.notFound === undefined, 'err.notFound is `undefined`, should only be on NotFoundError')
done()
})
},

'open() with createIfMissing expects directory to be created': function (done) {
levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: true }, function (err, db) {
this.closeableDatabases.push(db)
refute(err)
assert.isTrue(db.isOpen())
fs.stat(this.cleanupDirs[0], function (err, stat) {
refute(err)
assert(stat.isDirectory())
done()
})
}.bind(this))
},

'constructor with options argument uses factory': function (done) {
var db = levelup({ db: MemDOWN })
assert.isNull(db.location, 'location property is null')
Expand Down
2 changes: 0 additions & 2 deletions test/inject-encoding-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ buster.testCase('JSON API', {
this.runTest = function (testData, assertType, done) {
var location = common.nextLocation()
this.cleanupDirs.push(location)
console.log(location)
levelup(location, {
createIfMissing: true,
valueEncoding: {
encode: msgpack.encode,
decode: msgpack.decode,
Expand Down
2 changes: 1 addition & 1 deletion test/json-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ buster.testCase('JSON API', {
this.runTest = function (testData, assertType, done) {
var location = common.nextLocation()
this.cleanupDirs.push(location)
levelup(location, { createIfMissing: true, valueEncoding: { encode: JSON.stringify, decode: JSON.parse } }, function (err, db) {
levelup(location, { valueEncoding: { encode: JSON.stringify, decode: JSON.parse } }, function (err, db) {
refute(err)
if (err) return

Expand Down
2 changes: 1 addition & 1 deletion test/null-and-undefined-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ buster.testCase('null & undefined keys & values', {

'null and undefined': {
'setUp': function (done) {
levelup(this.cleanupDirs[0] = common.nextLocation(), { createIfMissing: true }, function (err, db) {
levelup(this.cleanupDirs[0] = common.nextLocation(), function (err, db) {
refute(err) // sanity
this.closeableDatabases.push(db)
assert.isTrue(db.isOpen())
Expand Down
2 changes: 1 addition & 1 deletion test/open-patchsafe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function test (fun) {
return function (done) {
var location = common.nextLocation()
// 1) open database without callback, opens in worker thread
var db = levelup(location, { createIfMissing: true, valueEncoding: 'utf8' })
var db = levelup(location, { valueEncoding: 'utf8' })

this.closeableDatabases.push(db)
this.cleanupDirs.push(location)
Expand Down
Loading

0 comments on commit 26e2e05

Please sign in to comment.