adding ttl field to volume super block
This commit is contained in:
@@ -2,6 +2,7 @@ package storage
|
||||
|
||||
import (
|
||||
"code.google.com/p/weed-fs/go/glog"
|
||||
"code.google.com/p/weed-fs/go/util"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
@@ -10,9 +11,16 @@ const (
|
||||
SuperBlockSize = 8
|
||||
)
|
||||
|
||||
/*
|
||||
* Super block currently has 8 bytes allocated for each volume.
|
||||
* Byte 0: version, 1 or 2
|
||||
* Byte 1: Replica Placement strategy, 000, 001, 002, 010, etc
|
||||
* Byte 2 and byte 3: Time to live in minutes
|
||||
*/
|
||||
type SuperBlock struct {
|
||||
version Version
|
||||
ReplicaPlacement *ReplicaPlacement
|
||||
Ttl uint16
|
||||
}
|
||||
|
||||
func (s *SuperBlock) Version() Version {
|
||||
@@ -22,6 +30,7 @@ func (s *SuperBlock) Bytes() []byte {
|
||||
header := make([]byte, SuperBlockSize)
|
||||
header[0] = byte(s.version)
|
||||
header[1] = s.ReplicaPlacement.Byte()
|
||||
util.Uint16toBytes(header[2:4], s.Ttl)
|
||||
return header
|
||||
}
|
||||
|
||||
@@ -61,5 +70,6 @@ func ParseSuperBlock(header []byte) (superBlock SuperBlock, err error) {
|
||||
if superBlock.ReplicaPlacement, err = NewReplicaPlacementFromByte(header[1]); err != nil {
|
||||
err = fmt.Errorf("cannot read replica type: %s", err.Error())
|
||||
}
|
||||
superBlock.Ttl = util.BytesToUint16(header[2:4])
|
||||
return
|
||||
}
|
||||
|
||||
22
go/storage/volume_super_block_test.go
Normal file
22
go/storage/volume_super_block_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSuperBlockReadWrite(t *testing.T) {
|
||||
rp, _ := NewReplicaPlacementFromByte(byte(001))
|
||||
s := &SuperBlock{
|
||||
version: CurrentVersion,
|
||||
ReplicaPlacement: rp,
|
||||
Ttl: uint16(35),
|
||||
}
|
||||
|
||||
bytes := s.Bytes()
|
||||
|
||||
if !(bytes[2] == 0 && bytes[3] == 35) {
|
||||
println("byte[2]:", bytes[2], "byte[3]:", bytes[3])
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user