2021-12-07 02:36:12 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2022-01-10 04:09:18 +01:00
|
|
|
"bytes"
|
2021-12-24 13:17:53 +01:00
|
|
|
"encoding/csv"
|
2021-12-11 23:22:45 +01:00
|
|
|
"encoding/hex"
|
|
|
|
"log"
|
2021-12-24 13:17:53 +01:00
|
|
|
"os"
|
2021-12-07 02:36:12 +01:00
|
|
|
"testing"
|
2021-12-11 23:22:45 +01:00
|
|
|
|
|
|
|
"github.com/lbryio/hub/db/prefixes"
|
2021-12-24 13:17:53 +01:00
|
|
|
"github.com/linxGnu/grocksdb"
|
2021-12-07 02:36:12 +01:00
|
|
|
)
|
|
|
|
|
2022-01-10 05:46:52 +01:00
|
|
|
func TestIter(t *testing.T) {
|
2021-12-24 13:17:53 +01:00
|
|
|
|
2022-01-10 05:46:52 +01:00
|
|
|
filePath := "../resources/reposted_claim.csv"
|
2022-01-10 04:09:18 +01:00
|
|
|
|
|
|
|
log.Println(filePath)
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
reader := csv.NewReader(file)
|
|
|
|
records, err := reader.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wOpts := grocksdb.NewDefaultWriteOptions()
|
|
|
|
opts := grocksdb.NewDefaultOptions()
|
|
|
|
opts.SetCreateIfMissing(true)
|
|
|
|
db, err := grocksdb.OpenDb(opts, "tmp")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2022-01-10 05:46:52 +01:00
|
|
|
defer func() {
|
|
|
|
db.Close()
|
|
|
|
err = os.RemoveAll("./tmp")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2022-01-10 04:09:18 +01:00
|
|
|
for _, record := range records {
|
|
|
|
key, err := hex.DecodeString(record[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
val, err := hex.DecodeString(record[1])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
db.Put(wOpts, key, val)
|
|
|
|
}
|
|
|
|
// test prefix
|
2022-01-10 05:46:52 +01:00
|
|
|
options := NewIterateOptions().WithPrefix([]byte{prefixes.RepostedClaim}).WithIncludeValue(true)
|
2022-01-10 04:09:18 +01:00
|
|
|
ch := Iter(db, options)
|
|
|
|
var i = 0
|
|
|
|
for kv := range ch {
|
|
|
|
// log.Println(kv.Key)
|
2022-01-10 05:46:52 +01:00
|
|
|
gotKey := kv.Key.(*prefixes.RepostedKey).PackKey()
|
|
|
|
|
|
|
|
keyPartial3 := prefixes.RepostedKeyPackPartial(kv.Key.(*prefixes.RepostedKey), 3)
|
|
|
|
keyPartial2 := prefixes.RepostedKeyPackPartial(kv.Key.(*prefixes.RepostedKey), 2)
|
|
|
|
keyPartial1 := prefixes.RepostedKeyPackPartial(kv.Key.(*prefixes.RepostedKey), 1)
|
|
|
|
|
|
|
|
// Check pack partial for sanity
|
|
|
|
if !bytes.HasPrefix(gotKey, keyPartial3) {
|
|
|
|
t.Errorf("%+v should be prefix of %+v\n", keyPartial3, gotKey)
|
|
|
|
}
|
|
|
|
if !bytes.HasPrefix(gotKey, keyPartial2) {
|
|
|
|
t.Errorf("%+v should be prefix of %+v\n", keyPartial2, gotKey)
|
|
|
|
}
|
|
|
|
if !bytes.HasPrefix(gotKey, keyPartial1) {
|
|
|
|
t.Errorf("%+v should be prefix of %+v\n", keyPartial1, gotKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := kv.Value.(*prefixes.RepostedValue).PackValue()
|
2022-01-10 04:09:18 +01:00
|
|
|
wantKey, err := hex.DecodeString(records[i][0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
want, err := hex.DecodeString(records[i][1])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(gotKey, wantKey) {
|
|
|
|
t.Errorf("gotKey: %+v, wantKey: %+v\n", got, want)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(got, want) {
|
|
|
|
t.Errorf("got: %+v, want: %+v\n", got, want)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test start / stop
|
|
|
|
start, err := hex.DecodeString(records[0][0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
stop, err := hex.DecodeString(records[9][0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
options2 := NewIterateOptions().WithStart(start).WithStop(stop).WithIncludeValue(true)
|
|
|
|
ch2 := Iter(db, options2)
|
|
|
|
i = 0
|
|
|
|
for kv := range ch2 {
|
2022-01-10 05:46:52 +01:00
|
|
|
got := kv.Value.(*prefixes.RepostedValue).PackValue()
|
2022-01-10 04:09:18 +01:00
|
|
|
want, err := hex.DecodeString(records[i][1])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(got, want) {
|
|
|
|
t.Errorf("got: %+v, want: %+v\n", got, want)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
2021-12-11 23:22:45 +01:00
|
|
|
}
|