Add: interface methods + Fix: testing
This commit is contained in:
parent
f045593496
commit
cf3b1bfff2
|
@ -16,7 +16,7 @@ func (s Store) Search(query string) (<-chan *kv.KeyValue, chan error) {
|
|||
close(resChan)
|
||||
close(errChan)
|
||||
}()
|
||||
for _, key := range s.AllKeys() {
|
||||
for _, key := range s.Keys() {
|
||||
raw, err := s.Get(key)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
|
@ -31,12 +31,16 @@ func (s Store) Search(query string) (<-chan *kv.KeyValue, chan error) {
|
|||
return resChan, errChan
|
||||
}
|
||||
|
||||
// ValueExists will check for the existence of a Value anywhere within the keyspace, returning the Key and true if found, or nil and false if not found.
|
||||
// ValueExists will check for the existence of a Value anywhere within the keyspace;
|
||||
// returning the first Key found, true if found || nil and false if not found.
|
||||
func (s Store) ValueExists(value []byte) (key []byte, ok bool) {
|
||||
var raw []byte
|
||||
var needle = kv.NewValue(value)
|
||||
for _, k := range s.AllKeys() {
|
||||
raw, _ = s.Get(k)
|
||||
for _, key = range s.Keys() {
|
||||
raw, _ = s.Get(key)
|
||||
if len(key) == 0 {
|
||||
continue
|
||||
}
|
||||
v := kv.NewValue(raw)
|
||||
if v.Equal(needle) {
|
||||
ok = true
|
||||
|
@ -75,10 +79,10 @@ func (s Store) PrefixScan(prefix string) (<-chan *kv.KeyValue, chan error) {
|
|||
return resChan, errChan
|
||||
}
|
||||
|
||||
// AllKeys will return all keys in the database as a slice of byte slices.
|
||||
func (s Store) AllKeys() (keys [][]byte) {
|
||||
keychan := s.Keys()
|
||||
for key := range keychan {
|
||||
// Keys will return all keys in the database as a slice of byte slices.
|
||||
func (s Store) Keys() (keys [][]byte) {
|
||||
allkeys := s.Bitcask.Keys()
|
||||
for key := range allkeys {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
return
|
||||
|
|
|
@ -169,12 +169,20 @@ func Test_ValueExists(t *testing.T) {
|
|||
t.Run("ValueExists", func(t *testing.T) {
|
||||
needles := addJunk(db, storename, c.RNG(100), c.RNG(100), c.RNG(100), c.RNG(100), c.RNG(100), t, true)
|
||||
|
||||
for _, needle := range needles {
|
||||
if k, exists := db.With(storename).ValueExists(needle); !exists {
|
||||
t.Errorf("[FAIL] store should have contained a value %s somewhere, it did not.", string(needle))
|
||||
} else {
|
||||
t.Logf("[SUCCESS] successfully located value: %s, at key: %s", string(k), string(needle))
|
||||
for _, ndl := range needles {
|
||||
k, exists := db.With(storename).ValueExists(ndl)
|
||||
if !exists {
|
||||
t.Fatalf("[FAIL] store should have contained a value %s somewhere, it did not.", string(ndl))
|
||||
}
|
||||
if k == nil {
|
||||
t.Fatalf("[FAIL] store should have contained a value %s somewhere, "+
|
||||
"it said it did but key was nil", string(ndl))
|
||||
}
|
||||
v, _ := db.With(storename).Get(k)
|
||||
if string(v) != string(ndl) {
|
||||
t.Fatalf("[FAIL] retrieved value does not match search target %s != %s", string(v), string(ndl))
|
||||
}
|
||||
t.Logf("[SUCCESS] successfully located value: %s, at key: %s", string(k), string(v))
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
@ -6,13 +6,19 @@ import (
|
|||
"git.tcp.direct/tcp.direct/database"
|
||||
)
|
||||
|
||||
func needKeeper(keeper database.Keeper) {}
|
||||
func needFiler(filer database.Filer) {}
|
||||
|
||||
func Test_Keeper(t *testing.T) {
|
||||
needKeeper(OpenDB(""))
|
||||
}
|
||||
|
||||
func Test_Filer(t *testing.T) {
|
||||
needFiler(OpenDB("").With(""))
|
||||
func Test_Interfaces(t *testing.T) {
|
||||
v := OpenDB(t.TempDir())
|
||||
var keeper interface{} = v
|
||||
if _, ok := keeper.(database.Keeper); !ok {
|
||||
t.Error("Keeper interface not implemented")
|
||||
}
|
||||
vs := v.WithNew("test")
|
||||
var searcher interface{} = vs
|
||||
if _, ok := searcher.(database.Searcher); !ok {
|
||||
t.Error("Searcher interface not implemented")
|
||||
}
|
||||
var filer interface{} = vs
|
||||
if _, ok := filer.(database.Filer); !ok {
|
||||
t.Error("Filer interface not implemented")
|
||||
}
|
||||
}
|
||||
|
|
3
filer.go
3
filer.go
|
@ -25,4 +25,7 @@ type Filer interface {
|
|||
Close() error
|
||||
// Sync should take any volatile data and solidify it somehow if relevant. (ram to disk in most cases)
|
||||
Sync() error
|
||||
|
||||
Keys() [][]byte
|
||||
Len() int
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -4,7 +4,7 @@ go 1.18
|
|||
|
||||
require (
|
||||
git.tcp.direct/Mirrors/bitcask-mirror v0.0.0-20220228092422-1ec4297c7e34
|
||||
git.tcp.direct/kayos/common v0.7.0
|
||||
git.tcp.direct/kayos/common v0.7.1
|
||||
github.com/davecgh/go-spew v1.1.1
|
||||
)
|
||||
|
||||
|
|
4
go.sum
4
go.sum
|
@ -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.7.0 h1:KZDwoCzUiwQaYSWESr080N8wUVyLD27QYgzXgc7LiAQ=
|
||||
git.tcp.direct/kayos/common v0.7.0/go.mod h1:7tMZBVNPLFSZk+JXTA6pgXWpf/XHqYRfT7Q3OziI++Y=
|
||||
git.tcp.direct/kayos/common v0.7.1 h1:srCbByDmcvItXrz82FRhDOv9w+LvWl4sYQzy1bup7rw=
|
||||
git.tcp.direct/kayos/common v0.7.1/go.mod h1:Xq+Ln4uK48HttJFNp0uCofNGWaRHSmvAUeB3jlwcv/o=
|
||||
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=
|
||||
|
|
|
@ -4,8 +4,6 @@ import "git.tcp.direct/tcp.direct/database/kv"
|
|||
|
||||
// Searcher must be able to search through our datastore(s) with strings.
|
||||
type Searcher interface {
|
||||
// AllKeys must retrieve all keys in the datastore with the given storeName.
|
||||
AllKeys() [][]byte
|
||||
// PrefixScan must retrieve all keys in the datastore and stream them to the given channel.
|
||||
PrefixScan(prefix string) (<-chan *kv.KeyValue, chan error)
|
||||
// Search must be able to search through the value contents of our database and stream the results to the given channel.
|
||||
|
|
Loading…
Reference in New Issue