Skip to content

Commit

Permalink
sql: move databaseKey and tableKey to sqlbase
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
vivekmenezes committed May 16, 2019
1 parent b77a323 commit a6f98a7
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 91 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/sql/create_sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/create_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 4 additions & 17 deletions pkg/sql/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/physical_schema_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/rename_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/schema_changer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
41 changes: 41 additions & 0 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
55 changes: 0 additions & 55 deletions pkg/sql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/sql/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6f98a7

Please sign in to comment.