Chore: lint

This commit is contained in:
kayos@tcp.direct 2022-07-23 18:07:09 -07:00
parent 058d36edd7
commit 269965c55b
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
7 changed files with 52 additions and 76 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
.idea
*.swp
*.save

View File

@ -36,22 +36,24 @@ func (db *DB) Path() string {
return db.path
}
var DefaultBitcaskOptions []bitcask.Option
var defaultBitcaskOptions []bitcask.Option
// SetDefaultBitcaskOptions options will set the options used for all subsequent bitcask stores that are initialized.
func SetDefaultBitcaskOptions(bitcaskopts ...bitcask.Option) {
for _, opt := range bitcaskopts {
DefaultBitcaskOptions = append(DefaultBitcaskOptions, opt)
}
defaultBitcaskOptions = append(defaultBitcaskOptions, bitcaskopts...)
}
// WithMaxDatafileSize is a shim for bitcask's WithMaxDataFileSize function.
func WithMaxDatafileSize(size int) bitcask.Option {
return bitcask.WithMaxDatafileSize(size)
}
// WithMaxKeySize is a shim for bitcask's WithMaxKeySize function.
func WithMaxKeySize(size uint32) bitcask.Option {
return bitcask.WithMaxKeySize(size)
}
// WithMaxValueSize is a shim for bitcask's WithMaxValueSize function.
func WithMaxValueSize(size uint64) bitcask.Option {
return bitcask.WithMaxValueSize(size)
}
@ -61,8 +63,8 @@ func (db *DB) Init(storeName string, bitcaskopts ...bitcask.Option) error {
db.mu.Lock()
defer db.mu.Unlock()
if len(DefaultBitcaskOptions) > 0 {
bitcaskopts = append(bitcaskopts, DefaultBitcaskOptions...)
if len(defaultBitcaskOptions) > 0 {
bitcaskopts = append(bitcaskopts, defaultBitcaskOptions...)
}
if _, ok := db.store[storeName]; ok {
@ -173,7 +175,7 @@ func (db *DB) withAll(action withAllAction) error {
return compoundErrors(errs)
}
// SyncAndCloseAll implements the method from Keeper.
// SyncAndCloseAll implements the method from Keeper to sync and close all bitcask stores.
func (db *DB) SyncAndCloseAll() error {
var errs = make([]error, len(db.store))
errSync := namedErr("sync", db.SyncAll())

View File

@ -106,9 +106,7 @@ func Test_Search(t *testing.T) {
// For coverage
db.store["yeet"] = Store{Bitcask: nil}
t.Run("BasicSearch", func(t *testing.T) {
t.Logf("executing search for %s", needle)
results, err := db.With(storename).Search(needle)
@ -184,63 +182,36 @@ func Test_ValueExists(t *testing.T) {
} else {
t.Log("[SUCCESS] store succeeded in being nil")
}
})
}
func Test_PrefixScan(t *testing.T) {
var storename = "test_prefix_scan"
var db = setupTest(storename, t)
addJunk(db, storename, c.RNG(5), c.RNG(5), c.RNG(5), c.RNG(5), c.RNG(5), t, false)
var needles = []KeyValue{
{
Key: Key{b: []byte("user:Fuckhole")},
Value: Value{b: []byte(c.RandStr(55))},
},
{
Key: Key{b: []byte("user:Johnson")},
Value: Value{b: []byte(c.RandStr(55))},
},
{
Key: Key{b: []byte("user:Jackson")},
Value: Value{b: []byte(c.RandStr(55))},
},
{
Key: Key{b: []byte("user:Frackhole")},
Value: Value{b: []byte(c.RandStr(55))},
},
{
Key: Key{b: []byte("user:Baboshka")},
Value: Value{b: []byte(c.RandStr(55))},
},
{Key: Key{b: []byte("user:Frickhole")}, Value: Value{b: []byte(c.RandStr(55))}},
{Key: Key{b: []byte("user:Johnson")}, Value: Value{b: []byte(c.RandStr(55))}},
{Key: Key{b: []byte("user:Jackson")}, Value: Value{b: []byte(c.RandStr(55))}},
{Key: Key{b: []byte("user:Frackhole")}, Value: Value{b: []byte(c.RandStr(55))}},
{Key: Key{b: []byte("user:Baboshka")}, Value: Value{b: []byte(c.RandStr(55))}},
}
for _, kv := range needles {
err := db.With(storename).Put(kv.Key.Bytes(), kv.Value.Bytes())
if err != nil {
t.Errorf("failed to add data to %s: %e", storename, err)
t.Fatalf("failed to add data to %s: %e", storename, err)
} else {
t.Logf("added needle with key(value): %s(%s)", kv.Key.String(), kv.Value.String())
}
}
res, err := db.With(storename).PrefixScan("user:")
if err != nil {
t.Errorf("failed to PrefixScan: %e", err)
}
if len(res) != len(needles) {
t.Errorf(
"[FAIL] Length of results (%d) is not the amount of needles we generated (%d)",
len(res), len(needles),
)
t.Errorf("[FAIL] Length of results (%d) is not the amount of needles we generated (%d)", len(res), len(needles))
}
var keysmatched = 0
for _, kv := range res {
for _, ogkv := range needles {
if kv.Key.String() != ogkv.Key.String() {
@ -248,21 +219,12 @@ func Test_PrefixScan(t *testing.T) {
}
t.Logf("[%s] Found needle key", ogkv.Key.String())
keysmatched++
if kv.Value.String() != ogkv.Value.String() {
t.Errorf(
"[FAIL] values of key %s should have matched. wanted: %s, got: %s",
kv.Key.String(), ogkv.Value.String(), kv.Value.String(),
)
t.Errorf("[FAIL] values of key %s should have matched. wanted: %s, got: %s", kv.Key.String(), ogkv.Value.String(), kv.Value.String())
}
t.Logf(
"[%s] Found needle value: %s",
ogkv.Key.String(), ogkv.Value.String(),
)
t.Logf("[%s] Found needle value: %s", ogkv.Key.String(), ogkv.Value.String())
}
}
if keysmatched != len(needles) {
t.Errorf("Needed to match %d keys, only matched %d", len(needles), len(needles))
}

View File

@ -2,6 +2,7 @@ package bitcask
import (
"bytes"
"errors"
"os"
"testing"
@ -36,19 +37,15 @@ func seedRandStores(db *DB, t *testing.T) {
t.Logf("seeded random stores with random values for test %s", t.Name())
}
func TestDB_Init(t *testing.T) {
func TestDB_Init(t *testing.T) { //nolint:funlen,gocognit,cyclop
var db = newTestDB(t)
type args struct {
storeName string
}
type args struct{ storeName string }
type test struct {
name string
args args
wantErr bool
specErr error
}
tests := []test{
{
name: "simple",
@ -74,7 +71,7 @@ func TestDB_Init(t *testing.T) {
if (err != nil) != tt.wantErr {
t.Errorf("[FAIL] Init() error = %v, wantErr %v", err, tt.wantErr)
}
if (err != nil) != tt.wantErr && tt.specErr != nil && err != tt.specErr {
if (err != nil) != tt.wantErr && tt.specErr != nil && !errors.Is(err, tt.specErr) {
t.Errorf("[FAIL] wanted error %e, got error %e", tt.specErr, err)
}
})
@ -97,6 +94,7 @@ func TestDB_Init(t *testing.T) {
}
t.Logf("Got Value %v at Key %v", string(gvalue), key)
})
t.Run("withNewStoreDoesntExist", func(t *testing.T) {
if nope := db.WithNew("asdfqwerty"); nope.Bitcask == nil {
t.Fatalf("[FAIL] got nil result for nonexistent store when it should have made itself: %T, %v", nope, nope)
@ -159,7 +157,7 @@ func TestDB_Init(t *testing.T) {
}
func Test_Sync(t *testing.T) {
// TODO: make sure sync is ACTUALLY sycing instead of only checking for nil err... ( ._. )
// TODO: make sure sync is ACTUALLY sycing instead of only checking for nil err...
var db = newTestDB(t)
seedRandStores(db, t)
@ -204,7 +202,7 @@ func Test_Close(t *testing.T) {
t.Run("CantCloseBogusStore", func(t *testing.T) {
err := db.Close(c.RandStr(55))
if err != errBogusStore {
if !errors.Is(err, errBogusStore) {
t.Errorf("[FAIL] got err %e, wanted err %e", err, errBogusStore)
}
})
@ -214,7 +212,7 @@ func Test_withAll(t *testing.T) {
var db = newTestDB(t)
t.Run("withAllNoStores", func(t *testing.T) {
err := db.withAll(121)
if err != errNoStores {
if !errors.Is(err, errNoStores) {
t.Errorf("[FAIL] got err %e, wanted err %e", err, errBogusStore)
}
})
@ -224,7 +222,7 @@ func Test_withAll(t *testing.T) {
t.Errorf("[FAIL] unexpected error: %e", err)
}
wAllErr := db.withAll(121)
if wAllErr != errUnknownAction {
if !errors.Is(wAllErr, errUnknownAction) {
t.Errorf("[FAIL] wanted error %e, got error %e", errUnknownAction, err)
}
})

View File

@ -5,10 +5,12 @@ import (
"strings"
)
var errUnknownAction = errors.New("unknown action")
var errBogusStore = errors.New("bogus store backend")
var errStoreExists = errors.New("store name already exists")
var errNoStores = errors.New("no stores initialized")
var (
errUnknownAction = errors.New("unknown action")
errBogusStore = errors.New("bogus store backend")
errStoreExists = errors.New("store name already exists")
errNoStores = errors.New("no stores initialized")
)
func namedErr(name string, err error) error {
if err == nil {

8
go.mod
View File

@ -4,16 +4,18 @@ go 1.18
require (
git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34
git.tcp.direct/kayos/common v0.5.2
git.tcp.direct/kayos/common v0.7.0
)
require (
github.com/abcum/lcp v0.0.0-20201209214815-7a3f3840be81 // indirect
github.com/gofrs/flock v0.8.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/plar/go-adaptive-radix-tree v1.0.4 // indirect
github.com/rs/zerolog v1.26.1 // indirect
github.com/rs/zerolog v1.27.0 // indirect
golang.org/x/exp v0.0.0-20200228211341-fcea875c7e85 // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
nullprogram.com/x/rng v1.1.0 // indirect
)

16
go.sum
View File

@ -39,8 +39,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34 h1:tvoLGGLsQ0IYKKQPweMF5qRm3qO4gcTpuzi9jAr3Wkk=
git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34/go.mod h1:NX/Gqm/iy6Tg2CfcmmB/kW/qzKKrGR6o2koOKA5KWnc=
git.tcp.direct/kayos/common v0.5.2 h1:3+3POz5akWQDoWOQ+L8WHf+pCd5cbLgH/fZqQkRblIc=
git.tcp.direct/kayos/common v0.5.2/go.mod h1:2PenBSSXY/kw0iO7ngPgowlU3OA9vak1obTJlxkO5nk=
git.tcp.direct/kayos/common v0.7.0 h1:KZDwoCzUiwQaYSWESr080N8wUVyLD27QYgzXgc7LiAQ=
git.tcp.direct/kayos/common v0.7.0/go.mod h1:7tMZBVNPLFSZk+JXTA6pgXWpf/XHqYRfT7Q3OziI++Y=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
@ -72,6 +72,7 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -217,7 +218,11 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@ -261,8 +266,9 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
github.com/rs/zerolog v1.27.0 h1:1T7qCieN22GVc8S4Q2yuexzBb1EqjbgjSH9RohbMjKs=
github.com/rs/zerolog v1.27.0/go.mod h1:7frBqO0oezxmnO7GF86FY++uy8I0Tk/If5ni1G9Qc0U=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
@ -482,8 +488,10 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=