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-06 23:39:30 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Mgr struct {
|
2020-07-07 09:38:22 +00:00
|
|
|
mds *MultiStore
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-07-06 23:39:30 +00:00
|
|
|
func New(mds *MultiStore, ds datastore.Batching) *Mgr {
|
|
|
|
return &Mgr{
|
|
|
|
mds: mds,
|
2020-07-07 09:38:22 +00:00
|
|
|
Blockstore: &multiReadBs{
|
2020-07-06 23:39:30 +00:00
|
|
|
mds: mds,
|
|
|
|
},
|
|
|
|
|
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() (int64, *Store, error) {
|
|
|
|
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 int64, key, value string) error { // source, file path, data CID..
|
|
|
|
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-07 08:52:19 +00:00
|
|
|
func (m *Mgr) List() []int64 {
|
|
|
|
return m.mds.List()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mgr) Info(id int64) (*StoreMeta, error) {
|
|
|
|
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-06 23:39:30 +00:00
|
|
|
// m.Info
|
2020-07-07 09:38:22 +00:00
|
|
|
// m.Delete
|