Skip to content

Commit

Permalink
Merge pull request #511 from Level/finalize-next
Browse files Browse the repository at this point in the history
Finalize next
  • Loading branch information
vweevers committed Oct 20, 2018
2 parents 0ccf1a2 + 1e1b1de commit d979b30
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 389 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -111,7 +111,7 @@ The following options are for advanced performance tuning. Modify them only if y
### `db.put(key, value[, options], callback)`
<code>put()</code> is an instance method on an existing database object, used to store new entries, or overwrite existing entries in the LevelDB store.

The `key` and `value` objects may either be strings or Buffers. Other object types are converted to strings with the `toString()` method. Keys may not be `null` or `undefined` and objects converted with `toString()` should not result in an empty-string. Values of `null`, `undefined`, `''`, `[]` and `Buffer.alloc(0)` (and any object resulting in a `toString()` of one of these) will be stored as a zero-length character array and will therefore be retrieved as either `''` or `Buffer.alloc(0)` depending on the type requested.
The `key` and `value` objects may either be strings or Buffers. Other object types are converted to strings with the `toString()` method. Keys may not be `null` or `undefined` and objects converted with `toString()` should not result in an empty-string. Values may not be `null` or `undefined`. Values of `''`, `[]` and `Buffer.alloc(0)` (and any object resulting in a `toString()` of one of these) will be stored as a zero-length character array and will therefore be retrieved as either `''` or `Buffer.alloc(0)` depending on the type requested.

A richer set of data-types is catered for in `levelup`.

Expand Down
2 changes: 1 addition & 1 deletion chained-batch.js
Expand Up @@ -19,7 +19,7 @@ ChainedBatch.prototype._clear = function (key) {
}

ChainedBatch.prototype._write = function (options, callback) {
this.binding.write(options, callback)
this.binding.write(callback)
}

util.inherits(ChainedBatch, AbstractChainedBatch)
Expand Down
13 changes: 2 additions & 11 deletions iterator.js
Expand Up @@ -13,18 +13,9 @@ function Iterator (db, options) {

util.inherits(Iterator, AbstractIterator)

Iterator.prototype.seek = function (target) {
if (this._ended) {
throw new Error('cannot call seek() after end()')
}
if (this._nexting) {
throw new Error('cannot call seek() before next() has completed')
}
if (typeof target !== 'string' && !Buffer.isBuffer(target)) {
throw new Error('seek() requires a string or buffer key')
}
Iterator.prototype._seek = function (target) {
if (target.length === 0) {
throw new Error('cannot seek() to an empty key')
throw new Error('cannot seek() to an empty target')
}

this.cache = null
Expand Down
16 changes: 15 additions & 1 deletion leveldown.js
Expand Up @@ -9,7 +9,13 @@ function LevelDOWN (location) {
return new LevelDOWN(location)
}

AbstractLevelDOWN.call(this, location)
if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument')
}

AbstractLevelDOWN.call(this)

this.location = location
this.binding = binding(location)
}

Expand All @@ -23,6 +29,14 @@ LevelDOWN.prototype._close = function (callback) {
this.binding.close(callback)
}

LevelDOWN.prototype._serializeKey = function (key) {
return Buffer.isBuffer(key) ? key : String(key)
}

LevelDOWN.prototype._serializeValue = function (value) {
return Buffer.isBuffer(value) ? value : String(value)
}

LevelDOWN.prototype._put = function (key, value, options, callback) {
this.binding.put(key, value, options, callback)
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -13,7 +13,7 @@
],
"main": "leveldown.js",
"dependencies": {
"abstract-leveldown": "~5.0.0",
"abstract-leveldown": "~6.0.0",
"bindings": "~1.3.0",
"fast-future": "~1.0.2",
"nan": "~2.11.0",
Expand All @@ -24,8 +24,7 @@
"coveralls": "^3.0.2",
"delayed": "~1.0.1",
"du": "~0.1.0",
"iota-array": "~1.0.0",
"lexicographic-integer": "~1.1.0",
"level-concat-iterator": "^2.0.0",
"mkfiletree": "~1.0.1",
"monotonic-timestamp": "~0.0.8",
"nyc": "^12.0.2",
Expand All @@ -39,6 +38,7 @@
"slump": "~2.0.0",
"standard": "^12.0.0",
"tape": "^4.5.1",
"tempy": "^0.2.1",
"uuid": "^3.2.1",
"verify-travis-appveyor": "^3.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions test/abstract-leveldown-test.js
@@ -0,0 +1 @@
require('abstract-leveldown/test')(require('./common'))
7 changes: 3 additions & 4 deletions test/approximate-size-test.js
@@ -1,13 +1,12 @@
const test = require('tape')
const leveldown = require('..')
const testCommon = require('abstract-leveldown/testCommon')
const testCommon = require('./common')

var db

test('setUp common for approximate size', testCommon.setUp)

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(t.end.bind(t))
})

