@@ -35,6 +35,9 @@ type CatalogReader interface {
3535 // is known to be in the cache.
3636 IsIDInCache (id descpb.ID ) bool
3737
38+ // IsCommentInCache return true when the comment is cached for this descriptor.
39+ IsCommentInCache (id descpb.ID ) bool
40+
3841 // IsNameInCache return true when all the by-name catalog data for this name
3942 // key is known to be in the cache.
4043 IsNameInCache (key descpb.NameInfo ) bool
@@ -85,13 +88,15 @@ type CatalogReader interface {
8588
8689 // GetByIDs reads the system.descriptor, system.comments and system.zone
8790 // entries for the desired IDs, but looks in the system database cache
88- // first if there is one.
91+ // first if there is one. opts can be used to control which information should
92+ // be read, where the default is to WithDescriptor(true) and WithMetaData(true).
8993 GetByIDs (
9094 ctx context.Context ,
9195 txn * kv.Txn ,
9296 ids []descpb.ID ,
9397 isDescriptorRequired bool ,
9498 expectedType catalog.DescriptorType ,
99+ opts ... GetByIDOption ,
95100 ) (nstree.Catalog , error )
96101
97102 // GetByNames reads the system.namespace entries for the given keys, but
@@ -109,6 +114,51 @@ func NewUncachedCatalogReader(codec keys.SQLCodec) CatalogReader {
109114 }
110115}
111116
117+ // getByIDOptions options supported by GetByID.
118+ type getByIDOptions struct {
119+ withDescriptor bool
120+ withZoneConfig bool
121+ withComments bool
122+ }
123+
124+ // defaultGetByIDOptions are the default options for GetByID.
125+ var defaultGetByIDOptions = []GetByIDOption {WithDescriptor (true ), WithMetaData (true )}
126+
127+ // withDefaultOptions are the default options for GetByID.
128+ func withDefaultOptions () []GetByIDOption {
129+ return defaultGetByIDOptions
130+ }
131+
132+ // GetByIDOption are options that GetByID supports.
133+ type GetByIDOption interface {
134+ apply (* getByIDOptions )
135+ }
136+
137+ func WithMetaData (b bool ) GetByIDOption {
138+ return getByIDWithMetaData (b )
139+ }
140+
141+ func WithDescriptor (b bool ) GetByIDOption {
142+ return getByIDWithDescriptor (b )
143+ }
144+
145+ // getByIDWithMetaData determines if metadata should be fetched.
146+ type getByIDWithMetaData bool
147+
148+ // apply implements GetByIDOption.
149+ func (g getByIDWithMetaData ) apply (o * getByIDOptions ) {
150+ o .withComments = bool (g )
151+ o .withZoneConfig = bool (g )
152+ }
153+
154+ // getByIDWithDescriptor determines if the descriptor should be fetched.
155+ type getByIDWithDescriptor bool
156+
157+ // apply implements GetByIDOption.
158+ func (g getByIDWithDescriptor ) apply (o * getByIDOptions ) {
159+ o .withDescriptor = bool (g )
160+ }
161+
112162// catalogReader implements the CatalogReader interface by building catalogQuery
113163// objects and running them, leveraging the SystemDatabaseCache if present.
114164type catalogReader struct {
@@ -135,6 +185,11 @@ func (cr catalogReader) IsIDInCache(_ descpb.ID) bool {
135185 return false
136186}
137187
188+ // IsCommentInCache is part of the CatalogReader interface.
189+ func (cr catalogReader ) IsCommentInCache (_ descpb.ID ) bool {
190+ return false
191+ }
192+
138193// IsNameInCache is part of the CatalogReader interface.
139194func (cr catalogReader ) IsNameInCache (_ descpb.NameInfo ) bool {
140195 return false
@@ -342,11 +397,19 @@ func (cr catalogReader) GetByIDs(
342397 ids []descpb.ID ,
343398 isDescriptorRequired bool ,
344399 expectedType catalog.DescriptorType ,
400+ opts ... GetByIDOption ,
345401) (nstree.Catalog , error ) {
346402 var mc nstree.MutableCatalog
347403 if len (ids ) == 0 {
348404 return nstree.Catalog {}, nil
349405 }
406+ var options getByIDOptions
407+ if len (opts ) == 0 {
408+ opts = withDefaultOptions ()
409+ }
410+ for _ , opt := range opts {
411+ opt .apply (& options )
412+ }
350413 cq := catalogQuery {
351414 codec : cr .codec ,
352415 isDescriptorRequired : isDescriptorRequired ,
@@ -359,22 +422,34 @@ func (cr catalogReader) GetByIDs(
359422 forEachDescriptorIDSpan (ids , func (startID descpb.ID , endID descpb.ID ) {
360423 // Only a single descriptor run, so generate a Get request.
361424 if startID == endID {
362- get (ctx , b , catalogkeys .MakeDescMetadataKey (codec , startID ))
363- for _ , t := range catalogkeys .AllCommentTypes {
364- scan (ctx , b , catalogkeys .MakeObjectCommentsMetadataPrefix (codec , t , startID ))
425+ if options .withDescriptor {
426+ get (ctx , b , catalogkeys .MakeDescMetadataKey (codec , startID ))
427+ }
428+ if options .withComments {
429+ for _ , t := range catalogkeys .AllCommentTypes {
430+ scan (ctx , b , catalogkeys .MakeObjectCommentsMetadataPrefix (codec , t , startID ))
431+ }
432+ }
433+ if options .withZoneConfig {
434+ get (ctx , b , config .MakeZoneKey (codec , startID ))
365435 }
366- get (ctx , b , config .MakeZoneKey (codec , startID ))
367436 } else {
368437 // Otherwise, generate a Scan request instead. The end key is exclusive,
369438 // so we will need to increment the endID.
370- scanRange (ctx , b , catalogkeys .MakeDescMetadataKey (codec , startID ),
371- catalogkeys .MakeDescMetadataKey (codec , endID + 1 ))
372- for _ , t := range catalogkeys .AllCommentTypes {
373- scanRange (ctx , b , catalogkeys .MakeObjectCommentsMetadataPrefix (codec , t , startID ),
374- catalogkeys .MakeObjectCommentsMetadataPrefix (codec , t , endID + 1 ))
439+ if options .withDescriptor {
440+ scanRange (ctx , b , catalogkeys .MakeDescMetadataKey (codec , startID ),
441+ catalogkeys .MakeDescMetadataKey (codec , endID + 1 ))
442+ }
443+ if options .withComments {
444+ for _ , t := range catalogkeys .AllCommentTypes {
445+ scanRange (ctx , b , catalogkeys .MakeObjectCommentsMetadataPrefix (codec , t , startID ),
446+ catalogkeys .MakeObjectCommentsMetadataPrefix (codec , t , endID + 1 ))
447+ }
448+ }
449+ if options .withZoneConfig {
450+ scanRange (ctx , b , config .MakeZoneKey (codec , startID ),
451+ config .MakeZoneKey (codec , endID + 1 ))
375452 }
376- scanRange (ctx , b , config .MakeZoneKey (codec , startID ),
377- config .MakeZoneKey (codec , endID + 1 ))
378453 }
379454 })
380455 })
0 commit comments