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

Commit

Permalink
Add IncrBy function
Browse files Browse the repository at this point in the history
IncrBy function added as a new feature. Example code added to pkg/main.go and README.

Signed-off-by: Gökhan Özeloğlu <gozeloglu@gmail.com>
  • Loading branch information
gozeloglu committed Oct 27, 2021
1 parent 7fa27f8 commit 10c807b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func main() {
}
fmt.Printf("John's new age is %s", newAge)

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

err = db.Close() // Call while closing the database.
if err != nil {
log.Fatalf(err.Error())
Expand Down
8 changes: 7 additions & 1 deletion example/pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ func main() {
if err != nil {
log.Fatalf(err.Error())
}
fmt.Printf("John's new age is %s", newAge)
fmt.Printf("John's new age is %s\n", newAge)

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

err = db.Close()
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions incr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ func (k *Kvs) Incr(key string) (string, error) {
return k.kv[key], nil
}

// IncrBy increments the value by given value. If non-integer value is being
// tried to increment,
func (k *Kvs) IncrBy(key string, val int) (string, error) {
v := k.kv[key]
valInt, err := strconv.Atoi(v)
if err != nil {
return "", err
}
k.kv[key] = convStr(valInt + val)
return k.kv[key], err
}

// convStr converts integer to string.
func convStr(i int) string {
return strconv.Itoa(i)
Expand Down
65 changes: 65 additions & 0 deletions incr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,68 @@ func TestKvs_IncrStr(t *testing.T) {
}
t.Logf("temp db removed.")
}

func TestKvs_IncrBy(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.IncrBy(k, i)
if err != nil {
t.Fatalf(err.Error())
}

if val != "15" {
t.Errorf("incremented value is wrong: %s", val)
}
t.Logf("value is incremented.")
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_IncrByStr(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.IncrBy(k, 3)
if err == nil {
t.Fatalf("error expected, but nil got.")
}
t.Logf("value could not incremented. 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.")
}

0 comments on commit 10c807b

Please sign in to comment.