2017-05-27 05:51:25 +00:00
|
|
|
package needle
|
2012-09-29 23:07:24 +00:00
|
|
|
|
|
|
|
import (
|
2018-07-08 09:28:04 +00:00
|
|
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
2018-07-22 00:39:10 +00:00
|
|
|
"testing"
|
2012-09-29 23:07:24 +00:00
|
|
|
)
|
|
|
|
|
2013-10-31 19:55:51 +00:00
|
|
|
func TestIssue52(t *testing.T) {
|
|
|
|
m := NewCompactMap()
|
2018-07-08 09:28:04 +00:00
|
|
|
m.Set(NeedleId(10002), 10002, 10002)
|
|
|
|
if element, ok := m.Get(NeedleId(10002)); ok {
|
2013-10-31 19:55:51 +00:00
|
|
|
println("key", 10002, "ok", ok, element.Key, element.Offset, element.Size)
|
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
m.Set(NeedleId(10001), 10001, 10001)
|
|
|
|
if element, ok := m.Get(NeedleId(10002)); ok {
|
2013-10-31 19:55:51 +00:00
|
|
|
println("key", 10002, "ok", ok, element.Key, element.Offset, element.Size)
|
|
|
|
} else {
|
|
|
|
t.Fatal("key 10002 missing after setting 10001")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 09:27:11 +00:00
|
|
|
func TestCompactMap(t *testing.T) {
|
2012-09-29 23:07:24 +00:00
|
|
|
m := NewCompactMap()
|
|
|
|
for i := uint32(0); i < 100*batch; i += 2 {
|
2018-07-08 09:39:04 +00:00
|
|
|
m.Set(NeedleId(i), Offset(i), i)
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); i < 100*batch; i += 37 {
|
2018-07-08 09:28:04 +00:00
|
|
|
m.Delete(NeedleId(i))
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); i < 10*batch; i += 3 {
|
2018-07-08 09:39:04 +00:00
|
|
|
m.Set(NeedleId(i), Offset(i+11), i+5)
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
2013-01-17 08:56:56 +00:00
|
|
|
// for i := uint32(0); i < 100; i++ {
|
|
|
|
// if v := m.Get(Key(i)); v != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
// glog.V(4).Infoln(i, "=", v.Key, v.Offset, v.Size)
|
2013-01-17 08:56:56 +00:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-09-29 23:07:24 +00:00
|
|
|
for i := uint32(0); i < 10*batch; i++ {
|
2018-07-08 09:28:04 +00:00
|
|
|
v, ok := m.Get(NeedleId(i))
|
2012-09-29 23:07:24 +00:00
|
|
|
if i%3 == 0 {
|
2013-01-17 08:56:56 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("key", i, "missing!")
|
|
|
|
}
|
2012-09-29 23:07:24 +00:00
|
|
|
if v.Size != i+5 {
|
|
|
|
t.Fatal("key", i, "size", v.Size)
|
|
|
|
}
|
|
|
|
} else if i%37 == 0 {
|
2013-01-17 08:56:56 +00:00
|
|
|
if ok && v.Size > 0 {
|
2012-09-29 23:07:24 +00:00
|
|
|
t.Fatal("key", i, "should have been deleted needle value", v)
|
|
|
|
}
|
|
|
|
} else if i%2 == 0 {
|
2013-01-17 08:56:56 +00:00
|
|
|
if v.Size != i {
|
2012-09-29 23:07:24 +00:00
|
|
|
t.Fatal("key", i, "size", v.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(10 * batch); i < 100*batch; i++ {
|
2018-07-08 09:28:04 +00:00
|
|
|
v, ok := m.Get(NeedleId(i))
|
2012-09-29 23:07:24 +00:00
|
|
|
if i%37 == 0 {
|
2013-01-17 08:56:56 +00:00
|
|
|
if ok && v.Size > 0 {
|
|
|
|
t.Fatal("key", i, "should have been deleted needle value", v)
|
|
|
|
}
|
2012-09-29 23:07:24 +00:00
|
|
|
} else if i%2 == 0 {
|
2013-01-17 08:56:56 +00:00
|
|
|
if v == nil {
|
|
|
|
t.Fatal("key", i, "missing")
|
|
|
|
}
|
2012-09-29 23:07:24 +00:00
|
|
|
if v.Size != i {
|
|
|
|
t.Fatal("key", i, "size", v.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-12-09 09:27:11 +00:00
|
|
|
|
|
|
|
func TestOverflow(t *testing.T) {
|
|
|
|
o := Overflow(make([]NeedleValue, 0))
|
|
|
|
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 1, Offset: 12, Size: 12})
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 2, Offset: 12, Size: 12})
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 3, Offset: 12, Size: 12})
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 4, Offset: 12, Size: 12})
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 5, Offset: 12, Size: 12})
|
|
|
|
|
|
|
|
if o[2].Key != 3 {
|
|
|
|
t.Fatalf("expecting o[2] has key 3: %+v", o[2].Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 3, Offset: 24, Size: 24})
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 4, Offset: 44, Size: 44})
|
|
|
|
for i, x := range o {
|
|
|
|
println("overflow[", i, "]:", x.Key)
|
|
|
|
}
|
|
|
|
println()
|
|
|
|
|
|
|
|
o = o.setOverflowEntry(NeedleValue{Key: 1, Offset: 11, Size: 11})
|
|
|
|
|
|
|
|
for i, x := range o {
|
|
|
|
println("overflow[", i, "]:", x.Key)
|
|
|
|
}
|
|
|
|
println()
|
|
|
|
|
|
|
|
}
|