-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_index.go
194 lines (170 loc) · 4.15 KB
/
grid_index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package tsi2
import (
"cycledb/pkg/tsdb"
"sync"
"github.com/influxdata/influxdb/v2/models"
)
type GridIndex struct {
grids []*Grid
optimizer Optimizer
mu sync.RWMutex
}
func NewGridIndex(optimizer *MultiplierOptimizer) *GridIndex {
return &GridIndex{
grids: []*Grid{},
optimizer: optimizer,
}
}
func (gi *GridIndex) WithAnalyzer(analyzer *MultiplierOptimizer) {
gi.optimizer = analyzer
}
// GetSeriesIDsForTags:
func (gi *GridIndex) GetSeriesIDsForTags(tags models.Tags) *tsdb.SeriesIDSet {
ids := tsdb.NewSeriesIDSet()
gi.mu.RLock()
defer gi.mu.RUnlock()
for _, grid := range gi.grids {
idsForGrid := grid.GetSeriesIDSetForTags(tags)
if idsForGrid != nil {
ids.MergeInPlace(idsForGrid)
}
}
return ids
}
// GetStrictlyMatchedSeriesIDForTags: each dimension must match strictly, or return -1
func (gi *GridIndex) GetStrictlyMatchedSeriesIDForTags(tags models.Tags) (uint64, bool) {
for _, grid := range gi.grids {
id, ok := grid.GetStrictlyMatchedIDForTags(tags)
if ok {
return id, true
}
}
return 0, false
}
// SetTags: (insert series keys, then) return corresponding id
func (gi *GridIndex) SetTags(tags models.Tags) (uint64, bool) {
// 1. if tag pair sets already exist
gi.mu.RLock()
id, ok := gi.GetStrictlyMatchedSeriesIDForTags(tags)
if ok {
gi.mu.RUnlock()
return id, false
}
gi.mu.RUnlock()
// 2. try to do insert within existed grids
// double check
gi.mu.Lock()
defer gi.mu.Unlock()
id, ok = gi.GetStrictlyMatchedSeriesIDForTags(tags)
if ok {
return id, false
}
for _, grid := range gi.grids {
if id, ok = grid.SetTags(tags); ok {
return id, true
}
}
// else create a new grid
return gi.initGridAndSetTags(tags)
}
func (gi *GridIndex) HasTagKey(key string) bool {
gi.mu.RLock()
defer gi.mu.RUnlock()
for _, grid := range gi.grids {
if _, ok := grid.tagKeyToIndex[key]; ok {
return true
}
}
return false
}
func (gi *GridIndex) HasTagValue(key, value string) bool {
gi.mu.RLock()
defer gi.mu.Unlock()
for _, grid := range gi.grids {
if index, ok := grid.tagKeyToIndex[key]; ok {
if _, ok := grid.tagValuesSlice[index].valueToIndex[value]; ok {
return true
}
}
}
return false
}
func (gi *GridIndex) initGridAndSetTags(tags models.Tags) (uint64, bool) {
grid := gi.optimizer.NewOptimizedGrid(gi, tags)
gi.grids = append(gi.grids, grid)
grid.seriesIDSet.Add(grid.offset)
return grid.offset, true
}
func (gi *GridIndex) GetNumOfFilledUpGridForSingleTagKey(tagKey string) int {
cnt := 0
for _, g := range gi.grids {
if g.tagKeyExistsAndFilledUp(tagKey) {
cnt++
}
}
return cnt
}
func (gi *GridIndex) NewTagKeyIterator() *TagKeyIterator {
gi.mu.RLock()
res := map[string]struct{}{}
for _, g := range gi.grids {
res = unionStringSets2(res, g.tagKeyToIndex)
}
gi.mu.RUnlock()
slice := mapToSlice(res)
return &TagKeyIterator{
keys: slice,
}
}
func (gi *GridIndex) NewTagValueIterator(key string) *TagValueIterator {
gi.mu.RLock()
res := map[string]struct{}{}
for _, g := range gi.grids {
if index, ok := g.tagKeyToIndex[key]; ok {
res = unionStringSets2(res, g.tagValuesSlice[index].valueToIndex)
}
}
gi.mu.RUnlock()
slice := mapToSlice(res)
return &TagValueIterator{
values: slice,
}
}
func (gi *GridIndex) SeriesIDSet() *tsdb.SeriesIDSet {
gi.mu.RLock()
defer gi.mu.RUnlock()
idsSet := tsdb.NewSeriesIDSet()
for _, g := range gi.grids {
idsSet.MergeInPlace(g.GetSeriesIDSetForTags(nil))
}
return idsSet
}
func (gi *GridIndex) SeriesIDSetForTagKey(key string) *tsdb.SeriesIDSet {
gi.mu.RLock()
defer gi.mu.RUnlock()
idsSet := tsdb.NewSeriesIDSet()
for _, g := range gi.grids {
if g.HasTagKey(key) {
idsSet.MergeInPlace(g.GetSeriesIDSetForTags(nil))
}
}
return idsSet
}
func (gi *GridIndex) SeriesIDSetForTagValue(key, value string) *tsdb.SeriesIDSet {
gi.mu.RLock()
defer gi.mu.RUnlock()
idsSet := tsdb.NewSeriesIDSet()
for _, g := range gi.grids {
if g.HasTagValue(key, value) {
idsSet.MergeInPlace(g.GetSeriesIDSetForTags(models.NewTags(
map[string]string{
key: value,
},
)))
}
}
// idsSet.ForEach(func (id uint64) {
// fmt.Printf("SeriesIDSetWithTagValue returns idSet containing %d\n", id)
// })
return idsSet
}