|
@ -5,9 +5,9 @@ import ( |
|
|
"errors" |
|
|
"errors" |
|
|
"log" |
|
|
"log" |
|
|
"net/url" |
|
|
"net/url" |
|
|
|
|
|
"pkg/util" |
|
|
"strconv" |
|
|
"strconv" |
|
|
"strings" |
|
|
"strings" |
|
|
"pkg/util" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type Store struct { |
|
|
type Store struct { |
|
@ -22,12 +22,12 @@ func NewStore(port int, publicUrl, dirname string, volumeListString string) (s * |
|
|
s = &Store{Port: port, PublicUrl: publicUrl, dir: dirname} |
|
|
s = &Store{Port: port, PublicUrl: publicUrl, dir: dirname} |
|
|
s.volumes = make(map[VolumeId]*Volume) |
|
|
s.volumes = make(map[VolumeId]*Volume) |
|
|
|
|
|
|
|
|
s.AddVolume(volumeListString) |
|
|
|
|
|
|
|
|
s.AddVolume(volumeListString, "00") |
|
|
|
|
|
|
|
|
log.Println("Store started on dir:", dirname, "with", len(s.volumes), "volumes") |
|
|
log.Println("Store started on dir:", dirname, "with", len(s.volumes), "volumes") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
func (s *Store) AddVolume(volumeListString string) error { |
|
|
|
|
|
|
|
|
func (s *Store) AddVolume(volumeListString string, replicationType string) error { |
|
|
for _, range_string := range strings.Split(volumeListString, ",") { |
|
|
for _, range_string := range strings.Split(volumeListString, ",") { |
|
|
if strings.Index(range_string, "-") < 0 { |
|
|
if strings.Index(range_string, "-") < 0 { |
|
|
id_string := range_string |
|
|
id_string := range_string |
|
@ -35,7 +35,7 @@ func (s *Store) AddVolume(volumeListString string) error { |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return errors.New("Volume Id " + id_string + " is not a valid unsigned integer!") |
|
|
return errors.New("Volume Id " + id_string + " is not a valid unsigned integer!") |
|
|
} |
|
|
} |
|
|
s.addVolume(VolumeId(id)) |
|
|
|
|
|
|
|
|
s.addVolume(VolumeId(id), NewReplicationType(replicationType)) |
|
|
} else { |
|
|
} else { |
|
|
pair := strings.Split(range_string, "-") |
|
|
pair := strings.Split(range_string, "-") |
|
|
start, start_err := strconv.ParseUint(pair[0], 10, 64) |
|
|
start, start_err := strconv.ParseUint(pair[0], 10, 64) |
|
@ -47,24 +47,24 @@ func (s *Store) AddVolume(volumeListString string) error { |
|
|
return errors.New("Volume End Id" + pair[1] + " is not a valid unsigned integer!") |
|
|
return errors.New("Volume End Id" + pair[1] + " is not a valid unsigned integer!") |
|
|
} |
|
|
} |
|
|
for id := start; id <= end; id++ { |
|
|
for id := start; id <= end; id++ { |
|
|
s.addVolume(VolumeId(id)) |
|
|
|
|
|
|
|
|
s.addVolume(VolumeId(id), NewReplicationType(replicationType)) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
func (s *Store) addVolume(vid VolumeId) error { |
|
|
|
|
|
|
|
|
func (s *Store) addVolume(vid VolumeId, replicationType ReplicationType) error { |
|
|
if s.volumes[vid] != nil { |
|
|
if s.volumes[vid] != nil { |
|
|
return errors.New("Volume Id " + vid.String() + " already exists!") |
|
|
return errors.New("Volume Id " + vid.String() + " already exists!") |
|
|
} |
|
|
} |
|
|
s.volumes[vid] = NewVolume(s.dir, vid) |
|
|
|
|
|
|
|
|
s.volumes[vid] = NewVolume(s.dir, vid, replicationType) |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
func (s *Store) Status() *[]*VolumeInfo { |
|
|
func (s *Store) Status() *[]*VolumeInfo { |
|
|
stats := new([]*VolumeInfo) |
|
|
stats := new([]*VolumeInfo) |
|
|
for k, v := range s.volumes { |
|
|
for k, v := range s.volumes { |
|
|
s := new(VolumeInfo) |
|
|
s := new(VolumeInfo) |
|
|
s.Id, s.Size = VolumeId(k), v.Size() |
|
|
|
|
|
|
|
|
s.Id, s.Size, s.RepType = VolumeId(k), v.Size(), v.replicaType |
|
|
*stats = append(*stats, s) |
|
|
*stats = append(*stats, s) |
|
|
} |
|
|
} |
|
|
return stats |
|
|
return stats |
|
@ -73,7 +73,7 @@ func (s *Store) Join(mserver string) { |
|
|
stats := new([]*VolumeInfo) |
|
|
stats := new([]*VolumeInfo) |
|
|
for k, v := range s.volumes { |
|
|
for k, v := range s.volumes { |
|
|
s := new(VolumeInfo) |
|
|
s := new(VolumeInfo) |
|
|
s.Id, s.Size = VolumeId(k), v.Size() |
|
|
|
|
|
|
|
|
s.Id, s.Size, s.RepType = VolumeId(k), v.Size(), v.replicaType |
|
|
*stats = append(*stats, s) |
|
|
*stats = append(*stats, s) |
|
|
} |
|
|
} |
|
|
bytes, _ := json.Marshal(stats) |
|
|
bytes, _ := json.Marshal(stats) |
|
@ -90,22 +90,38 @@ func (s *Store) Close() { |
|
|
} |
|
|
} |
|
|
func (s *Store) Write(i VolumeId, n *Needle) uint32 { |
|
|
func (s *Store) Write(i VolumeId, n *Needle) uint32 { |
|
|
v := s.volumes[i] |
|
|
v := s.volumes[i] |
|
|
if v!=nil{ |
|
|
|
|
|
|
|
|
if v != nil { |
|
|
return v.write(n) |
|
|
return v.write(n) |
|
|
} |
|
|
} |
|
|
return 0 |
|
|
return 0 |
|
|
} |
|
|
} |
|
|
func (s *Store) Delete(i VolumeId, n *Needle) uint32 { |
|
|
func (s *Store) Delete(i VolumeId, n *Needle) uint32 { |
|
|
v := s.volumes[i] |
|
|
v := s.volumes[i] |
|
|
if v!=nil{ |
|
|
|
|
|
|
|
|
if v != nil { |
|
|
return v.delete(n) |
|
|
return v.delete(n) |
|
|
} |
|
|
} |
|
|
return 0 |
|
|
return 0 |
|
|
} |
|
|
} |
|
|
func (s *Store) Read(i VolumeId, n *Needle) (int, error) { |
|
|
func (s *Store) Read(i VolumeId, n *Needle) (int, error) { |
|
|
v := s.volumes[i] |
|
|
v := s.volumes[i] |
|
|
if v!=nil{ |
|
|
|
|
|
|
|
|
if v != nil { |
|
|
return v.read(n) |
|
|
return v.read(n) |
|
|
} |
|
|
} |
|
|
return 0, errors.New("Not Found") |
|
|
return 0, errors.New("Not Found") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type VolumeLocations struct { |
|
|
|
|
|
Vid VolumeId |
|
|
|
|
|
Locations []string |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Store) SetVolumeLocations(volumeLocationList []VolumeLocations) error { |
|
|
|
|
|
for _, volumeLocations := range volumeLocationList { |
|
|
|
|
|
vid := volumeLocations.Vid |
|
|
|
|
|
v := s.volumes[vid] |
|
|
|
|
|
if v != nil { |
|
|
|
|
|
v.locations = volumeLocations.Locations |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nil |
|
|
|
|
|
} |