From 5b1fd374be060a3ca695d14e6f2f023c669b0ac7 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 8 Jul 2018 02:39:04 -0700 Subject: [PATCH] fix all tests --- weed/storage/needle/compact_map_perf_test.go | 7 ++++--- weed/storage/needle/compact_map_test.go | 4 ++-- weed/storage/needle_map_metric_test.go | 5 +++-- weed/storage/needle_test.go | 9 ++++++--- weed/storage/types/needle_types.go | 4 ++++ weed/storage/volume_vacuum_test.go | 5 +++-- weed/tools/read_index.go | 5 +++-- 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/weed/storage/needle/compact_map_perf_test.go b/weed/storage/needle/compact_map_perf_test.go index 59dd7f7f0..7908e8d1d 100644 --- a/weed/storage/needle/compact_map_perf_test.go +++ b/weed/storage/needle/compact_map_perf_test.go @@ -30,9 +30,10 @@ func loadNewNeedleMap(file *os.File) { } for count > 0 && e == nil { for i := 0; i < count; i += 16 { - key := util.BytesToUint64(bytes[i: i+8]) - offset := util.BytesToUint32(bytes[i+8: i+12]) - size := util.BytesToUint32(bytes[i+12: i+16]) + key := BytesToNeedleId(bytes[i:i+NeedleIdSize]) + offset := BytesToOffset(bytes[i+NeedleIdSize:i+NeedleIdSize+OffsetSize]) + size := util.BytesToUint32(bytes[i+NeedleIdSize+OffsetSize:i+NeedleIdSize+OffsetSize+SizeSize]) + if offset > 0 { m.Set(NeedleId(key), offset, size) } else { diff --git a/weed/storage/needle/compact_map_test.go b/weed/storage/needle/compact_map_test.go index 3c5c90fff..11d98eab3 100644 --- a/weed/storage/needle/compact_map_test.go +++ b/weed/storage/needle/compact_map_test.go @@ -22,7 +22,7 @@ func TestIssue52(t *testing.T) { func TestXYZ(t *testing.T) { m := NewCompactMap() for i := uint32(0); i < 100*batch; i += 2 { - m.Set(NeedleId(i), i, i) + m.Set(NeedleId(i), Offset(i), i) } for i := uint32(0); i < 100*batch; i += 37 { @@ -30,7 +30,7 @@ func TestXYZ(t *testing.T) { } for i := uint32(0); i < 10*batch; i += 3 { - m.Set(NeedleId(i), i+11, i+5) + m.Set(NeedleId(i), Offset(i+11), i+5) } // for i := uint32(0); i < 100; i++ { diff --git a/weed/storage/needle_map_metric_test.go b/weed/storage/needle_map_metric_test.go index 0be2e1d7c..400e655a7 100644 --- a/weed/storage/needle_map_metric_test.go +++ b/weed/storage/needle_map_metric_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "math/rand" "github.com/chrislusf/seaweedfs/weed/glog" + . "github.com/chrislusf/seaweedfs/weed/storage/types" ) func TestFastLoadingNeedleMapMetrics(t *testing.T) { @@ -13,9 +14,9 @@ func TestFastLoadingNeedleMapMetrics(t *testing.T) { nm := NewBtreeNeedleMap(idxFile) for i := 0; i < 10000; i++ { - nm.Put(uint64(i+1), uint32(0), uint32(1)) + nm.Put(Uint64ToNeedleId(uint64(i+1)), Uint32ToOffset(uint32(0)), uint32(1)) if rand.Float32() < 0.2 { - nm.Delete(uint64(rand.Int63n(int64(i))+1), uint32(0)) + nm.Delete(Uint64ToNeedleId(uint64(rand.Int63n(int64(i))+1)), Uint32ToOffset(uint32(0))) } } diff --git a/weed/storage/needle_test.go b/weed/storage/needle_test.go index f2b578aa6..4dce3f123 100644 --- a/weed/storage/needle_test.go +++ b/weed/storage/needle_test.go @@ -1,12 +1,15 @@ package storage -import "testing" +import ( + "testing" + "github.com/chrislusf/seaweedfs/weed/storage/types" +) func TestParseKeyHash(t *testing.T) { testcases := []struct { KeyHash string - ID uint64 - Cookie uint32 + ID types.NeedleId + Cookie types.Cookie Err bool }{ // normal diff --git a/weed/storage/types/needle_types.go b/weed/storage/types/needle_types.go index 6e092f100..0c0b6a7a3 100644 --- a/weed/storage/types/needle_types.go +++ b/weed/storage/types/needle_types.go @@ -54,6 +54,10 @@ func OffsetToBytes(bytes []byte, offset Offset) { util.Uint32toBytes(bytes, uint32(offset)) } +func Uint32ToOffset(offset uint32) (Offset) { + return Offset(offset) +} + func BytesToOffset(bytes []byte) (Offset) { return Offset(util.BytesToUint32(bytes[0:4])) } diff --git a/weed/storage/volume_vacuum_test.go b/weed/storage/volume_vacuum_test.go index c74c24b16..896540621 100644 --- a/weed/storage/volume_vacuum_test.go +++ b/weed/storage/volume_vacuum_test.go @@ -5,6 +5,7 @@ import ( "math/rand" "os" "testing" + "github.com/chrislusf/seaweedfs/weed/storage/types" ) /* @@ -153,12 +154,12 @@ func newRandomNeedle(id uint64) *Needle { rand.Read(n.Data) n.Checksum = NewCRC(n.Data) - n.Id = id + n.Id = types.Uint64ToNeedleId(id) return n } func newEmptyNeedle(id uint64) *Needle { n := new(Needle) - n.Id = id + n.Id = types.Uint64ToNeedleId(id) return n } diff --git a/weed/tools/read_index.go b/weed/tools/read_index.go index 642ff786b..d53f489ea 100644 --- a/weed/tools/read_index.go +++ b/weed/tools/read_index.go @@ -7,6 +7,7 @@ import ( "os" "github.com/chrislusf/seaweedfs/weed/storage" + "github.com/chrislusf/seaweedfs/weed/storage/types" ) var ( @@ -21,8 +22,8 @@ func main() { } defer indexFile.Close() - storage.WalkIndexFile(indexFile, func(key uint64, offset, size uint32) error { - fmt.Printf("key %d, offset %d, size %d, nextOffset %d\n", key, offset*8, size, offset*8+size) + storage.WalkIndexFile(indexFile, func(key types.NeedleId, offset types.Offset, size uint32) error { + fmt.Printf("key %d, offset %d, size %d, nextOffset %d\n", key, offset*8, size, int64(offset)*types.NeedlePaddingSize+int64(size)) return nil }) }