keep

package module
v0.0.0-...-f19bb97 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 8 Imported by: 0

README

keep

Godoc Reference Go Version Last Commit Coverage Go Report Card License

This library helps you decide which elements should be kept for a given number of hours, days, weeks, months, and years. Most obvious use case are backup files.

Installation

CLI tool keep
go install github.com/jojomi/keep/command/keep@latest
Golang library keep
go get github.com/jojomi/keep

Algorithm

Given the configuration below (3 last, 12 hours, 7 days, 12 weeks, 12 months, and 4 years) this library will operate on the list of elements sorted by date, youngest first.

  • It will select the first 3 elements from the list (last)
  • It then will select 12 elements that are not more than an hour apart (plus one minute extended range), the first of them being the youngest file not yet processed in the set.
  • It will then select 7 elements that are not less than a day (plus one hour) apart, the first of them being the youngest file not yet processed in the set.
  • and so on as far as levels are defined.
  • It is allowed to skip definitions, so you don't have to select daily elements even if you specify hourly and weekly selections.

Usage

import "github.com/jojomi/keep"

func main() {
    j := NewDefaultJailhouse()
    j.AddElements(...)

    reqs := NewRequirementsFromMap(map[TimeRange]uint16{
        LAST: 3,
        DAY:  2,
        MONTH: 4,
    })

    j.ApplyRequirements(*reqs)
    kept := j.KeptElements()
    deletable := j.FreeElements()

    // print kept elements
    for _, k := range kept {
        // handle here
    }
	
    // delete now
    for _, k := range deletable {
        // handle here
    }
}

Your input data must implement the TimeResource interface:

type TimeResource interface {
	GetTime() time.Time
}

Development

Add git hooks:

git config --local core.hooksPath .githooks/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TimeRangeNames

func TimeRangeNames() []string

TimeRangeNames returns a list of possible string values of TimeRange.

Types

type File

type File struct {
	Filename string
	Time     time.Time
}

func (File) GetTime

func (x File) GetTime() time.Time

func (File) String

func (x File) String() string

type Jailhouse

type Jailhouse[T TimeResource] struct {
	// contains filtered or unexported fields
}

func NewDefaultJailhouse

func NewDefaultJailhouse[T TimeResource]() *Jailhouse[T]

func (*Jailhouse[T]) AddElements

func (x *Jailhouse[T]) AddElements(elems ...T) *Jailhouse[T]

func (*Jailhouse[T]) ApplyRequirements

func (x *Jailhouse[T]) ApplyRequirements(reqs Requirements) *Jailhouse[T]

func (*Jailhouse[T]) ApplyRequirementsForDate

func (x *Jailhouse[T]) ApplyRequirementsForDate(reqs Requirements, referenceDate time.Time) *Jailhouse[T]

func (*Jailhouse[T]) Elements

func (x *Jailhouse[T]) Elements() []*JailhouseTimeResource[T]

func (*Jailhouse[T]) FilteredElements

func (x *Jailhouse[T]) FilteredElements(filter func(*JailhouseTimeResource[T]) bool) []*JailhouseTimeResource[T]

func (*Jailhouse[T]) FreeElements

func (x *Jailhouse[T]) FreeElements() []*JailhouseTimeResource[T]

func (*Jailhouse[T]) GetLevels

func (x *Jailhouse[T]) GetLevels() []TimeRange

func (*Jailhouse[T]) KeptElements

func (x *Jailhouse[T]) KeptElements() []*JailhouseTimeResource[T]

func (*Jailhouse[T]) KeptElementsByLevel

func (x *Jailhouse[T]) KeptElementsByLevel(level TimeRange) []*JailhouseTimeResource[T]

type JailhouseTimeResource

type JailhouseTimeResource[T TimeResource] struct {
	Tags         []TimeRangeTag
	TimeResource T
}

JailhouseTimeResource is an TimeResource with annotated Jailhouse data

func NewJailhouseTimeResource

func NewJailhouseTimeResource[T TimeResource](resource T) *JailhouseTimeResource[T]

