Datastore is used to store metadata (what you have pinned, MFS root)
datastore does not store metadata, it stores anything that KV and we could need it for.
What is the main difference between them? Does the datastore store things in a structured way that allows fast querying
No not really, we could but we don’t.
This is mainly a type difference and helps make the code clearer, it’s mostly looking like this (pseudo code):
type Blockstore interface{
Get(m mh.Multihash) ([]byte, error)
Put(m mh.Multihash, b []byte) error
}
type Datastore interface{
Get(k string) ([]byte, error)
Put(k string, b []byte) error
}
type DatastoreToBlockstoreWrapper struct{
d Datastore
preffix string
hashOnRead bool
}
func (w DatastoreToBlockstoreWrapper) Get(m mh.Multihash) ([]byte, error) {
data, err := w.Datastore.Get(w.mhToKey(m))
if err != nil {
return nil, err
}
if w.hashOnRead {
sum := m.Header().Sum(data)
if sum != m {
return nil, errors.New("red data doesn't match expected hash")
}
}
return data, nil
}
func (w DatastoreToBlockstoreWrapper) Put(m mh.Multihash, b []byte) error {
return w.Datastore.Put(w.mhToKey(m), b)
}
func (w DatastoreToBlockstoreWrapper) mhToKey(m mh.Multihash) string {
return "/blockstore/" + w.pref + "/" + string(m)
}
One of them is a bytes to bytes mapping (datastore), one of them is multihash to bytes (blockstore).
This begs the question: when using Badger, is it used as both a blockstore and datastore?
Yes but not really.
Yes in practice it’s true, however badger does not know that, badger is just mapping keys to bytes “dumbly”.
Here are the actual interfaces: