2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"io/ioutil"
|
|
|
|
"json"
|
2011-12-24 08:40:56 +00:00
|
|
|
"os"
|
2011-12-16 14:51:26 +00:00
|
|
|
"strings"
|
|
|
|
"strconv"
|
|
|
|
"url"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Store struct {
|
|
|
|
volumes map[uint64]*Volume
|
2011-12-17 06:47:23 +00:00
|
|
|
capacity int
|
2011-12-16 14:51:26 +00:00
|
|
|
dir string
|
2011-12-17 06:47:23 +00:00
|
|
|
Port int
|
2011-12-16 14:51:26 +00:00
|
|
|
PublicServer string
|
|
|
|
}
|
2011-12-18 07:22:04 +00:00
|
|
|
type VolumeInfo struct {
|
2011-12-20 09:00:01 +00:00
|
|
|
Id uint32
|
2011-12-18 07:22:04 +00:00
|
|
|
Size int64
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2011-12-17 06:47:23 +00:00
|
|
|
func NewStore(port int, publicServer, dirname string, chunkSize, capacity int) (s *Store) {
|
2011-12-19 05:59:37 +00:00
|
|
|
s = &Store{Port: port, PublicServer: publicServer, dir: dirname, capacity: capacity}
|
2011-12-16 14:51:26 +00:00
|
|
|
s.volumes = make(map[uint64]*Volume)
|
|
|
|
|
|
|
|
files, _ := ioutil.ReadDir(dirname)
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDirectory() || !strings.HasSuffix(f.Name, ".dat") {
|
|
|
|
continue
|
|
|
|
}
|
2011-12-17 06:47:23 +00:00
|
|
|
id, err := strconv.Atoui64(f.Name[0:(strings.LastIndex(f.Name, ".dat"))])
|
|
|
|
log.Println("Loading data file name:", f.Name)
|
|
|
|
if err != nil {
|
2011-12-16 14:51:26 +00:00
|
|
|
continue
|
|
|
|
}
|
2011-12-20 09:00:01 +00:00
|
|
|
s.volumes[id] = NewVolume(s.dir, uint32(id))
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-17 06:47:23 +00:00
|
|
|
log.Println("Store started on dir:", dirname, "with", len(s.volumes), "existing volumes")
|
|
|
|
log.Println("Expected capacity=", s.capacity, "volumes")
|
2011-12-16 14:51:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-22 04:04:47 +00:00
|
|
|
func (s *Store) Status()(*[]*VolumeInfo){
|
|
|
|
stats := new([]*VolumeInfo)
|
|
|
|
for k, v := range s.volumes {
|
|
|
|
s := new(VolumeInfo)
|
|
|
|
s.Id, s.Size = uint32(k), v.Size()
|
|
|
|
*stats = append(*stats, s)
|
|
|
|
}
|
|
|
|
return stats
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
func (s *Store) Join(mserver string) {
|
2011-12-18 07:22:04 +00:00
|
|
|
stats := new([]*VolumeInfo)
|
|
|
|
for k, v := range s.volumes {
|
|
|
|
s := new(VolumeInfo)
|
2011-12-20 09:00:01 +00:00
|
|
|
s.Id, s.Size = uint32(k), v.Size()
|
2011-12-17 06:47:23 +00:00
|
|
|
*stats = append(*stats, s)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
bytes, _ := json.Marshal(stats)
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Add("port", strconv.Itoa(s.Port))
|
2011-12-17 06:47:23 +00:00
|
|
|
values.Add("publicServer", s.PublicServer)
|
2011-12-16 14:51:26 +00:00
|
|
|
values.Add("volumes", string(bytes))
|
2011-12-17 06:47:23 +00:00
|
|
|
log.Println("Registering exiting volumes", string(bytes))
|
|
|
|
values.Add("capacity", strconv.Itoa(s.capacity))
|
|
|
|
retString := post("http://"+mserver+"/dir/join", values)
|
|
|
|
if retString != nil {
|
|
|
|
newVids := new([]int)
|
2011-12-19 05:59:37 +00:00
|
|
|
log.Println("Instructed to create volume", string(retString))
|
2011-12-17 06:47:23 +00:00
|
|
|
e := json.Unmarshal(retString, newVids)
|
|
|
|
if e == nil {
|
|
|
|
for _, vid := range *newVids {
|
2011-12-20 09:00:01 +00:00
|
|
|
s.volumes[uint64(vid)] = NewVolume(s.dir, uint32(vid))
|
2011-12-17 06:47:23 +00:00
|
|
|
log.Println("Adding volume", vid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
func (s *Store) Close() {
|
|
|
|
for _, v := range s.volumes {
|
|
|
|
v.Close()
|
|
|
|
}
|
|
|
|
}
|
2011-12-24 01:25:22 +00:00
|
|
|
func (s *Store) Write(i uint64, n *Needle) (uint32){
|
|
|
|
return s.volumes[i].write(n)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-24 08:40:56 +00:00
|
|
|
func (s *Store) Read(i uint64, n *Needle) (int, os.Error){
|
|
|
|
return s.volumes[i].read(n)
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|