fix all tests

This commit is contained in:
Chris Lu 2018-07-08 02:39:04 -07:00
parent d4d7ced922
commit 5b1fd374be
7 changed files with 25 additions and 14 deletions

View file

@ -30,9 +30,10 @@ func loadNewNeedleMap(file *os.File) {
} }
for count > 0 && e == nil { for count > 0 && e == nil {
for i := 0; i < count; i += 16 { for i := 0; i < count; i += 16 {
key := util.BytesToUint64(bytes[i: i+8]) key := BytesToNeedleId(bytes[i:i+NeedleIdSize])
offset := util.BytesToUint32(bytes[i+8: i+12]) offset := BytesToOffset(bytes[i+NeedleIdSize:i+NeedleIdSize+OffsetSize])
size := util.BytesToUint32(bytes[i+12: i+16]) size := util.BytesToUint32(bytes[i+NeedleIdSize+OffsetSize:i+NeedleIdSize+OffsetSize+SizeSize])
if offset > 0 { if offset > 0 {
m.Set(NeedleId(key), offset, size) m.Set(NeedleId(key), offset, size)
} else { } else {

View file

@ -22,7 +22,7 @@ func TestIssue52(t *testing.T) {
func TestXYZ(t *testing.T) { func TestXYZ(t *testing.T) {
m := NewCompactMap() m := NewCompactMap()
for i := uint32(0); i < 100*batch; i += 2 { 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 { 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 { 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++ { // for i := uint32(0); i < 100; i++ {

View file

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
. "github.com/chrislusf/seaweedfs/weed/storage/types"
) )
func TestFastLoadingNeedleMapMetrics(t *testing.T) { func TestFastLoadingNeedleMapMetrics(t *testing.T) {
@ -13,9 +14,9 @@ func TestFastLoadingNeedleMapMetrics(t *testing.T) {
nm := NewBtreeNeedleMap(idxFile) nm := NewBtreeNeedleMap(idxFile)
for i := 0; i < 10000; i++ { 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 { 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)))
} }
} }

View file

@ -1,12 +1,15 @@
package storage package storage
import "testing" import (
"testing"
"github.com/chrislusf/seaweedfs/weed/storage/types"
)
func TestParseKeyHash(t *testing.T) { func TestParseKeyHash(t *testing.T) {
testcases := []struct { testcases := []struct {
KeyHash string KeyHash string
ID uint64 ID types.NeedleId
Cookie uint32 Cookie types.Cookie
Err bool Err bool
}{ }{
// normal // normal

View file

@ -54,6 +54,10 @@ func OffsetToBytes(bytes []byte, offset Offset) {
util.Uint32toBytes(bytes, uint32(offset)) util.Uint32toBytes(bytes, uint32(offset))
} }
func Uint32ToOffset(offset uint32) (Offset) {
return Offset(offset)
}
func BytesToOffset(bytes []byte) (Offset) { func BytesToOffset(bytes []byte) (Offset) {
return Offset(util.BytesToUint32(bytes[0:4])) return Offset(util.BytesToUint32(bytes[0:4]))
} }

View file

@ -5,6 +5,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"testing" "testing"
"github.com/chrislusf/seaweedfs/weed/storage/types"
) )
/* /*
@ -153,12 +154,12 @@ func newRandomNeedle(id uint64) *Needle {
rand.Read(n.Data) rand.Read(n.Data)
n.Checksum = NewCRC(n.Data) n.Checksum = NewCRC(n.Data)
n.Id = id n.Id = types.Uint64ToNeedleId(id)
return n return n
} }
func newEmptyNeedle(id uint64) *Needle { func newEmptyNeedle(id uint64) *Needle {
n := new(Needle) n := new(Needle)
n.Id = id n.Id = types.Uint64ToNeedleId(id)
return n return n
} }

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"github.com/chrislusf/seaweedfs/weed/storage" "github.com/chrislusf/seaweedfs/weed/storage"
"github.com/chrislusf/seaweedfs/weed/storage/types"
) )
var ( var (
@ -21,8 +22,8 @@ func main() {
} }
defer indexFile.Close() defer indexFile.Close()
storage.WalkIndexFile(indexFile, func(key uint64, offset, size uint32) error { 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, offset*8+size) fmt.Printf("key %d, offset %d, size %d, nextOffset %d\n", key, offset*8, size, int64(offset)*types.NeedlePaddingSize+int64(size))
return nil return nil
}) })
} }