-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add internal KVS pogreb package (#2302)
* feat: add internal pogreb kvs package Signed-off-by: hlts2 <[email protected]> * fix: typo Signed-off-by: hlts2 <[email protected]> * fix: tweak Signed-off-by: hlts2 <[email protected]> * fix: deepsource warning Signed-off-by: hlts2 <[email protected]> * feat: add test for kvs and small refactor Signed-off-by: hlts2 <[email protected]> * feat: add condition to check key existance Signed-off-by: hlts2 <[email protected]> * fix: small refactor Signed-off-by: hlts2 <[email protected]> * fix: tuning option value Signed-off-by: hlts2 <[email protected]> * fix: deepsource warning Signed-off-by: hlts2 <[email protected]> * fix: make format Signed-off-by: hlts2 <[email protected]> * Update internal/db/kvs/pogreb/pogreb.go Co-authored-by: datelier <[email protected]> * fix: error handling for option and update comment Signed-off-by: hlts2 <[email protected]> * fix: option setting Signed-off-by: hlts2 <[email protected]> --------- Signed-off-by: hlts2 <[email protected]> Co-authored-by: datelier <[email protected]>
- Loading branch information
Showing
7 changed files
with
1,287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (C) 2019-2024 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 pogreb | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/akrylysov/pogreb" | ||
) | ||
|
||
var deafultOpts = []Option{ | ||
WithPath("pogreb.db"), | ||
WithBackgroundSyncInterval("5s"), | ||
} | ||
|
||
// Option represents the functional option for database. | ||
type Option func(*db) error | ||
|
||
// WithPath returns the option to set path. | ||
func WithPath(path string) Option { | ||
return func(d *db) error { | ||
if path != "" { | ||
d.path = path | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// WithBackgroundSyncInterval returns the option to sets the amount of time between background Sync() calls. | ||
// Setting the value to 0 disables the automatic background synchronization. | ||
// Setting the value to -1 or less makes the DB call Sync() after every write operation. | ||
func WithBackgroundSyncInterval(s string) Option { | ||
return func(d *db) error { | ||
if s == "" { | ||
return nil | ||
} | ||
dur, err := time.ParseDuration(s) | ||
if err != nil { | ||
return err | ||
} | ||
if d.opts == nil { | ||
d.opts = new(pogreb.Options) | ||
} | ||
if dur < -1 { | ||
dur = -1 | ||
} | ||
d.opts.BackgroundSyncInterval = dur | ||
return nil | ||
} | ||
} | ||
|
||
// WithBackgroundCompactionInterval returns the option to sets the amount of time between background Compact() calls. | ||
// Setting the value to 0 or less disables the automatic background compaction. | ||
func WithBackgroundCompactionInterval(s string) Option { | ||
return func(d *db) error { | ||
if s == "" { | ||
return nil | ||
} | ||
dur, err := time.ParseDuration(s) | ||
if err != nil { | ||
return err | ||
} | ||
if d.opts == nil { | ||
d.opts = new(pogreb.Options) | ||
} | ||
|
||
if dur < 0 { | ||
dur = 0 | ||
} | ||
d.opts.BackgroundCompactionInterval = dur | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,261 @@ | ||
// Copyright (C) 2019-2024 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 pogreb | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/akrylysov/pogreb" | ||
"github.com/vdaas/vald/internal/errors" | ||
"github.com/vdaas/vald/internal/test/goleak" | ||
) | ||
|
||
func TestWithPath(t *testing.T) { | ||
t.Parallel() | ||
type args struct { | ||
path string | ||
} | ||
type want struct { | ||
want Option | ||
err error | ||
path string | ||
} | ||
type test struct { | ||
name string | ||
args args | ||
want want | ||
checkFunc func(want, *db, error) error | ||
beforeFunc func(*testing.T, args) | ||
afterFunc func(*testing.T, args) | ||
} | ||
defaultCheckFunc := func(w want, got *db, err error) error { | ||
if !errors.Is(err, w.err) { | ||
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) | ||
} | ||
if err == nil { | ||
if !reflect.DeepEqual(got.path, w.path) { | ||
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
} | ||
return nil | ||
} | ||
tests := []test{ | ||
func() test { | ||
path := "db-dir" | ||
return test{ | ||
name: "Succeeds to apply option", | ||
args: args{ | ||
path: path, | ||
}, | ||
want: want{ | ||
path: path, | ||
}, | ||
} | ||
}(), | ||
} | ||
|
||
for _, tc := range tests { | ||
test := tc | ||
t.Run(test.name, func(tt *testing.T) { | ||
tt.Parallel() | ||
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) | ||
if test.beforeFunc != nil { | ||
test.beforeFunc(tt, test.args) | ||
} | ||
if test.afterFunc != nil { | ||
defer test.afterFunc(tt, test.args) | ||
} | ||
checkFunc := test.checkFunc | ||
if test.checkFunc == nil { | ||
checkFunc = defaultCheckFunc | ||
} | ||
|
||
got := new(db) | ||
err := WithPath(test.args.path)(got) | ||
if err := checkFunc(test.want, got, err); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWithBackgroundSyncInterval(t *testing.T) { | ||
t.Parallel() | ||
type args struct { | ||
s string | ||
} | ||
type want struct { | ||
want Option | ||
err error | ||
opts *pogreb.Options | ||
} | ||
type test struct { | ||
name string | ||
args args | ||
want want | ||
checkFunc func(want, *db, error) error | ||
beforeFunc func(*testing.T, args) | ||
afterFunc func(*testing.T, args) | ||
} | ||
defaultCheckFunc := func(w want, got *db, err error) error { | ||
if !errors.Is(err, w.err) { | ||
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) | ||
} | ||
if err == nil { | ||
if !reflect.DeepEqual(got.opts.BackgroundSyncInterval, w.opts.BackgroundSyncInterval) { | ||
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
} | ||
return nil | ||
} | ||
tests := []test{ | ||
func() test { | ||
dur := "100ms" | ||
return test{ | ||
name: "Succeeds to apply option", | ||
args: args{ | ||
s: dur, | ||
}, | ||
want: want{ | ||
opts: &pogreb.Options{ | ||
BackgroundSyncInterval: 100 * time.Millisecond, | ||
}, | ||
}, | ||
} | ||
}(), | ||
func() test { | ||
dur := "invalid" | ||
return test{ | ||
name: "Fails to apply option with invalid value", | ||
args: args{ | ||
s: dur, | ||
}, | ||
want: want{ | ||
err: errors.New("time: invalid duration \"invalid\""), | ||
}, | ||
} | ||
}(), | ||
} | ||
|
||
for _, tc := range tests { | ||
test := tc | ||
t.Run(test.name, func(tt *testing.T) { | ||
tt.Parallel() | ||
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) | ||
if test.beforeFunc != nil { | ||
test.beforeFunc(tt, test.args) | ||
} | ||
if test.afterFunc != nil { | ||
defer test.afterFunc(tt, test.args) | ||
} | ||
checkFunc := test.checkFunc | ||
if test.checkFunc == nil { | ||
checkFunc = defaultCheckFunc | ||
} | ||
|
||
got := new(db) | ||
err := WithBackgroundSyncInterval(test.args.s)(got) | ||
if err := checkFunc(test.want, got, err); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWithBackgroundCompactionInterval(t *testing.T) { | ||
t.Parallel() | ||
type args struct { | ||
s string | ||
} | ||
type want struct { | ||
want Option | ||
err error | ||
opts *pogreb.Options | ||
} | ||
type test struct { | ||
name string | ||
args args | ||
want want | ||
checkFunc func(want, *db, error) error | ||
beforeFunc func(*testing.T, args) | ||
afterFunc func(*testing.T, args) | ||
} | ||
defaultCheckFunc := func(w want, got *db, err error) error { | ||
if !errors.Is(err, w.err) { | ||
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err) | ||
} | ||
if err == nil { | ||
if !reflect.DeepEqual(got.opts.BackgroundCompactionInterval, w.opts.BackgroundCompactionInterval) { | ||
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want) | ||
} | ||
} | ||
return nil | ||
} | ||
tests := []test{ | ||
func() test { | ||
dur := "100ms" | ||
return test{ | ||
name: "Succeeds to apply option", | ||
args: args{ | ||
s: dur, | ||
}, | ||
want: want{ | ||
opts: &pogreb.Options{ | ||
BackgroundCompactionInterval: 100 * time.Millisecond, | ||
}, | ||
}, | ||
} | ||
}(), | ||
func() test { | ||
dur := "invalid" | ||
return test{ | ||
name: "Fails to apply option with invalid value", | ||
args: args{ | ||
s: dur, | ||
}, | ||
want: want{ | ||
err: errors.New("time: invalid duration \"invalid\""), | ||
}, | ||
} | ||
}(), | ||
} | ||
|
||
for _, tc := range tests { | ||
test := tc | ||
t.Run(test.name, func(tt *testing.T) { | ||
tt.Parallel() | ||
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent()) | ||
if test.beforeFunc != nil { | ||
test.beforeFunc(tt, test.args) | ||
} | ||
if test.afterFunc != nil { | ||
defer test.afterFunc(tt, test.args) | ||
} | ||
checkFunc := test.checkFunc | ||
if test.checkFunc == nil { | ||
checkFunc = defaultCheckFunc | ||
} | ||
|
||
got := new(db) | ||
err := WithBackgroundCompactionInterval(test.args.s)(got) | ||
if err := checkFunc(test.want, got, err); err != nil { | ||
tt.Errorf("error = %v", err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// NOT IMPLEMENTED BELOW |
Oops, something went wrong.