refactor lookup result types into package "operation"

This commit is contained in:
Chris Lu 2014-04-13 23:56:15 -07:00
parent f20ef922fd
commit 5878f7c3a1
2 changed files with 31 additions and 22 deletions

View file

@ -11,12 +11,13 @@ import (
) )
type Location struct { type Location struct {
Url string `json:"url"` Url string `json:"url,omitempty"`
PublicUrl string `json:"publicUrl"` PublicUrl string `json:"publicUrl,omitempty"`
} }
type LookupResult struct { type LookupResult struct {
Locations []Location `json:"locations"` VolumeId string `json:"volumeId,omitempty"`
Error string `json:"error"` Locations []Location `json:"locations,omitempty"`
Error string `json:"error,omitempty"`
} }
func Lookup(server string, vid string) (*LookupResult, error) { func Lookup(server string, vid string) (*LookupResult, error) {
@ -51,3 +52,20 @@ func LookupFileId(server string, fileId string) (fullUrl string, err error) {
} }
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil
} }
func LookupVolumeIds(server string, vids []string) ([]LookupResult, error) {
values := make(url.Values)
for _, vid := range vids {
values.Add("volumeId", vid)
}
jsonBlob, err := util.Post("http://"+server+"/vol/lookup", values)
if err != nil {
return nil, err
}
var ret []LookupResult
err = json.Unmarshal(jsonBlob, &ret)
if err != nil {
return nil, err
}
return ret, nil
}

View file

@ -1,6 +1,7 @@
package weed_server package weed_server
import ( import (
"code.google.com/p/weed-fs/go/operation"
"code.google.com/p/weed-fs/go/stats" "code.google.com/p/weed-fs/go/stats"
"code.google.com/p/weed-fs/go/storage" "code.google.com/p/weed-fs/go/storage"
"code.google.com/p/weed-fs/go/topology" "code.google.com/p/weed-fs/go/topology"
@ -12,18 +13,8 @@ import (
"strings" "strings"
) )
type LookupResultLocation struct { func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]operation.LookupResult) {
Url string `json:"url,omitempty"` volumeLocations = make(map[string]operation.LookupResult)
PublicUrl string `json:"publicUrl,omitempty"`
}
type LookupResult struct {
VolumeId string `json:"volumeId,omitempty"`
Locations []LookupResultLocation `json:"locations,omitempty"`
Error string `json:"error,omitempty"`
}
func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volumeLocations map[string]LookupResult) {
volumeLocations = make(map[string]LookupResult)
for _, vid := range vids { for _, vid := range vids {
commaSep := strings.Index(vid, ",") commaSep := strings.Index(vid, ",")
if commaSep > 0 { if commaSep > 0 {
@ -36,16 +27,16 @@ func (ms *MasterServer) lookupVolumeId(vids []string, collection string) (volume
if err == nil { if err == nil {
machines := ms.Topo.Lookup(collection, volumeId) machines := ms.Topo.Lookup(collection, volumeId)
if machines != nil { if machines != nil {
var ret []LookupResultLocation var ret []operation.Location
for _, dn := range machines { for _, dn := range machines {
ret = append(ret, LookupResultLocation{Url: dn.Url(), PublicUrl: dn.PublicUrl}) ret = append(ret, operation.Location{Url: dn.Url(), PublicUrl: dn.PublicUrl})
} }
volumeLocations[vid] = LookupResult{VolumeId: vid, Locations: ret} volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Locations: ret}
} else { } else {
volumeLocations[vid] = LookupResult{VolumeId: vid, Error: "volumeId not found."} volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: "volumeId not found."}
} }
} else { } else {
volumeLocations[vid] = LookupResult{VolumeId: vid, Error: "Unknown volumeId format."} volumeLocations[vid] = operation.LookupResult{VolumeId: vid, Error: "Unknown volumeId format."}
} }
} }
return return
@ -74,7 +65,7 @@ func (ms *MasterServer) volumeLookupHandler(w http.ResponseWriter, r *http.Reque
vids := r.Form["volumeId"] vids := r.Form["volumeId"]
collection := r.FormValue("collection") //optional, but can be faster if too many collections collection := r.FormValue("collection") //optional, but can be faster if too many collections
volumeLocations := ms.lookupVolumeId(vids, collection) volumeLocations := ms.lookupVolumeId(vids, collection)
var ret []LookupResult var ret []operation.LookupResult
for _, volumeLocation := range volumeLocations { for _, volumeLocation := range volumeLocations {
ret = append(ret, volumeLocation) ret = append(ret, volumeLocation)
} }