mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactoring code
git-svn-id: https://weed-fs.googlecode.com/svn/trunk@24 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
parent
6813f118d4
commit
ae3a53388f
|
@ -38,19 +38,8 @@ func storeHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
func GetHandler(w http.ResponseWriter, r *http.Request) {
|
func GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
n := new(storage.Needle)
|
n := new(storage.Needle)
|
||||||
path := r.URL.Path
|
vid, fid, ext := parseURLPath(r.URL.Path)
|
||||||
sepIndex := strings.LastIndex(path, "/")
|
volumeId, _ := strconv.Atoui64(vid)
|
||||||
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
|
||||||
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
|
||||||
fid := path[commaIndex+1:]
|
|
||||||
if dotIndex > 0 {
|
|
||||||
fid = path[commaIndex+1 : dotIndex]
|
|
||||||
}
|
|
||||||
if commaIndex <= 0 {
|
|
||||||
log.Println("unknown file id", path[sepIndex+1:commaIndex])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
volumeId, _ := strconv.Atoui64(path[sepIndex+1 : commaIndex])
|
|
||||||
n.ParsePath(fid)
|
n.ParsePath(fid)
|
||||||
|
|
||||||
if *IsDebug {
|
if *IsDebug {
|
||||||
|
@ -65,17 +54,14 @@ func GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("request with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
|
log.Println("request with unmaching cookie from ", r.RemoteAddr, "agent", r.UserAgent())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if dotIndex > 0 {
|
if ext!="" {
|
||||||
ext := path[dotIndex:]
|
|
||||||
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
|
||||||
}
|
}
|
||||||
w.Write(n.Data)
|
w.Write(n.Data)
|
||||||
}
|
}
|
||||||
func PostHandler(w http.ResponseWriter, r *http.Request) {
|
func PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
path := r.URL.Path
|
vid, _, _ := parseURLPath(r.URL.Path)
|
||||||
commaIndex := strings.LastIndex(path, ",")
|
volumeId, e := strconv.Atoui64(vid)
|
||||||
sepIndex := strings.LastIndex(path[:commaIndex], "/")
|
|
||||||
volumeId, e := strconv.Atoui64(path[sepIndex+1 : commaIndex])
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
writeJson(w, r, e)
|
writeJson(w, r, e)
|
||||||
} else {
|
} else {
|
||||||
|
@ -87,19 +73,8 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
n := new(storage.Needle)
|
n := new(storage.Needle)
|
||||||
path := r.URL.Path
|
vid, fid, _ := parseURLPath(r.URL.Path)
|
||||||
sepIndex := strings.LastIndex(path, "/")
|
volumeId, _ := strconv.Atoui64(vid)
|
||||||
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
|
||||||
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
|
||||||
fid := path[commaIndex+1:]
|
|
||||||
if dotIndex > 0 {
|
|
||||||
fid = path[commaIndex+1 : dotIndex]
|
|
||||||
}
|
|
||||||
if commaIndex <= 0 {
|
|
||||||
log.Println("unknown file id", path[sepIndex+1:commaIndex])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
volumeId, _ := strconv.Atoui64(path[sepIndex+1 : commaIndex])
|
|
||||||
n.ParsePath(fid)
|
n.ParsePath(fid)
|
||||||
|
|
||||||
cookie := n.Cookie
|
cookie := n.Cookie
|
||||||
|
@ -130,6 +105,23 @@ func writeJson(w http.ResponseWriter, r *http.Request, obj interface{}) {
|
||||||
}
|
}
|
||||||
//log.Println("JSON Response", string(bytes))
|
//log.Println("JSON Response", string(bytes))
|
||||||
}
|
}
|
||||||
|
func parseURLPath(path string)(vid, fid, ext string){
|
||||||
|
sepIndex := strings.LastIndex(path, "/")
|
||||||
|
commaIndex := strings.LastIndex(path[sepIndex:], ",")
|
||||||
|
dotIndex := strings.LastIndex(path[sepIndex:], ".")
|
||||||
|
vid = path[sepIndex+1 : commaIndex]
|
||||||
|
fid = path[commaIndex+1:]
|
||||||
|
ext = ""
|
||||||
|
if dotIndex > 0 {
|
||||||
|
fid = path[commaIndex+1 : dotIndex]
|
||||||
|
ext = path[dotIndex+1:]
|
||||||
|
}
|
||||||
|
if commaIndex <= 0 {
|
||||||
|
log.Println("unknown file id", path[sepIndex+1:commaIndex])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"storage"
|
"storage"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileId struct {
|
type FileId struct {
|
||||||
|
@ -30,8 +31,8 @@ func ParseFileId(fid string) *FileId {
|
||||||
}
|
}
|
||||||
func (n *FileId) String() string {
|
func (n *FileId) String() string {
|
||||||
bytes := make([]byte, 12)
|
bytes := make([]byte, 12)
|
||||||
storage.Uint64toBytes(bytes[0:8], n.Key)
|
util.Uint64toBytes(bytes[0:8], n.Key)
|
||||||
storage.Uint32toBytes(bytes[8:12], n.Hashcode)
|
util.Uint32toBytes(bytes[8:12], n.Hashcode)
|
||||||
nonzero_index := 0
|
nonzero_index := 0
|
||||||
for ; bytes[nonzero_index] == 0; nonzero_index++ {
|
for ; bytes[nonzero_index] == 0; nonzero_index++ {
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
. "util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Needle struct {
|
type Needle struct {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package storage
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
. "util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NeedleValue struct {
|
type NeedleValue struct {
|
||||||
|
@ -50,11 +51,8 @@ func LoadNeedleMap(file *os.File) *NeedleMap {
|
||||||
}
|
}
|
||||||
return nm
|
return nm
|
||||||
}
|
}
|
||||||
func (nm *NeedleMap) PutInMap(key uint64, offset uint32, size uint32) {
|
|
||||||
nm.m[key] = &NeedleValue{Offset: offset, Size: size}
|
|
||||||
}
|
|
||||||
func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, os.Error) {
|
func (nm *NeedleMap) Put(key uint64, offset uint32, size uint32) (int, os.Error) {
|
||||||
nm.PutInMap(key,offset,size)
|
nm.m[key] = &NeedleValue{Offset: offset, Size: size}
|
||||||
Uint64toBytes(nm.bytes[0:8], key)
|
Uint64toBytes(nm.bytes[0:8], key)
|
||||||
Uint32toBytes(nm.bytes[8:12], offset)
|
Uint32toBytes(nm.bytes[8:12], offset)
|
||||||
Uint32toBytes(nm.bytes[12:16], size)
|
Uint32toBytes(nm.bytes[12:16], size)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"strconv"
|
"strconv"
|
||||||
"url"
|
"url"
|
||||||
|
"util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
|
@ -66,7 +67,7 @@ func (s *Store) Join(mserver string) {
|
||||||
values.Add("volumes", string(bytes))
|
values.Add("volumes", string(bytes))
|
||||||
log.Println("Registering exiting volumes", string(bytes))
|
log.Println("Registering exiting volumes", string(bytes))
|
||||||
values.Add("capacity", strconv.Itoa(s.capacity))
|
values.Add("capacity", strconv.Itoa(s.capacity))
|
||||||
retString := post("http://"+mserver+"/dir/join", values)
|
retString := util.Post("http://"+mserver+"/dir/join", values)
|
||||||
if retString != nil {
|
if retString != nil {
|
||||||
newVids := new([]int)
|
newVids := new([]int)
|
||||||
log.Println("Instructed to create volume", string(retString))
|
log.Println("Instructed to create volume", string(retString))
|
||||||
|
|
|
@ -1,11 +1,4 @@
|
||||||
package storage
|
package util
|
||||||
|
|
||||||
import (
|
|
||||||
"http"
|
|
||||||
"io/ioutil"
|
|
||||||
"url"
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func BytesToUint64(b []byte)(v uint64){
|
func BytesToUint64(b []byte)(v uint64){
|
||||||
length := uint(len(b))
|
length := uint(len(b))
|
||||||
|
@ -36,17 +29,3 @@ func Uint32toBytes(b []byte, v uint32){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func post(url string, values url.Values)[]byte{
|
|
||||||
r, err := http.PostForm(url, values)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("post:", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
b, err := ioutil.ReadAll(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("post:", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
23
weed-fs/src/pkg/util/post.go
Normal file
23
weed-fs/src/pkg/util/post.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"http"
|
||||||
|
"io/ioutil"
|
||||||
|
"url"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Post(url string, values url.Values)[]byte{
|
||||||
|
r, err := http.PostForm(url, values)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("post:", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
b, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("post:", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
Loading…
Reference in a new issue