Skip to content

Commit

Permalink
make things super customizable
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Jan 8, 2017
1 parent 2af9a44 commit bc6d256
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 31 deletions.
120 changes: 90 additions & 30 deletions repo/fsrepo/datastores.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,140 @@ package fsrepo
import (
"encoding/json"
"fmt"
"strings"
"path/filepath"

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

ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
mount "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore/syncmount"
levelds "gx/ipfs/QmaHHmfEozrrotyhyN44omJouyuEtx6ahddqV6W5yRaUSQ/go-ds-leveldb"
ldb "gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/opt"
ldbopts "gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/opt"
"gx/ipfs/QmbUSMTQtK9GRrUbD4ngqJwSzHsquUc8nyDubRWp4vPybH/go-ds-measure"
"gx/ipfs/Qmbx2KUs8mUbDUiiESzC1ms7mdmh4pRu8X1V1tffC46M4n/go-ds-flatfs"
)

func openDatastore(kind string, params []byte) (repo.Datastore, error) {
func (r *FSRepo) constructDatastore(kind string, params []byte) (repo.Datastore, error) {
switch kind {
case "mount":
var mountmap map[string]*json.RawMessage
if err := json.Unmarshal(params, &mountmap); err != nil {
var mounts []*mountConfig
if err := json.Unmarshal(params, &mounts); err != nil {
return nil, fmt.Errorf("datastore mount: %v", err)
}

return openMountDatastore(mountmap)
return r.openMountDatastore(mounts)
case "flatfs":
var flatfsparams config.FlatDS
if err := json.Unmarshal(params, &flatfsparams); err != nil {
return nil, fmt.Errorf("datastore mount: %v", err)
return nil, fmt.Errorf("datastore flatfs: %v", err)
}

return r.openFlatfsDatastore(&flatfsparams)
case "mem":
return ds.NewMapDatastore(), nil
case "log":
var cfg struct {
Name string
ChildType string
Child *json.RawMessage
}

if err := json.Unmarshal(params, &cfg); err != nil {
return nil, fmt.Errorf("datastore measure: %v", err)
}

child, err := r.constructDatastore(cfg.ChildType, []byte(*cfg.Child))
if err != nil {
return nil, err
}

return ds.NewLogDatastore(child, cfg.Name), nil

case "measure":
var measureOpts struct {
Prefix string
ChildType string
Child *json.RawMessage
}

if err := json.Unmarshal(params, &measureOpts); err != nil {
return nil, fmt.Errorf("datastore measure: %v", err)
}

child, err := r.constructDatastore(measureOpts.ChildType, []byte(*measureOpts.Child))
if err != nil {
return nil, err
}

return openFlatfsDatastore(&flatfsparams)
return r.openMeasureDB(measureOpts.Prefix, child)

case "levelds":
var c config.LevelDB
if err := json.Unmarshal(params, &c); err != nil {
return nil, fmt.Errorf("datastore levelds: %v", err)
}

return r.openLeveldbDatastore(&c)

default:
return nil, fmt.Errorf("unknown datastore type: %s", kind)
}
}

func openMountDatastore(mountmap map[string]*json.RawMessage) (repo.Datastore, error) {
var mounts []mount.Mount
for k, v := range mountmap {
vals := strings.Split(k, "@")
if len(vals) != 2 {
return nil, fmt.Errorf("mount config must be 'type@path'")
}
type mountConfig struct {
Path string
ChildType string
Child *json.RawMessage
}

kind := vals[0]
path := vals[1]
func (r *FSRepo) openMountDatastore(mountcfg []*mountConfig) (repo.Datastore, error) {
var mounts []mount.Mount
for _, cfg := range mountcfg {

child, err := openDatastore(kind, []byte(*v))
child, err := r.constructDatastore(cfg.ChildType, []byte(*cfg.Child))
if err != nil {
return nil, err
}

mounts = append(mounts, mount.Mount{
Datastore: child,
Prefix: ds.NewKey(path),
Prefix: ds.NewKey(cfg.Path),
})
}

return mount.New(mounts), nil
}

func openFlatfsDatastore(params *config.FlatDS) (repo.Datastore, error) {
return flatfs.New(params.Path, params.PrefixLen, params.Sync)
func (r *FSRepo) openFlatfsDatastore(params *config.FlatDS) (repo.Datastore, error) {
p := params.Path
if !filepath.IsAbs(p) {
p = filepath.Join(r.path, p)
}
return flatfs.New(p, params.PrefixLen, params.Sync)
}

func openLeveldbDatastore(params *config.LevelDB) (repo.Datastore, error) {
var compress ldb.Compression
func (r *FSRepo) openLeveldbDatastore(params *config.LevelDB) (repo.Datastore, error) {
p := params.Path
if !filepath.IsAbs(p) {
p = filepath.Join(r.path, p)
}

var c ldbopts.Compression
switch params.Compression {
case "snappy":
compress = ldb.SnappyCompression
case "none":
compress = ldb.NoCompression
c = ldbopts.NoCompression
case "snappy":
c = ldbopts.SnappyCompression
case "":
fallthrough
default:
compress = ldb.DefaultCompression
c = ldbopts.DefaultCompression
}

return levelds.NewDatastore(params.Path, &levelds.Options{
Compression: compress,
return levelds.NewDatastore(p, &levelds.Options{
Compression: c,
})
}

func (r *FSRepo) openMeasureDB(prefix string, child repo.Datastore) (repo.Datastore, error) {
return measure.New(prefix, child), nil
}
2 changes: 1 addition & 1 deletion repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (r *FSRepo) openDatastore() error {
}
r.ds = d
default:
d, err := openDatastore(r.config.Datastore.Type, r.config.Datastore.ParamData())
d, err := r.constructDatastore(r.config.Datastore.Type, r.config.Datastore.ParamData())
if err != nil {
return err
}
Expand Down

0 comments on commit bc6d256

Please sign in to comment.