-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It doesn't work due to dep cycle License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
- Loading branch information
Showing
9 changed files
with
218 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.