func (*JailhouseTimeResource[T]) AddTag

func (*JailhouseTimeResource[T]) ClearTags

func (x *JailhouseTimeResource[T]) ClearTags() *JailhouseTimeResource[T]

func (JailhouseTimeResource[T]) GetTags

func (x JailhouseTimeResource[T]) GetTags() []TimeRangeTag

func (JailhouseTimeResource[T]) GetTime

func (x JailhouseTimeResource[T]) GetTime() time.Time

func (*JailhouseTimeResource[T]) HasLevel

func (x *JailhouseTimeResource[T]) HasLevel(level TimeRange) bool

func (JailhouseTimeResource[T]) IsFree

func (x JailhouseTimeResource[T]) IsFree() bool

func (JailhouseTimeResource[T]) String

func (x JailhouseTimeResource[T]) String() string

type Requirements

type Requirements struct {
	// contains filtered or unexported fields
}

Requirements define which elements in an input slice should be kept

func NewRequirements

func NewRequirements() *Requirements

NewRequirements creates a new empty Requirement definition.

func NewRequirementsFromMap

func NewRequirementsFromMap(data map[TimeRange]uint16) *Requirements

NewRequirementsFromMap makes a Requirement from a map with TimeRange as keys and their number as values.

func NewRequirementsFromString

func NewRequirementsFromString(source string) *Requirements

func (*Requirements) Add

func (x *Requirements) Add(timeRange TimeRange, value int8) *Requirements

Add adds a number of required elements for a given TimeRange.

func (Requirements) DeepCopy

func (x Requirements) DeepCopy() Requirements

DeepCopy returns a Requirement copy with the same properties.

func (Requirements) Get

func (x Requirements) Get(timeRange TimeRange) uint16

Get returns the number of elements in this Requirement for a given TimeRange.

func (Requirements) IsEmpty

func (x Requirements) IsEmpty() bool

IsEmpty is true iff no files should be kept.

func (Requirements) String

func (x Requirements) String() string

String prints a Requirement configuration

type TimeRange

type TimeRange int8
 ENUM(
	LAST
	SECOND
	MINUTE
	HOUR
	DAY
	WEEK
	MONTH
	QUARTER
	YEAR
	DECADE
	CENTURY
	MILLENIUM

)

const (
	// LAST is a TimeRange of type LAST.
	LAST TimeRange = iota
	// SECOND is a TimeRange of type SECOND.
	SECOND
	// MINUTE is a TimeRange of type MINUTE.
	MINUTE
	// HOUR is a TimeRange of type HOUR.
	HOUR
	// DAY is a TimeRange of type DAY.
	DAY
	// WEEK is a TimeRange of type WEEK.
	WEEK
	// MONTH is a TimeRange of type MONTH.
	MONTH
	// QUARTER is a TimeRange of type QUARTER.
	QUARTER
	// YEAR is a TimeRange of type YEAR.
	YEAR
	// DECADE is a TimeRange of type DECADE.
	DECADE
	// CENTURY is a TimeRange of type CENTURY.
	CENTURY
	// MILLENIUM is a TimeRange of type MILLENIUM.
	MILLENIUM
)

func MustParseTimeRange

func MustParseTimeRange(name string) TimeRange

MustParseTimeRange converts a string to a TimeRange, and panics if is not valid.

func ParseTimeRange

func ParseTimeRange(name string) (TimeRange, error)

ParseTimeRange attempts to convert a string to a TimeRange.

func (TimeRange) String

func (x TimeRange) String() string

String implements the Stringer interface.

type TimeRangeTag

type TimeRangeTag struct {
	TimeRange TimeRange
	Index     uint16
}

func TimeRangeTagFrom

func TimeRangeTagFrom(timeRange TimeRange, index uint16) TimeRangeTag

func (TimeRangeTag) String

func (x TimeRangeTag) String() string

type TimeResource

type TimeResource interface {
	GetTime() time.Time
}

TimeResource is an element with an associated time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL