1// Copyright 2017 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package tsdb
15
16import (
17	"github.com/pkg/errors"
18
19	"github.com/prometheus/prometheus/pkg/labels"
20	"github.com/prometheus/prometheus/tsdb/chunkenc"
21	"github.com/prometheus/prometheus/tsdb/chunks"
22	"github.com/prometheus/prometheus/tsdb/tombstones"
23)
24
25type mockIndexWriter struct {
26	seriesChunks []series
27}
28
29func copyChunk(c chunkenc.Chunk) (chunkenc.Chunk, error) {
30	b := c.Bytes()
31	nb := make([]byte, len(b))
32	copy(nb, b)
33	return chunkenc.FromData(c.Encoding(), nb)
34}
35
36func (mockIndexWriter) AddSymbol(string) error { return nil }
37func (m *mockIndexWriter) AddSeries(_ uint64, l labels.Labels, chks ...chunks.Meta) error {
38	// Copy chunks as their bytes are pooled.
39	chksNew := make([]chunks.Meta, len(chks))
40	for i, chk := range chks {
41		c, err := copyChunk(chk.Chunk)
42		if err != nil {
43			return errors.Wrap(err, "mockIndexWriter: copy chunk")
44		}
45		chksNew[i] = chunks.Meta{MaxTime: chk.MaxTime, MinTime: chk.MinTime, Chunk: c}
46	}
47
48	// We don't combine multiple same series together, by design as `AddSeries` requires full series to be saved.
49	m.seriesChunks = append(m.seriesChunks, series{l: l, chunks: chksNew})
50	return nil
51}
52
53func (mockIndexWriter) WriteLabelIndex([]string, []string) error { return nil }
54func (mockIndexWriter) Close() error                             { return nil }
55
56type mockBReader struct {
57	ir   IndexReader
58	cr   ChunkReader
59	mint int64
60	maxt int64
61}
62
63func (r *mockBReader) Index() (IndexReader, error)  { return r.ir, nil }
64func (r *mockBReader) Chunks() (ChunkReader, error) { return r.cr, nil }
65func (r *mockBReader) Tombstones() (tombstones.Reader, error) {
66	return tombstones.NewMemTombstones(), nil
67}
68func (r *mockBReader) Meta() BlockMeta { return BlockMeta{MinTime: r.mint, MaxTime: r.maxt} }
69func (r *mockBReader) Size() int64     { return 0 }
70