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

Commit

Permalink
Add del function
Browse files Browse the repository at this point in the history
Delete key feature added with its tests. Example usage added to pkg/main.go and README.
README updated.

Signed-off-by: Gökhan Özeloğlu <gozeloglu@gmail.com>
  • Loading branch information
gozeloglu committed Oct 27, 2021
1 parent 211bfe8 commit d3acd50
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func main() {
jackAge := db.Get("jack")
fmt.Println(jackAge)

db.Del("jack")

jack = db.Get("jack")
fmt.Println("Jack:", jack)

err = db.Close() // Call while closing the database.
if err != nil {
log.Fatalf(err.Error())
Expand Down Expand Up @@ -136,11 +141,11 @@ john=12
fizz=buzz
```

This is a sample data file. If you want to save data manually,>
This is a sample data file.

## NOTE

kvs is still under development stage, and it is created for experimental purposes.
kvs is still under development stage, and it is created for experimental purposes.

## LICENSE

Expand Down
5 changes: 5 additions & 0 deletions del.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kvs

func (k *Kvs) Del(key string) {
delete(k.kv, key)
}
90 changes: 90 additions & 0 deletions del_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package kvs

import (
"os"
"testing"
"time"
)

func TestDel(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)

val := db.Get(k)
if val != v {
t.Logf("set value could not get.")
}

db.Del(k)
if db.Get(k) != "" {
t.Logf("key could not deleted.")
}

t.Logf("Key is deleted successfully.")

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("test file removed.")
}

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

k, v := "foo", "bar"
db.Set(k, v)
val := db.Get(k)
t.Logf("%s: %s", k, val)
db.Del(k)
val = db.Get(k)
if val != "" {
t.Errorf("val is not empty string: %s", val)
}
time.Sleep(3 * time.Second)

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

tmpDb, err := open(t.Name(), "", 2*time.Minute)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("tmpdb created.")

val = tmpDb.Get(k)
if val != "" {
t.Errorf("val is not empty string: %s", val)
}

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

err = os.RemoveAll(tmpDb.dir)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("tmp file removed.")
}
7 changes: 6 additions & 1 deletion example/pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ func main() {
fmt.Println("John:", john)

jack := db.Get("jack")
fmt.Println("Jack", jack)
fmt.Println("Jack:", jack)

db.Del("jack")

jack = db.Get("jack")
fmt.Println("Jack:", jack)

err = db.Close()
if err != nil {
Expand Down

0 comments on commit d3acd50

Please sign in to comment.