Skip to content

Commit

Permalink
add functional option for bbolt
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Sep 15, 2023
1 parent f23a118 commit 097c138
Show file tree
Hide file tree
Showing 4 changed files with 971 additions and 8 deletions.
12 changes: 10 additions & 2 deletions internal/db/kvs/bbolt/bbolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"io/fs"
"os"
"reflect"

"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/sync/errgroup"
Expand All @@ -41,8 +42,15 @@ 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, bucket string, mode fs.FileMode, opts *bolt.Options) (Bbolt, error) {
db, err := bolt.Open(file, mode, opts)
func New(file, bucket string, mode fs.FileMode, opts ...Option) (Bbolt, error) {
bopts := new(bolt.Options)
for _, opt := range opts {
if err := opt(bopts); err != nil {
return nil, errors.ErrOptionFailed(err, reflect.ValueOf(opt))
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/bbolt.go#L48-L50

Added lines #L48 - L50 were not covered by tests
}

db, err := bolt.Open(file, mode, bopts)
if err != nil {
return nil, err
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/bbolt.go#L55-L56

Added lines #L55 - L56 were not covered by tests
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 @@ -41,7 +41,7 @@ func TestNew(t *testing.T) {
tempdir := t.TempDir()
tmpfile := filepath.Join(tempdir, "test.db")

b, err := bbolt.New(tmpfile, "", mode, nil)
b, err := bbolt.New(tmpfile, "", mode)
require.NoError(t, err)
require.NotNil(t, b)
},
Expand All @@ -58,7 +58,7 @@ func TestNew(t *testing.T) {
err = f.Close()
require.NoError(t, err)

b, err := bbolt.New(f.Name(), "", mode, nil)
b, err := bbolt.New(f.Name(), "", mode)
require.NoError(t, err)
require.NotNil(t, b)
},
Expand All @@ -69,7 +69,7 @@ func TestNew(t *testing.T) {
tempdir := t.TempDir()
tmpfile := filepath.Join(tempdir, "test.db")

b, err := bbolt.New(tmpfile, "my bucket name", mode, nil)
b, err := bbolt.New(tmpfile, "my bucket name", mode)
require.NoError(t, err)
require.NotNil(t, b)
},
Expand All @@ -96,7 +96,7 @@ func Test_bbolt_GetSetClose(t *testing.T) {
setup := func(t *testing.T) (b bbolt.Bbolt, file string) {
tempdir := t.TempDir()
tmpfile := filepath.Join(tempdir, "test.db")
b, err := bbolt.New(tmpfile, "", mode, nil)
b, err := bbolt.New(tmpfile, "", mode)
require.NoError(t, err)

return b, tmpfile
Expand Down Expand Up @@ -140,7 +140,7 @@ func Test_bbolt_GetSetClose(t *testing.T) {
require.NoError(t, err)

// recover from the file
b, err = bbolt.New(file, "", mode, nil)
b, err = bbolt.New(file, "", mode)
require.NoError(t, err)

res, ok, err := b.Get(k)
Expand Down Expand Up @@ -180,7 +180,7 @@ func Test_bbolt_AsyncSet(t *testing.T) {

tempdir := t.TempDir()
tmpfile := filepath.Join(tempdir, "test.db")
b, err := bbolt.New(tmpfile, "", mode, nil)
b, err := bbolt.New(tmpfile, "", mode)
require.NoError(t, err)

kv := map[string]string{
Expand Down
92 changes: 92 additions & 0 deletions internal/db/kvs/bbolt/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (C) 2019-2023 vdaas.org vald team <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bbolt

import (
"time"

bolt "go.etcd.io/bbolt"
)

type Option func(*bolt.Options) error

func WithTimeout(dur time.Duration) Option {
return func(opts *bolt.Options) error {
opts.Timeout = dur
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L24-L28

Added lines #L24 - L28 were not covered by tests
}

func WithNoGrowSync(noGrowSync bool) Option {
return func(opts *bolt.Options) error {
opts.NoGrowSync = noGrowSync
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L31-L35

Added lines #L31 - L35 were not covered by tests
}

func WithNoFreeListSync(noFreeListSync bool) Option {
return func(opts *bolt.Options) error {
opts.NoFreelistSync = noFreeListSync
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L38-L42

Added lines #L38 - L42 were not covered by tests
}

func WithPreLoadFreelist(preloadFreelist bool) Option {
return func(opts *bolt.Options) error {
opts.NoFreelistSync = preloadFreelist
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L45-L49

Added lines #L45 - L49 were not covered by tests
}

func WithReadOnly(readOnly bool) Option {
return func(opts *bolt.Options) error {
opts.ReadOnly = readOnly
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L52-L56

Added lines #L52 - L56 were not covered by tests
}

func WithMmapFlags(flags int) Option {
return func(opts *bolt.Options) error {
opts.MmapFlags = flags
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L59-L63

Added lines #L59 - L63 were not covered by tests
}

func WithInitialMmapSize(initialMmapSize int) Option {
return func(opts *bolt.Options) error {
opts.InitialMmapSize = initialMmapSize
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L66-L70

Added lines #L66 - L70 were not covered by tests
}

func WithPageSize(pageSize int) Option {
return func(opts *bolt.Options) error {
opts.PageSize = pageSize
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L73-L77

Added lines #L73 - L77 were not covered by tests
}

func WithNoSync(noSync bool) Option {
return func(opts *bolt.Options) error {
opts.NoSync = noSync
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L80-L84

Added lines #L80 - L84 were not covered by tests
}

func WithMlock(mlock bool) Option {
return func(opts *bolt.Options) error {
opts.Mlock = mlock
return nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/db/kvs/bbolt/option.go#L87-L91

Added lines #L87 - L91 were not covered by tests
}
Loading

0 comments on commit 097c138

Please sign in to comment.