From 10c807b172d91e8d01e20fbd63caeaba2cba19a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6khan=20=C3=96zelo=C4=9Flu?= Date: Wed, 27 Oct 2021 16:38:10 +0300 Subject: [PATCH] Add IncrBy function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IncrBy function added as a new feature. Example code added to pkg/main.go and README. Signed-off-by: Gökhan Özeloğlu --- README.md | 6 +++++ example/pkg/main.go | 8 +++++- incr.go | 12 +++++++++ incr_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f4a556a..6f761ec 100644 --- a/README.md +++ b/README.md @@ -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()) diff --git a/example/pkg/main.go b/example/pkg/main.go index 6e2d99e..d1df089 100644 --- a/example/pkg/main.go +++ b/example/pkg/main.go @@ -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 { diff --git a/incr.go b/incr.go index ca01e7f..ad1d321 100644 --- a/incr.go +++ b/incr.go @@ -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) diff --git a/incr_test.go b/incr_test.go index 51e5f13..391b6dd 100644 --- a/incr_test.go +++ b/incr_test.go @@ -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.") +}