holepunchto/hyperbee

`createHistoryStream` on `sub` returns the history with empty keys

Opened this issue · 3 comments

Hey guys,

I was trying the sub namespacing of hyperbee and I notice that running a createHistoryStream over sub databases gets the history of the entire database and also for some reason with empty keys.

const ram = require('random-access-memory')
const hypercore = require('hypercore')
const Hyperbee = require('hyperbee')

function config() {
  const feed = hypercore(ram)

  const db = new Hyperbee(feed, {
    keyEncoding: 'utf-8',
    valueEncoding: 'utf-8'
  })

  return db
}

;(async () => {
  const db = config()

  const sub = db.sub('prefix-a')

  await db.put('key0', 'value0')

  for await (const item of sub.createHistoryStream()) {
    console.log('sub', item) // sub { type: 'put', seq: 1, key: '', value: 'value0' }
  }
})()

I ran into this problem as well. The reason that your keys are empty is that sub.createHistoryStream actually creates a stream on the root hyperbee, but applies the sub's encoding to all of the keys in the events (which includes stripping out or appending the sub's key prefix). So every key is being trimmed by the length of your sub prefix (which is prefix-a = 8 characters). If you try it with a longer key like

await db.put('a-very-long-key', 'value0')

Then you'll see that the key in the stream is truncated to just 'long-key'.

The workaround that I employed was just to create a historyStream on the root hyperbee and then filter the keys manually, looking for my given sub prefix. But I agree that the library should handle the streams properly on sub hyperbees.

Ya, we should add a field to the node protobuf with a uint containing how many subs the key contains. Then we can parse it properly

Ran into this problem as well! I nearly threw my keyboard thinking it was something wrong I was doing.