Skip to content

Commit

Permalink
nstree: Catalog method cleanup
Browse files Browse the repository at this point in the history
This commit renames existing methods and adds a few missing pieces for
the sake of consistency.

Release note: None
  • Loading branch information
Marius Posta committed Dec 13, 2022
1 parent 966463a commit 5dd8946
Show file tree
Hide file tree
Showing 32 changed files with 217 additions and 176 deletions.
14 changes: 7 additions & 7 deletions pkg/ccl/backupccl/restore_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ func (r *restoreResumer) publishDescriptors(
// Write the new TableDescriptors and flip state over to public so they can be
// accessed.
for i := range details.TableDescs {
mutTable := all.LookupDescriptorEntry(details.TableDescs[i].GetID()).(*tabledesc.Mutable)
mutTable := all.LookupDescriptor(details.TableDescs[i].GetID()).(*tabledesc.Mutable)
// Note that we don't need to worry about the re-validated indexes for descriptors
// with a declarative schema change job.
if mutTable.GetDeclarativeSchemaChangerState() != nil {
Expand Down Expand Up @@ -2132,7 +2132,7 @@ func (r *restoreResumer) publishDescriptors(
// For all of the newly created types, make type schema change jobs for any
// type descriptors that were backed up in the middle of a type schema change.
for i := range details.TypeDescs {
typ := all.LookupDescriptorEntry(details.TypeDescs[i].GetID()).(catalog.TypeDescriptor)
typ := all.LookupDescriptor(details.TypeDescs[i].GetID()).(catalog.TypeDescriptor)
newTypes = append(newTypes, typ.TypeDesc())
if typ.GetDeclarativeSchemaChangerState() == nil &&
typ.HasPendingSchemaChanges() {
Expand All @@ -2144,19 +2144,19 @@ func (r *restoreResumer) publishDescriptors(
}
}
for i := range details.SchemaDescs {
sc := all.LookupDescriptorEntry(details.SchemaDescs[i].GetID()).(catalog.SchemaDescriptor)
sc := all.LookupDescriptor(details.SchemaDescs[i].GetID()).(catalog.SchemaDescriptor)
newSchemas = append(newSchemas, sc.SchemaDesc())
}
for i := range details.DatabaseDescs {
db := all.LookupDescriptorEntry(details.DatabaseDescs[i].GetID()).(catalog.DatabaseDescriptor)
db := all.LookupDescriptor(details.DatabaseDescs[i].GetID()).(catalog.DatabaseDescriptor)
newDBs = append(newDBs, db.DatabaseDesc())
}
for i := range details.FunctionDescs {
fn := all.LookupDescriptorEntry(details.FunctionDescs[i].GetID()).(catalog.FunctionDescriptor)
fn := all.LookupDescriptor(details.FunctionDescs[i].GetID()).(catalog.FunctionDescriptor)
newFunctions = append(newFunctions, fn.FuncDesc())
}
b := txn.NewBatch()
if err := all.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
if err := all.ForEachDescriptor(func(desc catalog.Descriptor) error {
d := desc.(catalog.MutableDescriptor)
d.SetPublic()
return descsCol.WriteDescToBatch(
Expand Down Expand Up @@ -2250,7 +2250,7 @@ func prefetchDescriptors(
got[i].GetID(), got[i].GetVersion(), expVersion[id],
)
}
all.UpsertDescriptorEntry(got[i])
all.UpsertDescriptor(got[i])
}
return all.Catalog, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/ccl/backupccl/restore_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func maybeUpgradeDescriptors(descs []catalog.Descriptor, skipFKsWithNoMatchingTa
// A data structure for efficient descriptor lookup by ID or by name.
descCatalog := &nstree.MutableCatalog{}
for _, d := range descs {
descCatalog.UpsertDescriptorEntry(d)
descCatalog.UpsertDescriptor(d)
}

for j, desc := range descs {
Expand All @@ -781,7 +781,7 @@ func maybeUpgradeDescriptors(descs []catalog.Descriptor, skipFKsWithNoMatchingTa
if err := b.RunPostDeserializationChanges(); err != nil {
return errors.NewAssertionErrorWithWrappedErrf(err, "error during RunPostDeserializationChanges")
}
err := b.RunRestoreChanges(descCatalog.LookupDescriptorEntry)
err := b.RunRestoreChanges(descCatalog.LookupDescriptor)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/backupccl/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func backupShowerDefault(p sql.PlanHookState, showSchemas bool, opts tree.KVOpti
if len(hydratedDescriptors) == 0 {
var c nstree.MutableCatalog
for _, desc := range descriptors {
c.UpsertDescriptorEntry(desc)
c.UpsertDescriptor(desc)
}
if err := descs.HydrateCatalog(ctx, c); err != nil {
return "", err
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/catalog/dbdesc/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ func TestValidateCrossDatabaseReferences(t *testing.T) {
var cb nstree.MutableCatalog
test.desc.Privileges = privilege
desc := NewBuilder(&test.desc).BuildImmutable()
cb.UpsertDescriptorEntry(desc)
cb.UpsertDescriptor(desc)
test.multiRegionEnum.Privileges = privilege
cb.UpsertDescriptorEntry(typedesc.NewBuilder(&test.multiRegionEnum).BuildImmutable())
cb.UpsertDescriptor(typedesc.NewBuilder(&test.multiRegionEnum).BuildImmutable())
for _, schemaDesc := range test.schemaDescs {
schemaDesc.Privileges = privilege
cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&schemaDesc).BuildImmutable())
cb.UpsertDescriptor(schemadesc.NewBuilder(&schemaDesc).BuildImmutable())
}
_ = cb.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
_ = cb.ForEachDescriptor(func(desc catalog.Descriptor) error {
cb.UpsertNamespaceEntry(desc, desc.GetID(), desc.GetModificationTime())
return nil
})
Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/catalog/descs/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (tc *Collection) DeleteNamespaceEntryToBatch(
}

func (tc *Collection) markAsShadowedName(id descpb.ID) {
desc := tc.cr.Cache().LookupDescriptorEntry(id)
desc := tc.cr.Cache().LookupDescriptor(id)
if desc == nil {
return
}
Expand Down Expand Up @@ -741,7 +741,7 @@ func (tc *Collection) GetAllDescriptorsForDatabase(
var ret nstree.MutableCatalog
var functionIDs catalog.DescriptorIDSet
for _, desc := range descs {
ret.UpsertDescriptorEntry(desc)
ret.UpsertDescriptor(desc)
if sc, ok := desc.(catalog.SchemaDescriptor); ok {
_ = sc.ForEachFunctionOverload(func(overload descpb.SchemaDescriptor_FunctionOverload) error {
functionIDs.Add(overload.ID)
Expand All @@ -755,7 +755,7 @@ func (tc *Collection) GetAllDescriptorsForDatabase(
return nstree.Catalog{}, err
}
for _, desc := range descs {
ret.UpsertDescriptorEntry(desc)
ret.UpsertDescriptor(desc)
}
return ret.Catalog, nil
}
Expand All @@ -767,7 +767,7 @@ func (tc *Collection) GetAllDescriptors(ctx context.Context, txn *kv.Txn) (nstre
return nstree.Catalog{}, err
}
var ids catalog.DescriptorIDSet
_ = read.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
_ = read.ForEachDescriptor(func(desc catalog.Descriptor) error {
ids.Add(desc.GetID())
return nil
})
Expand All @@ -793,7 +793,7 @@ func (tc *Collection) GetAllDescriptors(ctx context.Context, txn *kv.Txn) (nstre
}
var ret nstree.MutableCatalog
for _, desc := range descs {
ret.UpsertDescriptorEntry(desc)
ret.UpsertDescriptor(desc)
}
return ret.Catalog, nil
}
Expand Down Expand Up @@ -821,7 +821,7 @@ func (tc *Collection) GetAllDatabaseDescriptors(
return nil, err
}
var m nstree.NameMap
_ = read.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
_ = read.ForEachDescriptor(func(desc catalog.Descriptor) error {
m.Upsert(desc, desc.SkipNamespace())
return nil
})
Expand Down Expand Up @@ -875,12 +875,12 @@ func (tc *Collection) GetAllTableDescriptorsInDatabase(
return nil, err
}
// Ensure the given ID does indeed belong to a database.
if desc := all.LookupDescriptorEntry(db.GetID()); desc == nil || desc.DescriptorType() != catalog.Database {
if desc := all.LookupDescriptor(db.GetID()); desc == nil || desc.DescriptorType() != catalog.Database {
return nil, sqlerrors.NewUndefinedDatabaseError(db.GetName())
}
dbID := db.GetID()
var ret []catalog.TableDescriptor
_ = all.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
_ = all.ForEachDescriptor(func(desc catalog.Descriptor) error {
if desc.GetParentID() != dbID {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/sql/catalog/descs/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ func TestDescriptorCache(t *testing.T) {
if err != nil {
return err
}
found := cat.LookupDescriptorEntry(mut.ID)
found := cat.LookupDescriptor(mut.ID)
require.NotEmpty(t, found)
require.Equal(t, mut.ImmutableCopy().DescriptorProto(), found.DescriptorProto())
return nil
Expand Down Expand Up @@ -999,21 +999,21 @@ func TestHydrateCatalog(t *testing.T) {
deleteDescriptor := func(name string) catalogTamperFn {
return func(cat nstree.Catalog) nstree.Catalog {
var descToDelete catid.DescID
_ = cat.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
_ = cat.ForEachDescriptor(func(desc catalog.Descriptor) error {
if desc.GetName() == name {
descToDelete = desc.GetID()
}
return nil
})
mutCat := nstree.MutableCatalog{Catalog: cat}
mutCat.DeleteDescriptorEntry(descToDelete)
mutCat.DeleteByID(descToDelete)
return mutCat.Catalog
}
}
replaceTypeDescWithNonTypeDesc := func(name string) catalogTamperFn {
return func(cat nstree.Catalog) nstree.Catalog {
var typeDescID catid.DescID
_ = cat.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
_ = cat.ForEachDescriptor(func(desc catalog.Descriptor) error {
if desc.GetName() == name {
typeDescID = desc.GetID()
}
Expand All @@ -1022,7 +1022,7 @@ func TestHydrateCatalog(t *testing.T) {
// Make a dummy database descriptor to replace the type descriptor.
dbDesc := dbdesc.NewBuilder(&descpb.DatabaseDescriptor{ID: typeDescID}).BuildImmutable()
mutCat := nstree.MutableCatalog{Catalog: cat}
mutCat.UpsertDescriptorEntry(dbDesc)
mutCat.UpsertDescriptor(dbDesc)
return mutCat.Catalog
}
}
Expand Down Expand Up @@ -1064,7 +1064,7 @@ func TestHydrateCatalog(t *testing.T) {
mc := nstree.MutableCatalog{Catalog: cat}
require.NoError(t, descs.HydrateCatalog(ctx, mc))
tbl := desctestutils.TestingGetTableDescriptor(txn.DB(), keys.SystemSQLCodec, "db", "schema", "table")
tblDesc := cat.LookupDescriptorEntry(tbl.GetID()).(catalog.TableDescriptor)
tblDesc := cat.LookupDescriptor(tbl.GetID()).(catalog.TableDescriptor)
expectedEnum := types.UserDefinedTypeMetadata{
Name: &types.UserDefinedTypeName{
Catalog: "db",
Expand Down Expand Up @@ -1152,7 +1152,7 @@ SELECT id
if err != nil {
return err
}
require.Equal(t, tabImm, all.LookupDescriptorEntry(tabImm.GetID()))
require.Equal(t, tabImm, all.LookupDescriptor(tabImm.GetID()))
return nil
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/catalog/descs/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (tc *Collection) GetComment(key catalogkeys.CommentKey) (string, bool) {
return cmt, hasCmt
}
if tc.cr.IsIDInCache(descpb.ID(key.ObjectID)) {
return tc.cr.Cache().LookupCommentEntry(key)
return tc.cr.Cache().LookupComment(key)
}
// TODO(chengxiong): we need to ensure descriptor if it's not in either cache
// and it's not a pseudo descriptor.
Expand Down Expand Up @@ -162,7 +162,7 @@ func (tc *Collection) GetZoneConfigs(
if err != nil {
return nil, err
}
_ = read.ForEachZoneConfigEntry(func(id descpb.ID, zc catalog.ZoneConfig) error {
_ = read.ForEachZoneConfig(func(id descpb.ID, zc catalog.ZoneConfig) error {
ret[id] = zc.Clone()
return nil
})
Expand Down Expand Up @@ -329,7 +329,7 @@ func getDescriptorsByID(
}
for i, id := range ids {
if descs[i] == nil {
descs[i] = read.LookupDescriptorEntry(id)
descs[i] = read.LookupDescriptor(id)
vls[i] = tc.validationLevels[id]
}
}
Expand Down Expand Up @@ -412,7 +412,7 @@ func (q *byIDLookupContext) lookupCached(
id descpb.ID,
) (catalog.Descriptor, catalog.ValidationLevel, error) {
if q.tc.cr.IsIDInCache(id) {
if desc := q.tc.cr.Cache().LookupDescriptorEntry(id); desc != nil {
if desc := q.tc.cr.Cache().LookupDescriptor(id); desc != nil {
return desc, q.tc.validationLevels[id], nil
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/catalog/descs/hydrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func makeMutableTypeLookupFunc(
if _, ok := desc.(catalog.MutableDescriptor); !ok {
continue
}
mut.UpsertDescriptorEntry(desc)
mut.UpsertDescriptor(desc)
}
mutableLookupFunc := func(ctx context.Context, id descpb.ID) (catalog.Descriptor, error) {
// This special case exists to deal with the desire to use enums in the
Expand Down Expand Up @@ -150,7 +150,7 @@ func makeImmutableTypeLookupFunc(
if _, ok := desc.(catalog.MutableDescriptor); ok {
continue
}
imm.UpsertDescriptorEntry(desc)
imm.UpsertDescriptor(desc)
}
immutableLookupFunc := func(ctx context.Context, id descpb.ID) (catalog.Descriptor, error) {
return tc.GetImmutableDescriptorByID(ctx, txn, id, tree.CommonLookupFlags{
Expand All @@ -173,7 +173,7 @@ func HydrateCatalog(ctx context.Context, c nstree.MutableCatalog) error {
return nil, catalog.WrapDescRefErr(id, catalog.ErrDescriptorNotFound)
}
typeLookupFunc := hydrateddesc.MakeTypeLookupFuncForHydration(c.Catalog, fakeLookupFunc)
return c.ForEachDescriptorEntry(func(desc catalog.Descriptor) error {
return c.ForEachDescriptor(func(desc catalog.Descriptor) error {
if !hydrateddesc.IsHydratable(desc) {
return nil
}
Expand All @@ -182,7 +182,7 @@ func HydrateCatalog(ctx context.Context, c nstree.MutableCatalog) error {
}
// Deep-copy the immutable descriptor and overwrite the catalog entry.
desc = desc.NewBuilder().BuildImmutable()
defer c.UpsertDescriptorEntry(desc)
defer c.UpsertDescriptor(desc)
return hydrateddesc.Hydrate(ctx, desc, typeLookupFunc)
})
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/sql/catalog/funcdesc/func_desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,42 @@ func TestValidateFuncDesc(t *testing.T) {
funcDescID := descpb.ID(bootstrap.TestingUserDescID(0))

var cb nstree.MutableCatalog
cb.UpsertDescriptorEntry(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{
cb.UpsertDescriptor(dbdesc.NewBuilder(&descpb.DatabaseDescriptor{
Name: "db",
ID: dbID,
}).BuildImmutable())
cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&descpb.SchemaDescriptor{
cb.UpsertDescriptor(schemadesc.NewBuilder(&descpb.SchemaDescriptor{
ID: schemaID,
ParentID: dbID,
Name: "schema",
}).BuildImmutable())
cb.UpsertDescriptorEntry(typedesc.NewBuilder(&descpb.TypeDescriptor{
cb.UpsertDescriptor(typedesc.NewBuilder(&descpb.TypeDescriptor{
ID: typeID,
Name: "type",
}).BuildImmutable())
cb.UpsertDescriptorEntry(tabledesc.NewBuilder(&descpb.TableDescriptor{
cb.UpsertDescriptor(tabledesc.NewBuilder(&descpb.TableDescriptor{
ID: tableID,
Name: "tbl",
}).BuildImmutable())
cb.UpsertDescriptorEntry(schemadesc.NewBuilder(&descpb.SchemaDescriptor{
cb.UpsertDescriptor(schemadesc.NewBuilder(&descpb.SchemaDescriptor{
ID: schemaWithFuncRefID,
ParentID: dbID,
Name: "schema",
Functions: map[string]descpb.SchemaDescriptor_Function{
"f": {Overloads: []descpb.SchemaDescriptor_FunctionOverload{{ID: funcDescID}}},
},
}).BuildImmutable())
cb.UpsertDescriptorEntry(typedesc.NewBuilder(&descpb.TypeDescriptor{
cb.UpsertDescriptor(typedesc.NewBuilder(&descpb.TypeDescriptor{
ID: typeWithFuncRefID,
Name: "type",
ReferencingDescriptorIDs: []descpb.ID{funcDescID},
}).BuildImmutable())
cb.UpsertDescriptorEntry(tabledesc.NewBuilder(&descpb.TableDescriptor{
cb.UpsertDescriptor(tabledesc.NewBuilder(&descpb.TableDescriptor{
ID: tableWithFuncBackRefID,
Name: "tbl",
DependedOnBy: []descpb.TableDescriptor_Reference{{ID: funcDescID}},
}).BuildImmutable())
cb.UpsertDescriptorEntry(tabledesc.NewBuilder(&descpb.TableDescriptor{
cb.UpsertDescriptor(tabledesc.NewBuilder(&descpb.TableDescriptor{
ID: tableWithFuncForwardRefID,
Name: "tbl",
DependsOn: []descpb.ID{funcDescID},
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/catalog/hydrateddesc/hydrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func MakeTypeLookupFuncForHydration(
c nstree.Catalog, lookupFn HydrationLookupFunc,
) typedesc.TypeLookupFunc {
return func(ctx context.Context, id descpb.ID) (tn tree.TypeName, typ catalog.TypeDescriptor, err error) {
typDesc := c.LookupDescriptorEntry(id)
typDesc := c.LookupDescriptor(id)
if typDesc == nil {
typDesc, err = lookupFn(ctx, id)
if err != nil {
Expand All @@ -89,7 +89,7 @@ func MakeTypeLookupFuncForHydration(
if err != nil {
return tree.TypeName{}, nil, err
}
dbDesc := c.LookupDescriptorEntry(typ.GetParentID())
dbDesc := c.LookupDescriptor(typ.GetParentID())
if dbDesc == nil {
dbDesc, err = lookupFn(ctx, typ.GetParentID())
if err != nil {
Expand All @@ -103,7 +103,7 @@ func MakeTypeLookupFuncForHydration(
if _, err = catalog.AsDatabaseDescriptor(dbDesc); err != nil {
return tree.TypeName{}, nil, err
}
scDesc := c.LookupDescriptorEntry(typ.GetParentSchemaID())
scDesc := c.LookupDescriptor(typ.GetParentSchemaID())
if scDesc == nil {
scDesc, err = lookupFn(ctx, typ.GetParentSchemaID())
if err != nil {
Expand Down
Loading

0 comments on commit 5dd8946

Please sign in to comment.