PageRenderTime 363ms CodeModel.GetById 120ms app.highlight 156ms RepoModel.GetById 79ms app.codeStats 1ms

/weed-fs/src/pkg/storage/store.go

http://weed-fs.googlecode.com/
Go | 102 lines | 97 code | 5 blank | 0 comment | 15 complexity | 59649af72491ed21c86d4da2df95be61 MD5 | raw file
  1package storage
  2
  3import (
  4	"encoding/json"
  5	"errors"
  6	"log"
  7	"net/url"
  8	"strconv"
  9	"strings"
 10	"pkg/util"
 11)
 12
 13type Store struct {
 14	volumes   map[uint64]*Volume
 15	dir       string
 16	Port      int
 17	PublicUrl string
 18}
 19type VolumeInfo struct {
 20	Id   uint32
 21	Size int64
 22}
 23
 24func NewStore(port int, publicUrl, dirname string, volumeListString string) (s *Store) {
 25	s = &Store{Port: port, PublicUrl: publicUrl, dir: dirname}
 26	s.volumes = make(map[uint64]*Volume)
 27
 28	s.AddVolume(volumeListString)
 29
 30	log.Println("Store started on dir:", dirname, "with", len(s.volumes), "volumes")
 31	return
 32}
 33func (s *Store) AddVolume(volumeListString string) error {
 34	for _, range_string := range strings.Split(volumeListString, ",") {
 35		if strings.Index(range_string, "-") < 0 {
 36			id_string := range_string
 37			id, err := strconv.ParseUint(id_string, 10, 64)
 38			if err != nil {
 39				return errors.New("Volume Id " + id_string + " is not a valid unsigned integer!")
 40			}
 41			s.addVolume(id)
 42		} else {
 43			pair := strings.Split(range_string, "-")
 44			start, start_err := strconv.ParseUint(pair[0], 10, 64)
 45			if start_err != nil {
 46				return errors.New("Volume Start Id" + pair[0] + " is not a valid unsigned integer!")
 47			}
 48			end, end_err := strconv.ParseUint(pair[1], 10, 64)
 49			if end_err != nil {
 50				return errors.New("Volume End Id" + pair[1] + " is not a valid unsigned integer!")
 51			}
 52			for id := start; id <= end; id++ {
 53				s.addVolume(id)
 54			}
 55		}
 56	}
 57	return nil
 58}
 59func (s *Store) addVolume(vid uint64) error {
 60	if s.volumes[vid] != nil {
 61		return errors.New("Volume Id " + strconv.FormatUint(vid, 10) + " already exists!")
 62	}
 63	s.volumes[vid] = NewVolume(s.dir, uint32(vid))
 64	return nil
 65}
 66func (s *Store) Status() *[]*VolumeInfo {
 67	stats := new([]*VolumeInfo)
 68	for k, v := range s.volumes {
 69		s := new(VolumeInfo)
 70		s.Id, s.Size = uint32(k), v.Size()
 71		*stats = append(*stats, s)
 72	}
 73	return stats
 74}
 75func (s *Store) Join(mserver string) {
 76	stats := new([]*VolumeInfo)
 77	for k, v := range s.volumes {
 78		s := new(VolumeInfo)
 79		s.Id, s.Size = uint32(k), v.Size()
 80		*stats = append(*stats, s)
 81	}
 82	bytes, _ := json.Marshal(stats)
 83	values := make(url.Values)
 84	values.Add("port", strconv.Itoa(s.Port))
 85	values.Add("publicUrl", s.PublicUrl)
 86	values.Add("volumes", string(bytes))
 87	util.Post("http://"+mserver+"/dir/join", values)
 88}
 89func (s *Store) Close() {
 90	for _, v := range s.volumes {
 91		v.Close()
 92	}
 93}
 94func (s *Store) Write(i uint64, n *Needle) uint32 {
 95	return s.volumes[i].write(n)
 96}
 97func (s *Store) Delete(i uint64, n *Needle) uint32 {
 98	return s.volumes[i].delete(n)
 99}
100func (s *Store) Read(i uint64, n *Needle) (int, error) {
101	return s.volumes[i].read(n)
102}