@@ -90,6 +90,8 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
90
90
itergen := itergens [0 ]
91
91
ts := itergen .Ts
92
92
93
+ addKeysDirect := len (mc .keys ) == 0 || mc .keys [len (mc .keys )- 1 ] < ts
94
+
93
95
// if previous chunk has not been passed we try to be smart and figure it out.
94
96
// this is common in a scenario where a metric continuously gets queried
95
97
// for a range that starts less than one chunkspan before now().
@@ -115,6 +117,9 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
115
117
Next : itergens [1 ].Ts ,
116
118
Itgen : itergen ,
117
119
}
120
+ if addKeysDirect {
121
+ mc .keys = append (mc .keys , ts )
122
+ }
118
123
}
119
124
120
125
prev = ts
@@ -131,6 +136,9 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
131
136
Next : itergens [i + 1 ].Ts ,
132
137
Itgen : itergen ,
133
138
}
139
+ if addKeysDirect {
140
+ mc .keys = append (mc .keys , ts )
141
+ }
134
142
}
135
143
prev = ts
136
144
}
@@ -160,9 +168,15 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
160
168
Next : next ,
161
169
Itgen : itergen ,
162
170
}
171
+ if addKeysDirect {
172
+ mc .keys = append (mc .keys , ts )
173
+ }
174
+ }
175
+
176
+ if ! addKeysDirect {
177
+ // regenerate the list of sorted keys
178
+ mc .generateKeys ()
163
179
}
164
- // regenerate the list of sorted keys after adding a chunk
165
- mc .generateKeys ()
166
180
167
181
return
168
182
}
@@ -215,12 +229,29 @@ func (mc *CCacheMetric) Add(prev uint32, itergen chunk.IterGen) {
215
229
}
216
230
}
217
231
218
- // regenerate the list of sorted keys after adding a chunk
219
- mc .generateKeys ()
232
+ mc .addKey (ts )
220
233
221
234
return
222
235
}
223
236
237
+ func (mc * CCacheMetric ) addKey (ts uint32 ) {
238
+
239
+ // if no keys yet, just add it and it's sorted
240
+ if len (mc .keys ) == 0 {
241
+ mc .keys = append (mc .keys , ts )
242
+ return
243
+ }
244
+
245
+ // if ts is newer than any previous chunk, can just add to the back
246
+ if mc .keys [len (mc .keys )- 1 ] < ts {
247
+ mc .keys = append (mc .keys , ts )
248
+ return
249
+ }
250
+
251
+ // we have to insert it in the middle, needs to be re-generated
252
+ mc .generateKeys ()
253
+ }
254
+
224
255
// generateKeys generates sorted slice of all chunk timestamps
225
256
// assumes we have at least read lock
226
257
func (mc * CCacheMetric ) generateKeys () {
0 commit comments