Skip to content

Commit

Permalink
--wip--
Browse files Browse the repository at this point in the history
It doesn't work due to dep cycle

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
  • Loading branch information
Kubuxu committed Mar 20, 2017
1 parent a0be5f9 commit a23eb2a
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 159 deletions.
21 changes: 5 additions & 16 deletions repo/config/datastore.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package config

import (
cfgds "github.com/ipfs/go-ipfs/repo/config/datastore"
)

// DefaultDataStoreDirectory is the directory to store all the local IPFS data.
const DefaultDataStoreDirectory = "datastore"

Expand All @@ -11,7 +15,7 @@ type Datastore struct {
Path string
NoSync bool // deprecated

Spec map[string]interface{}
Spec *cfgds.CoreCtor

HashOnRead bool
BloomFilterSize int
Expand All @@ -23,21 +27,6 @@ type S3Datastore struct {
ACL string `json:"acl"`
}

type FlatDS struct {
Path string
ShardFunc string
Sync bool
}

type LevelDB struct {
Path string
Compression string
}

type SbsDS struct {
Path string
}

// DataStorePath returns the default data store path given a configuration root
// (set an empty string to have the default configuration root)
func DataStorePath(configroot string) (string, error) {
Expand Down
24 changes: 24 additions & 0 deletions repo/config/datastore/ctor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cfgds

import (
"github.com/ipfs/go-ipfs/repo"

ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
)

var (
fErrUnknownDatastoreType = "unknown datastore: %s"
)

// Ctor interface is used to create Datastore from config specification
type Ctor interface {
Create(repo.Repo) (repo.Datastore, error)
}

type memCtor struct{}

func (_ *memCtor) Create(_ repo.Repo) (repo.Datastore, error) {
return ds.NewMapDatastore(), nil
}

var _ Ctor = (*memCtor)(nil)
13 changes: 13 additions & 0 deletions repo/config/datastore/mount.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cfgds

import "github.com/ipfs/go-ipfs/repo"

type mountCtor struct {
Mounts map[string]*CoreCtor
}

func (ctr mountCtor) Create(r repo.Repo) (repo.Datastore, error) {
return nil, nil
}

var _ Ctor = (*mountCtor)(nil)
13 changes: 13 additions & 0 deletions repo/config/datastore/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cfgds

// CtorCtor is a function that creates new Ctors
type CtorCtor func() Ctor

var registeredCtors = map[string]CtorCtor{
"mem": func() Ctor { return &memCtor{} },
}

// RegisterCtor allows for registration of Ctor under given name/type
func RegisterCtor(name string, cctor CtorCtor) {
registeredCtors[name] = cctor
}
39 changes: 39 additions & 0 deletions repo/config/datastore/trampoline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cfgds

import (
"encoding/json"
"fmt"

"github.com/ipfs/go-ipfs/repo"
)

// CoreCtor is entrypoint and trampoline for other datastore Ctors
type CoreCtor struct {
sub Ctor
}

func (tr *CoreCtor) UnmarshalJSON(data []byte) error {
typeStruct := &struct {
Type string
}{}

err := json.Unmarshal(data, typeStruct)
if err != nil {
return err
}

ctrctr, ok := registeredCtors[typeStruct.Type]
if !ok {
return fmt.Errorf(fErrUnknownDatastoreType, typeStruct.Type)
}
tr.sub = ctrctr()

return json.Unmarshal(data, tr.sub)
}

func (tr *CoreCtor) Create(r repo.Repo) (repo.Datastore, error) {
return tr.sub.Create(r)
}

var _ json.Unmarshaler = (*CoreCtor)(nil)
var _ Ctor = (*CoreCtor)(nil)
28 changes: 28 additions & 0 deletions repo/config/datastore/trampoline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cfgds

import (
"encoding/json"
"testing"

ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
)

func TestTrampolineToMem(t *testing.T) {
in := []byte(`{ "type": "mem" }`)
tr := &CoreCtor{}
err := json.Unmarshal(in, tr)
if err != nil {
t.Fatal(err)
}

uds, err := tr.Create()
if err != nil {
t.Fatal(err)
}

_, ok := uds.(*ds.MapDatastore)
if !ok {
t.Fatal("wrong datastore type")
}

}
138 changes: 0 additions & 138 deletions repo/fsrepo/datastores.go

This file was deleted.

87 changes: 87 additions & 0 deletions repo/fsrepo/datstores.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fsrepo

import (
"errors"
"path/filepath"

"github.com/ipfs/go-ipfs/repo"
dcfg "github.com/ipfs/go-ipfs/repo/config/datastore"

flatfs "gx/ipfs/QmXZEfbEv9sXG9JnLoMNhREDMDgkq5Jd7uWJ7d77VJ4pxn/go-ds-flatfs"
leveldb "gx/ipfs/QmaHHmfEozrrotyhyN44omJouyuEtx6ahddqV6W5yRaUSQ/go-ds-leveldb"
ldbopts "gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/opt"
)

var (
errNotFSRepo = errors.New("used repo is not FSRepo")
)

func fsRepoOrError(r repo.Repo) (*FSRepo, error) {
fsrepo, ok := r.(*FSRepo)
if !ok {
return nil, errNotFSRepo
}
return fsrepo, nil
}

func relPath(fsrepo *FSRepo, p string) string {
if !filepath.IsAbs(p) {
p = filepath.Join(fsrepo.path, p)
}
return p
}

func registerDStoreCtors() {
dcfg.RegisterCtor("leveldb", func() dcfg.Ctor { return &levelDBCtor{} })
dcfg.RegisterCtor("flatfs", func() dcfg.Ctor { return &flatfsCtor{} })
}

type flatfsCtor struct {
Path string
ShardFunc string
NoSync bool
}

func (ctr *flatfsCtor) Create(r repo.Repo) (repo.Datastore, error) {
fsrepo, err := fsRepoOrError(r)
if err != nil {
return nil, err
}

p := relPath(fsrepo, ctr.Path)
shardFunc, err := flatfs.ParseShardFunc(ctr.ShardFunc)
if err != nil {
return nil, err
}

return flatfs.CreateOrOpen(p, shardFunc, ctr.NoSync)
}

var _ dcfg.Ctor = (*flatfsCtor)(nil)

type levelDBCtor struct {
Path string
Compression string
}

func (ctr *levelDBCtor) Create(r repo.Repo) (repo.Datastore, error) {
fsrepo, err := fsRepoOrError(r)
if err != nil {
return nil, err
}

p := relPath(fsrepo, ctr.Path)
c := ldbopts.DefaultCompression
switch ctr.Compression {
case "none":
c = ldbopts.NoCompression
case "snappy":
c = ldbopts.SnappyCompression
}

return leveldb.NewDatastore(p, &leveldb.Options{
Compression: c,
})
}

var _ dcfg.Ctor = (*levelDBCtor)(nil)
Loading

0 comments on commit a23eb2a

Please sign in to comment.