mafintosh / hyperdb

Distributed scalable database
MIT License
753 stars 75 forks source link

Referring to a kvpair at a specific point in time? #15

Closed hackergrrl closed 6 years ago

hackergrrl commented 6 years ago

With hyperlog + hyperkv, you could fetch a node by its key (via hyperkv), but also via hyperlog ask for a node by its hash, which gets a kvpair at specific point in time. We found this really useful in osm-p2p-db, since data structures at HEAD would sometimes want to refer to other documents at older versions.

hackergrrl commented 6 years ago

Maybe some kind of string notation that gets exposed?

key@feed-id@seq-num

hackergrrl commented 6 years ago

Just to update, me and @mafintosh chatted and the route we're taking is using a combination of db.version, db.checkout, and db.get to retrieve a specific value in time.

You would get the version at insertion time, using the version API:

db.put('/home/sww/dev/projects.md', '...', function (err, node) {
  db.version(function (err, version) {
    var versionString = node.key + '@' + version.toString('hex')
    console.log('version is', versionString)
  })
})

You could implement getByVersion like this:

function getByVersion (db, versionString, cb) {
  var version = versionString.substring(versionString.lastIndexOf('@') + 1)
  var key = versionString.substring(0, versionString.lastIndexOf('@'))
  var snapshot = db.checkout(version)
  snapshot.get(key, function (err, nodes) {
    if (err) cb(err)
    else cb(null, nodes[0])
  })
}
hackergrrl commented 6 years ago

The APIs for this are now implemented. :tada: