-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
258 lines (226 loc) · 6.3 KB
/
grid.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package tsi2
import (
"cycledb/pkg/tsdb"
"github.com/influxdata/influxdb/v2/models"
)
type Grid struct {
// the grids are linked, so id should skip
offset uint64
// for each Grid, the size of tag values array (tagValuesSlice)
// and size of element(capacity) within is pre-allocated
tagValuesSlice []*TagValues
tagKeys []string
// to accelerate search
tagKeyToIndex map[string]int
// bitmap
seriesIDSet *tsdb.SeriesIDSet
}
func NewGridWithSingleTags(offset uint64, tags models.Tags, tagValuesSlice []*TagValues) *Grid {
g := &Grid{
offset: offset,
tagValuesSlice: tagValuesSlice,
tagKeys: []string{},
tagKeyToIndex: map[string]int{},
seriesIDSet: tsdb.NewSeriesIDSet(offset),
}
for i, tag := range tags {
g.tagKeyToIndex[string(tag.Key)] = i
g.tagKeys = append(g.tagKeys, string(tag.Key))
}
return g
}
func NewGridWithKeysAndValuesSlice(offset uint64, keys []string, tagValuesSlice []*TagValues, seriesIDSet *tsdb.SeriesIDSet) *Grid {
g := &Grid{
offset: offset,
tagValuesSlice: tagValuesSlice,
tagKeys: keys,
tagKeyToIndex: map[string]int{},
seriesIDSet: seriesIDSet,
}
for i, key := range keys {
g.tagKeyToIndex[key] = i
}
return g
}
func (g *Grid) HasTagKey(key string) bool {
if _, ok := g.tagKeyToIndex[key]; ok {
return true
}
return false
}
func (g *Grid) HasTagValue(key, value string) bool {
if index, ok := g.tagKeyToIndex[key]; !ok {
return false
} else {
values := g.tagValuesSlice[index]
if _, ok = values.valueToIndex[value]; !ok {
return false
} else {
return true
}
}
}
// getNumOfDimensions: return the number of tag keys inside
func (g *Grid) getNumOfDimensions() int {
return len(g.tagKeys)
}
// getCapacityOfIDs: the number of ids in this grid
func (g *Grid) getCapacityOfIDs() uint64 {
capacity := uint64(1)
for _, tagValues := range g.tagValuesSlice {
capacity *= tagValues.capacity
}
return capacity
}
func (g *Grid) tagValueExists(tag models.Tag) bool {
if tagValuesIndex, ok := g.tagKeyToIndex[string(tag.Key)]; !ok {
return false
} else {
if g.tagValuesSlice[tagValuesIndex].GetValueIndex(string(tag.Value)) == -1 {
return false
} else {
return true
}
}
}
// GetStrictlyMatchedIDForTags: return 0, false if not find it, else return id, true
func (g *Grid) GetStrictlyMatchedIDForTags(tags models.Tags) (uint64, bool) {
id, ok := g.GetStrictlyMatchedIDForTagsNoIDSet(tags)
if !ok || !g.seriesIDSet.Contains(id) {
return 0, false
}
return id, true
}
// GetStrictlyMatchedIDForTagsNoIDSet: Only check grid, ignore SeriesIdsSet, return 0, false if not find it, else return id, true
func (g *Grid) GetStrictlyMatchedIDForTagsNoIDSet(tags models.Tags) (uint64, bool) {
if len(tags) != g.getNumOfDimensions() {
return 0, false
}
ids := g.GetSeriesIDsWithTagsNoIDSet(tags)
if len(ids) == 0 {
return 0, false
}
return ids[0], true
}
// SetTags: return whether the insert succeed and the corresponding id.
// The returned bool represents whether the id will exist after calling, which means, 1) already exist and 2) insert successfully both return true.
// Only when tags matches and all have free slot, the insert succeeds.
func (g *Grid) SetTags(tags models.Tags) (uint64, bool) {
id, ok := g.GetStrictlyMatchedIDForTags(tags)
if ok {
return id, ok
}
if !g.ableToSetTags(tags) {
return 0, false
}
// get the tag pairs already exist, which means they don't need to be inserted
existedTagIndex := map[int]struct{}{}
for i, tag := range tags {
index, ok := g.tagKeyToIndex[string(tag.Key)]
// the tag key does not match
if !ok {
return 0, false
}
// if value already exist, no need to insert
tagValues := g.tagValuesSlice[index]
if tagValues.GetValueIndex(string(tag.Value)) != -1 {
existedTagIndex[i] = struct{}{}
}
}
// do the insert
for i, tag := range tags {
if _, ok := existedTagIndex[i]; ok {
continue
}
index := g.tagKeyToIndex[string(tag.Key)]
tagValues := g.tagValuesSlice[index]
tagValues.SetValue(string(tag.Value))
}
// calculate id
id, _ = g.GetStrictlyMatchedIDForTagsNoIDSet(tags)
g.seriesIDSet.Add(id)
return id, true
}
// ableToSetTags: returns whether tags could be inserted in grid.
// For grids could represent tags already, also return true.
func (g *Grid) ableToSetTags(tags models.Tags) bool {
// check each tag key matches and have free slot
// the tag key does not match
if len(tags) != g.getNumOfDimensions() {
return false
}
for _, tag := range tags {
index, ok := g.tagKeyToIndex[string(tag.Key)]
// the tag key does not match
if !ok {
return false
}
// if value already exist, no need to insert
tagValues := g.tagValuesSlice[index]
if tagValues.GetValueIndex(string(tag.Value)) != -1 {
continue
}
// no free slot
if g.tagKeyExistsAndFilledUp(string(tag.Key)) {
return false
}
}
return true
}
func (g *Grid) tagKeyExistsAndFilledUp(tagKey string) bool {
index, ok := g.tagKeyToIndex[tagKey]
// the tag key does not exist
if !ok {
return false
}
// filled up already
tagValues := g.tagValuesSlice[index]
return tagValues.capacity == uint64(len(tagValues.values))
}
func (g *Grid) GetSeriesIDSetForTags(tags models.Tags) *tsdb.SeriesIDSet {
ids := g.GetSeriesIDsWithTagsNoIDSet(tags)
idsSet := tsdb.NewSeriesIDSet(ids...)
return idsSet.And(g.seriesIDSet)
}
func (g *Grid) GetSeriesIDsWithTagsNoIDSet(tags models.Tags) []uint64 {
ids := []uint64{}
// check if tag pairs match
if len(tags) > g.getNumOfDimensions() {
return ids
}
for _, tag := range tags {
if !g.tagValueExists(tag) {
return ids
}
}
if len(tags) == 0 {
g.seriesIDSet.ForEach(func(id uint64) {
ids = append(ids, id)
})
return ids
}
indexes := make([]int, 0, g.getNumOfDimensions())
capacities := make([]uint64, 0, g.getNumOfDimensions())
for i := range g.tagKeys {
indexes = append(indexes, -1)
capacities = append(capacities, g.tagValuesSlice[i].capacity)
}
for _, tag := range tags {
idx := g.tagKeyToIndex[string(tag.Key)]
valueIdx := g.tagValuesSlice[idx].GetValueIndex(string(tag.Value))
indexes[idx] = valueIdx
}
prev := []uint64{}
if indexes[0] != -1 {
prev = append(prev, uint64(indexes[0]))
} else {
for i := uint64(0); i < capacities[0]; i++ {
prev = append(prev, i)
}
}
ids = VariableBaseConvert(indexes, capacities, 1, prev)
for i := range ids {
ids[i] += g.offset
}
return ids
}