Skip to content

Commit

Permalink
feat(context): adding ctx as first argument
Browse files Browse the repository at this point in the history
To maintain backward compatibility
addint WithContext set of functions
instead of changing the signatures of existing ones.

The Ctx parameter is not used yet in many functions yet.
The usage will come in the following PR after interface is changed.

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Oct 25, 2023
1 parent 99127de commit d8e9ca7
Show file tree
Hide file tree
Showing 23 changed files with 439 additions and 21 deletions.
19 changes: 19 additions & 0 deletions badgerdb/badgerdb.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package badgerdb

import (
"context"
"github.com/dgraph-io/badger"

"github.com/philippgille/gokv/encoding"
Expand All @@ -17,6 +18,12 @@ type Store struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
ctx := context.Background()
return s.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (s Store) SetWithContext(_ context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -43,6 +50,12 @@ func (s Store) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
ctx := context.Background()
return s.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (s Store) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down Expand Up @@ -77,6 +90,12 @@ func (s Store) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (s Store) Delete(k string) error {
ctx := context.Background()
return s.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (s Store) DeleteWithContext(_ context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions bbolt/bbolt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bbolt

import (
"context"
bolt "go.etcd.io/bbolt"

"github.com/philippgille/gokv/encoding"
Expand All @@ -18,6 +19,12 @@ type Store struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
ctx := context.Background()
return s.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (s Store) SetWithContext(_ context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -45,6 +52,12 @@ func (s Store) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
ctx := context.Background()
return s.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (s Store) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down Expand Up @@ -80,6 +93,12 @@ func (s Store) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (s Store) Delete(k string) error {
ctx := context.Background()
return s.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (s Store) DeleteWithContext(_ context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions bigcache/bigcache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bigcache

import (
"context"
"time"

"github.com/allegro/bigcache/v2"
Expand All @@ -19,6 +20,12 @@ type Store struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (s Store) Set(k string, v interface{}) error {
ctx := context.Background()
return s.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (s Store) SetWithContext(_ context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -38,6 +45,12 @@ func (s Store) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (s Store) Get(k string, v interface{}) (found bool, err error) {
ctx := context.Background()
return s.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (s Store) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand All @@ -57,6 +70,12 @@ func (s Store) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (s Store) Delete(k string) error {
ctx := context.Background()
return s.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (s Store) DeleteWithContext(_ context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions consul/consul.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package consul

import (
"context"
"github.com/hashicorp/consul/api"

"github.com/philippgille/gokv/encoding"
Expand All @@ -18,6 +19,12 @@ type Client struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
ctx := context.Background()
return c.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (c Client) SetWithContext(_ context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -50,6 +57,12 @@ func (c Client) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
ctx := context.Background()
return c.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (c Client) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand All @@ -74,6 +87,12 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (c Client) Delete(k string) error {
ctx := context.Background()
return c.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (c Client) DeleteWithContext(_ context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}
Expand Down
33 changes: 24 additions & 9 deletions datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ type Client struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
return c.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (c Client) SetWithContext(ctx context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -42,16 +49,14 @@ func (c Client) Set(k string, v interface{}) error {
return err
}

tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
key := datastore.Key{
Kind: kind,
Name: k,
}
src := entity{
V: data,
}
_, err = c.c.Put(tctx, &key, &src)
_, err = c.c.Put(ctx, &key, &src)

return err
}
Expand All @@ -63,18 +68,23 @@ func (c Client) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
return c.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (c Client) GetWithContext(ctx context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}

tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
key := datastore.Key{
Kind: kind,
Name: k,
}
dst := new(entity)
err = c.c.Get(tctx, &key, dst)
err = c.c.Get(ctx, &key, dst)
if err != nil {
if err == datastore.ErrNoSuchEntity {
return false, nil
Expand All @@ -90,17 +100,22 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (c Client) Delete(k string) error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
return c.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (c Client) DeleteWithContext(ctx context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}

tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
key := datastore.Key{
Kind: kind,
Name: k,
}
return c.c.Delete(tctx, &key)
return c.c.Delete(ctx, &key)
}

// Close closes the client.
Expand Down
18 changes: 18 additions & 0 deletions dynamodb/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type Client struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
ctx := context.Background()
return c.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (c Client) SetWithContext(_ context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand Down Expand Up @@ -67,6 +73,12 @@ func (c Client) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
ctx := context.Background()
return c.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (c Client) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}
Expand Down Expand Up @@ -101,6 +113,12 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (c Client) Delete(k string) error {
ctx := context.Background()
return c.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (c Client) DeleteWithContext(_ context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}
Expand Down
33 changes: 24 additions & 9 deletions etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type Client struct {
// Values are automatically marshalled to JSON or gob (depending on the configuration).
// The key must not be "" and the value must not be nil.
func (c Client) Set(k string, v interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
return c.SetWithContext(ctx, k, v)
}

// SetWithContext is exactly like Set function just with added context as first argument.
func (c Client) SetWithContext(ctx context.Context, k string, v interface{}) error {
if err := util.CheckKeyAndValue(k, v); err != nil {
return err
}
Expand All @@ -34,9 +41,7 @@ func (c Client) Set(k string, v interface{}) error {
return err
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
_, err = c.c.Put(ctxWithTimeout, k, string(data))
_, err = c.c.Put(ctx, k, string(data))
if err != nil {
return err
}
Expand All @@ -51,13 +56,18 @@ func (c Client) Set(k string, v interface{}) error {
// If no value is found it returns (false, nil).
// The key must not be "" and the pointer must not be nil.
func (c Client) Get(k string, v interface{}) (found bool, err error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
return c.GetWithContext(ctx, k, v)
}

// GetWithContext is exactly like Get function just with added context as first argument.
func (c Client) GetWithContext(ctx context.Context, k string, v interface{}) (found bool, err error) {
if err := util.CheckKeyAndValue(k, v); err != nil {
return false, err
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
getRes, err := c.c.Get(ctxWithTimeout, k)
getRes, err := c.c.Get(ctx, k)
if err != nil {
return false, err
}
Expand All @@ -75,13 +85,18 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
// Deleting a non-existing key-value pair does NOT lead to an error.
// The key must not be "".
func (c Client) Delete(k string) error {
ctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
return c.DeleteWithContext(ctx, k)
}

// DeleteWithContext is exactly like Delete function just with added context as first argument.
func (c Client) DeleteWithContext(ctx context.Context, k string) error {
if err := util.CheckKey(k); err != nil {
return err
}

ctxWithTimeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
defer cancel()
_, err := c.c.Delete(ctxWithTimeout, k)
_, err := c.c.Delete(ctx, k)
return err
}

Expand Down

0 comments on commit d8e9ca7

Please sign in to comment.