Skip to content

Commit

Permalink
changed key type to []byte
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Sep 12, 2023
1 parent ee840ea commit 5c52508
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
26 changes: 11 additions & 15 deletions internal/db/kvs/bbolt/bbolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ type Bbolt struct {
bucket string
}

const default_bucket = "vald-bbolt-bucket"
const defaultBucket = "vald-bbolt-bucket"

// New returns a new Bbolt instance.
// If file does not exist, it creates a new file. If bucket is empty, it uses default_bucket.
// If opts is nil, it uses default options.
func New(file string, bucket string, opts *bolt.Options) (*Bbolt, error) {
func New(file, bucket string, opts *bolt.Options) (*Bbolt, error) {
db, err := bolt.Open(file, 0600, opts)
if err != nil {
return nil, err
}

Check warning on line 27 in internal/db/kvs/bbolt/bbolt.go

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/bbolt.go#L26-L27

Added lines #L26 - L27 were not covered by tests

if bucket == "" {
bucket = default_bucket
bucket = defaultBucket
}
db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte(bucket))
Expand All @@ -43,23 +43,19 @@ func New(file string, bucket string, opts *bolt.Options) (*Bbolt, error) {
}, nil
}

func (b *Bbolt) Set(key string, val []byte) error {
if err := b.db.Update(func(tx *bolt.Tx) error {
func (b *Bbolt) Set(key []byte, val []byte) error {
return b.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(b.bucket))
err := b.Put([]byte(key), val)
err := b.Put(key, val)
return err
}); err != nil {
return err
}

return nil
})
}

func (b *Bbolt) Get(key string) ([]byte, bool, error) {
func (b *Bbolt) Get(key []byte) ([]byte, bool, error) {
var val []byte
if err := b.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(b.bucket))
ret := b.Get([]byte(key))
ret := b.Get(key)
if ret == nil {
// key not found. just return without copying anything to val
return nil
Expand All @@ -83,14 +79,14 @@ func (b *Bbolt) Get(key string) ([]byte, bool, error) {
// AsyncSet sets the key and value asynchronously for better write performance.
// It accumulates the keys and values until the batch size is reached or the timeout comes, then
// writes them all at once. Wait for the errgroup to make sure all the batches finished if required.
func (b *Bbolt) AsyncSet(eg *errgroup.Group, key string, val []byte) error {
func (b *Bbolt) AsyncSet(eg *errgroup.Group, key []byte, val []byte) error {
if eg == nil {
return errors.ErrNilErrGroup
}

Check warning on line 85 in internal/db/kvs/bbolt/bbolt.go

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/bbolt.go#L84-L85

Added lines #L84 - L85 were not covered by tests
(*eg).Go(func() error {
b.db.Batch(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(b.bucket))
err := b.Put([]byte(key), val)
err := b.Put(key, val)
return err
})
return nil
Expand Down
12 changes: 6 additions & 6 deletions internal/db/kvs/bbolt/bbolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ func TestGetSetClose(t *testing.T) {
b, err := bbolt.New(tmpfile, "", nil)
require.NoError(t, err)

err = b.Set("key", []byte("value"))
err = b.Set([]byte("key"), []byte("value"))
require.NoError(t, err)

val, ok, err := b.Get("key")
val, ok, err := b.Get([]byte("key"))
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, []byte("value"), val)

val, ok, err = b.Get("no exist key")
val, ok, err = b.Get([]byte("no exist key"))
require.NoError(t, err)
require.False(t, ok)
require.Nil(t, val)
Expand All @@ -39,7 +39,7 @@ func TestGetSetClose(t *testing.T) {
require.NoError(t, err)

// recover from the file
val, ok, err = b.Get("key")
val, ok, err = b.Get([]byte("key"))
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, []byte("value"), val)
Expand Down Expand Up @@ -70,14 +70,14 @@ func TestAsyncSet(t *testing.T) {

eg, _ := errgroup.New(context.Background())
for k, v := range kv {
b.AsyncSet(&eg, k, []byte(v))
b.AsyncSet(&eg, []byte(k), []byte(v))
}

// wait until all set is done
eg.Wait()

for k := range kv {
_, ok, err := b.Get(k)
_, ok, err := b.Get([]byte(k))
require.NoError(t, err)
require.True(t, ok)
}
Expand Down

0 comments on commit 5c52508

Please sign in to comment.