-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor encodings #102
Comments
Updated the above with a more complete plan, that removes the need for serialization and Example of a transcoder (some should be hardcoded to optimize them, others like exports['utf8+view'] = new Encoding({
encode (data) {
return ArrayBuffer.isView(data) ? data : textEncoder.encode(data)
},
decode (data) {
return textDecoder.decode(data)
},
type: 'utf8+view',
format: 'view'
}) |
Doing this in |
Ugh, this works better than I expected. Now The following tests are passing (locally; haven't pushed yet): Click to expandconst test = require('tape')
const suite = require('abstract-leveldown/test')
const memdown = require('memdown')
const subleveldown = require('subleveldown')
// Test abstract-leveldown compliance
function runSuite (factory) {
suite({ test, factory })
}
// Test basic prefix
runSuite(function factory (opts) {
return subleveldown(memdown(), 'test', opts)
})
// Test empty prefix
runSuite(function factory (opts) {
return subleveldown(memdown(), '', opts)
})
// Test custom separator
runSuite(function factory (opts) {
return subleveldown(memdown(), 'test', { ...opts, separator: '%' })
})
// Test on db with buffer encoding
runSuite(function factory (opts) {
return subleveldown(memdown({ keyEncoding: 'buffer' }), 'test', opts)
})
// Test on db with view encoding (Uint8Array)
runSuite(function factory (opts) {
return subleveldown(memdown({ keyEncoding: 'view' }), 'test', opts)
})
// Have memdown internally use views too
runSuite(function factory (opts) {
return subleveldown(memdown({ keyEncoding: 'view', storeEncoding: 'view' }), 'test', opts)
})
// Lastly, for good measure:
runSuite(function factory (opts) {
return subleveldown(memdown({ keyEncoding: 'buffer', storeEncoding: 'view' }), 'test', opts)
}) @ralphtheninja @juliangruber @MeirionHughes ARE YOU EXCITED? Because I am! Fuck! |
Sounds like this gets us a lot of flexibility and api simplicity as well. Well done! 👏 |
Added support of other ecosystem encodings ( |
As written in #58, there are too many forms of encoding now. Yet for an
abstract-leveldown
implementation there's no builtin primitive to decode/deserialize data.So here's a WIP plan for refactoring. I'll be editing this.
format
property to each encodingutf8
,buffer
,view
,idjson
it will beutf8
, forutf8
it will beutf8
, etcbinary
encoding tobuffer
, withbuffer
as an alias (in limited places, i.e. where we already supported this)view
encoding that works with typed arrays (including Buffer because that's a UInt8Array)utf8
encoding to always return a string, because return types of.encode()
must be predictableabstract-leveldown
encoding.format
to the private APIput('a', 123, { valueEncoding: 'json' })
could result in_put('a', '123', { valueEncoding: 'utf8' })
level
, the default encoding will beutf8
abstract-leveldown
via the manifestmemdown
will set{ encodings: { buffer: true } }
which says "I want buffers"memdown({ storeEncoding: 'view' })
which will tellmemdown
to set{ encodings: { view: true } }
- andmemdown
won't have to do any translation itselfutf8
tobuffer
..type
will be e.g.utf8+buffer
,json+view
abstract-leveldown
.memdown
you'll seedb.supports.encodings.json
even thoughmemdown
itself doesn't handlejson
asBuffer
,valueAsBuffer
,keyAsBuffer
_serializeKey
,_serializeValue
abstract-leveldown
will lookup a transcoder.memdown
, adb.get(9)
will result in_get(<Buffer 39>)
by passing it trough the transcoderutf8+buffer
.In theory we can also support native encodings. If an implementation advertises e.g.encodings: { json: true }
, we can bypassabstract-leveldown
encoding. But if the encoding is idempotent (meaning.type === .format
) we won't, in order to still normalize user input.ascii
,ucs2
andutf16le
buffer
optional, becauseutf8
is supported by both Buffer and TextEncoder{ code: LEVEL_ENCODING_NOT_SUPPORTED }
, rather than falling back to theid
(identity) encodingThe text was updated successfully, but these errors were encountered: