seaweedfs/weed/storage/needle/compact_map_test.go

160 lines
4.0 KiB
Go
Raw Normal View History

2017-05-27 13:51:25 +08:00
package needle
import (
2019-04-09 10:40:56 +08:00
"fmt"
. "github.com/chrislusf/seaweedfs/weed/storage/types"
2018-07-22 08:39:10 +08:00
"testing"
)
2018-12-23 03:05:29 +08:00
func TestOverflow2(t *testing.T) {
m := NewCompactMap()
2019-04-09 10:40:56 +08:00
m.Set(NeedleId(150088), ToOffset(8), 3000073)
m.Set(NeedleId(150073), ToOffset(8), 3000073)
m.Set(NeedleId(150089), ToOffset(8), 3000073)
m.Set(NeedleId(150076), ToOffset(8), 3000073)
m.Set(NeedleId(150124), ToOffset(8), 3000073)
m.Set(NeedleId(150137), ToOffset(8), 3000073)
m.Set(NeedleId(150147), ToOffset(8), 3000073)
m.Set(NeedleId(150145), ToOffset(8), 3000073)
m.Set(NeedleId(150158), ToOffset(8), 3000073)
m.Set(NeedleId(150162), ToOffset(8), 3000073)
2018-12-23 03:05:29 +08:00
m.Visit(func(value NeedleValue) error {
println("needle key:", value.Key)
return nil
})
}
2013-11-01 03:55:51 +08:00
func TestIssue52(t *testing.T) {
m := NewCompactMap()
2019-04-09 10:40:56 +08:00
m.Set(NeedleId(10002), ToOffset(10002), 10002)
if element, ok := m.Get(NeedleId(10002)); ok {
2019-04-09 10:40:56 +08:00
fmt.Printf("key %d ok %v %d, %v, %d\n", 10002, ok, element.Key, element.Offset, element.Size)
2013-11-01 03:55:51 +08:00
}
2019-04-09 10:40:56 +08:00
m.Set(NeedleId(10001), ToOffset(10001), 10001)
if element, ok := m.Get(NeedleId(10002)); ok {
2019-04-09 10:40:56 +08:00
fmt.Printf("key %d ok %v %d, %v, %d\n", 10002, ok, element.Key, element.Offset, element.Size)
2013-11-01 03:55:51 +08:00
} else {
t.Fatal("key 10002 missing after setting 10001")
}
}
2018-12-09 17:27:11 +08:00
func TestCompactMap(t *testing.T) {
m := NewCompactMap()
for i := uint32(0); i < 100*batch; i += 2 {
2019-04-09 10:40:56 +08:00
m.Set(NeedleId(i), ToOffset(int64(i)), i)
}
for i := uint32(0); i < 100*batch; i += 37 {
m.Delete(NeedleId(i))
}
for i := uint32(0); i < 10*batch; i += 3 {
2019-04-09 10:40:56 +08:00
m.Set(NeedleId(i), ToOffset(int64(i+11)), i+5)
}
2013-01-17 16:56:56 +08:00
// for i := uint32(0); i < 100; i++ {
// if v := m.Get(Key(i)); v != nil {
// glog.V(4).Infoln(i, "=", v.Key, v.Offset, v.Size)
2013-01-17 16:56:56 +08:00
// }
// }
for i := uint32(0); i < 10*batch; i++ {
v, ok := m.Get(NeedleId(i))
if i%3 == 0 {
2013-01-17 16:56:56 +08:00
if !ok {
t.Fatal("key", i, "missing!")
}
if v.Size != i+5 {
t.Fatal("key", i, "size", v.Size)
}
} else if i%37 == 0 {
2019-01-26 14:10:29 +08:00
if ok && v.Size != TombstoneFileSize {
t.Fatal("key", i, "should have been deleted needle value", v)
}
} else if i%2 == 0 {
2013-01-17 16:56:56 +08:00
if v.Size != i {
t.Fatal("key", i, "size", v.Size)
}
}
}
for i := uint32(10 * batch); i < 100*batch; i++ {
v, ok := m.Get(NeedleId(i))
if i%37 == 0 {
2019-01-26 14:10:29 +08:00
if ok && v.Size != TombstoneFileSize {
2013-01-17 16:56:56 +08:00
t.Fatal("key", i, "should have been deleted needle value", v)
}
} else if i%2 == 0 {
2013-01-17 16:56:56 +08:00
if v == nil {
t.Fatal("key", i, "missing")
}
if v.Size != i {
t.Fatal("key", i, "size", v.Size)
}
}
}
}
2018-12-09 17:27:11 +08:00
func TestOverflow(t *testing.T) {
2018-12-15 21:55:56 +08:00
o := Overflow(make([]SectionalNeedleValue, 0))
2018-12-09 17:27:11 +08:00
2019-04-09 10:40:56 +08:00
o = o.setOverflowEntry(SectionalNeedleValue{Key: 1, Offset: ToOffset(12), Size: 12})
o = o.setOverflowEntry(SectionalNeedleValue{Key: 2, Offset: ToOffset(12), Size: 12})
o = o.setOverflowEntry(SectionalNeedleValue{Key: 3, Offset: ToOffset(12), Size: 12})
o = o.setOverflowEntry(SectionalNeedleValue{Key: 4, Offset: ToOffset(12), Size: 12})
o = o.setOverflowEntry(SectionalNeedleValue{Key: 5, Offset: ToOffset(12), Size: 12})
2018-12-09 17:27:11 +08:00
if o[2].Key != 3 {
t.Fatalf("expecting o[2] has key 3: %+v", o[2].Key)
}
2019-04-09 10:40:56 +08:00
o = o.setOverflowEntry(SectionalNeedleValue{Key: 3, Offset: ToOffset(24), Size: 24})
2018-12-09 17:27:11 +08:00
if o[2].Key != 3 {
t.Fatalf("expecting o[2] has key 3: %+v", o[2].Key)
}
if o[2].Size != 24 {
t.Fatalf("expecting o[2] has size 24: %+v", o[2].Size)
}
o = o.deleteOverflowEntry(4)
if len(o) != 4 {
t.Fatalf("expecting 4 entries now: %+v", o)
}
x, _ := o.findOverflowEntry(5)
if x.Key != 5 {
t.Fatalf("expecting entry 5 now: %+v", x)
}
for i, x := range o {
println("overflow[", i, "]:", x.Key)
}
println()
o = o.deleteOverflowEntry(1)
for i, x := range o {
println("overflow[", i, "]:", x.Key)
}
println()
2019-04-09 10:40:56 +08:00
o = o.setOverflowEntry(SectionalNeedleValue{Key: 4, Offset: ToOffset(44), Size: 44})
2018-12-09 17:27:11 +08:00
for i, x := range o {
println("overflow[", i, "]:", x.Key)
}
println()
2019-04-09 10:40:56 +08:00
o = o.setOverflowEntry(SectionalNeedleValue{Key: 1, Offset: ToOffset(11), Size: 11})
2018-12-09 17:27:11 +08:00
for i, x := range o {
println("overflow[", i, "]:", x.Key)
}
println()
}