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

Update abstract-leveldown to the latest version 🚀 #174

Merged
merged 9 commits into from
Mar 29, 2019
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Your data is discarded when the process ends or you release a reference to the s

## Data types

Unlike [`leveldown`], `memdown` does not stringify keys or values. This means that in addition to Buffers, you can store any JS type without the need for [`encoding-down`]. For keys for example, you could use Buffers or strings, which sort lexicographically, or numbers, even Dates, which sort naturally. The only exceptions are `null` and `undefined`. Keys of that type are rejected; values of that type are converted to empty strings.
Unlike [`leveldown`], `memdown` does not stringify keys or values. This means that in addition to Buffers, you can store any JS type without the need for [`encoding-down`]. For keys for example, you could use Buffers or strings, which sort lexicographically, or numbers, even Dates, which sort naturally. The only exceptions are `null` and `undefined`. Keys and values of that type are rejected.

```js
const db = levelup(memdown())
Expand Down
21 changes: 11 additions & 10 deletions memdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Buffer = require('safe-buffer').Buffer
// In Node, use global.setImmediate. In the browser, use a consistent
// microtask library to give consistent microtask experience to all browsers
var setImmediate = require('./immediate')
var NONE = {}

function gt (value) {
return ltgt.compare(value, this._upperBound) > 0
Expand Down Expand Up @@ -41,18 +42,18 @@ function MemIterator (db, options) {

if (!this._reverse) {
this._incr = 'next'
this._lowerBound = ltgt.lowerBound(options)
this._upperBound = ltgt.upperBound(options)
this._lowerBound = ltgt.lowerBound(options, NONE)
this._upperBound = ltgt.upperBound(options, NONE)

if (typeof this._lowerBound === 'undefined') {
if (this._lowerBound === NONE) {
this._tree = tree.begin
} else if (ltgt.lowerBoundInclusive(options)) {
this._tree = tree.ge(this._lowerBound)
} else {
this._tree = tree.gt(this._lowerBound)
}

if (this._upperBound) {
if (this._upperBound !== NONE) {
if (ltgt.upperBoundInclusive(options)) {
this._test = lte
} else {
Expand All @@ -61,18 +62,18 @@ function MemIterator (db, options) {
}
} else {
this._incr = 'prev'
this._lowerBound = ltgt.upperBound(options)
this._upperBound = ltgt.lowerBound(options)
this._lowerBound = ltgt.upperBound(options, NONE)
this._upperBound = ltgt.lowerBound(options, NONE)

if (typeof this._lowerBound === 'undefined') {
if (this._lowerBound === NONE) {
this._tree = tree.end
} else if (ltgt.upperBoundInclusive(options)) {
this._tree = tree.le(this._lowerBound)
} else {
this._tree = tree.lt(this._lowerBound)
}

if (this._upperBound) {
if (this._upperBound !== NONE) {
if (ltgt.lowerBoundInclusive(options)) {
this._test = gte
} else {
Expand Down Expand Up @@ -118,7 +119,7 @@ MemIterator.prototype._test = function () {
function MemDOWN () {
if (!(this instanceof MemDOWN)) return new MemDOWN()

AbstractLevelDOWN.call(this, '')
AbstractLevelDOWN.call(this)

this._store = createRBT(ltgt.compare)
}
Expand All @@ -137,7 +138,7 @@ MemDOWN.prototype._serializeKey = function (key) {
}

MemDOWN.prototype._serializeValue = function (value) {
return value == null ? '' : value
return value
}

MemDOWN.prototype._put = function (key, value, options, callback) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"./immediate.js": "./immediate-browser.js"
},
"dependencies": {
"abstract-leveldown": "~5.0.0",
"abstract-leveldown": "~6.0.1",
"functional-red-black-tree": "~1.0.1",
"immediate": "~3.2.3",
"inherits": "~2.0.1",
Expand All @@ -41,8 +41,8 @@
"dependency-check": "^3.3.0",
"hallmark": "^0.1.0",
"level-community": "^3.0.0",
"level-concat-iterator": "^2.0.0",
"nyc": "^13.2.0",
"rimraf": "^2.6.2",
"standard": "^12.0.0",
"tape": "^4.8.0"
},
Expand Down
87 changes: 40 additions & 47 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
var test = require('tape')
var testCommon = require('abstract-leveldown/testCommon')
var MemDOWN = require('./').default
var suite = require('abstract-leveldown/test')
var concat = require('level-concat-iterator')
var memdown = require('.').default
var ltgt = require('ltgt')
var Buffer = require('safe-buffer').Buffer
var noop = function () {}

/** compatibility with basic LevelDOWN API **/
var testCommon = suite.common({
test: test,
factory: function () {
return memdown()
},

// Skip this test because memdown doesn't have a location or constructor options
// require('abstract-leveldown/abstract/leveldown-test').args(MemDOWN, test)

require('abstract-leveldown/abstract/open-test').args(MemDOWN, test, testCommon)
require('abstract-leveldown/abstract/open-test').open(MemDOWN, test, testCommon)

require('abstract-leveldown/abstract/del-test').all(MemDOWN, test)

require('abstract-leveldown/abstract/get-test').all(MemDOWN, test)

require('abstract-leveldown/abstract/put-test').all(MemDOWN, test)

require('abstract-leveldown/abstract/put-get-del-test').all(MemDOWN, test)

require('abstract-leveldown/abstract/batch-test').all(MemDOWN, test)
require('abstract-leveldown/abstract/chained-batch-test').all(MemDOWN, test)

require('abstract-leveldown/abstract/close-test').close(MemDOWN, test)
// Unsupported features
createIfMissing: false,
errorIfExists: false,
seek: false
})

require('abstract-leveldown/abstract/iterator-test').all(MemDOWN, test)
require('abstract-leveldown/abstract/iterator-range-test').all(MemDOWN, test)
// Test abstract-leveldown compliance
suite(testCommon)

// Additional tests for this implementation
test('unsorted entry, sorted iterator', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(noop)

Expand All @@ -48,7 +41,7 @@ test('unsorted entry, sorted iterator', function (t) {
noop
)

testCommon.collectEntries(
concat(
db.iterator({ keyAsBuffer: false, valueAsBuffer: false }),
function (err, data) {
t.notOk(err, 'no error')
Expand All @@ -71,7 +64,7 @@ test('unsorted entry, sorted iterator', function (t) {
})

test('reading while putting', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(noop)

Expand All @@ -98,7 +91,7 @@ test('reading while putting', function (t) {
})

test('reading while deleting', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(noop)

Expand Down Expand Up @@ -126,7 +119,7 @@ test('reading while deleting', function (t) {
})

test('reverse ranges', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(noop)

Expand All @@ -149,7 +142,7 @@ test('reverse ranges', function (t) {
})

test('delete while iterating', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand Down Expand Up @@ -184,7 +177,7 @@ test('delete while iterating', function (t) {
})

test('iterator with byte range', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand All @@ -205,13 +198,13 @@ test('iterator with byte range', function (t) {
test('iterator does not clone buffers', function (t) {
t.plan(3)

var db = new MemDOWN()
var db = testCommon.factory()
var buf = Buffer.from('a')

db.open(noop)
db.put(buf, buf, noop)

testCommon.collectEntries(db.iterator(), function (err, entries) {
concat(db.iterator(), function (err, entries) {
t.ifError(err, 'no iterator error')
t.ok(entries[0].key === buf, 'key is same buffer')
t.ok(entries[0].value === buf, 'value is same buffer')
Expand All @@ -221,20 +214,20 @@ test('iterator does not clone buffers', function (t) {
test('iterator stringifies buffer input', function (t) {
t.plan(3)

var db = new MemDOWN()
var db = testCommon.factory()

db.open(noop)
db.put(1, 2, noop)

testCommon.collectEntries(db.iterator(), function (err, entries) {
concat(db.iterator(), function (err, entries) {
t.ifError(err, 'no iterator error')
t.same(entries[0].key, Buffer.from('1'), 'key is stringified')
t.same(entries[0].value, Buffer.from('2'), 'value is stringified')
})
})

test('backing rbtree is buffer-aware', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand Down Expand Up @@ -269,7 +262,7 @@ test('backing rbtree is buffer-aware', function (t) {
test('empty value in batch', function (t) {
t.plan(6)

var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand Down Expand Up @@ -302,7 +295,7 @@ test('empty value in batch', function (t) {
})

test('empty buffer key in batch', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand All @@ -319,7 +312,7 @@ test('empty buffer key in batch', function (t) {
})

test('buffer key in batch', function (t) {
var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand All @@ -343,7 +336,7 @@ test('buffer key in batch', function (t) {
test('put multiple times', function (t) {
t.plan(5)

var db = new MemDOWN()
var db = testCommon.factory()

db.open(function (err) {
t.error(err, 'opens correctly')
Expand All @@ -366,8 +359,8 @@ test('put multiple times', function (t) {
test('number keys', function (t) {
t.plan(4)

var db = new MemDOWN()
var numbers = [2, 12]
var db = testCommon.factory()
var numbers = [-Infinity, 0, 2, 12, +Infinity]
var buffers = numbers.map(stringBuffer)

db.open(noop)
Expand All @@ -376,12 +369,12 @@ test('number keys', function (t) {
var iterator1 = db.iterator({ keyAsBuffer: false })
var iterator2 = db.iterator({ keyAsBuffer: true })

testCommon.collectEntries(iterator1, function (err, entries) {
concat(iterator1, function (err, entries) {
t.ifError(err, 'no iterator error')
t.same(entries.map(getKey), numbers, 'sorts naturally')
})

testCommon.collectEntries(iterator2, function (err, entries) {
concat(iterator2, function (err, entries) {
t.ifError(err, 'no iterator error')
t.same(entries.map(getKey), buffers, 'buffer input is stringified')
})
Expand All @@ -390,7 +383,7 @@ test('number keys', function (t) {
test('date keys', function (t) {
t.plan(4)

var db = new MemDOWN()
var db = testCommon.factory()
var dates = [new Date(0), new Date(1)]
var buffers = dates.map(stringBuffer)

Expand All @@ -400,12 +393,12 @@ test('date keys', function (t) {
var iterator = db.iterator({ keyAsBuffer: false })
var iterator2 = db.iterator({ keyAsBuffer: true })

testCommon.collectEntries(iterator, function (err, entries) {
concat(iterator, function (err, entries) {
t.ifError(err, 'no iterator error')
t.same(entries.map(getKey), dates, 'sorts naturally')
})

testCommon.collectEntries(iterator2, function (err, entries) {
concat(iterator2, function (err, entries) {
t.ifError(err, 'no iterator error')
t.same(entries.map(getKey), buffers, 'buffer input is stringified')
})
Expand All @@ -414,7 +407,7 @@ test('date keys', function (t) {
test('object value', function (t) {
t.plan(2)

var db = new MemDOWN()
var db = testCommon.factory()
var obj = {}

db.open(noop)
Expand Down