Skip to content

Commit

Permalink
create option for low mem scenarios avoiding mmap (#255)
Browse files Browse the repository at this point in the history
* create option for low mem scenarios avoiding mmap

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* add variadic helper

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>

* add missing return

Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign authored Feb 21, 2020
1 parent ee78a66 commit b7556d7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ api/pb/dart/lib
# vscode config folder
.vscode/

**/node_modules
**/node_modules
tags
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/alecthomas/jsonschema v0.0.0-20191017121752-4bb6e3fae4f2
github.com/c-bata/go-prompt v0.2.3
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/dgraph-io/badger v1.6.0
github.com/evanphx/json-patch v4.5.0+incompatible
github.com/fatih/color v1.7.0
github.com/gogo/googleapis v1.3.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion store/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewManager(ts service.Service, opts ...Option) (*Manager, error) {
}

if config.Datastore == nil {
datastore, err := newDefaultDatastore(config.RepoPath)
datastore, err := newDefaultDatastore(config.RepoPath, config.LowMem)
if err != nil {
return nil, err
}
Expand Down
17 changes: 15 additions & 2 deletions store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"

"github.com/dgraph-io/badger/options"
ds "github.com/ipfs/go-datastore"
badger "github.com/ipfs/go-ds-badger"
core "github.com/textileio/go-threads/core/store"
Expand All @@ -24,18 +25,30 @@ type Config struct {
EventCodec core.EventCodec
JsonMode bool
Debug bool
LowMem bool
}

func newDefaultEventCodec(jsonMode bool) core.EventCodec {
return jsonpatcher.New(jsonMode)
}

func newDefaultDatastore(repoPath string) (ds.TxnDatastore, error) {
func newDefaultDatastore(repoPath string, lowMem bool) (ds.TxnDatastore, error) {
path := filepath.Join(repoPath, defaultDatastorePath)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return nil, err
}
return badger.NewDatastore(path, &badger.DefaultOptions)
opts := badger.DefaultOptions
if lowMem {
opts.TableLoadingMode = options.FileIO
}
return badger.NewDatastore(path, &opts)
}

func WithLowMem(low bool) Option {
return func(sc *Config) error {
sc.LowMem = low
return nil
}
}

func WithJsonMode(enabled bool) Option {
Expand Down
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewStore(ts service.Service, opts ...Option) (*Store, error) {
// with the same config.
func newStore(ts service.Service, config *Config) (*Store, error) {
if config.Datastore == nil {
datastore, err := newDefaultDatastore(config.RepoPath)
datastore, err := newDefaultDatastore(config.RepoPath, config.LowMem)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions store/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func DefaultService(repoPath string, opts ...ServiceOption) (ServiceBoostrapper,
// Build a logstore
logstorePath := filepath.Join(repoPath, defaultLogstorePath)
if err := os.MkdirAll(logstorePath, os.ModePerm); err != nil {
cancel()
return nil, err
}
logstore, err := badger.NewDatastore(logstorePath, &badger.DefaultOptions)
Expand Down

0 comments on commit b7556d7

Please sign in to comment.