-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Current functionality and the future (hoping on support for all use cases) #34
Comments
Amazing write-up!
I think it'd suffice to do it the same as the browserified encDown.prototype._isEncodingDown = true
encDown.isEncodingDown = function (db) {
return db != null && db._isEncodingDown === true &&
db !== encDown.prototype // so encDown.isEncodingDown(encDown.prototype) will be false
} However, I wonder if there's isn't a more generic solution. There are similar use cases for wanting to get the "inner db", like getting access to So what if both
|
Or |
Hmm AbstractLevelDOWN.prototype.down = function (type) {
var db = this._down(type)
return db == null ? null : db === this ? db : db.down(type)
}
AbstractLevelDOWN.prototype._down = function (type) {
return type ? null : this
} SubDown.prototype._down = function (type) {
return type === 'subleveldown' ? this : this.leveldown
}
SubDown.prototype._open = function (opts, cb) {
if (this.db.isOpen()) {
var subdb = this.db.down('subleveldown')
if (subdb && subdb.prefix) {
this.prefix = subdb.prefix + this.prefix
this.leveldown = subdb.down()
} else {
this.leveldown = this.db.down()
}
}
..
} |
Great idea! This is also why I wanted to write this to get feedback on smart solutions. I just skimmed it right now, but will give it some more thought the coming day(s). |
I'm wondering what we can do as minimal change to get this working, without having to rewrite the whole world, i.e. make a quick hack, then replace the hack underneath with a proper |
var subdb = this.db.down('subleveldown') That line calls |
I was thinking about that too. We might be able to get by with |
Sounds good. I have something to play with now at least! |
More complete plan for a temporary solution:
function down (db, type) {
if (typeof db.down === 'function') {
return db.down(type)
}
if (type && db.type === type) return db
if (isAbstract(db.db)) return down(db.db, type) // encdown, subleveldown, levelup
if (isAbstract(db._db)) return down(db._db, type) // deferred-leveldown
return type ? null : db
} For |
So basically do SubDown.prototype._open = function (opts, cb) {
if (this.db.isOpen()) {
var subdb = down(this.db, 'subleveldown')
if (subdb && subdb.prefix) {
this.prefix = subdb.prefix + this.prefix
this.leveldown = down(subdb)
} else {
this.leveldown = down(this.db)
}
}
..
} |
Exactly. And later: LevelUP.prototype.down = function (type) {
return down(this.db, type)
} AbstractLevelDOWN.prototype.down = function (type) {
var db = this._down(type)
return db == null ? null : db === this ? db : down(db, type)
} |
@vweevers Had to tweak the first |
I now think it's better to not add Closing this, continuing at Level/community#82. |
Just wanted to jot down how it actually works but with words. Mostly for my own processing but also to explain to others that might be interested.
What I like the most with this module, is that it allows you to create sub levels (duh) but also with their specific encodings and it does this quite cleverly by peeling off the
levelup
layer, adding one down layer (SubDb
) on top of the current down (leveldown
,memdown
etc wrapped bylevelup
) and then finishes off by adding back alevelup
layer.This trickery happens in
._open()
(with some added comments):The reason this works is because older versions of
levelup
takes care of the encodings and just applies them to a *down (SubDown
in this case).From
index.js
:The problem we face now is that
levelup
was rewritten and encodings moved out intoencoding-down
so if we want to support encodings in the same way we can no longer rely onlevelup
alone.So what if we in
index.js
finish off with:That should take care of the current functionality of
levelup
. But it's not enough. What if we want to create a sub level out of alevel
? This will not work (as @emilbayes pointed out here #7 (comment)) since it would mean double decodings (and encodings I guess).So what I propose is that we continue with this trickery a bit and tweak
._open()
to peel off two layers if there's anencoding-down
:It's a bit hacky, but I think it should work well. We need to implement a proper static
encDown.isEncodingDown()
function though which should support cross realms (usingSymbol.for()
etc).The text was updated successfully, but these errors were encountered: