aboutsummaryrefslogtreecommitdiff
path: root/go/operation/lookup.go
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2014-04-14 01:05:57 -0700
committerChris Lu <chris.lu@gmail.com>2014-04-14 01:05:57 -0700
commit2eb9014606b94b0266ff5501736cbd54d2bc0052 (patch)
tree8ea3307c62deb22e661c56e7f614688ee4a9921d /go/operation/lookup.go
parent56a3d30e755328e16329816b96b8d6efdc7a4b46 (diff)
downloadseaweedfs-2eb9014606b94b0266ff5501736cbd54d2bc0052.tar.xz
seaweedfs-2eb9014606b94b0266ff5501736cbd54d2bc0052.zip
rename file from lookup_volume_id.go to lookup.go
Diffstat (limited to 'go/operation/lookup.go')
-rw-r--r--go/operation/lookup.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/go/operation/lookup.go b/go/operation/lookup.go
new file mode 100644
index 000000000..7e4f5dd08
--- /dev/null
+++ b/go/operation/lookup.go
@@ -0,0 +1,71 @@
+package operation
+
+import (
+ "code.google.com/p/weed-fs/go/util"
+ "encoding/json"
+ "errors"
+ _ "fmt"
+ "math/rand"
+ "net/url"
+ "strings"
+)
+
+type Location struct {
+ Url string `json:"url,omitempty"`
+ PublicUrl string `json:"publicUrl,omitempty"`
+}
+type LookupResult struct {
+ VolumeId string `json:"volumeId,omitempty"`
+ Locations []Location `json:"locations,omitempty"`
+ Error string `json:"error,omitempty"`
+}
+
+func Lookup(server string, vid string) (*LookupResult, error) {
+ values := make(url.Values)
+ values.Add("volumeId", vid)
+ 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
+}
+
+func LookupFileId(server string, fileId string) (fullUrl string, err error) {
+ parts := strings.Split(fileId, ",")
+ if len(parts) != 2 {
+ return "", errors.New("Invalid fileId " + fileId)
+ }
+ lookup, lookupError := Lookup(server, parts[0])
+ if lookupError != nil {
+ return "", lookupError
+ }
+ if len(lookup.Locations) == 0 {
+ return "", errors.New("File Not Found")
+ }
+ 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
+}