@@ -14,80 +14,61 @@ type schemaCaching struct {
14
14
}
15
15
16
16
func (s * schemaCaching ) GetReadQueriesForMetric (from , through model.Time , userID string , metricName string ) ([]IndexQuery , error ) {
17
- cFrom , cThrough , from , through := splitTimesByCacheability (from , through , model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ()))
18
-
19
- cacheableQueries , err := s .Schema .GetReadQueriesForMetric (cFrom , cThrough , userID , metricName )
20
- if err != nil {
21
- return nil , err
22
- }
23
-
24
- activeQueries , err := s .Schema .GetReadQueriesForMetric (from , through , userID , metricName )
25
- if err != nil {
26
- return nil , err
27
- }
28
-
29
- return mergeCacheableAndActiveQueries (cacheableQueries , activeQueries ), nil
17
+ return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
18
+ return s .Schema .GetReadQueriesForMetric (from , through , userID , metricName )
19
+ })
30
20
}
31
21
32
22
func (s * schemaCaching ) GetReadQueriesForMetricLabel (from , through model.Time , userID string , metricName string , labelName string ) ([]IndexQuery , error ) {
33
- cFrom , cThrough , from , through := splitTimesByCacheability (from , through , model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ()))
34
-
35
- cacheableQueries , err := s .Schema .GetReadQueriesForMetricLabel (cFrom , cThrough , userID , metricName , labelName )
36
- if err != nil {
37
- return nil , err
38
- }
39
-
40
- activeQueries , err := s .Schema .GetReadQueriesForMetricLabel (from , through , userID , metricName , labelName )
41
- if err != nil {
42
- return nil , err
43
- }
44
-
45
- return mergeCacheableAndActiveQueries (cacheableQueries , activeQueries ), nil
23
+ return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
24
+ return s .Schema .GetReadQueriesForMetricLabel (from , through , userID , metricName , labelName )
25
+ })
46
26
}
47
27
48
28
func (s * schemaCaching ) GetReadQueriesForMetricLabelValue (from , through model.Time , userID string , metricName string , labelName string , labelValue string ) ([]IndexQuery , error ) {
49
- cFrom , cThrough , from , through := splitTimesByCacheability (from , through , model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ()))
50
-
51
- cacheableQueries , err := s .Schema .GetReadQueriesForMetricLabelValue (cFrom , cThrough , userID , metricName , labelName , labelValue )
52
- if err != nil {
53
- return nil , err
54
- }
55
-
56
- activeQueries , err := s .Schema .GetReadQueriesForMetricLabelValue (from , through , userID , metricName , labelName , labelValue )
57
- if err != nil {
58
- return nil , err
59
- }
60
-
61
- return mergeCacheableAndActiveQueries (cacheableQueries , activeQueries ), nil
29
+ return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
30
+ return s .Schema .GetReadQueriesForMetricLabelValue (from , through , userID , metricName , labelName , labelValue )
31
+ })
62
32
}
63
33
64
34
// If the query resulted in series IDs, use this method to find chunks.
65
35
func (s * schemaCaching ) GetChunksForSeries (from , through model.Time , userID string , seriesID []byte ) ([]IndexQuery , error ) {
66
- cFrom , cThrough , from , through := splitTimesByCacheability (from , through , model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ()))
67
-
68
- cacheableQueries , err := s .Schema .GetChunksForSeries (cFrom , cThrough , userID , seriesID )
69
- if err != nil {
70
- return nil , err
71
- }
72
-
73
- activeQueries , err := s .Schema .GetChunksForSeries (from , through , userID , seriesID )
74
- if err != nil {
75
- return nil , err
76
- }
77
-
78
- return mergeCacheableAndActiveQueries (cacheableQueries , activeQueries ), nil
36
+ return s .splitTimesByCacheability (from , through , func (from , through model.Time ) ([]IndexQuery , error ) {
37
+ return s .Schema .GetChunksForSeries (from , through , userID , seriesID )
38
+ })
79
39
}
80
40
81
- func splitTimesByCacheability (from , through model.Time , cacheBefore model.Time ) (model.Time , model.Time , model.Time , model.Time ) {
41
+ func (s * schemaCaching ) splitTimesByCacheability (from , through model.Time , f func (from , through model.Time ) ([]IndexQuery , error )) ([]IndexQuery , error ) {
42
+ var (
43
+ cacheableQueries []IndexQuery
44
+ activeQueries []IndexQuery
45
+ err error
46
+ cacheBefore = model .TimeFromUnix (mtime .Now ().Add (- s .cacheOlderThan ).Unix ())
47
+ )
48
+
82
49
if from .After (cacheBefore ) {
83
- return 0 , 0 , from , through
84
- }
50
+ activeQueries , err = f (from , through )
51
+ if err != nil {
52
+ return nil , err
53
+ }
54
+ } else if through .Before (cacheBefore ) {
55
+ cacheableQueries , err = f (from , through )
56
+ if err != nil {
57
+ return nil , err
58
+ }
59
+ } else {
60
+ cacheableQueries , err = f (from , cacheBefore )
61
+ if err != nil {
62
+ return nil , err
63
+ }
85
64
86
- if through .Before (cacheBefore ) {
87
- return from , through , 0 , 0
65
+ activeQueries , err = f (cacheBefore , through )
66
+ if err != nil {
67
+ return nil , err
68
+ }
88
69
}
89
70
90
- return from , cacheBefore , cacheBefore , through
71
+ return mergeCacheableAndActiveQueries ( cacheableQueries , activeQueries ), nil
91
72
}
92
73
93
74
func mergeCacheableAndActiveQueries (cacheableQueries []IndexQuery , activeQueries []IndexQuery ) []IndexQuery {
0 commit comments