Browse Source

refactor for reading super block

pull/2/head
Chris Lu 12 years ago
parent
commit
79a49ada39
  1. 15
      weed-fs/src/cmd/weed/fix.go
  2. 16
      weed-fs/src/pkg/storage/needle_read_write.go
  3. 23
      weed-fs/src/pkg/storage/volume.go

15
weed-fs/src/cmd/weed/fix.go

@ -1,10 +1,10 @@
package main
import (
"pkg/storage"
"log"
"os"
"path"
"pkg/storage"
"strconv"
)
@ -44,10 +44,15 @@ func runFix(cmd *Command, args []string) bool {
}
defer indexFile.Close()
//skip the volume super block
dataFile.Seek(storage.SuperBlockSize, 0)
dataFile.Seek(0, 0)
header := make([]byte, storage.SuperBlockSize)
if _, e := dataFile.Read(header); e != nil {
log.Fatalf("cannot read superblock: %s", e)
}
ver, _, _ := storage.ParseSuperBlock(header)
n, rest := storage.ReadNeedle(dataFile)
n, rest := storage.ReadNeedle(dataFile, ver)
dataFile.Seek(int64(rest), 1)
nm := storage.NewNeedleMap(indexFile)
offset := uint32(storage.SuperBlockSize)
@ -58,7 +63,7 @@ func runFix(cmd *Command, args []string) bool {
debug("saved", count, "with error", pe)
}
offset += rest + 16
n, rest = storage.ReadNeedle(dataFile)
n, rest = storage.ReadNeedle(dataFile, ver)
dataFile.Seek(int64(rest), 1)
}
return true

16
weed-fs/src/pkg/storage/needle_read_write.go

@ -15,14 +15,14 @@ func (n *Needle) Append(w io.Writer) uint32 {
util.Uint32toBytes(header[12:16], n.Size)
w.Write(header)
w.Write(n.Data)
rest := 8 - ((n.Size + 16 + 4) % 8)
rest := 8 - ((16 + n.Size + 4) % 8)
util.Uint32toBytes(header[0:4], n.Checksum.Value())
w.Write(header[0 : rest+4])
w.Write(header[0 : 4+rest])
return n.Size
}
func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
if version == Version1 {
bytes := make([]byte, size+16+4)
bytes := make([]byte, 16+size+4)
ret, e := r.Read(bytes)
n.Cookie = util.BytesToUint32(bytes[0:4])
n.Id = util.BytesToUint64(bytes[4:12])
@ -37,8 +37,9 @@ func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
}
return 0, errors.New("Unsupported Version!")
}
func ReadNeedle(r *os.File) (*Needle, uint32) {
n := new(Needle)
func ReadNeedle(r *os.File, version Version) (n *Needle, bytesTillNextFile uint32) {
n = new(Needle)
if version == Version1 {
bytes := make([]byte, 16)
count, e := r.Read(bytes)
if count <= 0 || e != nil {
@ -48,5 +49,8 @@ func ReadNeedle(r *os.File) (*Needle, uint32) {
n.Id = util.BytesToUint64(bytes[4:12])
n.Size = util.BytesToUint32(bytes[12:16])
rest := 8 - ((n.Size + 16 + 4) % 8)
return n, n.Size + 4 + rest
bytesTillNextFile = n.Size + 4 + rest
} else if version == Version2 {
}
return
}

23
weed-fs/src/pkg/storage/volume.go

@ -18,8 +18,8 @@ type Volume struct {
dataFile *os.File
nm *NeedleMap
replicaType ReplicationType
version Version
replicaType ReplicationType
accessLock sync.Mutex
}
@ -85,12 +85,17 @@ func (v *Volume) readSuperBlock() error {
if _, e := v.dataFile.Read(header); e != nil {
return fmt.Errorf("cannot read superblock: %s", e)
}
v.version = Version(header[0])
var err error
if v.replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
return fmt.Errorf("cannot read replica type: %s", err)
v.version, v.replicaType, err = ParseSuperBlock(header)
return err
}
return nil
func ParseSuperBlock(header []byte) (version Version, replicaType ReplicationType, e error) {
version = Version(header[0])
var err error
if replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
e = fmt.Errorf("cannot read replica type: %s", err)
}
return
}
func (v *Volume) NeedToReplicate() bool {
return v.replicaType.GetCopyCount() > 1
@ -143,7 +148,7 @@ func (v *Volume) compact() error {
filePath := path.Join(v.dir, v.Id.String())
return v.copyDataAndGenerateIndexFile(filePath+".dat", filePath+".cpd", filePath+".cpx")
}
func (v *Volume) commitCompact() (error) {
func (v *Volume) commitCompact() error {
v.accessLock.Lock()
defer v.accessLock.Unlock()
v.dataFile.Close()
@ -185,7 +190,9 @@ func (v *Volume) copyDataAndGenerateIndexFile(srcName, dstName, idxName string)
dst.Write(header)
}
n, rest := ReadNeedle(src)
version, _, _ := ParseSuperBlock(header)
n, rest := ReadNeedle(src, version)
nm := NewNeedleMap(idx)
old_offset := uint32(SuperBlockSize)
new_offset := uint32(SuperBlockSize)
@ -208,7 +215,7 @@ func (v *Volume) copyDataAndGenerateIndexFile(srcName, dstName, idxName string)
src.Seek(int64(rest-n.Size-4), 1)
}
old_offset += rest + 16
n, rest = ReadNeedle(src)
n, rest = ReadNeedle(src, version)
}
return nil

Loading…
Cancel
Save