Skip to content

Commit

Permalink
FIX: nil pointer when ErrKeyNotFound & rename var
Browse files Browse the repository at this point in the history
  • Loading branch information
wilhelmliao committed Oct 17, 2023
1 parent 3956d47 commit 5a5d61e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion config.yaml.sample
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ListenAddress: :8962
Engine: file
DataPath: ./.data/dump
KeyDiscardInterval: 10s
KeyDiscardInterval: 90s
KeyDiscardRatio: 0.7
LogFlags:
- default
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func main() {
conn.WriteError(errors.New("ERR wrong number of arguments for 'IncrBy' command"))
} else {
var (
name = args[1].Bytes()
value = int64(args[2].Integer())
name = args[1].Bytes()
increment = int64(args[2].Integer())

constraints []sdk.Constraint[int64]
)
Expand Down Expand Up @@ -170,7 +170,7 @@ func main() {
}
}

value, err := db.IncrBy(name, value, constraints...)
value, err := db.IncrBy(name, increment, constraints...)
if err != nil {
conn.WriteError(err)
return true
Expand All @@ -184,8 +184,8 @@ func main() {
conn.WriteError(errors.New("ERR wrong number of arguments for 'IncrByFloat' command"))
} else {
var (
name = args[1].Bytes()
value = args[2].Float()
name = args[1].Bytes()
increment = args[2].Float()

constraints []sdk.Constraint[float64]
)
Expand Down Expand Up @@ -250,7 +250,7 @@ func main() {
}
}

value, err := db.IncrByFloat(name, value, constraints...)
value, err := db.IncrByFloat(name, increment, constraints...)
if err != nil {
conn.WriteError(err)
} else {
Expand Down
40 changes: 22 additions & 18 deletions storage/badger/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,19 @@ func (db *DB) IncrBy(key []byte, increment int64, constraints ...sdk.Constraint[
{
var number int64 = 0

err = item.Value(func(val []byte) error {
n, err := strconv.ParseInt(string(val), 10, 64)
if item != nil {
err = item.Value(func(val []byte) error {
n, err := strconv.ParseInt(string(val), 10, 64)
if err != nil {
return sdk.ErrNonInteger
}

number = n
return nil
})
if err != nil {
return sdk.ErrNonInteger
return err
}

number = n
return nil
})
if err != nil {
return err
}

// add increment & export
Expand Down Expand Up @@ -266,17 +268,19 @@ func (db *DB) IncrByFloat(key []byte, increment float64, constraints ...sdk.Cons
{
var number float64 = 0

err = item.Value(func(val []byte) error {
n, err := strconv.ParseFloat(string(val), 64)
if item != nil {
err = item.Value(func(val []byte) error {
n, err := strconv.ParseFloat(string(val), 64)
if err != nil {
return sdk.ErrNonInteger
}

number = n
return nil
})
if err != nil {
return sdk.ErrNonInteger
return err
}

number = n
return nil
})
if err != nil {
return err
}

// add increment & export
Expand Down

0 comments on commit 5a5d61e

Please sign in to comment.