compatible with Go1
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@46 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
@@ -1,36 +0,0 @@
|
||||
# Makefile generated by gb: http://go-gb.googlecode.com
|
||||
# gb provides configuration-free building and distributing
|
||||
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=directory
|
||||
GOFILES=\
|
||||
file_id.go\
|
||||
volume_mapping.go\
|
||||
|
||||
# gb: this is the local install
|
||||
GBROOT=../../..
|
||||
|
||||
# gb: compile/link against local install
|
||||
GCIMPORTS+= -I $(GBROOT)/_obj
|
||||
LDIMPORTS+= -L $(GBROOT)/_obj
|
||||
|
||||
# gb: compile/link against GOPATH entries
|
||||
GOPATHSEP=:
|
||||
ifeq ($(GOHOSTOS),windows)
|
||||
GOPATHSEP=;
|
||||
endif
|
||||
GCIMPORTS+=-I $(subst $(GOPATHSEP),/pkg/$(GOOS)_$(GOARCH) -I , $(GOPATH))/pkg/$(GOOS)_$(GOARCH)
|
||||
LDIMPORTS+=-L $(subst $(GOPATHSEP),/pkg/$(GOOS)_$(GOARCH) -L , $(GOPATH))/pkg/$(GOOS)_$(GOARCH)
|
||||
|
||||
# gb: copy to local install
|
||||
$(GBROOT)/_obj/$(TARG).a: _obj/$(TARG).a
|
||||
mkdir -p $(dir $@); cp -f $< $@
|
||||
|
||||
package: $(GBROOT)/_obj/$(TARG).a
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
||||
|
||||
# gb: local dependencies
|
||||
_obj/$(TARG).a: $(GBROOT)/_obj/storage.a
|
||||
_obj/$(TARG).a: $(GBROOT)/_obj/util.a
|
||||
@@ -3,10 +3,10 @@ package directory
|
||||
import (
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"storage"
|
||||
"pkg/storage"
|
||||
"strconv"
|
||||
"strings"
|
||||
"util"
|
||||
"pkg/util"
|
||||
)
|
||||
|
||||
type FileId struct {
|
||||
@@ -18,14 +18,14 @@ type FileId struct {
|
||||
func NewFileId(VolumeId uint32, Key uint64, Hashcode uint32) *FileId {
|
||||
return &FileId{VolumeId: VolumeId, Key: Key, Hashcode: Hashcode}
|
||||
}
|
||||
func ParseFileId(fid string) *FileId {
|
||||
func ParseFileId(fid string) *FileId{
|
||||
a := strings.Split(fid, ",")
|
||||
if len(a) != 2 {
|
||||
log.Println("Invalid fid", fid, ", split length", len(a))
|
||||
log.Println("Invalid fid", fid, ", split length", len(a))
|
||||
return nil
|
||||
}
|
||||
vid_string, key_hash_string := a[0], a[1]
|
||||
vid, _ := strconv.Atoui64(vid_string)
|
||||
vid, _ := strconv.ParseUint(vid_string, 10, 64)
|
||||
key, hash := storage.ParseKeyHash(key_hash_string)
|
||||
return &FileId{VolumeId: uint32(vid), Key: key, Hashcode: hash}
|
||||
}
|
||||
@@ -36,5 +36,5 @@ func (n *FileId) String() string {
|
||||
nonzero_index := 0
|
||||
for ; bytes[nonzero_index] == 0; nonzero_index++ {
|
||||
}
|
||||
return strconv.Uitoa64(uint64(n.VolumeId)) + "," + hex.EncodeToString(bytes[nonzero_index:])
|
||||
return strconv.FormatUint(uint64(n.VolumeId), 10) + "," + hex.EncodeToString(bytes[nonzero_index:])
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package directory
|
||||
|
||||
import (
|
||||
"gob"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"rand"
|
||||
"log"
|
||||
"storage"
|
||||
"pkg/storage"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
@@ -31,7 +32,7 @@ type Mapper struct {
|
||||
lock sync.Mutex
|
||||
Machines []*Machine
|
||||
vid2machineId map[uint32]int //machineId is +1 of the index of []*Machine, to detect not found entries
|
||||
Writers []uint32 // transient array of Writers volume id
|
||||
Writers []uint32 // transient array of Writers volume id
|
||||
|
||||
FileIdSequence uint64
|
||||
fileIdCounter uint64
|
||||
@@ -64,11 +65,11 @@ func NewMapper(dirname string, filename string, volumeSizeLimit uint64) (m *Mapp
|
||||
}
|
||||
return
|
||||
}
|
||||
func (m *Mapper) PickForWrite() (string, MachineInfo, os.Error) {
|
||||
func (m *Mapper) PickForWrite() (string, MachineInfo, error) {
|
||||
len_writers := len(m.Writers)
|
||||
if len_writers <= 0 {
|
||||
log.Println("No more writable volumes!")
|
||||
return "", m.Machines[rand.Intn(len(m.Machines))].Server, os.NewError("No more writable volumes!")
|
||||
return "", m.Machines[rand.Intn(len(m.Machines))].Server, errors.New("No more writable volumes!")
|
||||
}
|
||||
vid := m.Writers[rand.Intn(len_writers)]
|
||||
machine_id := m.vid2machineId[vid]
|
||||
@@ -76,7 +77,7 @@ func (m *Mapper) PickForWrite() (string, MachineInfo, os.Error) {
|
||||
machine := m.Machines[machine_id-1]
|
||||
return NewFileId(vid, m.NextFileId(), rand.Uint32()).String(), machine.Server, nil
|
||||
}
|
||||
return "", m.Machines[rand.Intn(len(m.Machines))].Server, os.NewError("Strangely vid " + strconv.Uitoa64(uint64(vid)) + " is on no machine!")
|
||||
return "", m.Machines[rand.Intn(len(m.Machines))].Server, errors.New("Strangely vid " + strconv.FormatUint(uint64(vid), 10) + " is on no machine!")
|
||||
}
|
||||
func (m *Mapper) NextFileId() uint64 {
|
||||
if m.fileIdCounter <= 0 {
|
||||
@@ -87,16 +88,16 @@ func (m *Mapper) NextFileId() uint64 {
|
||||
m.fileIdCounter--
|
||||
return m.FileIdSequence - m.fileIdCounter
|
||||
}
|
||||
func (m *Mapper) Get(vid uint32) (*Machine, os.Error) {
|
||||
func (m *Mapper) Get(vid uint32) (*Machine, error) {
|
||||
machineId := m.vid2machineId[vid]
|
||||
if machineId <= 0 {
|
||||
return nil, os.NewError("invalid volume id " + strconv.Uitob64(uint64(vid), 10))
|
||||
return nil, errors.New("invalid volume id " + strconv.FormatUint(uint64(vid), 10))
|
||||
}
|
||||
return m.Machines[machineId-1], nil
|
||||
}
|
||||
func (m *Mapper) Add(machine Machine) {
|
||||
//check existing machine, linearly
|
||||
//log.Println("Adding machine", machine.Server.Url)
|
||||
//log.Println("Adding machine", machine.Server.Url)
|
||||
m.lock.Lock()
|
||||
foundExistingMachineId := -1
|
||||
for index, entry := range m.Machines {
|
||||
|
||||
Reference in New Issue
Block a user