refactoring code

git-svn-id: https://weed-fs.googlecode.com/svn/trunk@24 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
chris.lu@gmail.com
2011-12-25 05:30:57 +00:00
parent 6813f118d4
commit ae3a53388f
7 changed files with 56 additions and 61 deletions

View File

@@ -6,6 +6,7 @@ import (
"storage"
"strconv"
"strings"
"util"
)
type FileId struct {
@@ -30,8 +31,8 @@ func ParseFileId(fid string) *FileId {
}
func (n *FileId) String() string {
bytes := make([]byte, 12)
storage.Uint64toBytes(bytes[0:8], n.Key)
storage.Uint32toBytes(bytes[8:12], n.Hashcode)
util.Uint64toBytes(bytes[0:8], n.Key)
util.Uint32toBytes(bytes[8:12], n.Hashcode)
nonzero_index := 0
for ; bytes[nonzero_index] == 0; nonzero_index++ {
}

View File

@@ -8,6 +8,7 @@ import (
"log"
"os"
"strings"
. "util"
)
type Needle struct {

View File

@@ -3,6 +3,7 @@ package storage
import (
"log"
"os"
. "util"
)
type NeedleValue struct {
@@ -50,11 +51,8 @@ func LoadNeedleMap(file *os.File) *NeedleMap {
}
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) {
nm.PutInMap(key,offset,size)
nm.m[key] = &NeedleValue{Offset: offset, Size: size}
Uint64toBytes(nm.bytes[0:8], key)
Uint32toBytes(nm.bytes[8:12], offset)
Uint32toBytes(nm.bytes[12:16], size)

View File

@@ -8,6 +8,7 @@ import (
"strings"
"strconv"
"url"
"util"
)
type Store struct {
@@ -66,7 +67,7 @@ func (s *Store) Join(mserver string) {
values.Add("volumes", string(bytes))
log.Println("Registering exiting volumes", string(bytes))
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 {
newVids := new([]int)
log.Println("Instructed to create volume", string(retString))

View File

@@ -1,11 +1,4 @@
package storage
import (
"http"
"io/ioutil"
"url"
"log"
)
package util
func BytesToUint64(b []byte)(v uint64){
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
}

View 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
}