Expand Down Expand Up @@ -67,7 +66,7 @@ test('test 1-arg + callback approximateSize() throws', function (t) {

test('test custom _serialize*', function (t) {
t.plan(4)
var db = leveldown(testCommon.location())
var db = testCommon.factory()
db._serializeKey = function (data) { return data }
db.approximateSize = function (start, end, callback) {
t.deepEqual(start, { foo: 'bar' })
Expand Down
5 changes: 0 additions & 5 deletions test/batch-test.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/chained-batch-test.js

This file was deleted.

22 changes: 0 additions & 22 deletions test/close-test.js

This file was deleted.

11 changes: 11 additions & 0 deletions test/common.js
@@ -0,0 +1,11 @@
const test = require('tape')
const tempy = require('tempy')
const leveldown = require('..')
const suite = require('abstract-leveldown/test')

module.exports = suite.common({
test: test,
factory: function () {
return leveldown(tempy.directory())
}
})
5 changes: 2 additions & 3 deletions test/compact-range-test.js
@@ -1,13 +1,12 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const testCommon = require('./common')

let db

test('setUp common', testCommon.setUp)

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(t.end.bind(t))
})

Expand Down
15 changes: 5 additions & 10 deletions test/compression-test.js
@@ -1,12 +1,7 @@
/* Copyright (c) 2012-2017 LevelUP contributors
* See list at <https://github.com/level/leveldown#contributing>
* MIT License <https://github.com/level/leveldown/blob/master/LICENSE.md>
*/

const async = require('async')
const du = require('du')
const delayed = require('delayed')
const common = require('abstract-leveldown/testCommon')
const testCommon = require('./common')
const leveldown = require('..')
const test = require('tape')

Expand Down Expand Up @@ -46,10 +41,10 @@ const cycle = function (db, compression, t, callback) {
}

test('Compression', function (t) {
t.test('set up', common.setUp)
t.test('set up', testCommon.setUp)

t.test('test data is compressed by default (db.put())', function (t) {
var db = leveldown(common.location())
var db = testCommon.factory()
db.open(function (err) {
t.error(err)
async.forEach(
Expand All @@ -63,7 +58,7 @@ test('Compression', function (t) {
})

t.test('test data is not compressed with compression=false on open() (db.put())', function (t) {
var db = leveldown(common.location())
var db = testCommon.factory()
db.open({ compression: false }, function (err) {
t.error(err)
async.forEach(
Expand All @@ -77,7 +72,7 @@ test('Compression', function (t) {
})

t.test('test data is compressed by default (db.batch())', function (t) {
var db = leveldown(common.location())
var db = testCommon.factory()
db.open(function (err) {
t.error(err)
db.batch(
Expand Down
5 changes: 0 additions & 5 deletions test/del-test.js

This file was deleted.

10 changes: 6 additions & 4 deletions test/destroy-test.js
@@ -1,5 +1,5 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const tempy = require('tempy')
const fs = require('fs')
const path = require('path')
const mkfiletree = require('mkfiletree')
Expand Down Expand Up @@ -27,7 +27,7 @@ test('test callback-less, 1-arg, destroy() throws', function (t) {
test('test destroy non-existent directory', function (t) {
t.plan(4)

var location = testCommon.location()
var location = tempy.directory()
var parent = path.dirname(location)

// For symmetry with the opposite test below.
Expand Down Expand Up @@ -86,7 +86,8 @@ test('test destroy non leveldb directory', function (t) {
})
})

makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t, done, location) {
makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t, done) {
var location = db.location
db.close(function (err) {
t.ifError(err, 'no close error')

Expand All @@ -99,7 +100,8 @@ makeTest('test destroy() cleans and removes leveldb-only dir', function (db, t,
})
})

makeTest('test destroy() cleans and removes only leveldb parts of a dir', function (db, t, done, location) {
makeTest('test destroy() cleans and removes only leveldb parts of a dir', function (db, t, done) {
var location = db.location
fs.writeFileSync(path.join(location, 'foo'), 'FOO')

db.close(function (err) {
Expand Down
5 changes: 0 additions & 5 deletions test/get-test.js

This file was deleted.

5 changes: 2 additions & 3 deletions test/getproperty-test.js
@@ -1,13 +1,12 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const testCommon = require('./common')

let db

test('setUp common', testCommon.setUp)

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(t.end.bind(t))
})

Expand Down
8 changes: 4 additions & 4 deletions test/iterator-gc-test.js
@@ -1,8 +1,8 @@
'use strict'

const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const collectEntries = require('level-concat-iterator')
const testCommon = require('./common')
const sourceData = []

for (let i = 0; i < 1e3; i++) {
Expand All @@ -20,7 +20,7 @@ test('setUp', testCommon.setUp)
test('db without ref does not get GCed while iterating', function (t) {
t.plan(6)

let db = leveldown(testCommon.location())
let db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')
Expand Down Expand Up @@ -50,7 +50,7 @@ test('db without ref does not get GCed while iterating', function (t) {

function iterate (it) {
// No reference to db here, could be GCed. It shouldn't..
testCommon.collectEntries(it, function (err, entries) {
collectEntries(it, function (err, entries) {
t.ifError(err, 'no iterator error')
t.is(entries.length, sourceData.length, 'got data')

Expand Down
5 changes: 0 additions & 5 deletions test/iterator-range-test.js

This file was deleted.

5 changes: 2 additions & 3 deletions test/iterator-recursion-test.js
@@ -1,6 +1,5 @@
const test = require('tape')
const testCommon = require('abstract-leveldown/testCommon')
const leveldown = require('..')
const testCommon = require('./common')
const fork = require('child_process').fork
const path = require('path')

Expand Down Expand Up @@ -43,7 +42,7 @@ test('try to create an iterator with a blown stack', function (t) {
})

test('setUp db', function (t) {
db = leveldown(testCommon.location())
db = testCommon.factory()
db.open(function (err) {
t.error(err)
db.batch(sourceData, t.end.bind(t))
Expand Down

0 comments on commit d979b30

Please sign in to comment.