database/bitcask/bitcask_search_test.go

266 lines
6.9 KiB
Go
Raw Normal View History

2022-01-09 00:59:10 +00:00
package bitcask
2022-01-01 22:22:08 +00:00
import (
"encoding/json"
"fmt"
"os"
2022-01-01 22:34:15 +00:00
"strconv"
2022-01-01 22:22:08 +00:00
"testing"
2022-05-21 23:17:21 +00:00
c "git.tcp.direct/kayos/common/entropy"
2022-07-26 04:36:46 +00:00
"github.com/davecgh/go-spew/spew"
"git.tcp.direct/tcp.direct/database/kv"
2022-01-01 22:22:08 +00:00
)
var needle = "yeet"
type Foo struct {
Bar string
Yeet int
What map[string]int
}
2022-01-09 04:18:15 +00:00
func setupTest(storename string, t *testing.T) *DB {
var db *DB
testpath := t.TempDir()
t.Logf("opening database at %s", testpath)
db = OpenDB(testpath)
err := db.Init(storename)
if err != nil {
2022-01-19 09:46:51 +00:00
t.Fatalf("[FAIL] couuldn't initialize store: %e", err)
2022-01-09 04:18:15 +00:00
}
t.Cleanup(func() {
t.Logf("cleaning up file at; %s", testpath)
if err := os.RemoveAll(testpath); err != nil {
t.Error(err)
}
})
return db
}
2022-01-01 22:22:08 +00:00
func genJunk(t *testing.T, correct bool) []byte {
2022-05-21 23:17:21 +00:00
item := c.RandStr(5)
bar := c.RandStr(5)
2022-01-01 22:22:08 +00:00
if correct {
2022-05-21 23:17:21 +00:00
if c.RNG(100) > 50 {
item = needle + c.RandStr(c.RNG(5))
2022-01-01 22:34:15 +00:00
} else {
2022-05-21 23:17:21 +00:00
bar = c.RandStr(c.RNG(5)) + needle
2022-01-01 22:34:15 +00:00
}
2022-01-01 22:22:08 +00:00
}
f := Foo{
2022-01-01 22:34:15 +00:00
Bar: bar,
2022-05-21 23:17:21 +00:00
Yeet: c.RNG(50),
What: map[string]int{c.RandStr(5): 2, item: 7},
2022-01-01 22:22:08 +00:00
}
raw, err := json.Marshal(f)
if err != nil {
t.Fatal(err.Error())
}
return raw
}
2022-01-09 04:18:15 +00:00
func addJunk(db *DB, storename string, one, two, three, four, five int, t *testing.T, echo bool) [][]byte {
2022-01-09 03:32:42 +00:00
var needles [][]byte
for n := 0; n != 100; n++ {
var rawjson []byte
switch n {
case one, two, three, four, five:
rawjson = genJunk(t, true)
needles = append(needles, rawjson)
default:
rawjson = genJunk(t, false)
}
err := db.With(storename).Put([]byte(fmt.Sprintf("%d", n)), rawjson)
if err != nil {
t.Fail()
t.Logf("%e", err)
}
}
2022-01-09 04:18:15 +00:00
if echo {
t.Logf(
"created 100 entries of random data with needles located at %d, %d, %d, %d, %d",
one, two, three, four, five,
)
} else {
t.Log("created 100 entries of junk")
}
2022-01-01 22:22:08 +00:00
2022-01-09 03:32:42 +00:00
return needles
}
func Test_Search(t *testing.T) {
2022-01-09 04:18:15 +00:00
var storename = "test_search"
var db = setupTest(storename, t)
2022-01-01 22:22:08 +00:00
2022-05-21 23:17:21 +00:00
one := c.RNG(100)
two := c.RNG(100)
three := c.RNG(100)
four := c.RNG(100)
five := c.RNG(100)
2022-01-01 22:34:15 +00:00
2022-01-09 04:18:15 +00:00
addJunk(db, storename, one, two, three, four, five, t, true)
2022-01-01 22:22:08 +00:00
// For coverage
db.store["yeet"] = Store{Bitcask: nil}
t.Run("BasicSearch", func(t *testing.T) {
t.Logf("executing search for %s", needle)
2022-07-26 04:36:46 +00:00
resChan, errChan := db.With(storename).Search(needle)
var keys = []int{one, two, three, four, five}
var needed = len(keys)
2022-07-26 04:36:46 +00:00
for keyValue := range resChan {
keyint, err := strconv.Atoi(keyValue.Key.String())
for _, k := range keys {
if keyint == k {
needed--
}
2022-01-01 22:34:15 +00:00
}
keys = append(keys, keyint)
2022-07-26 04:36:46 +00:00
t.Logf("Found Key: %s, Value: %s", keyValue.Key.String(), keyValue.Value.String())
if err != nil {
t.Fatalf("failed to convert Key to int: %e", err)
}
select {
case err := <-errChan:
if err != nil {
t.Fatalf("failed to search: %e", err)
}
default:
continue
}
2022-01-01 22:34:15 +00:00
}
if needed != 0 {
t.Errorf("Needed %d results, got %d", len(keys), len(keys)-needed)
}
})
t.Run("NoResultsSearch", func(t *testing.T) {
2022-05-21 23:17:21 +00:00
bogus := c.RandStr(55)
t.Logf("executing search for %s", bogus)
2022-07-26 04:36:46 +00:00
var results []*kv.KeyValue
resChan, errChan := db.With(storename).Search(bogus)
select {
case err := <-errChan:
t.Errorf("failed to search: %s", err.Error())
case r := <-resChan:
if r != nil {
spew.Dump(r)
results = append(results, r)
}
if len(results) > 0 {
t.Errorf("[FAIL] got %d results, wanted 0", len(results))
}
}
})
2022-01-01 22:22:08 +00:00
}
2022-01-09 03:32:42 +00:00
func Test_ValueExists(t *testing.T) {
2022-01-09 04:18:15 +00:00
var storename = "test_value_exists"
var db = setupTest(storename, t)
2022-01-09 03:32:42 +00:00
t.Run("ValueExists", func(t *testing.T) {
2022-05-21 23:17:21 +00:00
needles := addJunk(db, storename, c.RNG(100), c.RNG(100), c.RNG(100), c.RNG(100), c.RNG(100), t, true)
2022-01-09 03:32:42 +00:00
2022-08-29 06:36:37 +00:00
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))
}
2022-08-29 06:36:37 +00:00
t.Logf("[SUCCESS] successfully located value: %s, at key: %s", string(k), string(v))
2022-01-09 03:32:42 +00:00
}
})
2022-01-09 03:32:42 +00:00
t.Run("ValueShouldNotExist", func(t *testing.T) {
for n := 0; n != 5; n++ {
2022-05-21 23:17:21 +00:00
garbage := c.RandStr(55)
if _, exists := db.With(storename).ValueExists([]byte(garbage)); exists {
t.Errorf("[FAIL] store should have not contained value %v, but it did", []byte(garbage))
} else {
t.Logf("[SUCCESS] store succeeded in not having random value %s", garbage)
}
}
})
t.Run("ValueExistNilBitcask", func(t *testing.T) {
db.store["asdb"] = Store{Bitcask: nil}
garbage := "yeet"
2022-01-09 03:32:42 +00:00
if _, exists := db.With(storename).ValueExists([]byte(garbage)); exists {
t.Errorf("[FAIL] store should have not contained value %v, should have been nil", []byte(garbage))
2022-01-09 03:32:42 +00:00
} else {
t.Log("[SUCCESS] store succeeded in being nil")
2022-01-09 03:32:42 +00:00
}
})
2022-01-09 03:32:42 +00:00
}
2022-01-09 04:18:15 +00:00
func Test_PrefixScan(t *testing.T) {
var storename = "test_prefix_scan"
var db = setupTest(storename, t)
2022-05-21 23:17:21 +00:00
addJunk(db, storename, c.RNG(5), c.RNG(5), c.RNG(5), c.RNG(5), c.RNG(5), t, false)
2022-07-26 04:36:46 +00:00
var needles = []*kv.KeyValue{
kv.NewKeyValue(kv.NewKey([]byte("user:Frickhole")), kv.NewValue([]byte(c.RandStr(55)))),
kv.NewKeyValue(kv.NewKey([]byte("user:Johnson")), kv.NewValue([]byte(c.RandStr(55)))),
kv.NewKeyValue(kv.NewKey([]byte("user:Jackson")), kv.NewValue([]byte(c.RandStr(55)))),
kv.NewKeyValue(kv.NewKey([]byte("user:Frackhole")), kv.NewValue([]byte(c.RandStr(55)))),
kv.NewKeyValue(kv.NewKey([]byte("user:Baboshka")), kv.NewValue([]byte(c.RandStr(55)))),
2022-01-09 04:18:15 +00:00
}
2022-07-26 04:36:46 +00:00
for _, combo := range needles {
err := db.With(storename).Put(combo.Key.Bytes(), combo.Value.Bytes())
2022-01-09 04:18:15 +00:00
if err != nil {
2022-07-24 01:07:09 +00:00
t.Fatalf("failed to add data to %s: %e", storename, err)
2022-01-09 04:18:15 +00:00
} else {
2022-07-26 04:36:46 +00:00
t.Logf("added needle with key(value): %s(%s)", combo.Key.String(), combo.Value.String())
2022-01-09 04:18:15 +00:00
}
}
2022-07-26 04:36:46 +00:00
resChan, errChan := db.With(storename).PrefixScan("user:")
var results []*kv.KeyValue
for keyValue := range resChan {
results = append(results, keyValue)
select {
case err := <-errChan:
if err != nil {
t.Fatalf("failed to PrefixScan: %e", err)
}
break
default:
continue
}
2022-01-09 04:18:15 +00:00
}
2022-07-26 04:36:46 +00:00
if len(results) != len(needles) {
t.Errorf("[FAIL] Length of results (%d) is not the amount of needles we generated (%d)", len(results), len(needles))
2022-01-09 04:18:15 +00:00
}
var keysmatched = 0
2022-07-26 04:36:46 +00:00
for _, result := range results {
2022-01-09 04:18:15 +00:00
for _, ogkv := range needles {
2022-07-26 04:36:46 +00:00
if result.Key.String() != ogkv.Key.String() {
2022-01-09 04:18:15 +00:00
continue
}
2022-07-26 04:36:46 +00:00
t.Logf("Found needle key: %s", result.Key.String())
2022-01-09 04:18:15 +00:00
keysmatched++
2022-07-26 04:36:46 +00:00
if result.Value.String() != ogkv.Value.String() {
t.Errorf("[FAIL] values of key %s should have matched. wanted: %s, got: %s",
result.Key.String(), ogkv.Value.String(), result.Value.String())
2022-01-09 04:18:15 +00:00
}
2022-07-26 04:36:46 +00:00
t.Logf("Found needle value: %s", ogkv.Value.String())
2022-01-09 04:18:15 +00:00
}
}
if keysmatched != len(needles) {
t.Errorf("Needed to match %d keys, only matched %d", len(needles), len(needles))
}
}