github.com/m3db/m3@v1.5.0/src/metrics/encoding/protobuf/aggregated_decoder.go (about)

     1  // Copyright (c) 2018 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 protobuf
    22  
    23  import (
    24  	"github.com/m3db/m3/src/metrics/generated/proto/metricpb"
    25  	"github.com/m3db/m3/src/metrics/policy"
    26  )
    27  
    28  // AggregatedDecoder is a decoder for decoding aggregated metrics.
    29  type AggregatedDecoder struct {
    30  	pool AggregatedDecoderPool
    31  	pb   metricpb.AggregatedMetric
    32  	sp   policy.StoragePolicy
    33  }
    34  
    35  // NewAggregatedDecoder creates an aggregated decoder.
    36  func NewAggregatedDecoder(p AggregatedDecoderPool) *AggregatedDecoder {
    37  	return &AggregatedDecoder{
    38  		pool: p,
    39  	}
    40  }
    41  
    42  // Decode decodes the aggregated metric from the given bytes.
    43  func (d *AggregatedDecoder) Decode(b []byte) error {
    44  	if err := d.pb.Unmarshal(b); err != nil {
    45  		return err
    46  	}
    47  	return d.sp.FromProto(d.pb.Metric.StoragePolicy)
    48  }
    49  
    50  // ID returns the decoded id.
    51  func (d AggregatedDecoder) ID() []byte {
    52  	return d.pb.Metric.TimedMetric.Id
    53  }
    54  
    55  // TimeNanos returns the decoded timestamp.
    56  func (d AggregatedDecoder) TimeNanos() int64 {
    57  	return d.pb.Metric.TimedMetric.TimeNanos
    58  }
    59  
    60  // Value returns the decoded value.
    61  func (d AggregatedDecoder) Value() float64 {
    62  	return d.pb.Metric.TimedMetric.Value
    63  }
    64  
    65  // Annotation returns the decoded annotation.
    66  func (d *AggregatedDecoder) Annotation() []byte {
    67  	return d.pb.Metric.TimedMetric.Annotation
    68  }
    69  
    70  // StoragePolicy returns the decoded storage policy.
    71  func (d AggregatedDecoder) StoragePolicy() policy.StoragePolicy {
    72  	return d.sp
    73  }
    74  
    75  // EncodeNanos returns the decoded encodeNanos.
    76  func (d AggregatedDecoder) EncodeNanos() int64 {
    77  	return d.pb.EncodeNanos
    78  }
    79  
    80  // Close closes the decoder.
    81  func (d *AggregatedDecoder) Close() {
    82  	d.sp = policy.StoragePolicy{}
    83  	ReuseAggregatedMetricProto(&d.pb)
    84  	if d.pool != nil {
    85  		d.pool.Put(d)
    86  	}
    87  }