-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcomment_cache.go
141 lines (126 loc) · 4.21 KB
/
comment_cache.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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package descmetadata
import (
"context"
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scdecomp"
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/errors"
)
type metadataCache struct {
txn *kv.Txn
ie sqlutil.InternalExecutor
comments map[scdecomp.CommentKey]string
objIDsChecked map[catid.DescID]struct{}
}
// Get implements the scdecomp.DescriptorCommentCache interface.
func (mf metadataCache) Get(
ctx context.Context, objID catid.DescID, subID descpb.ID, commentType keys.CommentType,
) (comment string, ok bool, err error) {
if _, checked := mf.objIDsChecked[objID]; !checked {
var descType catalog.DescriptorType
switch commentType {
case keys.DatabaseCommentType:
descType = catalog.Database
case keys.SchemaCommentType:
descType = catalog.Schema
case keys.TableCommentType, keys.ColumnCommentType, keys.IndexCommentType, keys.ConstraintCommentType:
descType = catalog.Table
}
if err := mf.LoadCommentsForObjects(ctx, descType, []catid.DescID{objID}); err != nil {
return "", false, err
}
}
key := scdecomp.CommentKey{
ObjectID: objID,
SubID: subID,
CommentType: commentType,
}
comment, ok = mf.comments[key]
return comment, ok, nil
}
// LoadCommentsForObjects implements the scdecomp.DescriptorCommentCache interface.
func (mf metadataCache) LoadCommentsForObjects(
ctx context.Context, descType catalog.DescriptorType, objIDs []descpb.ID,
) error {
var uncheckedObjIDs []catid.DescID
for _, id := range objIDs {
if _, ok := mf.objIDsChecked[id]; !ok {
uncheckedObjIDs = append(uncheckedObjIDs, id)
}
}
if len(uncheckedObjIDs) == 0 {
return nil
}
var commentTypes []keys.CommentType
switch descType {
case catalog.Database:
commentTypes = []keys.CommentType{keys.DatabaseCommentType}
case catalog.Schema:
commentTypes = []keys.CommentType{keys.SchemaCommentType}
case catalog.Table:
commentTypes = []keys.CommentType{
keys.TableCommentType, keys.ColumnCommentType, keys.IndexCommentType, keys.ConstraintCommentType,
}
default:
return errors.Errorf("unexpected descriptor type %q for fetching comment", descType)
}
var buf strings.Builder
_, _ = fmt.Fprintf(&buf, `SELECT type, object_id, sub_id, comment FROM system.comments WHERE type IN (%d`, commentTypes[0])
for _, typ := range commentTypes[1:] {
_, _ = fmt.Fprintf(&buf, ", %d", typ)
}
_, _ = fmt.Fprintf(&buf, `) AND object_id IN (%d`, uncheckedObjIDs[0])
for _, id := range uncheckedObjIDs[1:] {
_, _ = fmt.Fprintf(&buf, ", %d", id)
}
buf.WriteString(")")
rows, err := mf.ie.QueryBufferedEx(
ctx,
"mf-get-table-comments",
mf.txn,
sessiondata.InternalExecutorOverride{User: security.RootUserName()},
buf.String(),
)
if err != nil {
return err
}
for _, objID := range objIDs {
mf.objIDsChecked[objID] = struct{}{}
}
for _, row := range rows {
key := scdecomp.CommentKey{
ObjectID: catid.DescID(tree.MustBeDInt(row[1])),
SubID: descpb.ID(tree.MustBeDInt(row[2])),
CommentType: keys.CommentType(tree.MustBeDInt(row[0])),
}
mf.comments[key] = string(tree.MustBeDString(row[3]))
}
return nil
}
// NewCommentCache returns a new DescriptorCommentCache.
func NewCommentCache(txn *kv.Txn, ie sqlutil.InternalExecutor) scdecomp.DescriptorCommentCache {
return metadataCache{
txn: txn,
ie: ie,
comments: make(map[scdecomp.CommentKey]string),
objIDsChecked: make(map[catid.DescID]struct{}),
}
}