database/bitcask/bitcask_search_test.go

164 lines
3.5 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"
c "git.tcp.direct/kayos/common"
)
var needle = "yeet"
type Foo struct {
Bar string
Yeet int
What map[string]int
}
func genJunk(t *testing.T, correct bool) []byte {
item := c.RandStr(5)
2022-01-01 22:34:15 +00:00
bar := c.RandStr(5)
2022-01-01 22:22:08 +00:00
if correct {
2022-01-01 22:34:15 +00:00
if c.RNG(100) > 50 {
item = needle + c.RandStr(c.RNG(5))
} else {
bar = c.RandStr(c.RNG(5)) + needle
}
2022-01-01 22:22:08 +00:00
}
f := Foo{
2022-01-01 22:34:15 +00:00
Bar: bar,
Yeet: c.RNG(50),
2022-01-01 22:22:08 +00:00
What: map[string]int{c.RandStr(5): 2, item: 7},
}
raw, err := json.Marshal(f)
if err != nil {
t.Fatal(err.Error())
}
return raw
}
2022-01-09 03:32:42 +00:00
func addJunk(db *DB, storename string, one, two, three, four, five int, t *testing.T) [][]byte {
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)
}
}
t.Logf(
"created 100 entries of random data with needles located at %d, %d, %d, %d, %d",
one, two, three, four, five,
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-01 22:22:08 +00:00
var db *DB
2022-01-09 03:32:42 +00:00
testpath := t.TempDir()
storename := "searchtest"
2022-01-01 22:22:08 +00:00
2022-01-09 03:32:42 +00:00
t.Logf("opening database at %s", testpath)
2022-01-01 22:22:08 +00:00
db = OpenDB(testpath)
2022-01-09 03:32:42 +00:00
err := db.Init(storename)
2022-01-01 22:22:08 +00:00
if err != nil {
t.Fatalf("[FAIL] couuldn't initialize bucket: %e", err)
}
t.Cleanup(func() {
t.Logf("cleaning up file at; %s", testpath)
if err := os.RemoveAll(testpath); err != nil {
t.Error(err)
}
})
2022-01-01 22:34:15 +00:00
one := c.RNG(100)
two := c.RNG(100)
three := c.RNG(100)
four := c.RNG(100)
five := c.RNG(100)
2022-01-09 03:32:42 +00:00
addJunk(db, storename, one, two, three, four, five, t)
2022-01-01 22:22:08 +00:00
t.Logf("executing search for %s", needle)
2022-01-09 03:32:42 +00:00
results, err := db.With(storename).Search(needle)
2022-01-09 03:01:15 +00:00
if err != nil {
t.Errorf("failed to search: %e", err)
}
2022-01-01 22:34:15 +00:00
var keys = []int{one, two, three, four, five}
var needed = len(keys)
2022-01-09 03:01:15 +00:00
for _, kv := range results {
keyint, err := strconv.Atoi(kv.Key.String())
2022-01-01 22:34:15 +00:00
if err != nil {
2022-01-09 03:01:15 +00:00
t.Fatalf("failed to convert Key to int: %e", err)
2022-01-01 22:34:15 +00:00
}
for _, k := range keys {
if keyint == k {
needed--
}
}
keys = append(keys, keyint)
2022-01-09 03:01:15 +00:00
t.Logf("Found Key: %s, Value: %s", kv.Key.String(), kv.Value.String())
2022-01-01 22:22:08 +00:00
}
2022-01-01 22:34:15 +00:00
if needed != 0 {
t.Errorf("Needed %d results, got %d", len(keys), len(keys)-needed)
}
2022-01-01 22:22:08 +00:00
}
2022-01-09 03:32:42 +00:00
func Test_ValueExists(t *testing.T) {
var db *DB
testpath := t.TempDir()
var storename = "valueexists"
t.Logf("opening database at %s", testpath)
db = OpenDB(testpath)
err := db.Init(storename)
if err != nil {
t.Fatalf("[FAIL] couuldn't initialize bucket: %e", err)
}
t.Cleanup(func() {
t.Logf("cleaning up file at; %s", testpath)
if err := os.RemoveAll(testpath); err != nil {
t.Error(err)
}
})
needles := addJunk(db, storename, c.RNG(100), c.RNG(100), c.RNG(100), c.RNG(100), c.RNG(100), t)
for _, needle := range needles {
if k, ok := db.With(storename).ValueExists(needle); !ok {
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 n := 0; n != 5; n++ {
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)
}
}
}