From f93e5a39e5cbb7ffafdc2b3c4ab3c08dc4431e96 Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Thu, 4 May 2017 16:39:41 +0200 Subject: [PATCH] Small textual changes to apply, to reflect actual meaning. Added tests for the ExpirePolicy operations. --- src/restic/snapshot_filter.go | 43 ++++++++++++++---------------- src/restic/snapshot_filter_test.go | 22 +++++++++++++++ 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/restic/snapshot_filter.go b/src/restic/snapshot_filter.go index 707a68e09..69b9e44f8 100644 --- a/src/restic/snapshot_filter.go +++ b/src/restic/snapshot_filter.go @@ -125,49 +125,46 @@ func y(d time.Time) int { } // apply moves snapshots from Unprocess to either Keep or Remove. It sorts the -// snapshots into buckets according to the return of fn, and then moves the -// newest snapshot in each bucket to Keep and all others to Remove. When max -// snapshots were found, processing stops. +// snapshots into buckets according to the return value of fn, and then moves +// the newest snapshot in each bucket to Keep and all others to Remove. When +// max snapshots were found, processing stops. func (f *filter) apply(fn func(time.Time) int, max int) { if max == 0 || len(f.Unprocessed) == 0 { return } - sameDay := Snapshots{} - lastDay := fn(f.Unprocessed[0].Time) + sameBucket := Snapshots{} + lastBucket := fn(f.Unprocessed[0].Time) for len(f.Unprocessed) > 0 { cur := f.Unprocessed[0] - day := fn(cur.Time) + bucket := fn(cur.Time) - // if the snapshots are from a new day, forget all but the first (=last - // in time) snapshot from the previous day. - if day != lastDay { - f.Keep = append(f.Keep, sameDay[0]) - for _, snapshot := range sameDay[1:] { - f.Remove = append(f.Remove, snapshot) - } + // if the snapshots are from a new bucket, forget all but the first + // (=last in time) snapshot from the previous bucket. + if bucket != lastBucket { + f.Keep = append(f.Keep, sameBucket[0]) + f.Remove = append(f.Remove, sameBucket[1:]...) - sameDay = Snapshots{} - lastDay = day + sameBucket = Snapshots{} + lastBucket = bucket max-- if max == 0 { - break + return } } - // collect all snapshots for the current day - sameDay = append(sameDay, cur) + // collect all snapshots for the current bucket + sameBucket = append(sameBucket, cur) f.Unprocessed = f.Unprocessed[1:] } - if len(sameDay) > 0 { - f.Keep = append(f.Keep, sameDay[0]) - for _, snapshot := range sameDay[1:] { - f.Remove = append(f.Remove, snapshot) - } + // if we have leftovers, process them too. + if len(sameBucket) > 0 { + f.Keep = append(f.Keep, sameBucket[0]) + f.Remove = append(f.Remove, sameBucket[1:]...) } } diff --git a/src/restic/snapshot_filter_test.go b/src/restic/snapshot_filter_test.go index 46257fb50..fbeef229d 100644 --- a/src/restic/snapshot_filter_test.go +++ b/src/restic/snapshot_filter_test.go @@ -91,6 +91,28 @@ func TestFilterSnapshots(t *testing.T) { } } +func TestExpireSnapshotOps(t *testing.T) { + data := []struct { + expectEmpty bool + expectSum int + p *restic.ExpirePolicy + }{ + {true, 0, &restic.ExpirePolicy{}}, + {true, 0, &restic.ExpirePolicy{Tags: []string{}}}, + {false, 22, &restic.ExpirePolicy{Daily: 7, Weekly: 2, Monthly: 3, Yearly: 10}}, + } + for i, d := range data { + isEmpty := d.p.Empty() + if isEmpty != d.expectEmpty { + t.Errorf("empty test %v: wrong result, want:\n %#v\ngot:\n %#v", i, d.expectEmpty, isEmpty) + } + hasSum := d.p.Sum() + if hasSum != d.expectSum { + t.Errorf("sum test %v: wrong result, want:\n %#v\ngot:\n %#v", i, d.expectSum, hasSum) + } + } +} + var testExpireSnapshots = restic.Snapshots{ {Time: parseTimeUTC("2014-09-01 10:20:30")}, {Time: parseTimeUTC("2014-09-02 10:20:30")},