repo: update Datastore, Init
This commit is contained in:
parent
ba456be68f
commit
1f8c3f4145
1
go.mod
1
go.mod
@ -39,6 +39,7 @@ require (
|
|||||||
github.com/libp2p/go-libp2p-yamux v0.2.1
|
github.com/libp2p/go-libp2p-yamux v0.2.1
|
||||||
github.com/libp2p/go-maddr-filter v0.0.4
|
github.com/libp2p/go-maddr-filter v0.0.4
|
||||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
|
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
github.com/multiformats/go-multiaddr v0.0.4
|
github.com/multiformats/go-multiaddr v0.0.4
|
||||||
github.com/multiformats/go-multiaddr-dns v0.0.2
|
github.com/multiformats/go-multiaddr-dns v0.0.2
|
||||||
github.com/multiformats/go-multihash v0.0.5
|
github.com/multiformats/go-multihash v0.0.5
|
||||||
|
2
go.sum
2
go.sum
@ -350,6 +350,8 @@ github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+
|
|||||||
github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||||
github.com/minio/sha256-simd v0.1.0 h1:U41/2erhAKcmSI14xh/ZTUdBPOzDOIfS93ibzUSl8KM=
|
github.com/minio/sha256-simd v0.1.0 h1:U41/2erhAKcmSI14xh/ZTUdBPOzDOIfS93ibzUSl8KM=
|
||||||
github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||||
github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||||
github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
|
github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
|
||||||
|
@ -6,12 +6,17 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/ipfs/go-datastore"
|
"github.com/ipfs/go-datastore"
|
||||||
|
"github.com/ipfs/go-datastore/namespace"
|
||||||
badger "github.com/ipfs/go-ds-badger"
|
badger "github.com/ipfs/go-ds-badger"
|
||||||
fslock "github.com/ipfs/go-fs-lock"
|
fslock "github.com/ipfs/go-fs-lock"
|
||||||
|
logging "github.com/ipfs/go-log"
|
||||||
"github.com/libp2p/go-libp2p-core/crypto"
|
"github.com/libp2p/go-libp2p-core/crypto"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
"github.com/multiformats/go-multiaddr"
|
"github.com/multiformats/go-multiaddr"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/node/config"
|
"github.com/filecoin-project/go-lotus/node/config"
|
||||||
@ -25,6 +30,10 @@ const (
|
|||||||
fsLock = "repo.lock"
|
fsLock = "repo.lock"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logging.Logger("repo")
|
||||||
|
|
||||||
|
var ErrRepoExists = errors.New("repo exists")
|
||||||
|
|
||||||
// FsRepo is struct for repo, use NewFS to create
|
// FsRepo is struct for repo, use NewFS to create
|
||||||
type FsRepo struct {
|
type FsRepo struct {
|
||||||
path string
|
path string
|
||||||
@ -34,11 +43,28 @@ var _ Repo = &FsRepo{}
|
|||||||
|
|
||||||
// NewFS creates a repo instance based on a path on file system
|
// NewFS creates a repo instance based on a path on file system
|
||||||
func NewFS(path string) (*FsRepo, error) {
|
func NewFS(path string) (*FsRepo, error) {
|
||||||
|
path, err := homedir.Expand(path)
|
||||||
|
if err != nil {
|
||||||
|
return &FsRepo{}, err
|
||||||
|
}
|
||||||
|
|
||||||
return &FsRepo{
|
return &FsRepo{
|
||||||
path: path,
|
path: path,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fsr *FsRepo) Init() error {
|
||||||
|
if _, err := os.Stat(fsr.path); err == nil {
|
||||||
|
return ErrRepoExists
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Initializing repo at '%s'", fsr.path)
|
||||||
|
|
||||||
|
return os.Mkdir(fsr.path, 0755)
|
||||||
|
}
|
||||||
|
|
||||||
// APIEndpoint returns endpoint of API in this repo
|
// APIEndpoint returns endpoint of API in this repo
|
||||||
func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
|
func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
|
||||||
p := filepath.Join(fsr.path, fsAPI)
|
p := filepath.Join(fsr.path, fsAPI)
|
||||||
@ -88,6 +114,10 @@ func (fsr *FsRepo) Lock() (LockedRepo, error) {
|
|||||||
type fsLockedRepo struct {
|
type fsLockedRepo struct {
|
||||||
path string
|
path string
|
||||||
closer io.Closer
|
closer io.Closer
|
||||||
|
|
||||||
|
ds datastore.Batching
|
||||||
|
dsErr error
|
||||||
|
dsOnce sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fsr *fsLockedRepo) Close() error {
|
func (fsr *fsLockedRepo) Close() error {
|
||||||
@ -114,8 +144,11 @@ func (fsr *fsLockedRepo) stillValid() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fsr *fsLockedRepo) Datastore() (datastore.Datastore, error) {
|
func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) {
|
||||||
return badger.NewDatastore(fsr.join(fsDatastore), nil)
|
fsr.dsOnce.Do(func() {
|
||||||
|
fsr.ds, fsr.dsErr = badger.NewDatastore(fsr.join(fsDatastore), nil)
|
||||||
|
})
|
||||||
|
return namespace.Wrap(fsr.ds, datastore.NewKey(ns)), fsr.dsErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fsr *fsLockedRepo) Config() (*config.Root, error) {
|
func (fsr *fsLockedRepo) Config() (*config.Root, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user