holepunchto/hyperbee

Hyperbee put with the same key

akachi1409 opened this issue · 2 comments

I am using hyperbee and have a question.

bee.put('key', 'value')

If I put in hyperbee with the same key twice, what will happen?
The first one is replaced with the second one, or two will be added with the same key?

LuKks commented

It will append a new block again, so you will end up with the same value added twice

If you want to avoid re-appending the same value then you can use the cas option like this:

const sameData = require('same-data')

await bee.put(ket, value, { cas })

function cas (prev, next) {
  return !sameData(prev.value, next.value)
}

You can use same-object if the value is more complex, otherwise for simple values then same-data is good

LuKks commented

Btw if you want to "inspect" the Hyperbee then you can access to the Hypercore at bee.core, so you can do:
console.log(await bee.core.get(0)) and same for any index 1, 2, etc this way you will verify that it actually inserts the block twice, and the cas option fixes it