Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Badger datastore #4007

Merged
merged 11 commits into from
Sep 8, 2017
Merged
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,10 @@
"version": "1.1.11"
},
{
"author": "whyrusleeping",
"hash": "QmWRCn8vruNAzHx8i6SAXinuheRitKEGu8c7m26stKvsYx",
"name": "go-testutil",
"version": "1.1.11"
"author": "magik6k",
"hash": "QmNWbaGdPCA3anCcvh4jm3VAahAbmmAsU58sp8Ti4KTJkL",
"name": "go-ds-badger",
"version": "0.2.0"
}
],
"gxVersion": "0.10.0",
Expand Down
12 changes: 12 additions & 0 deletions repo/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,16 @@ var ConfigProfiles = map[string]func(*Config) error{
c.Discovery.MDNS.Enabled = false
return nil
},
"badgerds": func(c *Config) error {
c.Datastore.Spec = map[string]interface{}{
"type": "measure",
"prefix": "badger.datastore",
"child": map[string]interface{}{
"type": "badgerds",
"path": "badgerds",
"syncWrites": true,
},
}
return nil
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing something, but won't this config bypass the measure datastore? If so will this create a problem?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC there is global measure in fsrepo

74 changes: 67 additions & 7 deletions repo/fsrepo/datastores.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@ import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"

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

levelds "gx/ipfs/QmPdvXuXWAR6gtxxqZw42RtSADMwz4ijVmYHGS542b6cMz/go-ds-leveldb"
measure "gx/ipfs/QmSb95iHExSSb47zpmyn5CyY5PZidVWSjyKyDqgYQrnKor/go-ds-measure"
flatfs "gx/ipfs/QmUTshC2PP4ZDqkrFfDU4JGJFMWjYnunxPgkQ6ZCA2hGqh/go-ds-flatfs"

ds "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore"
mount "gx/ipfs/QmVSase1JP7cq9QkPT46oNwdp9pT6kBkG3oqS14y3QcZjG/go-datastore/syncmount"

badgerds "gx/ipfs/QmNWbaGdPCA3anCcvh4jm3VAahAbmmAsU58sp8Ti4KTJkL/go-ds-badger"
levelds "gx/ipfs/QmPdvXuXWAR6gtxxqZw42RtSADMwz4ijVmYHGS542b6cMz/go-ds-leveldb"
badger "gx/ipfs/QmQL7yJ4iWQdeAH9WvgJ4XYHS6m5DqL853Cck5SaUb8MAw/badger"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't declared in package.json. However, if you update to go-ds-badger 0.2.1, you can use the re-exported badgerds.DefaultOptions and badgerds.Options and avoid this import altogether.

ldbopts "gx/ipfs/QmbBhyDKsY4mbY6xsKt3qu9Y7FPvMJ6qbD8AMjYYvPRw1g/goleveldb/leveldb/opt"
)

Expand Down Expand Up @@ -55,12 +60,13 @@ var datastores map[string]ConfigFromMap

func init() {
datastores = map[string]ConfigFromMap{
"mount": MountDatastoreConfig,
"flatfs": FlatfsDatastoreConfig,
"levelds": LeveldsDatastoreConfig,
"mem": MemDatastoreConfig,
"log": LogDatastoreConfig,
"measure": MeasureDatastoreConfig,
"mount": MountDatastoreConfig,
"flatfs": FlatfsDatastoreConfig,
"levelds": LeveldsDatastoreConfig,
"badgerds": BadgerdsDatastoreConfig,
"mem": MemDatastoreConfig,
"log": LogDatastoreConfig,
"measure": MeasureDatastoreConfig,
}
}

Expand Down Expand Up @@ -333,3 +339,57 @@ func (c measureDatastoreConfig) Create(path string) (repo.Datastore, error) {
}
return measure.New(c.prefix, child), nil
}

type badgerdsDatastoreConfig struct {
path string
syncWrites bool
}

// BadgerdsDatastoreConfig returns a configuration stub for a badger datastore
// from the given parameters
func BadgerdsDatastoreConfig(params map[string]interface{}) (DatastoreConfig, error) {
var c badgerdsDatastoreConfig
var ok bool

c.path, ok = params["path"].(string)
if !ok {
return nil, fmt.Errorf("'path' field is missing or not string")
}

sw, ok := params["syncWrites"]
if !ok {
c.syncWrites = true
} else {
if swb, ok := sw.(bool); ok {
c.syncWrites = swb
} else {
return nil, fmt.Errorf("'syncWrites' field was not a boolean")
}
}

return &c, nil
}

func (c *badgerdsDatastoreConfig) DiskSpec() DiskSpec {
return map[string]interface{}{
"type": "badgerds",
"path": c.path,
}
}

func (c *badgerdsDatastoreConfig) Create(path string) (repo.Datastore, error) {
p := c.path
if !filepath.IsAbs(p) {
p = filepath.Join(path, p)
}

err := os.MkdirAll(p, 0755)
if err != nil {
return nil, err
}

defopts := badger.DefaultOptions
defopts.SyncWrites = c.syncWrites

return badgerds.NewDatastore(p, &defopts)
}