From a6f98a7dae5d729a63205c582c79156020e27bd8 Mon Sep 17 00:00:00 2001 From: Vivek Menezes Date: Tue, 14 May 2019 11:22:53 -0400 Subject: [PATCH] sql: move databaseKey and tableKey to sqlbase Release note: None --- pkg/sql/backfill.go | 2 +- pkg/sql/crdb_internal.go | 26 +++++++++++++ pkg/sql/create_sequence.go | 8 +--- pkg/sql/create_table.go | 2 +- pkg/sql/create_view.go | 2 +- pkg/sql/database.go | 21 ++--------- pkg/sql/descriptor.go | 2 +- pkg/sql/helpers_test.go | 6 +++ pkg/sql/lease.go | 3 +- pkg/sql/physical_schema_accessors.go | 4 +- pkg/sql/rename_table.go | 2 +- pkg/sql/schema_changer.go | 2 +- pkg/sql/sqlbase/structured.go | 41 +++++++++++++++++++++ pkg/sql/table.go | 55 ---------------------------- pkg/sql/truncate.go | 7 ++-- 15 files changed, 92 insertions(+), 91 deletions(-) diff --git a/pkg/sql/backfill.go b/pkg/sql/backfill.go index 8af0193b288b..6d2e0c7e4f52 100644 --- a/pkg/sql/backfill.go +++ b/pkg/sql/backfill.go @@ -1097,7 +1097,7 @@ func runSchemaChangesInTxn( // cleanup for later. b := txn.NewBatch() for _, drain := range tableDesc.DrainingNames { - tbKey := tableKey{drain.ParentID, drain.Name}.Key() + tbKey := sqlbase.NewTableKey(drain.ParentID, drain.Name).Key() b.Del(tbKey) } tableDesc.DrainingNames = nil diff --git a/pkg/sql/crdb_internal.go b/pkg/sql/crdb_internal.go index 4bdce8f08850..95f76ae8ae74 100644 --- a/pkg/sql/crdb_internal.go +++ b/pkg/sql/crdb_internal.go @@ -1802,6 +1802,32 @@ CREATE TABLE crdb_internal.ranges_no_leases ( }, } +type namespaceKey struct { + parentID sqlbase.ID + name string +} + +// getAllNames returns a map from ID to namespaceKey for every entry in +// system.namespace. +func (p *planner) getAllNames(ctx context.Context) (map[sqlbase.ID]namespaceKey, error) { + namespace := map[sqlbase.ID]namespaceKey{} + rows, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.Query( + ctx, "get-all-names", p.txn, + `SELECT id, "parentID", name FROM system.namespace`, + ) + if err != nil { + return nil, err + } + for _, r := range rows { + id, parentID, name := tree.MustBeDInt(r[0]), tree.MustBeDInt(r[1]), tree.MustBeDString(r[2]) + namespace[sqlbase.ID(id)] = namespaceKey{ + parentID: sqlbase.ID(parentID), + name: string(name), + } + } + return namespace, nil +} + // crdbInternalZonesTable decodes and exposes the zone configs in the // system.zones table. // The cli_specifier column is deprecated and only exists to be used diff --git a/pkg/sql/create_sequence.go b/pkg/sql/create_sequence.go index 1d2636ddfb9a..fa27ac9ab400 100644 --- a/pkg/sql/create_sequence.go +++ b/pkg/sql/create_sequence.go @@ -49,7 +49,7 @@ func (p *planner) CreateSequence(ctx context.Context, n *tree.CreateSequence) (p } func (n *createSequenceNode) startExec(params runParams) error { - tKey := getSequenceKey(n.dbDesc, n.n.Name.Table()) + tKey := sqlbase.NewTableKey(n.dbDesc.ID, n.n.Name.Table()) if exists, err := descExists(params.ctx, params.p.txn, tKey.Key()); err == nil && exists { if n.n.IfNotExists { // If the sequence exists but the user specified IF NOT EXISTS, return without doing anything. @@ -63,10 +63,6 @@ func (n *createSequenceNode) startExec(params runParams) error { return doCreateSequence(params, n.n.String(), n.dbDesc, &n.n.Name, n.n.Options) } -func getSequenceKey(dbDesc *DatabaseDescriptor, seqName string) tableKey { - return tableKey{parentID: dbDesc.ID, name: seqName} -} - // doCreateSequence performs the creation of a sequence in KV. The // context argument is a string to use in the event log. func doCreateSequence( @@ -93,7 +89,7 @@ func doCreateSequence( // makeSequenceTableDesc already validates the table. No call to // desc.ValidateTable() needed here. - key := getSequenceKey(dbDesc, name.Table()).Key() + key := sqlbase.NewTableKey(dbDesc.ID, name.Table()).Key() if err = params.p.createDescriptorWithID(params.ctx, key, id, &desc, params.EvalContext().Settings); err != nil { return err } diff --git a/pkg/sql/create_table.go b/pkg/sql/create_table.go index d108885b732a..78bdac26185e 100644 --- a/pkg/sql/create_table.go +++ b/pkg/sql/create_table.go @@ -104,7 +104,7 @@ type createTableRun struct { } func (n *createTableNode) startExec(params runParams) error { - tKey := tableKey{parentID: n.dbDesc.ID, name: n.n.Table.Table()} + tKey := sqlbase.NewTableKey(n.dbDesc.ID, n.n.Table.Table()) key := tKey.Key() if exists, err := descExists(params.ctx, params.p.txn, key); err == nil && exists { if n.n.IfNotExists { diff --git a/pkg/sql/create_view.go b/pkg/sql/create_view.go index c27de308ff2a..4b6e44a3a107 100644 --- a/pkg/sql/create_view.go +++ b/pkg/sql/create_view.go @@ -110,7 +110,7 @@ func (p *planner) CreateView(ctx context.Context, n *tree.CreateView) (planNode, func (n *createViewNode) startExec(params runParams) error { viewName := n.n.Name.Table() - tKey := tableKey{parentID: n.dbDesc.ID, name: viewName} + tKey := sqlbase.NewTableKey(n.dbDesc.ID, viewName) key := tKey.Key() if exists, err := descExists(params.ctx, params.p.txn, key); err == nil && exists { // TODO(a-robinson): Support CREATE OR REPLACE commands. diff --git a/pkg/sql/database.go b/pkg/sql/database.go index 4eada04c68be..a21f5d04acd1 100644 --- a/pkg/sql/database.go +++ b/pkg/sql/database.go @@ -37,19 +37,6 @@ import ( // suitable; consider instead schema_accessors.go and resolver.go. // -// databaseKey implements sqlbase.DescriptorKey. -type databaseKey struct { - name string -} - -func (dk databaseKey) Key() roachpb.Key { - return sqlbase.MakeNameMetadataKey(keys.RootNamespaceID, dk.name) -} - -func (dk databaseKey) Name() string { - return dk.name -} - // databaseCache holds a cache from database name to database ID. It is // populated as database IDs are requested and a new cache is created whenever // the system config changes. As such, no attempt is made to limit its size @@ -108,7 +95,7 @@ func getDatabaseID( if name == sqlbase.SystemDB.Name { return sqlbase.SystemDB.ID, nil } - dbID, err := getDescriptorID(ctx, txn, databaseKey{name}) + dbID, err := getDescriptorID(ctx, txn, sqlbase.NewDatabaseKey(name)) if err != nil { return sqlbase.InvalidID, err } @@ -273,7 +260,7 @@ func (dc *databaseCache) getCachedDatabaseID(name string) (sqlbase.ID, error) { return sqlbase.SystemDB.ID, nil } - nameKey := databaseKey{name} + nameKey := sqlbase.NewDatabaseKey(name) nameVal := dc.systemConfig.GetValue(nameKey.Key()) if nameVal == nil { return sqlbase.InvalidID, nil @@ -293,8 +280,8 @@ func (p *planner) renameDatabase( return err } - oldKey := databaseKey{oldName}.Key() - newKey := databaseKey{newName}.Key() + oldKey := sqlbase.NewDatabaseKey(oldName).Key() + newKey := sqlbase.NewDatabaseKey(newName).Key() descID := oldDesc.GetID() descKey := sqlbase.MakeDescMetadataKey(descID) descDesc := sqlbase.WrapDescriptor(oldDesc) diff --git a/pkg/sql/descriptor.go b/pkg/sql/descriptor.go index 266ca1cb4912..543be2117521 100644 --- a/pkg/sql/descriptor.go +++ b/pkg/sql/descriptor.go @@ -62,7 +62,7 @@ func GenerateUniqueDescID(ctx context.Context, db *client.DB) (sqlbase.ID, error func (p *planner) createDatabase( ctx context.Context, desc *sqlbase.DatabaseDescriptor, ifNotExists bool, ) (bool, error) { - plainKey := databaseKey{desc.Name} + plainKey := sqlbase.NewDatabaseKey(desc.Name) idKey := plainKey.Key() if exists, err := descExists(ctx, p.txn, idKey); err == nil && exists { diff --git a/pkg/sql/helpers_test.go b/pkg/sql/helpers_test.go index 26eae7d7670e..d5fde2647ce1 100644 --- a/pkg/sql/helpers_test.go +++ b/pkg/sql/helpers_test.go @@ -23,6 +23,12 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/syncutil" ) +// A unique id for a particular table descriptor version. +type tableVersionID struct { + id sqlbase.ID + version sqlbase.DescriptorVersion +} + // LeaseRemovalTracker can be used to wait for leases to be removed from the // store (leases are removed from the store async w.r.t. LeaseManager // operations). diff --git a/pkg/sql/lease.go b/pkg/sql/lease.go index 464f1808bef3..8f40c429037d 100644 --- a/pkg/sql/lease.go +++ b/pkg/sql/lease.go @@ -1489,8 +1489,7 @@ func (m *LeaseManager) AcquireByName( func (m *LeaseManager) resolveName( ctx context.Context, timestamp hlc.Timestamp, dbID sqlbase.ID, tableName string, ) (sqlbase.ID, error) { - nameKey := tableKey{dbID, tableName} - key := nameKey.Key() + key := sqlbase.NewTableKey(dbID, tableName).Key() id := sqlbase.InvalidID if err := m.execCfg.DB.Txn(ctx, func(ctx context.Context, txn *client.Txn) error { txn.SetFixedTimestamp(ctx, timestamp) diff --git a/pkg/sql/physical_schema_accessors.go b/pkg/sql/physical_schema_accessors.go index d95e1f3ba857..43975a873564 100644 --- a/pkg/sql/physical_schema_accessors.go +++ b/pkg/sql/physical_schema_accessors.go @@ -58,7 +58,7 @@ func (a UncachedPhysicalAccessor) GetDatabaseDesc( return &sysDB, nil } - descID, err := getDescriptorID(ctx, txn, databaseKey{name}) + descID, err := getDescriptorID(ctx, txn, sqlbase.NewDatabaseKey(name)) if err != nil { return nil, err } @@ -146,7 +146,7 @@ func (a UncachedPhysicalAccessor) GetObjectDesc( // can be modified on a running cluster. descID := sqlbase.LookupSystemTableDescriptorID(dbID, name.Table()) if descID == sqlbase.InvalidID { - descID, err = getDescriptorID(ctx, txn, tableKey{parentID: dbID, name: name.Table()}) + descID, err = getDescriptorID(ctx, txn, sqlbase.NewTableKey(dbID, name.Table())) if err != nil { return nil, err } diff --git a/pkg/sql/rename_table.go b/pkg/sql/rename_table.go index 8ba9ca4c159a..6b995ef53ad3 100644 --- a/pkg/sql/rename_table.go +++ b/pkg/sql/rename_table.go @@ -111,7 +111,7 @@ func (n *renameTableNode) startExec(params runParams) error { tableDesc.ParentID = targetDbDesc.ID descKey := sqlbase.MakeDescMetadataKey(tableDesc.GetID()) - newTbKey := tableKey{targetDbDesc.ID, newTn.Table()}.Key() + newTbKey := sqlbase.NewTableKey(targetDbDesc.ID, newTn.Table()).Key() if err := tableDesc.Validate(ctx, p.txn, p.EvalContext().Settings); err != nil { return err diff --git a/pkg/sql/schema_changer.go b/pkg/sql/schema_changer.go index 6cfa4d1cee46..835a157e7249 100644 --- a/pkg/sql/schema_changer.go +++ b/pkg/sql/schema_changer.go @@ -763,7 +763,7 @@ func (sc *SchemaChanger) drainNames(ctx context.Context) error { func(txn *client.Txn) error { b := txn.NewBatch() for _, drain := range namesToReclaim { - tbKey := tableKey{drain.ParentID, drain.Name}.Key() + tbKey := sqlbase.NewTableKey(drain.ParentID, drain.Name).Key() b.Del(tbKey) } diff --git a/pkg/sql/sqlbase/structured.go b/pkg/sql/sqlbase/structured.go index 9332ca4a3dc8..4e13ef5a0489 100644 --- a/pkg/sql/sqlbase/structured.go +++ b/pkg/sql/sqlbase/structured.go @@ -3033,3 +3033,44 @@ func (desc *MutableTableDescriptor) TableDesc() *TableDescriptor { func (desc *ImmutableTableDescriptor) TableDesc() *TableDescriptor { return &desc.TableDescriptor } + +// DatabaseKey implements DescriptorKey. +type DatabaseKey struct { + name string +} + +// NewDatabaseKey returns a new DatabaseKey. +func NewDatabaseKey(name string) DatabaseKey { + return DatabaseKey{name} +} + +// Key implements DescriptorKey interface. +func (dk DatabaseKey) Key() roachpb.Key { + return MakeNameMetadataKey(keys.RootNamespaceID, dk.name) +} + +// Name implements DescriptorKey interface. +func (dk DatabaseKey) Name() string { + return dk.name +} + +// TableKey implements DescriptorKey interface. +type TableKey struct { + parentID ID + name string +} + +// NewTableKey returns a new TableKey. +func NewTableKey(parentID ID, name string) TableKey { + return TableKey{parentID, name} +} + +// Key implements DescriptorKey interface. +func (tk TableKey) Key() roachpb.Key { + return MakeNameMetadataKey(tk.parentID, tk.name) +} + +// Name implements DescriptorKey interface. +func (tk TableKey) Name() string { + return tk.name +} diff --git a/pkg/sql/table.go b/pkg/sql/table.go index d642b417b219..c148352ad29c 100644 --- a/pkg/sql/table.go +++ b/pkg/sql/table.go @@ -19,7 +19,6 @@ import ( "fmt" "strings" - "github.com/cockroachdb/cockroach/pkg/config" "github.com/cockroachdb/cockroach/pkg/internal/client" "github.com/cockroachdb/cockroach/pkg/jobs" "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" @@ -50,60 +49,6 @@ func TestDisableTableLeases() func() { } } -type namespaceKey struct { - parentID sqlbase.ID - name string -} - -// getAllNames returns a map from ID to namespaceKey for every entry in -// system.namespace. -func (p *planner) getAllNames(ctx context.Context) (map[sqlbase.ID]namespaceKey, error) { - namespace := map[sqlbase.ID]namespaceKey{} - rows, err := p.ExtendedEvalContext().ExecCfg.InternalExecutor.Query( - ctx, "get-all-names", p.txn, - `SELECT id, "parentID", name FROM system.namespace`, - ) - if err != nil { - return nil, err - } - for _, r := range rows { - id, parentID, name := tree.MustBeDInt(r[0]), tree.MustBeDInt(r[1]), tree.MustBeDString(r[2]) - namespace[sqlbase.ID(id)] = namespaceKey{ - parentID: sqlbase.ID(parentID), - name: string(name), - } - } - return namespace, nil -} - -// tableKey implements sqlbase.DescriptorKey. -type tableKey namespaceKey - -func (tk tableKey) Key() roachpb.Key { - return sqlbase.MakeNameMetadataKey(tk.parentID, tk.name) -} - -func (tk tableKey) Name() string { - return tk.name -} - -// GetKeysForTableDescriptor retrieves the KV keys corresponding -// to the zone, name and descriptor of a table. -func GetKeysForTableDescriptor( - tableDesc *sqlbase.TableDescriptor, -) (zoneKey roachpb.Key, nameKey roachpb.Key, descKey roachpb.Key) { - zoneKey = config.MakeZoneKey(uint32(tableDesc.ID)) - nameKey = sqlbase.MakeNameMetadataKey(tableDesc.ParentID, tableDesc.GetName()) - descKey = sqlbase.MakeDescMetadataKey(tableDesc.ID) - return -} - -// A unique id for a particular table descriptor version. -type tableVersionID struct { - id sqlbase.ID - version sqlbase.DescriptorVersion -} - func (p *planner) getVirtualTabler() VirtualTabler { return p.extendedEvalCtx.VirtualSchemas } diff --git a/pkg/sql/truncate.go b/pkg/sql/truncate.go index 4a2b7f059292..ecd6b67ef878 100644 --- a/pkg/sql/truncate.go +++ b/pkg/sql/truncate.go @@ -16,6 +16,7 @@ package sql import ( "context" + "github.com/cockroachdb/cockroach/pkg/config" "github.com/cockroachdb/cockroach/pkg/internal/client" "github.com/cockroachdb/cockroach/pkg/jobs/jobspb" @@ -210,7 +211,8 @@ func (p *planner) truncateTable( // structured.proto // // TODO(vivek): Fix properly along with #12123. - zoneKey, nameKey, _ := GetKeysForTableDescriptor(tableDesc.TableDesc()) + zoneKey := config.MakeZoneKey(uint32(tableDesc.ID)) + nameKey := sqlbase.MakeNameMetadataKey(tableDesc.ParentID, tableDesc.GetName()) b := &client.Batch{} // Use CPut because we want to remove a specific name -> id map. if traceKV { @@ -267,8 +269,7 @@ func (p *planner) truncateTable( newTableDesc.Mutations = nil newTableDesc.GCMutations = nil newTableDesc.ModificationTime = p.txn.CommitTimestamp() - tKey := tableKey{parentID: newTableDesc.ParentID, name: newTableDesc.Name} - key := tKey.Key() + key := sqlbase.NewTableKey(newTableDesc.ParentID, newTableDesc.Name).Key() if err := p.createDescriptorWithID( ctx, key, newID, newTableDesc, p.ExtendedEvalContext().Settings); err != nil { return err