2020-06-01 18:11:55 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-07-17 20:14:03 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"golang.org/x/xerrors"
|
2020-06-01 18:11:55 +00:00
|
|
|
|
2020-07-07 13:29:16 +00:00
|
|
|
dgbadger "github.com/dgraph-io/badger/v2"
|
2020-06-01 18:11:55 +00:00
|
|
|
badger "github.com/ipfs/go-ds-badger2"
|
|
|
|
levelds "github.com/ipfs/go-ds-leveldb"
|
2020-07-07 13:29:16 +00:00
|
|
|
measure "github.com/ipfs/go-ds-measure"
|
2020-06-01 18:11:55 +00:00
|
|
|
ldbopts "github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
)
|
|
|
|
|
2020-07-06 20:03:37 +00:00
|
|
|
type dsCtor func(path string) (datastore.Batching, error)
|
|
|
|
|
|
|
|
var fsDatastores = map[string]dsCtor{
|
2020-06-01 18:11:55 +00:00
|
|
|
"chain": badgerDs,
|
|
|
|
"metadata": levelDs,
|
|
|
|
|
|
|
|
// Those need to be fast for large writes... but also need a really good GC :c
|
|
|
|
"staging": badgerDs, // miner specific
|
2020-07-06 20:03:37 +00:00
|
|
|
|
|
|
|
"client": badgerDs, // client specific
|
2020-06-01 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func badgerDs(path string) (datastore.Batching, error) {
|
|
|
|
opts := badger.DefaultOptions
|
2020-07-08 18:24:34 +00:00
|
|
|
opts.Options = dgbadger.DefaultOptions("").WithTruncate(true).
|
|
|
|
WithValueThreshold(1 << 10)
|
2020-06-01 18:11:55 +00:00
|
|
|
|
|
|
|
return badger.NewDatastore(path, &opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func levelDs(path string) (datastore.Batching, error) {
|
|
|
|
return levelds.NewDatastore(path, &levelds.Options{
|
|
|
|
Compression: ldbopts.NoCompression,
|
2020-08-06 01:16:54 +00:00
|
|
|
NoSync: false,
|
|
|
|
Strict: ldbopts.StrictAll,
|
2020-06-01 18:11:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:03:37 +00:00
|
|
|
func (fsr *fsLockedRepo) openDatastores() (map[string]datastore.Batching, error) {
|
2020-06-01 18:11:55 +00:00
|
|
|
if err := os.MkdirAll(fsr.join(fsDatastore), 0755); err != nil {
|
|
|
|
return nil, xerrors.Errorf("mkdir %s: %w", fsr.join(fsDatastore), err)
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:03:37 +00:00
|
|
|
out := map[string]datastore.Batching{}
|
2020-06-01 18:11:55 +00:00
|
|
|
|
|
|
|
for p, ctor := range fsDatastores {
|
|
|
|
prefix := datastore.NewKey(p)
|
|
|
|
|
|
|
|
// TODO: optimization: don't init datastores we don't need
|
|
|
|
ds, err := ctor(fsr.join(filepath.Join(fsDatastore, p)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("opening datastore %s: %w", prefix, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ds = measure.New("fsrepo."+p, ds)
|
|
|
|
|
2020-07-06 20:03:37 +00:00
|
|
|
out[datastore.NewKey(p).String()] = ds
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:11:55 +00:00
|
|
|
func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) {
|
|
|
|
fsr.dsOnce.Do(func() {
|
2020-07-17 20:14:03 +00:00
|
|
|
fsr.ds, fsr.dsErr = fsr.openDatastores()
|
2020-06-01 18:11:55 +00:00
|
|
|
})
|
2020-07-17 20:14:03 +00:00
|
|
|
|
2020-06-01 18:11:55 +00:00
|
|
|
if fsr.dsErr != nil {
|
|
|
|
return nil, fsr.dsErr
|
|
|
|
}
|
2020-07-06 20:03:37 +00:00
|
|
|
ds, ok := fsr.ds[ns]
|
|
|
|
if ok {
|
|
|
|
return ds, nil
|
|
|
|
}
|
2020-07-17 20:14:03 +00:00
|
|
|
return nil, xerrors.Errorf("no such datastore: %s", ns)
|
2020-07-07 09:38:22 +00:00
|
|
|
}
|