adding the integer parser utility
This commit is contained in:
parent
fa5de8e09a
commit
b04909d3ee
|
@ -633,19 +633,14 @@ func (s *Scorch) removeOldBoltSnapshots() (numRemoved int, err error) {
|
|||
return 0, err
|
||||
}
|
||||
|
||||
numSnapshotsToKeep := NumSnapshotsToKeep
|
||||
if val, ok := s.config["numSnapshotsToKeep"].(float64); ok && val > 0 {
|
||||
numSnapshotsToKeep = int(val)
|
||||
}
|
||||
|
||||
if len(persistedEpochs) <= numSnapshotsToKeep {
|
||||
if len(persistedEpochs) <= s.numSnapshotsToKeep {
|
||||
// we need to keep everything
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// make a map of epochs to protect from deletion
|
||||
protectedEpochs := make(map[uint64]struct{}, numSnapshotsToKeep)
|
||||
for _, epoch := range persistedEpochs[0:numSnapshotsToKeep] {
|
||||
protectedEpochs := make(map[uint64]struct{}, s.numSnapshotsToKeep)
|
||||
for _, epoch := range persistedEpochs[0:s.numSnapshotsToKeep] {
|
||||
protectedEpochs[epoch] = struct{}{}
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,7 @@ type Scorch struct {
|
|||
nextSnapshotEpoch uint64
|
||||
eligibleForRemoval []uint64 // Index snapshot epochs that are safe to GC.
|
||||
ineligibleForRemoval map[string]bool // Filenames that should not be GC'ed yet.
|
||||
numSnapshotsToKeep int
|
||||
|
||||
closeCh chan struct{}
|
||||
introductions chan *segmentIntroduction
|
||||
|
@ -191,6 +192,17 @@ func (s *Scorch) openBolt() error {
|
|||
}
|
||||
}
|
||||
|
||||
s.numSnapshotsToKeep = NumSnapshotsToKeep
|
||||
if v, ok := s.config["numSnapshotsToKeep"]; ok {
|
||||
var t int
|
||||
if t, err = parseToInteger(v); err != nil {
|
||||
return fmt.Errorf("numSnapshotsToKeep parse err: %v", err)
|
||||
}
|
||||
if t > 0 {
|
||||
s.numSnapshotsToKeep = t
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -503,3 +515,34 @@ func (s *Scorch) unmarkIneligibleForRemoval(filename string) {
|
|||
func init() {
|
||||
registry.RegisterIndexType(Name, NewScorch)
|
||||
}
|
||||
|
||||
func parseToInteger(v interface{}) (int, error) {
|
||||
switch v.(type) {
|
||||
case float32:
|
||||
return int(v.(float32)), nil
|
||||
case float64:
|
||||
return int(v.(float64)), nil
|
||||
case int:
|
||||
return v.(int), nil
|
||||
case int8:
|
||||
return int(v.(int8)), nil
|
||||
case int16:
|
||||
return int(v.(int16)), nil
|
||||
case int32:
|
||||
return int(v.(int32)), nil
|
||||
case int64:
|
||||
return int(v.(int64)), nil
|
||||
case uint:
|
||||
return int(v.(uint)), nil
|
||||
case uint8:
|
||||
return int(v.(uint8)), nil
|
||||
case uint16:
|
||||
return int(v.(uint16)), nil
|
||||
case uint32:
|
||||
return int(v.(uint32)), nil
|
||||
case uint64:
|
||||
return int(v.(uint64)), nil
|
||||
default:
|
||||
return 0, fmt.Errorf("expects a numeric value")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue