|
| 1 | +package encoding |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + |
| 6 | + "github.com/pkg/errors" |
| 7 | + "github.com/prometheus/common/model" |
| 8 | + "github.com/prometheus/prometheus/tsdb/chunkenc" |
| 9 | +) |
| 10 | + |
| 11 | +// Wrapper around Prometheus chunk. |
| 12 | +type prometheusXorChunk struct { |
| 13 | + chunk chunkenc.Chunk |
| 14 | +} |
| 15 | + |
| 16 | +func newPrometheusXorChunk() *prometheusXorChunk { |
| 17 | + return &prometheusXorChunk{} |
| 18 | +} |
| 19 | + |
| 20 | +// Add adds another sample to the chunk. While Add works, it is only implemented |
| 21 | +// to make tests work, and should not be used in production. In particular, it appends |
| 22 | +// all samples to single chunk, and uses new Appender for each Add. |
| 23 | +func (p *prometheusXorChunk) Add(m model.SamplePair) (Chunk, error) { |
| 24 | + if p.chunk == nil { |
| 25 | + p.chunk = chunkenc.NewXORChunk() |
| 26 | + } |
| 27 | + |
| 28 | + app, err := p.chunk.Appender() |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + app.Append(int64(m.Timestamp), float64(m.Value)) |
| 34 | + return nil, nil |
| 35 | +} |
| 36 | + |
| 37 | +func (p *prometheusXorChunk) NewIterator(iterator Iterator) Iterator { |
| 38 | + if p.chunk == nil { |
| 39 | + return errorIterator("Prometheus chunk is not set") |
| 40 | + } |
| 41 | + |
| 42 | + if pit, ok := iterator.(*prometheusChunkIterator); ok { |
| 43 | + pit.c = p.chunk |
| 44 | + pit.it = p.chunk.Iterator(pit.it) |
| 45 | + return pit |
| 46 | + } |
| 47 | + |
| 48 | + return &prometheusChunkIterator{c: p.chunk, it: p.chunk.Iterator(nil)} |
| 49 | +} |
| 50 | + |
| 51 | +func (p *prometheusXorChunk) Marshal(i io.Writer) error { |
| 52 | + if p.chunk == nil { |
| 53 | + return errors.New("chunk data not set") |
| 54 | + } |
| 55 | + _, err := i.Write(p.chunk.Bytes()) |
| 56 | + return err |
| 57 | +} |
| 58 | + |
| 59 | +func (p *prometheusXorChunk) UnmarshalFromBuf(bytes []byte) error { |
| 60 | + c, err := chunkenc.FromData(chunkenc.EncXOR, bytes) |
| 61 | + if err != nil { |
| 62 | + return errors.Wrap(err, "failed to create Prometheus chunk from bytes") |
| 63 | + } |
| 64 | + |
| 65 | + p.chunk = c |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func (p *prometheusXorChunk) Encoding() Encoding { |
| 70 | + return PrometheusXorChunk |
| 71 | +} |
| 72 | + |
| 73 | +func (p *prometheusXorChunk) Utilization() float64 { |
| 74 | + // Used for reporting when chunk is used to store new data. |
| 75 | + return 0 |
| 76 | +} |
| 77 | + |
| 78 | +func (p *prometheusXorChunk) Slice(_, _ model.Time) Chunk { |
| 79 | + return p |
| 80 | +} |
| 81 | + |
| 82 | +func (p *prometheusXorChunk) Rebound(from, to model.Time) (Chunk, error) { |
| 83 | + return nil, errors.New("Rebound not supported by PrometheusXorChunk") |
| 84 | +} |
| 85 | + |
| 86 | +func (p *prometheusXorChunk) Len() int { |
| 87 | + if p.chunk == nil { |
| 88 | + return 0 |
| 89 | + } |
| 90 | + return p.chunk.NumSamples() |
| 91 | +} |
| 92 | + |
| 93 | +func (p *prometheusXorChunk) Size() int { |
| 94 | + if p.chunk == nil { |
| 95 | + return 0 |
| 96 | + } |
| 97 | + return len(p.chunk.Bytes()) |
| 98 | +} |
| 99 | + |
| 100 | +type prometheusChunkIterator struct { |
| 101 | + c chunkenc.Chunk // we need chunk, because FindAtOrAfter needs to start with fresh iterator. |
| 102 | + it chunkenc.Iterator |
| 103 | +} |
| 104 | + |
| 105 | +func (p *prometheusChunkIterator) Scan() bool { |
| 106 | + return p.it.Next() |
| 107 | +} |
| 108 | + |
| 109 | +func (p *prometheusChunkIterator) FindAtOrAfter(time model.Time) bool { |
| 110 | + // FindAtOrAfter must return OLDEST value at given time. That means we need to start with a fresh iterator, |
| 111 | + // otherwise we cannot guarantee OLDEST. |
| 112 | + p.it = p.c.Iterator(p.it) |
| 113 | + return p.it.Seek(int64(time)) |
| 114 | +} |
| 115 | + |
| 116 | +func (p *prometheusChunkIterator) Value() model.SamplePair { |
| 117 | + ts, val := p.it.At() |
| 118 | + return model.SamplePair{ |
| 119 | + Timestamp: model.Time(ts), |
| 120 | + Value: model.SampleValue(val), |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func (p *prometheusChunkIterator) Batch(size int) Batch { |
| 125 | + var batch Batch |
| 126 | + j := 0 |
| 127 | + for j < size { |
| 128 | + t, v := p.it.At() |
| 129 | + batch.Timestamps[j] = t |
| 130 | + batch.Values[j] = v |
| 131 | + j++ |
| 132 | + if j < size && !p.it.Next() { |
| 133 | + break |
| 134 | + } |
| 135 | + } |
| 136 | + batch.Index = 0 |
| 137 | + batch.Length = j |
| 138 | + return batch |
| 139 | +} |
| 140 | + |
| 141 | +func (p *prometheusChunkIterator) Err() error { |
| 142 | + return p.it.Err() |
| 143 | +} |
| 144 | + |
| 145 | +type errorIterator string |
| 146 | + |
| 147 | +func (e errorIterator) Scan() bool { return false } |
| 148 | +func (e errorIterator) FindAtOrAfter(time model.Time) bool { return false } |
| 149 | +func (e errorIterator) Value() model.SamplePair { panic("no values") } |
| 150 | +func (e errorIterator) Batch(size int) Batch { panic("no values") } |
| 151 | +func (e errorIterator) Err() error { return errors.New(string(e)) } |
0 commit comments