Fix IncrBy and DecrBy in memory keyvalue implementation if there was no value set previously
This commit is contained in:
parent
0ab9ce9ec4
commit
d600d8b5a6
1 changed files with 10 additions and 0 deletions
|
@ -66,6 +66,11 @@ func (s *Storage) IncrBy(key string, update int64) (err error) {
|
||||||
s.mutex.Lock()
|
s.mutex.Lock()
|
||||||
defer s.mutex.Unlock()
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
|
_, exists := s.store[key]
|
||||||
|
if !exists {
|
||||||
|
s.store[key] = int64(0)
|
||||||
|
}
|
||||||
|
|
||||||
val, is := s.store[key].(int64)
|
val, is := s.store[key].(int64)
|
||||||
if !is {
|
if !is {
|
||||||
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
||||||
|
@ -80,6 +85,11 @@ func (s *Storage) DecrBy(key string, update int64) (err error) {
|
||||||
s.mutex.Lock()
|
s.mutex.Lock()
|
||||||
defer s.mutex.Unlock()
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
|
_, exists := s.store[key]
|
||||||
|
if !exists {
|
||||||
|
s.store[key] = int64(0)
|
||||||
|
}
|
||||||
|
|
||||||
val, is := s.store[key].(int64)
|
val, is := s.store[key].(int64)
|
||||||
if !is {
|
if !is {
|
||||||
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
return &e.ErrValueHasWrongType{Key: key, ExpectedValue: "int64"}
|
||||||
|
|
Loading…
Reference in a new issue