Documentation ¶
Overview ¶
Package snappr prunes snapshots according to a flexible retention policy.
Index ¶
- type Period
- type Policy
- func (p Policy) Clone() Policy
- func (p Policy) Each(fn func(period Period, count int))
- func (p Policy) Get(period Period) (count int)
- func (p Policy) MarshalText() ([]byte, error)
- func (p *Policy) MustSet(unit Unit, interval, count int)
- func (p *Policy) Set(period Period, count int) (ok bool)
- func (p Policy) String() string
- func (p *Policy) UnmarshalText(b []byte) error
- type Unit
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Period ¶
type Period struct { Unit Unit Interval int // ignored if Unit is Last (normalized to 1), must be > 0 }
Period is a specific time interval for snapshot retention.
type Policy ¶
type Policy struct {
// contains filtered or unexported fields
}
Policy defines a retention policy for snapshots.
All periods are valid and normalized.
func ParsePolicy ¶
ParsePolicy parses a policy from the provided rules.
Each rule is in the form N@unit:X, where N is the snapshot count, unit is a unit name, and X is the interval. If N is negative, an infinite number of snapshots is retained. N must not be zero. X must be greater than zero. If N@ is omitted, it defaults to -1. If :X is omitted, it defaults to 1. For the "last" unit, X must be 1. For the "secondly" unit, X can also be a duration in the format used by time.ParseDuration. Each rule must be unique by the unit:X.
func Prune ¶
Prune prunes the provided list of snapshots, returning a matching slice of periods requiring that snapshot, and the remaining number of snapshots required to fulfill the original policy.
All snapshots are placed in the provided timezone, and the monotonic time component is removed. The timezone affects the exact point at which calendar days/months/years are split. Beware of duplicate timestamps at DST transitions (if the offset isn't included whatever you use as the snapshot name, and your timezone has DST, you may end up with two snapshots for different times with the same name).
See pruneCorrectness in snappr_test.go for some additional notes about guarantees provided by Prune.
Example ¶
var times []time.Time for i := 0; i < 5000*24*2; i++ { times = append(times, time.Date(2000, 1, 1, 0, 30*i, prand(30*60, i, 0xABCDEF0123456789), 0, time.UTC)) } var policy Policy policy.MustSet(Yearly, 5, -1) policy.MustSet(Yearly, 2, 10) policy.MustSet(Yearly, 1, 3) policy.MustSet(Monthly, 6, 4) policy.MustSet(Monthly, 2, 6) policy.MustSet(Daily, 1, 7) policy.MustSet(Secondly, int(time.Hour/time.Second), 6) policy.MustSet(Last, 1, 3) fmt.Println(policy) keep, need := Prune(times, policy, time.UTC) for at, reason := range keep { at := times[at] if len(reason) != 0 { var b strings.Builder for i, r := range reason { if i != 0 { b.WriteString(", ") } b.WriteString(r.String()) } fmt.Println(at.Format(time.ANSIC), "|", b.String()) } } fmt.Println(need)
Output: last (3), 1h time (6), 1 day (7), 2 month (6), 6 month (4), 1 year (3), 2 year (10), 5 year (inf) Fri Dec 31 23:55:29 1999 | 2 year, 5 year Sat Jan 1 00:36:00 2000 | 2 year, 5 year Tue Jan 1 00:45:28 2002 | 2 year Thu Jan 1 00:04:24 2004 | 2 year Sat Jan 1 00:04:16 2005 | 5 year Sun Jan 1 00:43:52 2006 | 2 year Tue Jan 1 00:02:48 2008 | 2 year Fri Jan 1 00:42:16 2010 | 2 year, 5 year Sat Jan 1 00:11:21 2011 | 1 year Thu Dec 1 00:18:09 2011 | 6 month Sun Jan 1 00:01:12 2012 | 1 year, 2 year Fri Jun 1 00:43:36 2012 | 6 month Mon Oct 1 00:13:28 2012 | 2 month Sat Dec 1 00:38:47 2012 | 2 month, 6 month Tue Jan 1 00:01:04 2013 | 1 year Fri Feb 1 00:33:52 2013 | 2 month Mon Apr 1 00:27:37 2013 | 2 month Sat Jun 1 00:12:41 2013 | 2 month, 6 month Thu Aug 1 00:38:00 2013 | 2 month Mon Sep 2 00:01:04 2013 | 1 day Tue Sep 3 00:31:51 2013 | 1 day Wed Sep 4 00:01:37 2013 | 1 day Thu Sep 5 00:32:24 2013 | 1 day Fri Sep 6 00:12:25 2013 | 1 day Sat Sep 7 00:43:12 2013 | 1 day Sun Sep 8 00:03:28 2013 | 1 day Sun Sep 8 18:18:52 2013 | 1h time Sun Sep 8 19:09:38 2013 | 1h time Sun Sep 8 20:20:09 2013 | 1h time Sun Sep 8 21:51:26 2013 | 1h time Sun Sep 8 22:01:57 2013 | 1h time Sun Sep 8 22:12:12 2013 | last Sun Sep 8 23:22:43 2013 | last, 1h time Sun Sep 8 23:33:14 2013 | last last (0), 1h time (0), 1 day (0), 2 month (0), 6 month (0), 1 year (0), 2 year (2), 5 year (inf)
func (Policy) MarshalText ¶
MarshalText encodes the policy into a form usable by UnmarshalText. The output is the canonical form of the rules (i.e., all equivalent policies will result in the same output).
func (*Policy) MustSet ¶
MustSet is like Set, but panics if the period is invalid or has already been used.
func (*Policy) Set ¶
Set sets the count for a period if it is valid, replacing any existing count. A count of zero removes the period.
func (Policy) String ¶
String formats the policy in a human-readable form. The exact output is subject to change.
func (*Policy) UnmarshalText ¶
UnmarshalText parses the provided text into p, replacing the existing policy. It splits the text by whitespace and calls ParsePolicy.