Browse Source

add version type

pull/2/head
Chris Lu 12 years ago
parent
commit
5e020196f6
  1. 4
      weed-fs/src/cmd/weed/fix.go
  2. 9
      weed-fs/src/pkg/storage/needle.go
  3. 13
      weed-fs/src/pkg/storage/volume.go

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

@ -15,8 +15,8 @@ func init() {
var cmdFix = &Command{ var cmdFix = &Command{
UsageLine: "fix -dir=/tmp -volumeId=234 -debug=1", UsageLine: "fix -dir=/tmp -volumeId=234 -debug=1",
Short: "run weed tool fix on data file if corrupted",
Long: `Fix runs the WeedFS fix command on the .dat volume file.
Short: "run weed tool fix on index file if corrupted",
Long: `Fix runs the WeedFS fix command to re-create the index .idx file.
`, `,
} }

9
weed-fs/src/pkg/storage/needle.go

@ -17,8 +17,13 @@ import (
type Needle struct { type Needle struct {
Cookie uint32 "random number to mitigate brute force lookups" Cookie uint32 "random number to mitigate brute force lookups"
Id uint64 "needle id" Id uint64 "needle id"
Size uint32 "Data size"
Size uint32 "sum of DataSize,Data,NameSize,Name,MimeSize,Mime"
// DataSize uint32 "Data size"
Data []byte "The actual file data" Data []byte "The actual file data"
// NameSize uint16
// Name []byte "maximum 256 characters"
// MimeSize uint16
// Mime []byte "maximum 256 characters"
Checksum CRC "CRC32 to check integrity" Checksum CRC "CRC32 to check integrity"
Padding []byte "Aligned to 8 bytes" Padding []byte "Aligned to 8 bytes"
} }
@ -97,7 +102,7 @@ func (n *Needle) Append(w io.Writer) uint32 {
w.Write(header[0 : rest+4]) w.Write(header[0 : rest+4])
return n.Size return n.Size
} }
func (n *Needle) Read(r io.Reader, size uint32) (int, error) {
func (n *Needle) Read(r io.Reader, size uint32, version Version) (int, error) {
bytes := make([]byte, size+16+4) bytes := make([]byte, size+16+4)
ret, e := r.Read(bytes) ret, e := r.Read(bytes)
n.Cookie = util.BytesToUint32(bytes[0:4]) n.Cookie = util.BytesToUint32(bytes[0:4])

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

@ -9,9 +9,11 @@ import (
"sync" "sync"
) )
type Version uint8
const ( const (
SuperBlockSize = 8 SuperBlockSize = 8
Version = 1
CurrentVersion = Version(1)
) )
type Volume struct { type Volume struct {
@ -21,8 +23,7 @@ type Volume struct {
nm *NeedleMap nm *NeedleMap
replicaType ReplicationType replicaType ReplicationType
version uint8
version Version
accessLock sync.Mutex accessLock sync.Mutex
} }
@ -68,7 +69,7 @@ func (v *Volume) maybeWriteSuperBlock() {
stat, _ := v.dataFile.Stat() stat, _ := v.dataFile.Stat()
if stat.Size() == 0 { if stat.Size() == 0 {
header := make([]byte, SuperBlockSize) header := make([]byte, SuperBlockSize)
header[0] = Version
header[0] = byte(CurrentVersion)
header[1] = v.replicaType.Byte() header[1] = v.replicaType.Byte()
v.dataFile.Write(header) v.dataFile.Write(header)
} }
@ -79,7 +80,7 @@ func (v *Volume) readSuperBlock() error {
if _, e := v.dataFile.Read(header); e != nil { if _, e := v.dataFile.Read(header); e != nil {
return fmt.Errorf("cannot read superblock: %s", e) return fmt.Errorf("cannot read superblock: %s", e)
} }
v.version = header[0]
v.version = Version(header[0])
var err error var err error
if v.replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil { if v.replicaType, err = NewReplicationTypeFromByte(header[1]); err != nil {
return fmt.Errorf("cannot read replica type: %s", err) return fmt.Errorf("cannot read replica type: %s", err)
@ -120,7 +121,7 @@ func (v *Volume) read(n *Needle) (int, error) {
nv, ok := v.nm.Get(n.Id) nv, ok := v.nm.Get(n.Id)
if ok && nv.Offset > 0 { if ok && nv.Offset > 0 {
v.dataFile.Seek(int64(nv.Offset)*8, 0) v.dataFile.Seek(int64(nv.Offset)*8, 0)
return n.Read(v.dataFile, nv.Size)
return n.Read(v.dataFile, nv.Size, v.version)
} }
return -1, errors.New("Not Found") return -1, errors.New("Not Found")
} }

Loading…
Cancel
Save