lotus/node/repo/importmgr/mgr.go

112 lines
2.6 KiB
Go
Raw Normal View History

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"
"github.com/filecoin-project/lotus/lib/blockstore"
2020-07-06 23:39:30 +00:00
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
)
type Mgr struct {
mds *multistore.MultiStore
2020-07-07 09:38:22 +00:00
ds datastore.Batching
2020-07-07 08:52:19 +00:00
Blockstore blockstore.Blockstore
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 (
2020-07-07 09:38:22 +00:00
LSource = "source" // Function which created the import
LRootCid = "root" // Root CID
2020-07-07 08:52:19 +00:00
LFileName = "filename" // Local file path
2020-07-07 09:38:22 +00:00
LMTime = "mtime" // File modification timestamp
2020-07-07 08:52:19 +00:00
)
func New(mds *multistore.MultiStore, ds datastore.Batching) *Mgr {
2020-07-06 23:39:30 +00:00
return &Mgr{
mds: mds,
Blockstore: 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
}
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
}
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)
}
func (m *Mgr) List() []multistore.StoreID {
2020-07-07 08:52:19 +00:00
return m.mds.List()
}
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
}
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
}