Browse Source

simplify volume id printing

pull/2/head
Chris Lu 13 years ago
parent
commit
0c32e2e965
  1. 6
      weed-fs/src/cmd/weed/master.go
  2. 7
      weed-fs/src/pkg/directory/file_id.go
  3. 4
      weed-fs/src/pkg/directory/volume_mapping.go
  4. 2
      weed-fs/src/pkg/storage/volume.go

6
weed-fs/src/cmd/weed/master.go

@ -38,13 +38,13 @@ func dirLookupHandler(w http.ResponseWriter, r *http.Request) {
if commaSep > 0 {
vid = vid[0:commaSep]
}
volumeId, _ := strconv.ParseUint(vid, 10, 64)
machine, e := mapper.Get(storage.VolumeId(volumeId))
volumeId, _ := storage.NewVolumeId(vid)
machine, e := mapper.Get(volumeId)
if e == nil {
writeJson(w, r, machine.Server)
} else {
log.Println("Invalid volume id", volumeId)
writeJson(w, r, map[string]string{"error": "volume id " + strconv.FormatUint(volumeId, 10) + " not found"})
writeJson(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found"})
}
}
func dirAssignHandler(w http.ResponseWriter, r *http.Request) {

7
weed-fs/src/pkg/directory/file_id.go

@ -4,7 +4,6 @@ import (
"encoding/hex"
"log"
"pkg/storage"
"strconv"
"strings"
"pkg/util"
)
@ -25,9 +24,9 @@ func ParseFileId(fid string) *FileId{
return nil
}
vid_string, key_hash_string := a[0], a[1]
vid, _ := strconv.ParseUint(vid_string, 10, 64)
volumeId, _ := storage.NewVolumeId(vid_string)
key, hash := storage.ParseKeyHash(key_hash_string)
return &FileId{VolumeId: storage.VolumeId(vid), Key: key, Hashcode: hash}
return &FileId{VolumeId: volumeId, Key: key, Hashcode: hash}
}
func (n *FileId) String() string {
bytes := make([]byte, 12)
@ -36,5 +35,5 @@ func (n *FileId) String() string {
nonzero_index := 0
for ; bytes[nonzero_index] == 0; nonzero_index++ {
}
return strconv.FormatUint(uint64(n.VolumeId), 10) + "," + hex.EncodeToString(bytes[nonzero_index:])
return n.VolumeId.String() + "," + hex.EncodeToString(bytes[nonzero_index:])
}

4
weed-fs/src/pkg/directory/volume_mapping.go

@ -82,7 +82,7 @@ func (m *Mapper) PickForWrite(c string) (string, int, MachineInfo, error) {
}
return NewFileId(vid, fileId, rand.Uint32()).String(), count, machine.Server, nil
}
return "", 0, m.Machines[rand.Intn(len(m.Machines))].Server, errors.New("Strangely vid " + strconv.FormatUint(uint64(vid), 10) + " is on no machine!")
return "", 0, m.Machines[rand.Intn(len(m.Machines))].Server, errors.New("Strangely vid " + vid.String() + " is on no machine!")
}
func (m *Mapper) NextFileId(c string) (uint64,int) {
count, parseError := strconv.ParseUint(c,10,64)
@ -105,7 +105,7 @@ func (m *Mapper) NextFileId(c string) (uint64,int) {
func (m *Mapper) Get(vid storage.VolumeId) (*Machine, error) {
machineId := m.vid2machineId[vid]
if machineId <= 0 {
return nil, errors.New("invalid volume id " + strconv.FormatUint(uint64(vid), 10))
return nil, errors.New("invalid volume id " + vid.String())
}
return m.Machines[machineId-1], nil
}

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

@ -37,7 +37,7 @@ type Volume struct {
func NewVolume(dirname string, id VolumeId) (v *Volume) {
var e error
v = &Volume{dir: dirname, Id: id}
fileName := strconv.FormatUint(uint64(v.Id), 10)
fileName := id.String()
v.dataFile, e = os.OpenFile(path.Join(v.dir, fileName+".dat"), os.O_RDWR|os.O_CREATE, 0644)
if e != nil {
log.Fatalf("New Volume [ERROR] %s\n", e)

Loading…
Cancel
Save