2020-11-26 16:58:03 +00:00
|
|
|
package splitstore
|
|
|
|
|
|
|
|
import (
|
2021-03-02 08:53:24 +00:00
|
|
|
"io/ioutil"
|
2020-11-26 16:58:03 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
)
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
func TestBoltMarkSet(t *testing.T) {
|
|
|
|
testMarkSet(t, "bolt")
|
2021-02-27 10:50:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
func TestBloomMarkSet(t *testing.T) {
|
|
|
|
testMarkSet(t, "bloom")
|
2021-02-28 08:35:27 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
func testMarkSet(t *testing.T, lsType string) {
|
2021-02-27 10:50:48 +00:00
|
|
|
t.Helper()
|
2020-11-26 16:58:03 +00:00
|
|
|
|
2021-03-02 08:53:24 +00:00
|
|
|
path, err := ioutil.TempDir("", "sweep-test.*")
|
2020-11-26 16:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
env, err := OpenMarkSetEnv(path, lsType)
|
2020-11-26 16:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-27 10:50:48 +00:00
|
|
|
defer env.Close() //nolint:errcheck
|
2020-11-26 16:58:03 +00:00
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
hotSet, err := env.Create("hot", 0)
|
2020-11-26 16:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
coldSet, err := env.Create("cold", 0)
|
2020-11-26 16:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
makeCid := func(key string) cid.Cid {
|
|
|
|
h, err := multihash.Sum([]byte(key), multihash.SHA2_256, -1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid.NewCidV1(cid.Raw, h)
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
mustHave := func(s MarkSet, cid cid.Cid) {
|
2020-11-26 16:58:03 +00:00
|
|
|
has, err := s.Has(cid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
t.Fatal("mark not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
mustNotHave := func(s MarkSet, cid cid.Cid) {
|
2020-11-26 16:58:03 +00:00
|
|
|
has, err := s.Has(cid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
|
|
|
t.Fatal("unexpected mark")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
k1 := makeCid("a")
|
|
|
|
k2 := makeCid("b")
|
|
|
|
k3 := makeCid("c")
|
|
|
|
k4 := makeCid("d")
|
|
|
|
|
2020-11-26 17:47:54 +00:00
|
|
|
hotSet.Mark(k1) //nolint
|
|
|
|
hotSet.Mark(k2) //nolint
|
|
|
|
coldSet.Mark(k3) //nolint
|
2020-11-26 16:58:03 +00:00
|
|
|
|
|
|
|
mustHave(hotSet, k1)
|
|
|
|
mustHave(hotSet, k2)
|
|
|
|
mustNotHave(hotSet, k3)
|
|
|
|
mustNotHave(hotSet, k4)
|
|
|
|
|
|
|
|
mustNotHave(coldSet, k1)
|
|
|
|
mustNotHave(coldSet, k2)
|
|
|
|
mustHave(coldSet, k3)
|
|
|
|
mustNotHave(coldSet, k4)
|
|
|
|
|
|
|
|
// close them and reopen to redo the dance
|
|
|
|
|
|
|
|
err = hotSet.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = coldSet.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
hotSet, err = env.Create("hot", 0)
|
2020-11-26 16:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:47:21 +00:00
|
|
|
coldSet, err = env.Create("cold", 0)
|
2020-11-26 16:58:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-26 17:47:54 +00:00
|
|
|
hotSet.Mark(k3) //nolint
|
|
|
|
hotSet.Mark(k4) //nolint
|
|
|
|
coldSet.Mark(k1) //nolint
|
2020-11-26 16:58:03 +00:00
|
|
|
|
|
|
|
mustNotHave(hotSet, k1)
|
|
|
|
mustNotHave(hotSet, k2)
|
|
|
|
mustHave(hotSet, k3)
|
|
|
|
mustHave(hotSet, k4)
|
|
|
|
|
|
|
|
mustHave(coldSet, k1)
|
|
|
|
mustNotHave(coldSet, k2)
|
|
|
|
mustNotHave(coldSet, k3)
|
|
|
|
mustNotHave(coldSet, k4)
|
|
|
|
|
|
|
|
err = hotSet.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = coldSet.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|