git-svn-id: https://weed-fs.googlecode.com/svn/trunk@4 282b0af5-e82d-9cf1-ede4-77906d7719d0
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"directory"
|
||||
// "runtime"
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := directory.NewMapper("/tmp", "directory")
|
||||
log.Println("map size", len(m.Virtual2physical))
|
||||
m.Add(10, 11,12,13)
|
||||
m.Add(20, 21,22,23)
|
||||
log.Println("map(10)", m.Get(10))
|
||||
log.Println("map size", len(m.Virtual2physical))
|
||||
m.Save()
|
||||
defer m.Save()
|
||||
}
|
||||
@@ -6,9 +6,11 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"http"
|
||||
"json"
|
||||
"log"
|
||||
"mime"
|
||||
"os"
|
||||
"rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -18,6 +20,10 @@ var (
|
||||
chunkFolder = flag.String("dir", "/tmp", "data directory to store files")
|
||||
chunkCount = flag.Int("chunks", 5, "data chunks to store files")
|
||||
chunkEnabled = flag.Bool("data", false, "act as a store server")
|
||||
chunkServer = flag.String("cserver", "localhost:8080", "chunk server to store data")
|
||||
publicServer = flag.String("pserver", "localhost:8080", "public server to serve data read")
|
||||
metaServer = flag.String("mserver", "localhost:8080", "metadata server to store mappings")
|
||||
|
||||
metaEnabled = flag.Bool("meta", false, "act as a directory server")
|
||||
metaFolder = flag.String("mdir", "/tmp", "data directory to store mappings")
|
||||
)
|
||||
@@ -41,7 +47,7 @@ func (s *Haystack) GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
n := new(store.Needle)
|
||||
path := r.URL.Path
|
||||
sepIndex := strings.Index(path[1:], "/") + 1
|
||||
volumeId, _ := strconv.Atoi(path[1:sepIndex])
|
||||
volumeId, _ := strconv.Atoui64(path[1:sepIndex])
|
||||
dotIndex := strings.LastIndex(path, ".")
|
||||
n.ParsePath(path[sepIndex+1 : dotIndex])
|
||||
ext := path[dotIndex:]
|
||||
@@ -51,14 +57,47 @@ func (s *Haystack) GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(n.Data)
|
||||
}
|
||||
func (s *Haystack) PostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
volumeId := s.store.Write(store.NewNeedle(r))
|
||||
volumeId, _ := strconv.Atoui64(r.FormValue("volumeId"))
|
||||
s.store.Write(volumeId, store.NewNeedle(r))
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprint(w, "volumeId=", volumeId, "\n")
|
||||
}
|
||||
func (s *Haystack) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
func directoryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func dirReadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
volumeId, _ := strconv.Atoui64(r.FormValue("volumeId"))
|
||||
machineList := server.directory.Get((uint32)(volumeId))
|
||||
x := rand.Intn(len(machineList))
|
||||
machine := machineList[x]
|
||||
bytes, _ := json.Marshal(machine)
|
||||
callback := r.FormValue("callback")
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
if callback == "" {
|
||||
w.Write(bytes)
|
||||
} else {
|
||||
w.Write([]uint8(callback))
|
||||
w.Write([]uint8("("))
|
||||
w.Write(bytes)
|
||||
w.Write([]uint8(")"))
|
||||
}
|
||||
}
|
||||
func dirWriteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
machineList := server.directory.PickForWrite()
|
||||
bytes, _ := json.Marshal(machineList)
|
||||
callback := r.FormValue("callback")
|
||||
w.Header().Set("Content-Type", "application/javascript")
|
||||
if callback == "" {
|
||||
w.Write(bytes)
|
||||
} else {
|
||||
w.Write([]uint8(callback))
|
||||
w.Write([]uint8("("))
|
||||
w.Write(bytes)
|
||||
w.Write([]uint8(")"))
|
||||
}
|
||||
}
|
||||
func dirJoinHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
|
||||
var server *Haystack
|
||||
@@ -66,22 +105,24 @@ var server *Haystack
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if !*chunkEnabled && !*metaEnabled {
|
||||
fmt.Fprintf(os.Stderr, "Need to act as either a store server or a directory server, or both\n")
|
||||
flag.PrintDefaults()
|
||||
os.Exit(-1)
|
||||
fmt.Fprintf(os.Stdout, "Act as both a store server and a directory server\n")
|
||||
}
|
||||
server = new(Haystack)
|
||||
if *chunkEnabled {
|
||||
fmt.Fprintf(os.Stdout, "Chunk data stored in %s\n", *chunkFolder)
|
||||
server.store = store.NewStore(*chunkFolder, *chunkCount)
|
||||
server.store = store.NewStore(*chunkServer, *publicServer, *chunkFolder)
|
||||
defer server.store.Close()
|
||||
http.HandleFunc("/", storeHandler)
|
||||
}
|
||||
if *metaEnabled {
|
||||
server.directory = directory.NewMapper(*metaFolder, "directory")
|
||||
defer server.directory.Save()
|
||||
http.HandleFunc("/directory", directoryHandler)
|
||||
http.HandleFunc("/dir/read", dirReadHandler)
|
||||
http.HandleFunc("/dir/write", dirWriteHandler)
|
||||
http.HandleFunc("/dir/join", dirJoinHandler)
|
||||
}
|
||||
|
||||
server.store.Join(*metaServer)
|
||||
|
||||
log.Println("Serving at http://127.0.0.1:" + strconv.Itoa(*port))
|
||||
http.ListenAndServe(":"+strconv.Itoa(*port), nil)
|
||||
|
||||
Reference in New Issue
Block a user