simplify the file id format
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@19 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
44
weed-fs/src/pkg/directory/file_id.go
Normal file
44
weed-fs/src/pkg/directory/file_id.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package directory
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"storage"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileId struct {
|
||||
VolumeId uint32
|
||||
Key uint64
|
||||
Hashcode uint32
|
||||
}
|
||||
|
||||
func NewFileId(VolumeId uint32, Key uint64, Hashcode uint32) *FileId {
|
||||
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
|
||||
}
|
||||
|
||||
func ParseFileId(path string) *FileId {
|
||||
a := strings.Split(path, ",")
|
||||
if len(a) != 2 {
|
||||
return nil
|
||||
}
|
||||
vid_string, key_hash_string := a[0], a[1]
|
||||
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
||||
key_hash_len := len(key_hash_bytes)
|
||||
if khe != nil || key_hash_len <= 4 {
|
||||
return nil
|
||||
}
|
||||
vid, _ := strconv.Atoui64(vid_string)
|
||||
key := storage.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
|
||||
hash := storage.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
|
||||
return &FileId{VolumeId: uint32(vid), Key: key, Hashcode: hash}
|
||||
}
|
||||
func (n *FileId) String() string {
|
||||
bytes := make([]byte, 12)
|
||||
storage.Uint64toBytes(bytes[0:8], n.Key)
|
||||
storage.Uint32toBytes(bytes[8:12], n.Hashcode)
|
||||
nonzero_index := 0
|
||||
for ; bytes[nonzero_index] == 0; nonzero_index++ {
|
||||
}
|
||||
return strconv.Uitoa64(uint64(n.VolumeId)) + "," + hex.EncodeToString(bytes[nonzero_index:])
|
||||
}
|
||||
16
weed-fs/src/pkg/directory/file_id_test.go
Normal file
16
weed-fs/src/pkg/directory/file_id_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package directory
|
||||
|
||||
import {
|
||||
"testing"
|
||||
"log"
|
||||
}
|
||||
func TestSerialDeserialization(t *testing.T) {
|
||||
f1 := &FileId{VolumeId: 345, Key:8698, Hashcode: 23849095}
|
||||
log.Println("vid", f1.VolumeId, "key", f1.Key, "hash", f1.Hashcode)
|
||||
|
||||
f2 := ParseFileId(t.String())
|
||||
|
||||
log.Println("vvid", f2.VolumeId, "vkey", f2.Key, "vhash", f2.Hashcode)
|
||||
|
||||
t.
|
||||
}
|
||||
@@ -32,10 +32,10 @@ type Mapper struct {
|
||||
|
||||
lock sync.Mutex
|
||||
Machines []*Machine
|
||||
vid2machineId map[uint64]int
|
||||
vid2machineId map[uint32]int
|
||||
Writers []int // transient array of Writers volume id
|
||||
|
||||
GlobalVolumeSequence uint64
|
||||
GlobalVolumeSequence uint32
|
||||
|
||||
FileIdSequence uint64
|
||||
fileIdCounter uint64
|
||||
@@ -49,7 +49,7 @@ func NewMapper(dirname string, filename string, capacity int) (m *Mapper) {
|
||||
m = &Mapper{dir: dirname, fileName: filename, capacity: capacity}
|
||||
log.Println("Loading volume id to maching mapping:", path.Join(m.dir, m.fileName+".map"))
|
||||
dataFile, e := os.OpenFile(path.Join(m.dir, m.fileName+".map"), os.O_RDONLY, 0644)
|
||||
m.vid2machineId = make(map[uint64]int)
|
||||
m.vid2machineId = make(map[uint32]int)
|
||||
m.Writers = *new([]int)
|
||||
if e != nil {
|
||||
log.Println("Mapping File Read", e)
|
||||
@@ -86,10 +86,10 @@ func NewMapper(dirname string, filename string, capacity int) (m *Mapper) {
|
||||
}
|
||||
return
|
||||
}
|
||||
func (m *Mapper) PickForWrite() (vid uint64, server MachineInfo) {
|
||||
func (m *Mapper) PickForWrite() (string, MachineInfo) {
|
||||
machine := m.Machines[m.Writers[rand.Intn(len(m.Writers))]]
|
||||
vid = machine.Volumes[rand.Intn(len(machine.Volumes))].Id
|
||||
return vid, machine.Server
|
||||
vid := machine.Volumes[rand.Intn(len(machine.Volumes))].Id
|
||||
return NewFileId(vid,m.NextFileId(),rand.Uint32()).String(), machine.Server
|
||||
}
|
||||
func (m *Mapper) NextFileId() uint64 {
|
||||
if m.fileIdCounter <= 0 {
|
||||
@@ -99,10 +99,10 @@ func (m *Mapper) NextFileId() uint64 {
|
||||
m.fileIdCounter--
|
||||
return m.FileIdSequence - m.fileIdCounter
|
||||
}
|
||||
func (m *Mapper) Get(vid uint64) *Machine {
|
||||
func (m *Mapper) Get(vid uint32) *Machine {
|
||||
return m.Machines[m.vid2machineId[vid]]
|
||||
}
|
||||
func (m *Mapper) Add(machine Machine) []uint64 {
|
||||
func (m *Mapper) Add(machine Machine) []uint32 {
|
||||
log.Println("Adding existing", machine.Server.Url, len(machine.Volumes), "volumes to dir", len(m.Machines))
|
||||
log.Println("Adding new ", machine.Server.Url, machine.Capacity-len(machine.Volumes), "volumes to dir", len(m.Machines))
|
||||
//check existing machine, linearly
|
||||
@@ -121,7 +121,7 @@ func (m *Mapper) Add(machine Machine) []uint64 {
|
||||
}
|
||||
|
||||
//generate new volumes
|
||||
vids := new([]uint64)
|
||||
vids := new([]uint32)
|
||||
for vid, i := m.GlobalVolumeSequence, len(machine.Volumes); i < machine.Capacity; i, vid = i+1, vid+1 {
|
||||
newVolume := storage.VolumeInfo{Id: vid, Size: 0}
|
||||
machine.Volumes = append(machine.Volumes, newVolume)
|
||||
|
||||
Reference in New Issue
Block a user