Skip to content

Commit

Permalink
Merge pull request #85 from Level/remove/stringify-key-value
Browse files Browse the repository at this point in the history
Remove stringification of keys and values
  • Loading branch information
ralphtheninja committed Mar 1, 2016
2 parents da6d505 + 1138d6d commit b00f97f
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ If `batch()` is called without arguments or with only an options object then it
By default a `batch()` operation without arguments returns a blank `AbstractChainedBatch` object. The prototype is available on the main exports for you to extend. If you want to implement chainable batch operations then you should extend the `AbstractChaindBatch` and return your object in the `_chainedBatch()` method.

### AbstractLevelDOWN#_approximateSize(start, end, callback)
### AbstractLevelDOWN#_serializeKey(key)
### AbstractLevelDOWN#_serializeValue(value)
### AbstractLevelDOWN#_iterator(options)

By default an `iterator()` operation returns a blank `AbstractIterator` object. The prototype is available on the main exports for you to extend. If you want to implement iterator operations then you should extend the `AbstractIterator` and return your object in the `_iterator(options)` method.
Expand All @@ -124,6 +126,8 @@ Provided with the current instance of `AbstractLevelDOWN` by default.
### AbstractChainedBatch#_del(key)
### AbstractChainedBatch#_clear()
### AbstractChainedBatch#_write(options, callback)
### AbstractChainedBatch#_serializeKey(key)
### AbstractChainedBatch#_serializeValue(value)

### isLevelDown(db)

Expand Down
16 changes: 12 additions & 4 deletions abstract-chained-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ function AbstractChainedBatch (db) {
this._written = false
}

AbstractChainedBatch.prototype._serializeKey = function (key) {
return this._db._serializeKey(key)
}

AbstractChainedBatch.prototype._serializeValue = function (value) {
return this._db._serializeValue(value)
}

