Giter Site home page Giter Site logo

Comments (13)

vweevers avatar vweevers commented on June 7, 2024 1

That line calls levelup#down(). What would that implementation look like?

return this.db.down(type). It doesn't matter if this.db is deferred-leveldown or the open db, the result would be the same.

I'm wondering what we can do as minimal change to get this working, without having to rewrite the whole world

I was thinking about that too. We might be able to get by with while (db.db) db = db.db with some additional check that db.db is an abstract-leveldown (and not an underlying IndexedDB, for example).

from subleveldown.

ralphtheninja avatar ralphtheninja commented on June 7, 2024 1

@vweevers Had to tweak the first subdb check to this.leveldown = down(subdb.db) since calling this.leveldown = down(subdb) will result back in subdb.

from subleveldown.

vweevers avatar vweevers commented on June 7, 2024

Amazing write-up!

We need to implement a proper static encDown.isEncodingDown() function though which should support cross realms (using Symbol.for() etc).

I think it'd suffice to do it the same as the browserified Buffer.isBuffer():

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 .iterator() which is not exposed by levelup. If you want to get to leveldown's iterator for its iterator.seek() method, you possibly have to bypass deferred-leveldown and encoding-down.

So what if both levelup and abstract-leveldown had a innerDown method, that punches through the layers to get the innermost db?

levelup would return this.db, abstract-leveldown would default to returning this, deferred-leveldown would return this._db, subleveldown would return this.leveldown.

from subleveldown.

vweevers avatar vweevers commented on June 7, 2024

a innerDown method

Or down() :)

from subleveldown.

vweevers avatar vweevers commented on June 7, 2024

Hmm down() would also need an optional type argument that tells it where to stop traversing:

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()
    }
  }

  ..
}

from subleveldown.

ralphtheninja avatar ralphtheninja commented on June 7, 2024

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 .iterator() which is not exposed by levelup. If you want to get to leveldown's iterator for its iterator.seek() method, you possibly have to bypass deferred-leveldown and encoding-down.

So what if both levelup and abstract-leveldown had a innerDown method, that punches through the layers to get the innermost db?

levelup would return this.db, abstract-leveldown would default to returning this, deferred-leveldown would return this._db, subleveldown would return this.leveldown.

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).

from subleveldown.

ralphtheninja avatar ralphtheninja commented on June 7, 2024

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 .down() solution.

from subleveldown.

ralphtheninja avatar ralphtheninja commented on June 7, 2024

@vweevers

var subdb = this.db.down('subleveldown')

That line calls levelup#down(). What would that implementation look like?

from subleveldown.

ralphtheninja avatar ralphtheninja commented on June 7, 2024

I was thinking about that too. We might be able to get by with while (db.db) db = db.db with some additional check that db.db is an abstract-leveldown (and not an underlying IndexedDB, for example).

Sounds good. I have something to play with now at least!

from subleveldown.

vweevers avatar vweevers commented on June 7, 2024

More complete plan for a temporary solution:

  1. Rename subdown#leveldown to subdown#db. #db is already levelup, that works
  2. Set the value of subdown#type to subleveldown
  3. Write a recursive utility that we can use as fallback in subleveldown, levelup Γ‘nd abstract-leveldown:
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 isAbstract, perhaps we can use the old is-leveldown.js code.

from subleveldown.

ralphtheninja avatar ralphtheninja commented on June 7, 2024

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)
    }
  }

  ..
}

from subleveldown.

vweevers avatar vweevers commented on June 7, 2024

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)
}

from subleveldown.

vweevers avatar vweevers commented on June 7, 2024

I now think it's better to not add down() to levelup and/or abstract-leveldown, partly because of the recursive issue (#34 (comment)), partly because we don't need it in that many places, but mostly because we'd end up with multiple versions of the code.

Closing this, continuing at Level/community#82.

from subleveldown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.