github.com/m3db/m3@v1.5.0/src/dbnode/storage/series/util.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package series
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/m3db/m3/src/dbnode/encoding"
    27  	"github.com/m3db/m3/src/dbnode/x/xio"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  
    30  	"github.com/m3db/m3/src/dbnode/namespace"
    31  )
    32  
    33  // ValuesByTime is a sortable slice of DecodedTestValue.
    34  type ValuesByTime []DecodedTestValue
    35  
    36  // Len is the number of elements in the collection.
    37  func (v ValuesByTime) Len() int {
    38  	return len(v)
    39  }
    40  
    41  // Less reports whether the element with
    42  // index i should sort before the element with index j.
    43  func (v ValuesByTime) Less(lhs, rhs int) bool {
    44  	l := v[lhs].Timestamp
    45  	r := v[rhs].Timestamp
    46  	if l.Equal(r) {
    47  		return v[lhs].Value-v[rhs].Value < 0
    48  	}
    49  
    50  	return l.Before(r)
    51  }
    52  
    53  // Swap swaps the elements with indexes i and j.
    54  func (v ValuesByTime) Swap(lhs, rhs int) {
    55  	v[lhs], v[rhs] = v[rhs], v[lhs]
    56  }
    57  
    58  // DecodedTestValue is a decoded datapoint.
    59  type DecodedTestValue struct {
    60  	// Timestamp is the data point timestamp.
    61  	Timestamp xtime.UnixNano
    62  	// Value is the data point value.
    63  	Value float64
    64  	// Unit is the data point unit.
    65  	Unit xtime.Unit
    66  	// Annotation is the data point annotation.
    67  	Annotation []byte
    68  }
    69  
    70  // DecodeSegmentValues is a test utility to read through a slice of
    71  // SegmentReaders.
    72  func DecodeSegmentValues(
    73  	results []xio.SegmentReader,
    74  	iter encoding.MultiReaderIterator,
    75  	schema namespace.SchemaDescr,
    76  ) ([]DecodedTestValue, error) {
    77  	iter.Reset(results, 0, time.Duration(0), schema)
    78  	defer iter.Close()
    79  
    80  	var all []DecodedTestValue
    81  	for iter.Next() {
    82  		dp, unit, annotation := iter.Current()
    83  		// Iterator reuse annotation byte slices, so make a copy.
    84  		annotationCopy := append([]byte(nil), annotation...)
    85  		all = append(all, DecodedTestValue{dp.TimestampNanos, dp.Value, unit, annotationCopy})
    86  	}
    87  
    88  	if err := iter.Err(); err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	return all, nil
    93  }