2020-07-06 23:39:30 +00:00
|
|
|
package importmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-07-07 08:52:19 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-07-28 14:38:16 +00:00
|
|
|
"github.com/filecoin-project/go-multistore"
|
2021-01-29 20:01:00 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2020-07-06 23:39:30 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mgr struct {
|
2020-07-24 21:47:22 +00:00
|
|
|
mds *multistore.MultiStore
|
2020-07-07 09:38:22 +00:00
|
|
|
ds datastore.Batching
|
2020-07-07 08:52:19 +00:00
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
Blockstore blockstore.BasicBlockstore
|
2020-07-06 23:39:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
type Label string
|
2020-07-07 09:38:22 +00:00
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
const (
|
2021-07-01 16:34:51 +00:00
|
|
|
LSource = "source" // Function which created the import
|
|
|
|
LRootCid = "root" // Root CID
|
|
|
|
LFileName = "filename" // Local file path
|
|
|
|
LMTime = "mtime" // File modification timestamp
|
|
|
|
LCARv2FilePath = "CARv2Path" // path of the CARv2 file.
|
2020-07-07 08:52:19 +00:00
|
|
|
)
|
|
|
|
|
2020-07-24 21:47:22 +00:00
|
|
|
func New(mds *multistore.MultiStore, ds datastore.Batching) *Mgr {
|
2020-07-06 23:39:30 +00:00
|
|
|
return &Mgr{
|
2020-07-24 21:47:22 +00:00
|
|
|
mds: mds,
|
2021-01-29 20:01:00 +00:00
|
|
|
Blockstore: blockstore.Adapt(mds.MultiReadBlockstore()),
|
2020-07-06 23:39:30 +00:00
|
|
|
|
2020-07-07 09:38:09 +00:00
|
|
|
ds: datastore.NewLogDatastore(namespace.Wrap(ds, datastore.NewKey("/stores")), "storess"),
|
2020-07-06 23:39:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
type StoreMeta struct {
|
2020-07-06 23:39:30 +00:00
|
|
|
Labels map[string]string
|
|
|
|
}
|
|
|
|
|
2020-07-28 06:13:10 +00:00
|
|
|
func (m *Mgr) NewStore() (multistore.StoreID, *multistore.Store, error) {
|
2020-07-06 23:39:30 +00:00
|
|
|
id := m.mds.Next()
|
|
|
|
st, err := m.mds.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
meta, err := json.Marshal(&StoreMeta{Labels: map[string]string{
|
2020-07-06 23:39:30 +00:00
|
|
|
"source": "unknown",
|
|
|
|
}})
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, xerrors.Errorf("marshaling empty store metadata: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.ds.Put(datastore.NewKey(fmt.Sprintf("%d", id)), meta)
|
|
|
|
return id, st, err
|
|
|
|
}
|
|
|
|
|
2020-07-28 06:13:10 +00:00
|
|
|
func (m *Mgr) AddLabel(id multistore.StoreID, key, value string) error { // source, file path, data CID..
|
2020-07-06 23:39:30 +00:00
|
|
|
meta, err := m.ds.Get(datastore.NewKey(fmt.Sprintf("%d", id)))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting metadata form datastore: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:52:19 +00:00
|
|
|
var sm StoreMeta
|
2020-07-06 23:39:30 +00:00
|
|
|
if err := json.Unmarshal(meta, &sm); err != nil {
|
|
|
|
return xerrors.Errorf("unmarshaling store meta: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.Labels[key] = value
|
|
|
|
|
2020-07-07 09:38:09 +00:00
|
|
|
meta, err = json.Marshal(&sm)
|
2020-07-06 23:39:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("marshaling store meta: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.ds.Put(datastore.NewKey(fmt.Sprintf("%d", id)), meta)
|
|
|
|
}
|
|
|
|
|
2020-07-28 06:13:10 +00:00
|
|
|
func (m *Mgr) List() []multistore.StoreID {
|
2020-07-07 08:52:19 +00:00
|
|
|
return m.mds.List()
|
|
|
|
}
|
|
|
|
|
2020-07-28 06:13:10 +00:00
|
|
|
func (m *Mgr) Info(id multistore.StoreID) (*StoreMeta, error) {
|
2020-07-07 08:52:19 +00:00
|
|
|
meta, err := m.ds.Get(datastore.NewKey(fmt.Sprintf("%d", id)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting metadata form datastore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var sm StoreMeta
|
|
|
|
if err := json.Unmarshal(meta, &sm); err != nil {
|
|
|
|
return nil, xerrors.Errorf("unmarshaling store meta: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &sm, nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 06:13:10 +00:00
|
|
|
func (m *Mgr) Remove(id multistore.StoreID) error {
|
2020-07-07 11:45:02 +00:00
|
|
|
if err := m.mds.Delete(id); err != nil {
|
|
|
|
return xerrors.Errorf("removing import: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.ds.Delete(datastore.NewKey(fmt.Sprintf("%d", id))); err != nil {
|
|
|
|
return xerrors.Errorf("removing import metadata: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|