repo: update Datastore, Init

This commit is contained in:
Łukasz Magiera 2019-07-10 19:09:57 +02:00
parent ba456be68f
commit 1f8c3f4145
3 changed files with 38 additions and 2 deletions

1
go.mod
View File

@ -39,6 +39,7 @@ require (
github.com/libp2p/go-libp2p-yamux v0.2.1
github.com/libp2p/go-maddr-filter v0.0.4
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-dns v0.0.2
github.com/multiformats/go-multihash v0.0.5

2
go.sum
View File

@ -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.1.0 h1:U41/2erhAKcmSI14xh/ZTUdBPOzDOIfS93ibzUSl8KM=
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.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=

View File

@ -6,12 +6,17 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
badger "github.com/ipfs/go-ds-badger"
fslock "github.com/ipfs/go-fs-lock"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/mitchellh/go-homedir"
"github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-lotus/node/config"
@ -25,6 +30,10 @@ const (
fsLock = "repo.lock"
)
var log = logging.Logger("repo")
var ErrRepoExists = errors.New("repo exists")
// FsRepo is struct for repo, use NewFS to create
type FsRepo struct {
path string
@ -34,11 +43,28 @@ var _ Repo = &FsRepo{}
// NewFS creates a repo instance based on a path on file system
func NewFS(path string) (*FsRepo, error) {
path, err := homedir.Expand(path)
if err != nil {
return &FsRepo{}, err
}
return &FsRepo{
path: path,
}, 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
func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
p := filepath.Join(fsr.path, fsAPI)
@ -88,6 +114,10 @@ func (fsr *FsRepo) Lock() (LockedRepo, error) {
type fsLockedRepo struct {
path string
closer io.Closer
ds datastore.Batching
dsErr error
dsOnce sync.Once
}
func (fsr *fsLockedRepo) Close() error {
@ -114,8 +144,11 @@ func (fsr *fsLockedRepo) stillValid() error {
return nil
}
func (fsr *fsLockedRepo) Datastore() (datastore.Datastore, error) {
return badger.NewDatastore(fsr.join(fsDatastore), nil)
func (fsr *fsLockedRepo) Datastore(ns string) (datastore.Batching, error) {
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) {