fix: bucker

bucker "always" does not return a unique id in case of exact same timestamps
This commit is contained in:
Tobias Klein 2017-08-29 18:30:06 +02:00
parent 0ee1650f82
commit 761af08889
1 changed files with 11 additions and 9 deletions

View File

@ -34,34 +34,34 @@ func (e ExpirePolicy) Empty() bool {
} }
// ymdh returns an integer in the form YYYYMMDDHH. // ymdh returns an integer in the form YYYYMMDDHH.
func ymdh(d time.Time) int { func ymdh(d time.Time, _ int) int {
return d.Year()*1000000 + int(d.Month())*10000 + d.Day()*100 + d.Hour() return d.Year()*1000000 + int(d.Month())*10000 + d.Day()*100 + d.Hour()
} }
// ymd returns an integer in the form YYYYMMDD. // ymd returns an integer in the form YYYYMMDD.
func ymd(d time.Time) int { func ymd(d time.Time, _ int) int {
return d.Year()*10000 + int(d.Month())*100 + d.Day() return d.Year()*10000 + int(d.Month())*100 + d.Day()
} }
// yw returns an integer in the form YYYYWW, where WW is the week number. // yw returns an integer in the form YYYYWW, where WW is the week number.
func yw(d time.Time) int { func yw(d time.Time, _ int) int {
year, week := d.ISOWeek() year, week := d.ISOWeek()
return year*100 + week return year*100 + week
} }
// ym returns an integer in the form YYYYMM. // ym returns an integer in the form YYYYMM.
func ym(d time.Time) int { func ym(d time.Time, _ int) int {
return d.Year()*100 + int(d.Month()) return d.Year()*100 + int(d.Month())
} }
// y returns the year of d. // y returns the year of d.
func y(d time.Time) int { func y(d time.Time, _ int) int {
return d.Year() return d.Year()
} }
// always returns a unique number for d. // always returns a unique number for d.
func always(d time.Time) int { func always(d time.Time, nr int) int {
return int(d.UnixNano()) return nr
} }
// ApplyPolicy returns the snapshots from list that are to be kept and removed // ApplyPolicy returns the snapshots from list that are to be kept and removed
@ -79,7 +79,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) {
var buckets = [6]struct { var buckets = [6]struct {
Count int Count int
bucker func(d time.Time) int bucker func(d time.Time, nr int) int
Last int Last int
}{ }{
{p.Last, always, -1}, {p.Last, always, -1},
@ -90,7 +90,9 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) {
{p.Yearly, y, -1}, {p.Yearly, y, -1},
} }
var nr int
for _, cur := range list { for _, cur := range list {
nr = nr + 1
var keepSnap bool var keepSnap bool
// Tags are handled specially as they are not counted. // Tags are handled specially as they are not counted.
@ -103,7 +105,7 @@ func ApplyPolicy(list Snapshots, p ExpirePolicy) (keep, remove Snapshots) {
// Now update the other buckets and see if they have some counts left. // Now update the other buckets and see if they have some counts left.
for i, b := range buckets { for i, b := range buckets {
if b.Count > 0 { if b.Count > 0 {
val := b.bucker(cur.Time) val := b.bucker(cur.Time, nr)
if val != b.Last { if val != b.Last {
keepSnap = true keepSnap = true
buckets[i].Last = val buckets[i].Last = val