2019-04-19 12:43:36 +08:00
|
|
|
package needle_map
|
2012-09-30 07:07:24 +08:00
|
|
|
|
|
|
|
import (
|
2019-04-09 10:40:56 +08:00
|
|
|
"fmt"
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/sequence"
|
|
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2018-07-22 08:39:10 +08:00
|
|
|
"testing"
|
2012-09-30 07:07:24 +08:00
|
|
|
)
|
|
|
|
|
2022-06-24 12:45:51 +08:00
|
|
|
func TestSnowflakeSequencer(t *testing.T) {
|
|
|
|
m := NewCompactMap()
|
|
|
|
seq, _ := sequence.NewSnowflakeSequencer("for_test", 1)
|
|
|
|
|
|
|
|
for i := 0; i < 200000; i++ {
|
|
|
|
id := seq.NextFileId(1)
|
|
|
|
oldOffset, oldSize := m.Set(NeedleId(id), ToOffset(8), 3000073)
|
|
|
|
if oldSize != 0 {
|
|
|
|
t.Errorf("id %d oldOffset %v oldSize %d", id, oldOffset, oldSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-23 03:05:29 +08:00
|
|
|
func TestOverflow2(t *testing.T) {
|
|
|
|
m := NewCompactMap()
|
2019-12-25 03:29:26 +08:00
|
|
|
_, oldSize := m.Set(NeedleId(150088), ToOffset(8), 3000073)
|
2019-12-25 06:56:16 +08:00
|
|
|
if oldSize != 0 {
|
2019-12-25 03:29:26 +08:00
|
|
|
t.Fatalf("expecting no previous data")
|
|
|
|
}
|
|
|
|
_, oldSize = m.Set(NeedleId(150088), ToOffset(8), 3000073)
|
2019-12-25 06:56:16 +08:00
|
|
|
if oldSize != 3000073 {
|
2019-12-25 03:29:26 +08:00
|
|
|
t.Fatalf("expecting previous data size is %d, not %d", 3000073, oldSize)
|
|
|
|
}
|
2019-04-09 10:40:56 +08:00
|
|
|
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
|
|
|
|
2019-05-19 13:46:24 +08:00
|
|
|
m.AscendingVisit(func(value NeedleValue) error {
|
2018-12-23 03:05:29 +08:00
|
|
|
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)
|
2018-07-08 17:28:04 +08:00
|
|
|
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)
|
2018-07-08 17:28:04 +08:00
|
|
|
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) {
|
2012-09-30 07:07:24 +08:00
|
|
|
m := NewCompactMap()
|
|
|
|
for i := uint32(0); i < 100*batch; i += 2 {
|
2020-08-19 08:04:28 +08:00
|
|
|
m.Set(NeedleId(i), ToOffset(int64(i)), Size(i))
|
2012-09-30 07:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); i < 100*batch; i += 37 {
|
2018-07-08 17:28:04 +08:00
|
|
|
m.Delete(NeedleId(i))
|
2012-09-30 07:07:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(0); i < 10*batch; i += 3 {
|
2020-08-19 08:04:28 +08:00
|
|
|
m.Set(NeedleId(i), ToOffset(int64(i+11)), Size(i+5))
|
2012-09-30 07:07:24 +08:00
|
|
|
}
|
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
// for i := uint32(0); i < 100; i++ {
|
|
|
|
// if v := m.Get(Key(i)); v != nil {
|
2013-08-09 14:57:22 +08:00
|
|
|
// glog.V(4).Infoln(i, "=", v.Key, v.Offset, v.Size)
|
2013-01-17 16:56:56 +08:00
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2012-09-30 07:07:24 +08:00
|
|
|
for i := uint32(0); i < 10*batch; i++ {
|
2018-07-08 17:28:04 +08:00
|
|
|
v, ok := m.Get(NeedleId(i))
|
2012-09-30 07:07:24 +08:00
|
|
|
if i%3 == 0 {
|
2013-01-17 16:56:56 +08:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("key", i, "missing!")
|
|
|
|
}
|
2020-08-19 08:04:28 +08:00
|
|
|
if v.Size != Size(i+5) {
|
2012-09-30 07:07:24 +08:00
|
|
|
t.Fatal("key", i, "size", v.Size)
|
|
|
|
}
|
|
|
|
} else if i%37 == 0 {
|
2020-08-19 09:01:37 +08:00
|
|
|
if ok && v.Size.IsValid() {
|
2012-09-30 07:07:24 +08:00
|
|
|
t.Fatal("key", i, "should have been deleted needle value", v)
|
|
|
|
}
|
|
|
|
} else if i%2 == 0 {
|
2020-08-19 08:04:28 +08:00
|
|
|
if v.Size != Size(i) {
|
2012-09-30 07:07:24 +08:00
|
|
|
t.Fatal("key", i, "size", v.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := uint32(10 * batch); i < 100*batch; i++ {
|
2018-07-08 17:28:04 +08:00
|
|
|
v, ok := m.Get(NeedleId(i))
|
2012-09-30 07:07:24 +08:00
|
|
|
if i%37 == 0 {
|
2020-08-19 09:01:37 +08:00
|
|
|
if ok && v.Size.IsValid() {
|
2013-01-17 16:56:56 +08:00
|
|
|
t.Fatal("key", i, "should have been deleted needle value", v)
|
|
|
|
}
|
2012-09-30 07:07:24 +08:00
|
|
|
} else if i%2 == 0 {
|
2013-01-17 16:56:56 +08:00
|
|
|
if v == nil {
|
|
|
|
t.Fatal("key", i, "missing")
|
|
|
|
}
|
2020-08-19 08:04:28 +08:00
|
|
|
if v.Size != Size(i) {
|
2012-09-30 07:07:24 +08:00
|
|
|
t.Fatal("key", i, "size", v.Size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-12-09 17:27:11 +08:00
|
|
|
|
|
|
|
func TestOverflow(t *testing.T) {
|
2019-04-09 12:44:06 +08:00
|
|
|
cs := NewCompactSection(1)
|
2018-12-09 17:27:11 +08:00
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
cs.setOverflowEntry(1, ToOffset(12), 12)
|
|
|
|
cs.setOverflowEntry(2, ToOffset(12), 12)
|
|
|
|
cs.setOverflowEntry(3, ToOffset(12), 12)
|
|
|
|
cs.setOverflowEntry(4, ToOffset(12), 12)
|
|
|
|
cs.setOverflowEntry(5, ToOffset(12), 12)
|
2018-12-09 17:27:11 +08:00
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
if cs.overflow[2].Key != 3 {
|
|
|
|
t.Fatalf("expecting o[2] has key 3: %+v", cs.overflow[2].Key)
|
2018-12-09 17:27:11 +08:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
cs.setOverflowEntry(3, ToOffset(24), 24)
|
2018-12-09 17:27:11 +08:00
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
if cs.overflow[2].Key != 3 {
|
|
|
|
t.Fatalf("expecting o[2] has key 3: %+v", cs.overflow[2].Key)
|
2018-12-09 17:27:11 +08:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
if cs.overflow[2].Size != 24 {
|
|
|
|
t.Fatalf("expecting o[2] has size 24: %+v", cs.overflow[2].Size)
|
2018-12-09 17:27:11 +08:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
cs.deleteOverflowEntry(4)
|
2018-12-09 17:27:11 +08:00
|
|
|
|
2020-08-19 16:37:56 +08:00
|
|
|
if len(cs.overflow) != 5 {
|
|
|
|
t.Fatalf("expecting 5 entries now: %+v", cs.overflow)
|
2018-12-09 17:27:11 +08:00
|
|
|
}
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
_, x, _ := cs.findOverflowEntry(5)
|
2018-12-09 17:27:11 +08:00
|
|
|
if x.Key != 5 {
|
|
|
|
t.Fatalf("expecting entry 5 now: %+v", x)
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
for i, x := range cs.overflow {
|
2018-12-09 17:27:11 +08:00
|
|
|
println("overflow[", i, "]:", x.Key)
|
|
|
|
}
|
|
|
|
println()
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
cs.deleteOverflowEntry(1)
|
2018-12-09 17:27:11 +08:00
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
for i, x := range cs.overflow {
|
2020-08-19 16:37:56 +08:00
|
|
|
println("overflow[", i, "]:", x.Key, "size", x.Size)
|
2018-12-09 17:27:11 +08:00
|
|
|
}
|
|
|
|
println()
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
cs.setOverflowEntry(4, ToOffset(44), 44)
|
|
|
|
for i, x := range cs.overflow {
|
2018-12-09 17:27:11 +08:00
|
|
|
println("overflow[", i, "]:", x.Key)
|
|
|
|
}
|
|
|
|
println()
|
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
cs.setOverflowEntry(1, ToOffset(11), 11)
|
2018-12-09 17:27:11 +08:00
|
|
|
|
2019-04-09 12:44:06 +08:00
|
|
|
for i, x := range cs.overflow {
|
2018-12-09 17:27:11 +08:00
|
|
|
println("overflow[", i, "]:", x.Key)
|
|
|
|
}
|
|
|
|
println()
|
|
|
|
|
|
|
|
}
|