berty / go-orbit-db

Go version of P2P Database on IPFS
https://berty.tech
Apache License 2.0
405 stars 55 forks source link

API KeyValue always create a new key value store. #73

Closed MeowDada closed 3 years ago

MeowDada commented 3 years ago

Say we have an instance of iface.OrbitDB as db. If we're intended to open an existing key value store, we can use something described as below:

db.KeyValue(ctx, addr, &iface.CreateDBOptions{
    Create: boolPtr(false),
})

But it will always create a new database, even if we set the option Create to false. The fix should be removing the code which always set option.Create = true

glouvigny commented 3 years ago

I think your change will break the expected behaviour. Did you get a different address on reopened stores? You can normally inspect it via store.Address().String().

If you provide an orbitdb address instead of a name the store won't be recreated and data will be loaded from peers.

https://github.com/berty/go-orbit-db/blob/b1334b0bf96d76d28f6bf5ab89ce1eff9468bc54/baseorbitdb/orbitdb.go#L550-L562

This is also how the reference JS implementation works when no parameter has been specified.

https://github.com/orbitdb/orbit-db/blob/e402fdc4570bda52c5c2e4cc76e0fdbbcb4d0e82/src/OrbitDB.js#L142-L145

GitHub
orbitdb/orbit-db
Peer-to-Peer Databases for the Decentralized Web. Contribute to orbitdb/orbit-db development by creating an account on GitHub.

You keep the default value (options.Create = boolPtr(true)) when it has not been provided. This change could also be applied to the Log() method.

MeowDada commented 3 years ago

Hi, @glouvigny Thanks for your fast response ! It's pretty helpful.

I think this pull request could be closed due to the reason you had pointed out. It will break the API behaviour because the JS implementation does always set create = true when using keyvalue (address, options = {})

My original question and description is quite misleading so let me reintroduce what I thought. What I really want to do is to open an existing kv store but not to create a new one if it does not exist . And it seems like using KeyValue API to open a kv store will always create a new one if it does not exist.

Alternative

Although I haven't try it yet but below codes should work fine as my expectation.

store, _ := db.Open(ctx, addr, &iface.CreateDBOptions{
    Create: boolPtr(false),
})

kv := store.(iface.KeyValueStore)