Skip to content
This repository has been archived by the owner on Jun 15, 2022. It is now read-only.

Commit

Permalink
Add DecrBy function
Browse files Browse the repository at this point in the history
DecrementedBy feature added. Its tests and usage added. Also, missing comment line added in IncrBy

Signed-off-by: gozeloglu <gozeloglu@gmail.com>
  • Loading branch information
gozeloglu committed Nov 6, 2021
1 parent cf3b98c commit c78a70b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ func main() {
}
fmt.Printf("John's decremented age is: %s\n", decrAge)

decrAge, err = db.DecrBy("john", 3)
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("John's decremented age is: %s\n", decrAge)

exist := db.Exists("john")
fmt.Println(exist)

Expand Down
15 changes: 15 additions & 0 deletions decr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ func (k *Kvs) Decr(key string) (string, error) {
k.kv[key] = strconv.Itoa(valInt - 1)
return k.kv[key], nil
}

// DecrBy decreases the value by given value. If non-integer value is being
// tried to decrement, it returns error.
func (k *Kvs) DecrBy(key string, val int) (string, error) {
k.mu.Lock()
defer k.mu.Unlock()

v := k.kv[key]
valInt, err := strconv.Atoi(v)
if err != nil {
return "", err
}
k.kv[key] = strconv.Itoa(valInt - val)
return k.kv[key], nil
}
65 changes: 65 additions & 0 deletions decr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,68 @@ func TestKvs_DecrStr(t *testing.T) {
}
t.Logf("temp db removed.")
}

func TestKvs_DecrBy(t *testing.T) {
db, err := open(t.Name(), "", 2*time.Minute)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db created.")

k, v, i := "age", "12", 3
db.Set(k, v)
t.Logf("Key-value pair is set.")
t.Logf("%s: %s", k, v)
val, err := db.DecrBy(k, i)
if err != nil {
t.Fatalf(err.Error())
}

if val != "9" {
t.Errorf("decremented value is wrong: %s", val)
}
t.Logf("value is decremented.")
t.Logf("%s: %s", k, val)

err = db.Close()
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db closed.")

err = os.RemoveAll(db.dir)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("temp db removed.")
}

func TestKvs_DecrByStr(t *testing.T) {
db, err := open(t.Name(), "", 2*time.Minute)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db created.")

k, v := "foo", "bar"
db.Set(k, v)
t.Logf("Key-value pair is set.")
t.Logf("%s: %s", k, v)
_, err = db.DecrBy(k, 3)
if err == nil {
t.Fatalf("error expected, but nil got.")
}
t.Logf("value could not decremented. it is string.")

err = db.Close()
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db closed.")

err = os.RemoveAll(db.dir)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("temp db removed.")
}
6 changes: 6 additions & 0 deletions example/pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func main() {
}
fmt.Printf("John's decremented age is: %s\n", decrAge)

decrAge, err = db.DecrBy("john", 3)
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("John's decremented age is: %s\n", decrAge)

exist := db.Exists("john")
fmt.Println(exist)

Expand Down
2 changes: 1 addition & 1 deletion incr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (k *Kvs) Incr(key string) (string, error) {
}

// IncrBy increments the value by given value. If non-integer value is being
// tried to increment,
// tried to increment, it returns error.
func (k *Kvs) IncrBy(key string, val int) (string, error) {
v := k.kv[key]
valInt, err := strconv.Atoi(v)
Expand Down

0 comments on commit c78a70b

Please sign in to comment.