just refactoring
This commit is contained in:
@@ -13,10 +13,14 @@ type DiskLocation struct {
|
|||||||
volumes map[VolumeId]*Volume
|
volumes map[VolumeId]*Volume
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mn *DiskLocation) reset() {
|
func NewDiskLocation(dir string, maxVolumeCount int) *DiskLocation {
|
||||||
|
location := &DiskLocation{Directory: dir, MaxVolumeCount: maxVolumeCount}
|
||||||
|
location.volumes = make(map[VolumeId]*Volume)
|
||||||
|
return location
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
|
func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
|
||||||
|
|
||||||
if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
|
if dirs, err := ioutil.ReadDir(l.Directory); err == nil {
|
||||||
for _, dir := range dirs {
|
for _, dir := range dirs {
|
||||||
name := dir.Name()
|
name := dir.Name()
|
||||||
@@ -42,3 +46,28 @@ func (l *DiskLocation) loadExistingVolumes(needleMapKind NeedleMapType) {
|
|||||||
}
|
}
|
||||||
glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
|
glog.V(0).Infoln("Store started on dir:", l.Directory, "with", len(l.volumes), "volumes", "max", l.MaxVolumeCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *DiskLocation) DeleteCollectionFromDiskLocation(collection string) (e error) {
|
||||||
|
for k, v := range l.volumes {
|
||||||
|
if v.Collection == collection {
|
||||||
|
e = l.deleteVolumeById(k)
|
||||||
|
if e != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *DiskLocation) deleteVolumeById(vid VolumeId) (e error) {
|
||||||
|
v, ok := l.volumes[vid]
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e = v.Destroy()
|
||||||
|
if e != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
delete(l.volumes, vid)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -89,8 +89,7 @@ func NewStore(port int, ip, publicUrl string, dirnames []string, maxVolumeCounts
|
|||||||
s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl}
|
s = &Store{Port: port, Ip: ip, PublicUrl: publicUrl}
|
||||||
s.Locations = make([]*DiskLocation, 0)
|
s.Locations = make([]*DiskLocation, 0)
|
||||||
for i := 0; i < len(dirnames); i++ {
|
for i := 0; i < len(dirnames); i++ {
|
||||||
location := &DiskLocation{Directory: dirnames[i], MaxVolumeCount: maxVolumeCounts[i]}
|
location := NewDiskLocation(dirnames[i], maxVolumeCounts[i])
|
||||||
location.volumes = make(map[VolumeId]*Volume)
|
|
||||||
location.loadExistingVolumes(needleMapKind)
|
location.loadExistingVolumes(needleMapKind)
|
||||||
s.Locations = append(s.Locations, location)
|
s.Locations = append(s.Locations, location)
|
||||||
}
|
}
|
||||||
@@ -134,26 +133,14 @@ func (s *Store) AddVolume(volumeListString string, collection string, needleMapK
|
|||||||
}
|
}
|
||||||
func (s *Store) DeleteCollection(collection string) (e error) {
|
func (s *Store) DeleteCollection(collection string) (e error) {
|
||||||
for _, location := range s.Locations {
|
for _, location := range s.Locations {
|
||||||
for k, v := range location.volumes {
|
e = location.DeleteCollectionFromDiskLocation(collection)
|
||||||
if v.Collection == collection {
|
if e != nil {
|
||||||
e = v.Destroy()
|
return
|
||||||
if e != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
delete(location.volumes, k)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (s *Store) DeleteVolume(volumes map[VolumeId]*Volume, v *Volume) (e error) {
|
|
||||||
e = v.Destroy()
|
|
||||||
if e != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
delete(volumes, v.Id)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
func (s *Store) findVolume(vid VolumeId) *Volume {
|
func (s *Store) findVolume(vid VolumeId) *Volume {
|
||||||
for _, location := range s.Locations {
|
for _, location := range s.Locations {
|
||||||
if v, found := location.volumes[vid]; found {
|
if v, found := location.volumes[vid]; found {
|
||||||
@@ -252,7 +239,7 @@ func (s *Store) SendHeartbeatToMaster() (masterNode string, secretKey security.S
|
|||||||
volumeMessages = append(volumeMessages, volumeMessage)
|
volumeMessages = append(volumeMessages, volumeMessage)
|
||||||
} else {
|
} else {
|
||||||
if v.exiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
|
if v.exiredLongEnough(MAX_TTL_VOLUME_REMOVAL_DELAY) {
|
||||||
s.DeleteVolume(location.volumes, v)
|
location.deleteVolumeById(v.Id)
|
||||||
glog.V(0).Infoln("volume", v.Id, "is deleted.")
|
glog.V(0).Infoln("volume", v.Id, "is deleted.")
|
||||||
} else {
|
} else {
|
||||||
glog.V(0).Infoln("volume", v.Id, "is expired.")
|
glog.V(0).Infoln("volume", v.Id, "is expired.")
|
||||||
|
|||||||
Reference in New Issue
Block a user