2012-09-20 17:47:32 +08:00
|
|
|
package operation
|
|
|
|
|
|
|
|
import (
|
2014-09-25 00:47:09 +08:00
|
|
|
"github.com/aszxqw/weed-fs/go/util"
|
2013-01-17 16:56:56 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
_ "fmt"
|
2014-03-31 02:28:04 +08:00
|
|
|
"math/rand"
|
2013-01-17 16:56:56 +08:00
|
|
|
"net/url"
|
2014-02-15 09:10:49 +08:00
|
|
|
"strings"
|
2014-05-26 05:01:54 +08:00
|
|
|
"time"
|
2012-09-20 17:47:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Location struct {
|
2014-04-14 14:56:15 +08:00
|
|
|
Url string `json:"url,omitempty"`
|
|
|
|
PublicUrl string `json:"publicUrl,omitempty"`
|
2012-09-20 17:47:32 +08:00
|
|
|
}
|
|
|
|
type LookupResult struct {
|
2014-04-14 14:56:15 +08:00
|
|
|
VolumeId string `json:"volumeId,omitempty"`
|
|
|
|
Locations []Location `json:"locations,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
2012-09-20 17:47:32 +08:00
|
|
|
}
|
|
|
|
|
2014-05-26 05:01:54 +08:00
|
|
|
var (
|
|
|
|
vc VidCache
|
|
|
|
)
|
|
|
|
|
|
|
|
func Lookup(server string, vid string) (ret *LookupResult, err error) {
|
|
|
|
locations, cache_err := vc.Get(vid)
|
|
|
|
if cache_err != nil {
|
2014-06-03 00:22:23 +08:00
|
|
|
if ret, err = do_lookup(server, vid); err == nil {
|
|
|
|
vc.Set(vid, ret.Locations, 1*time.Minute)
|
|
|
|
}
|
2014-05-26 05:01:54 +08:00
|
|
|
} else {
|
|
|
|
ret = &LookupResult{VolumeId: vid, Locations: locations}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func do_lookup(server string, vid string) (*LookupResult, error) {
|
2013-01-17 16:56:56 +08:00
|
|
|
values := make(url.Values)
|
2014-02-15 09:10:49 +08:00
|
|
|
values.Add("volumeId", vid)
|
2013-01-17 16:56:56 +08:00
|
|
|
jsonBlob, err := util.Post("http://"+server+"/dir/lookup", values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ret LookupResult
|
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ret.Error != "" {
|
|
|
|
return nil, errors.New(ret.Error)
|
|
|
|
}
|
|
|
|
return &ret, nil
|
2012-09-20 17:47:32 +08:00
|
|
|
}
|
2013-11-19 15:03:59 +08:00
|
|
|
|
|
|
|
func LookupFileId(server string, fileId string) (fullUrl string, err error) {
|
2014-03-31 02:28:04 +08:00
|
|
|
parts := strings.Split(fileId, ",")
|
|
|
|
if len(parts) != 2 {
|
2014-02-15 09:10:49 +08:00
|
|
|
return "", errors.New("Invalid fileId " + fileId)
|
2013-11-19 15:03:59 +08:00
|
|
|
}
|
2014-03-31 02:28:04 +08:00
|
|
|
lookup, lookupError := Lookup(server, parts[0])
|
2013-11-19 15:03:59 +08:00
|
|
|
if lookupError != nil {
|
|
|
|
return "", lookupError
|
|
|
|
}
|
|
|
|
if len(lookup.Locations) == 0 {
|
|
|
|
return "", errors.New("File Not Found")
|
|
|
|
}
|
2014-03-31 02:28:04 +08:00
|
|
|
return "http://" + lookup.Locations[rand.Intn(len(lookup.Locations))].PublicUrl + "/" + fileId, nil
|
2013-11-19 15:03:59 +08:00
|
|
|
}
|
2014-04-14 14:56:15 +08:00
|
|
|
|
2014-04-16 00:09:40 +08:00
|
|
|
func LookupVolumeIds(server string, vids []string) (map[string]LookupResult, error) {
|
2014-04-14 14:56:15 +08:00
|
|
|
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
|
|
|
|
}
|
2014-04-16 00:09:40 +08:00
|
|
|
ret := make(map[string]LookupResult)
|
2014-04-14 14:56:15 +08:00
|
|
|
err = json.Unmarshal(jsonBlob, &ret)
|
|
|
|
if err != nil {
|
2014-04-16 00:09:40 +08:00
|
|
|
return nil, errors.New(err.Error() + " " + string(jsonBlob))
|
2014-04-14 14:56:15 +08:00
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|