aboutsummaryrefslogtreecommitdiff
path: root/weed/wdclient/vid_map.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/wdclient/vid_map.go')
-rw-r--r--weed/wdclient/vid_map.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/weed/wdclient/vid_map.go b/weed/wdclient/vid_map.go
index 0eb5ee5d0..93884c53d 100644
--- a/weed/wdclient/vid_map.go
+++ b/weed/wdclient/vid_map.go
@@ -2,6 +2,12 @@ package wdclient
import (
"sync"
+ "strings"
+ "math/rand"
+ "errors"
+ "strconv"
+ "github.com/chrislusf/seaweedfs/weed/glog"
+ "fmt"
)
type Location struct {
@@ -14,6 +20,33 @@ type VidMap struct {
vid2Locations map[uint32][]Location
}
+func (vc *VidMap) LookupVolumeServerUrl(vid string) (serverUrl string, err error) {
+ id, err := strconv.Atoi(vid)
+ if err != nil {
+ glog.V(1).Infof("Unknown volume id %s", vid)
+ return "", err
+ }
+
+ locations := vc.GetLocations(uint32(id))
+ if len(locations) == 0 {
+ return "", fmt.Errorf("volume %d not found", id)
+ }
+
+ return locations[rand.Intn(len(locations))].Url, nil
+}
+
+func (vc *VidMap) LookupFileId(fileId string) (fullUrl string, err error) {
+ parts := strings.Split(fileId, ",")
+ if len(parts) != 2 {
+ return "", errors.New("Invalid fileId " + fileId)
+ }
+ serverUrl, lookupError := LookupVolumeServerUrl(parts[0])
+ if lookupError != nil {
+ return "", lookupError
+ }
+ return "http://" + serverUrl + "/" + fileId, nil
+}
+
func (vc *VidMap) GetLocations(vid uint32) (locations []Location) {
vc.RLock()
defer vc.RUnlock()