This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
/
store_mock.go
74 lines (60 loc) · 1.61 KB
/
store_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package mdata
import (
"context"
"github.com/grafana/metrictank/schema"
"github.com/grafana/metrictank/mdata/chunk"
opentracing "github.com/opentracing/opentracing-go"
)
// MockStore is an in-memory Store implementation for unit tests
type MockStore struct {
// the itgens to be searched and returned, indexed by metric
results map[schema.AMKey][]chunk.IterGen
// count of chunks in the store.
items int
// dont save any data.
Drop bool
}
func NewMockStore() *MockStore {
return &MockStore{
results: make(map[schema.AMKey][]chunk.IterGen),
Drop: false,
}
}
func (c *MockStore) Reset() {
c.results = make(map[schema.AMKey][]chunk.IterGen)
c.items = 0
}
func (c *MockStore) Items() int {
return c.items
}
// Add adds a chunk to the store
func (c *MockStore) Add(cwr *ChunkWriteRequest) {
if !c.Drop {
intervalHint := cwr.Key.Archive.Span()
itgen, err := chunk.NewIterGen(cwr.T0, intervalHint, cwr.Data)
if err != nil {
panic(err)
}
c.results[cwr.Key] = append(c.results[cwr.Key], itgen)
c.items++
}
}
// searches through the mock results and returns the right ones according to start / end
func (c *MockStore) Search(ctx context.Context, metric schema.AMKey, ttl, start, end uint32) ([]chunk.IterGen, error) {
var itgens, res []chunk.IterGen
var ok bool
if itgens, ok = c.results[metric]; !ok {
return res, nil
}
for _, itgen := range itgens {
// start is inclusive, end is exclusive
if itgen.T0 < end && itgen.EndTs() > start && start < end {
res = append(res, itgen)
}
}
return res, nil
}
func (c *MockStore) Stop() {
}
func (c *MockStore) SetTracer(t opentracing.Tracer) {
}