Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type Whisper struct {
Create a new Whisper database file and write it's header.
*/
func Create(path string, retentions Retentions, aggregationMethod AggregationMethod, xFilesFactor float32) (whisper *Whisper, err error) {
sort.Sort(retentionsByPrecision{retentions})
sort.Sort(RetentionsByPrecision{retentions})
if err = validateRetentions(retentions); err != nil {
return nil, err
}
Expand Down Expand Up @@ -294,6 +294,23 @@ func (whisper *Whisper) MetadataSize() int {
return MetadataSize + (ArchiveInfoSize * len(whisper.archives))
}

/*
Retentions returns a Retentions type which lists each archive's retention
in this Whisper file. A deep copy so that the internal data structure
remains safe.
*/
func (whisper *Whisper) Retentions() (retentions Retentions) {
retentions = make(Retentions, 0)
for _, v := range whisper.archives {
r := new(Retention)
r.secondsPerPoint = v.Retention.secondsPerPoint
r.numberOfPoints = v.Retention.numberOfPoints
retentions = append(retentions, r)
}

return retentions
}

/*
Update a value in the database.

Expand Down Expand Up @@ -642,6 +659,14 @@ func (retention *Retention) Size() int {
return retention.numberOfPoints * PointSize
}

func (retention *Retention) SecondsPerPoint() int {
return retention.secondsPerPoint
}

func (retention *Retention) NumberOfPoints() int {
return retention.numberOfPoints
}

type Retentions []*Retention

func (r Retentions) Len() int {
Expand All @@ -652,12 +677,16 @@ func (r Retentions) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}

type retentionsByPrecision struct{ Retentions }
type RetentionsByPrecision struct{ Retentions }

func (r retentionsByPrecision) Less(i, j int) bool {
func (r RetentionsByPrecision) Less(i, j int) bool {
return r.Retentions[i].secondsPerPoint < r.Retentions[j].secondsPerPoint
}

func (r RetentionsByPrecision) Iterator() Retentions {
return r.Retentions
}

/*
Describes a time series in a file.

Expand Down
25 changes: 24 additions & 1 deletion whisper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestParseRetentionDefs(t *testing.T) {

func TestSortRetentions(t *testing.T) {
retentions := Retentions{{300, 12}, {60, 30}, {1, 300}}
sort.Sort(retentionsByPrecision{retentions})
sort.Sort(RetentionsByPrecision{retentions})
if retentions[0].secondsPerPoint != 1 {
t.Fatalf("Retentions array is not sorted")
}
Expand Down Expand Up @@ -231,6 +231,29 @@ func TestOpenFile(t *testing.T) {
tearDown()
}

func TestGetRetentions(t *testing.T) {
path, _, retentions, tearDown := setUpCreate()
wsp, err := Create(path, retentions, Average, 0.5)
if err != nil {
t.Errorf("Failed to create: %v", err)
}
defer wsp.Close()
archiveList := wsp.Retentions()
if len(archiveList) != len(retentions) {
t.Fatalf("Returned retentions is not the same length as inital retentions")
}
for i, _ := range retentions {
if retentions[i].secondsPerPoint != archiveList[i].SecondsPerPoint() {
t.Fatalf("Retention's secondsPerPoint do not match")
}
if retentions[i].numberOfPoints != archiveList[i].NumberOfPoints() {
t.Fatalf("Retention's numberOfPoints do not match")
}
}

tearDown()
}

/*
Test the full cycle of creating a whisper file, adding some
data points to it and then fetching a time series.
Expand Down