-
Notifications
You must be signed in to change notification settings - Fork 14
/
index_bucket.go
81 lines (70 loc) · 2.31 KB
/
index_bucket.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
package query_cache
import (
"log"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
// IndexBucket contains index items for all cache results for a given table and connection
type IndexBucket struct {
Items []*IndexItem
}
func newIndexBucket() *IndexBucket {
return &IndexBucket{}
}
func (b *IndexBucket) Append(item *IndexItem) *IndexBucket {
b.Items = append(b.Items, item)
return b
}
// Get finds an index item which satisfies all columns
func (b *IndexBucket) Get(req *CacheRequest, keyColumns map[string]*proto.KeyColumn) *IndexItem {
log.Printf("[TRACE] IndexBucket.Get %d items", len(b.Items))
for _, item := range b.Items {
log.Printf("[TRACE] IndexBucket.Get key %s limit %d (%s)", item.Key, item.Limit, req.CallId)
satisfiedRequest := item.satisfiesRequest(req.Columns, req.Limit, req.QualMap, keyColumns)
satisfiesTtl := item.satisfiesTtl(req.TtlSeconds)
log.Printf("[TRACE] satisfiedRequest: %v, satisfiesTtl: %v ttlSec: %d (%s)", satisfiedRequest, satisfiesTtl, req.TtlSeconds, req.CallId)
if satisfiedRequest && satisfiesTtl {
log.Printf("[TRACE] IndexBucket.Get CACHE HIT %d items", len(b.Items))
return item
}
}
// quals debug
//log.Printf("[TRACE] IndexBucket.Get CACHE MISS %d items", len(b.Items))
//log.Printf("[TRACE] req QUALS: %s", grpc.QualMapToLogLine(req.QualMap))
//for _, item := range b.Items {
// log.Printf("[TRACE] item QUALS: %s", grpc.QualMapToLogLine(item.Quals))
//}
return nil
}
func (b *IndexBucket) AsProto() *proto.IndexBucket {
res := &proto.IndexBucket{
Items: make([]*proto.IndexItem, len(b.Items)),
}
for i, item := range b.Items {
res.Items[i] = &proto.IndexItem{
Key: item.Key,
Quals: item.Quals,
Columns: item.Columns,
Limit: item.Limit,
PageCount: item.PageCount,
InsertionTime: timestamppb.New(item.InsertionTime),
}
}
return res
}
func IndexBucketfromProto(b *proto.IndexBucket) *IndexBucket {
res := &IndexBucket{
Items: make([]*IndexItem, len(b.Items)),
}
for i, item := range b.Items {
res.Items[i] = &IndexItem{
Key: item.Key,
Quals: item.Quals,
Columns: item.Columns,
Limit: item.Limit,
PageCount: item.PageCount,
InsertionTime: item.InsertionTime.AsTime(),
}
}
return res
}