AbstractChainedBatch.prototype._checkWritten = function () {
if (this._written)
throw new Error('write() already called on this batch')
Expand All @@ -18,8 +26,8 @@ AbstractChainedBatch.prototype.put = function (key, value) {
if (err)
throw err

if (!this._db._isBuffer(key)) key = String(key)
if (!this._db._isBuffer(value)) value = String(value)
key = this._serializeKey(key)
value = this._serializeValue(value)

if (typeof this._put == 'function' )
this._put(key, value)
Expand All @@ -35,7 +43,7 @@ AbstractChainedBatch.prototype.del = function (key) {
var err = this._db._checkKey(key, 'key', this._db._isBuffer)
if (err) throw err

if (!this._db._isBuffer(key)) key = String(key)
key = this._serializeKey(key)

if (typeof this._del == 'function' )
this._del(key)
Expand Down Expand Up @@ -77,4 +85,4 @@ AbstractChainedBatch.prototype.write = function (options, callback) {
process.nextTick(callback)
}

module.exports = AbstractChainedBatch
module.exports = AbstractChainedBatch
34 changes: 18 additions & 16 deletions abstract-leveldown.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) {
if (err = this._checkKey(key, 'key'))
return callback(err)

if (!this._isBuffer(key))
key = String(key)
key = this._serializeKey(key)

if (typeof options != 'object')
options = {}
Expand All @@ -108,13 +107,8 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) {
if (err = this._checkKey(key, 'key'))
return callback(err)

if (!this._isBuffer(key))
key = String(key)

// coerce value to string in node, don't touch it in browser
// (indexeddb can store any JS type)
if (value != null && !this._isBuffer(value) && !process.browser)
value = String(value)
key = this._serializeKey(key)
value = this._serializeValue(value)

if (typeof options != 'object')
options = {}
Expand All @@ -137,8 +131,7 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) {
if (err = this._checkKey(key, 'key'))
return callback(err)

if (!this._isBuffer(key))
key = String(key)
key = this._serializeKey(key)

if (typeof options != 'object')
options = {}
Expand Down Expand Up @@ -203,11 +196,8 @@ AbstractLevelDOWN.prototype.approximateSize = function (start, end, callback) {
if (typeof callback != 'function')
throw new Error('approximateSize() requires a callback argument')

if (!this._isBuffer(start))
start = String(start)

if (!this._isBuffer(end))
end = String(end)
start = this._serializeKey(start)
end = this._serializeKey(end)

if (typeof this._approximateSize == 'function')
return this._approximateSize(start, end, callback)
Expand Down Expand Up @@ -257,6 +247,18 @@ AbstractLevelDOWN.prototype._isBuffer = function (obj) {
return Buffer.isBuffer(obj)
}

AbstractLevelDOWN.prototype._serializeKey = function (key) {
return this._isBuffer(key)
? key
: String(key)
}

AbstractLevelDOWN.prototype._serializeValue = function (value) {
return this._isBuffer(value) || process.browser
? value
: String(value)
}

AbstractLevelDOWN.prototype._checkKey = function (obj, type) {
if (obj === null || obj === undefined)
return new Error(type + ' cannot be `null` or `undefined`')
Expand Down
50 changes: 48 additions & 2 deletions abstract/approximate-size-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var db
, leveldown
, testCommon

module.exports.setUp = function (leveldown, test, testCommon) {
test('setUp common', testCommon.setUp)
module.exports.setUp = function (_leveldown, test, _testCommon) {
test('setUp common', _testCommon.setUp)
test('setUp db', function (t) {
leveldown = _leveldown
testCommon = _testCommon
db = leveldown(testCommon.location())
db.open(t.end.bind(t))
})
Expand Down Expand Up @@ -62,6 +66,48 @@ module.exports.args = function (test) {
)
t.end()
})

test('test _serialize object', function (t) {
t.plan(3)
var db = leveldown(testCommon.location())
db._approximateSize = function (start, end, callback) {
t.equal(start, '[object Object]')
t.equal(end, '[object Object]')
callback()
}
db.approximateSize({}, {}, function (err, val) {
t.error(err)
})
})

test('test _serialize buffer', function (t) {
t.plan(3)
var db = leveldown(testCommon.location())
db._approximateSize = function (start, end, callback) {
t.same(start, Buffer('start'))
t.same(end, Buffer('end'))
callback()
}
db.approximateSize(Buffer('start'), Buffer('end'), function (err, val) {
t.error(err)
})
})

test('test custom _serialize*', function (t) {
t.plan(3)
var db = leveldown(testCommon.location())
db._serializeKey = function (data) { return data }
db._approximateSize = function (start, end, callback) {
t.deepEqual(start, { foo: 'bar' })
t.deepEqual(end, { beep: 'boop' })
callback()
}
db.open(function () {
db.approximateSize({ foo: 'bar' }, { beep: 'boop' }, function (err) {
t.error(err)
})
})
})
}

module.exports.approximateSize = function (test) {
Expand Down
46 changes: 43 additions & 3 deletions abstract/chained-batch-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var db
, leveldown
, testCommon

module.exports.setUp = function (leveldown, test, testCommon) {
test('setUp common', testCommon.setUp)
module.exports.setUp = function (_leveldown, test, _testCommon) {
test('setUp common', _testCommon.setUp)
test('setUp db', function (t) {
leveldown = _leveldown
testCommon = _testCommon
db = leveldown(testCommon.location())
db.open(t.end.bind(t))
})
Expand Down Expand Up @@ -152,6 +156,42 @@ module.exports.args = function (test) {
t.fail('should have thrown')
t.end()
})

test('test serialize object', function (t) {
var batch = db.batch()
.put({ foo: 'bar' }, { beep: 'boop' })
.del({ bar: 'baz' })
t.deepEqual(batch._operations, [
{ type: 'put', key: '[object Object]', value: '[object Object]' }
, { type: 'del', key: '[object Object]' }
])
t.end()
})

test('test serialize buffer', function (t) {
var batch = db.batch()
.put(Buffer('foo'), Buffer('bar'))
.del(Buffer('baz'))
t.equal(batch._operations[0].key.toString(), 'foo')
t.equal(batch._operations[0].value.toString(), 'bar')
t.equal(batch._operations[1].key.toString(), 'baz')
t.end()
})

test('test custom _serialize*', function (t) {
var db = leveldown(testCommon.location())
db._serializeKey = db._serializeValue = function (data) { return data }
db.open(function () {
var batch = db.batch()
.put({ foo: 'bar' }, { beep: 'boop' })
.del({ bar: 'baz' })
t.deepEqual(batch._operations, [
{ type: 'put', key: { foo: 'bar' }, value: { beep: 'boop' } }
, { type: 'del', key: { bar: 'baz' } }
])
db.close(t.end.bind(t))
})
})
}

module.exports.batch = function (test, testCommon) {
Expand Down Expand Up @@ -207,4 +247,4 @@ module.exports.all = function (leveldown, test, testCommon) {
module.exports.args(test)
module.exports.batch(test, testCommon)
module.exports.tearDown(test, testCommon)
}
}
47 changes: 45 additions & 2 deletions abstract/del-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
var db
, leveldown
, testCommon
, verifyNotFoundError = require('./util').verifyNotFoundError
, isTypedArray = require('./util').isTypedArray

module.exports.setUp = function (leveldown, test, testCommon) {
test('setUp common', testCommon.setUp)
module.exports.setUp = function (_leveldown, test, _testCommon) {
test('setUp common', _testCommon.setUp)
test('setUp db', function (t) {
leveldown = _leveldown
testCommon = _testCommon
db = leveldown(testCommon.location())
db.open(t.end.bind(t))
})
Expand Down Expand Up @@ -37,6 +41,45 @@ module.exports.args = function (test) {
)
t.end()
})

test('test _serialize object', function (t) {
t.plan(2)
var db = leveldown(testCommon.location())
db._del = function (key, opts, callback) {
t.equal(key, '[object Object]')
callback()
}
db.del({}, function (err, val) {
t.error(err)
})
})

test('test _serialize buffer', function (t) {
t.plan(2)
var db = leveldown(testCommon.location())
db._del = function (key, opts, callback) {
t.ok(Buffer.isBuffer(key))
callback()
}
db.del(Buffer('buf'), function (err, val) {
t.error(err)
})
})

test('test custom _serialize*', function (t) {
t.plan(2)
var db = leveldown(testCommon.location())
db._serializeKey = function (data) { return data }
db._del = function (key, options, callback) {
t.deepEqual(key, { foo: 'bar' })
callback()
}
db.open(function () {
db.del({ foo: 'bar' }, function (err) {
t.error(err)
})
})
})
}

module.exports.del = function (test) {
Expand Down
49 changes: 46 additions & 3 deletions abstract/get-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
var db
, leveldown
, testCommon
, verifyNotFoundError = require('./util').verifyNotFoundError
, isTypedArray = require('./util').isTypedArray

module.exports.setUp = function (leveldown, test, testCommon) {
test('setUp common', testCommon.setUp)
module.exports.setUp = function (_leveldown, test, _testCommon) {
test('setUp common', _testCommon.setUp)
test('setUp db', function (t) {
leveldown = _leveldown
testCommon = _testCommon
db = leveldown(testCommon.location())
db.open(t.end.bind(t))
})
Expand Down Expand Up @@ -37,6 +41,45 @@ module.exports.args = function (test) {
)
t.end()
})

test('test _serialize object', function (t) {
t.plan(2)
var db = leveldown(testCommon.location())
db._get = function (key, opts, callback) {
t.equal(key, '[object Object]')
callback()
}
db.get({}, function (err, val) {
t.error(err)
})
})

test('test _serialize buffer', function (t) {
t.plan(2)
var db = leveldown(testCommon.location())
db._get = function (key, opts, callback) {
t.same(key, Buffer('key'))
callback()
}
db.get(Buffer('key'), function (err, val) {
t.error(err)
})
})

test('test custom _serialize*', function (t) {
t.plan(2)
var db = leveldown(testCommon.location())
db._serializeKey = function (data) { return data }
db._get = function (key, options, callback) {
t.deepEqual(key, { foo: 'bar' })
callback()
}
db.open(function () {
db.get({ foo: 'bar' }, function (err) {
t.error(err)
})
})
})
}

module.exports.get = function (test) {
Expand Down Expand Up @@ -130,4 +173,4 @@ module.exports.all = function (leveldown, test, testCommon) {
module.exports.args(test)
module.exports.get(test)
module.exports.tearDown(test, testCommon)
}
}
Loading

0 comments on commit b00f97f

Please sign in to comment.