Skip to content

Commit

Permalink
apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
fredcarle committed Aug 18, 2022
1 parent 89724d0 commit cf2a0a1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 34 deletions.
45 changes: 23 additions & 22 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (cfg *Config) Load(rootDirPath string) error {
if err != nil {
return err
}
cfg.setBadgerVLogMaxSize()

cfg.handleParams(rootDirPath)
err = cfg.validate()
if err != nil {
Expand Down Expand Up @@ -137,7 +137,7 @@ func (cfg *Config) LoadWithoutRootDir() error {
if err != nil {
log.FatalE(context.Background(), "Could not get home directory", err)
}
cfg.setBadgerVLogMaxSize()

cfg.handleParams(rootDir)
err = cfg.validate()
if err != nil {
Expand Down Expand Up @@ -177,6 +177,7 @@ func (cfg *Config) handleParams(rootDir string) {
if !filepath.IsAbs(cfg.Datastore.Badger.Path) {
cfg.Datastore.Badger.Path = filepath.Join(rootDir, cfg.Datastore.Badger.Path)
}
cfg.setBadgerVLogMaxSize()
}

func (cfg *Config) setBadgerVLogMaxSize() {
Expand All @@ -200,15 +201,15 @@ type BadgerConfig struct {
type ByteSize uint64

const (
B ByteSize = 1
KB = B << 10
MB = KB << 10
GB = MB << 10
TB = GB << 10
PB = TB << 10
B ByteSize = 1
KiB = B << 10
MiB = KiB << 10
GiB = MiB << 10
TiB = GiB << 10
PiB = TiB << 10
)

// UnmarshallText calls Set on ByteSize with the given text
// UnmarshalText calls Set on ByteSize with the given text
func (bs *ByteSize) UnmarshalText(text []byte) error {
return bs.Set(string(text))
}
Expand All @@ -232,16 +233,16 @@ func (bs *ByteSize) Set(s string) error {
switch strings.ToUpper(strings.Trim(unit, " ")) {
case "B":
*bs = ByteSize(digits) * B
case "KB":
*bs = ByteSize(digits) * KB
case "MB":
*bs = ByteSize(digits) * MB
case "GB":
*bs = ByteSize(digits) * GB
case "TB":
*bs = ByteSize(digits) * TB
case "PB":
*bs = ByteSize(digits) * PB
case "KB", "KIB":
*bs = ByteSize(digits) * KiB
case "MB", "MIB":
*bs = ByteSize(digits) * MiB
case "GB", "GIB":
*bs = ByteSize(digits) * GiB
case "TB", "TIB":
*bs = ByteSize(digits) * TiB
case "PB", "PIB":
*bs = ByteSize(digits) * PiB
default:
*bs = ByteSize(digits)
}
Expand All @@ -251,7 +252,7 @@ func (bs *ByteSize) Set(s string) error {

// String returns the string formatted output of ByteSize
func (bs *ByteSize) String() string {
const unit = 1000
const unit = 1024
bsInt := int64(*bs)
if bsInt < unit {
return fmt.Sprintf("%d", bsInt)
Expand All @@ -261,7 +262,7 @@ func (bs *ByteSize) String() string {
div *= unit
exp++
}
return fmt.Sprintf("%d%cB", bsInt/div, "KMGTP"[exp])
return fmt.Sprintf("%d%ciB", bsInt/div, "KMGTP"[exp])
}

// Type returns the type as a string.
Expand All @@ -281,7 +282,7 @@ func defaultDatastoreConfig() *DatastoreConfig {
Store: "badger",
Badger: BadgerConfig{
Path: "data",
ValueLogFileSize: 1 * GB,
ValueLogFileSize: 1 * GiB,
Options: &opts,
},
}
Expand Down
53 changes: 44 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func TestNodeConfig(t *testing.T) {
assert.Equal(t, expectedOptions.EnableRelay, options.EnableRelay)
}

func TestUnmarshallByteSize(t *testing.T) {
func TestUnmarshalByteSize(t *testing.T) {
var bs ByteSize

b := []byte("10")
Expand Down Expand Up @@ -371,42 +371,77 @@ func TestUnmarshallByteSize(t *testing.T) {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*KB, bs)
assert.Equal(t, 10*KiB, bs)

kb = []byte("10KiB")
err = bs.UnmarshalText(kb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*KiB, bs)

kb = []byte("10 kb")
err = bs.UnmarshalText(kb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*KB, bs)
assert.Equal(t, 10*KiB, bs)

mb := []byte("10MB")
err = bs.UnmarshalText(mb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*MB, bs)
assert.Equal(t, 10*MiB, bs)

mb = []byte("10MiB")
err = bs.UnmarshalText(mb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*MiB, bs)

gb := []byte("10GB")
err = bs.UnmarshalText(gb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*GB, bs)
assert.Equal(t, 10*GiB, bs)

gb = []byte("10GiB")
err = bs.UnmarshalText(gb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*GiB, bs)

tb := []byte("10TB")
err = bs.UnmarshalText(tb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*TB, bs)
assert.Equal(t, 10*TiB, bs)

tb = []byte("10TiB")
err = bs.UnmarshalText(tb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*TiB, bs)

pb := []byte("10PB")
err = bs.UnmarshalText(pb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*PB, bs)
assert.Equal(t, 10*PiB, bs)

pb = []byte("10PiB")
err = bs.UnmarshalText(pb)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 10*PiB, bs)

eb := []byte("१")
err = bs.UnmarshalText(eb)
Expand All @@ -422,6 +457,6 @@ func TestByteSizeToString(t *testing.T) {
b := 999 * B
assert.Equal(t, "999", b.String())

mb := 10 * MB
assert.Equal(t, "10MB", mb.String())
mb := 10 * MiB
assert.Equal(t, "10MiB", mb.String())
}
2 changes: 1 addition & 1 deletion config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ datastore:
store: {{ .Datastore.Store }}
badger:
path: {{ .Datastore.Badger.Path }}
# Maximum file size of the value log files. The in memory file size will be 2*valuelogfilesize.
# Maximum file size of the value log files. The in-memory file size will be 2*valuelogfilesize.
# Human friendly units can be used (ex: 500MB).
valuelogfilesize: {{ .Datastore.Badger.ValueLogFileSize }}
# memory:
Expand Down
4 changes: 2 additions & 2 deletions config/configfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestReadConfigFileForDatastore(t *testing.T) {
cfg := DefaultConfig()
cfg.Datastore.Store = "badger"
cfg.Datastore.Badger.Path = "dataPath"
cfg.Datastore.Badger.ValueLogFileSize = 512 * MB
cfg.Datastore.Badger.ValueLogFileSize = 512 * MiB

err := cfg.WriteConfigFileToRootDir(dir)
if err != nil {
Expand All @@ -131,5 +131,5 @@ func TestReadConfigFileForDatastore(t *testing.T) {
}
assert.Equal(t, cfg.Datastore.Store, cfgFromFile.Datastore.Store)
assert.Equal(t, dir+"/"+cfg.Datastore.Badger.Path, cfgFromFile.Datastore.Badger.Path)
assert.NotEqual(t, cfg.Datastore.Badger.ValueLogFileSize, cfgFromFile.Datastore.Badger.ValueLogFileSize)
assert.Equal(t, cfg.Datastore.Badger.ValueLogFileSize, cfgFromFile.Datastore.Badger.ValueLogFileSize)
}

0 comments on commit cf2a0a1

Please sign in to comment.