本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/sqlbase.MakeDescMetadataKey函数的典型用法代码示例。如果您正苦于以下问题:Golang MakeDescMetadataKey函数的具体用法?Golang MakeDescMetadataKey怎么用?Golang MakeDescMetadataKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MakeDescMetadataKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getCachedDatabaseDesc
// getCachedDatabaseDesc looks up the database descriptor given its name in the
// descriptor cache.
func (p *planner) getCachedDatabaseDesc(name string) (*sqlbase.DatabaseDescriptor, error) {
if name == sqlbase.SystemDB.Name {
return &sqlbase.SystemDB, nil
}
nameKey := databaseKey{name}
nameVal := p.systemConfig.GetValue(nameKey.Key())
if nameVal == nil {
return nil, fmt.Errorf("database %q does not exist in system cache", name)
}
id, err := nameVal.GetInt()
if err != nil {
return nil, err
}
descKey := sqlbase.MakeDescMetadataKey(sqlbase.ID(id))
descVal := p.systemConfig.GetValue(descKey)
if descVal == nil {
return nil, fmt.Errorf("database %q has name entry, but no descriptor in system cache", name)
}
desc := &sqlbase.Descriptor{}
if err := descVal.GetProto(desc); err != nil {
return nil, err
}
database := desc.GetDatabase()
if database == nil {
return nil, util.Errorf("%q is not a database", name)
}
return database, database.Validate()
}
示例2: makeMutationsActive
// Convert all the mutations into live descriptors for the table
// and write the updated table descriptor to the DB.
func (mt mutationTest) makeMutationsActive() {
// Remove mutation to check real values in DB using SQL
if mt.tableDesc.Mutations == nil || len(mt.tableDesc.Mutations) == 0 {
mt.Fatal("No mutations to make active")
}
for _, m := range mt.tableDesc.Mutations {
if col := m.GetColumn(); col != nil {
mt.tableDesc.Columns = append(mt.tableDesc.Columns, *col)
} else if index := m.GetIndex(); index != nil {
mt.tableDesc.Indexes = append(mt.tableDesc.Indexes, *index)
} else {
mt.Fatalf("no descriptor in mutation: %v", m)
}
}
mt.tableDesc.Mutations = nil
if err := mt.tableDesc.Validate(); err != nil {
mt.Fatal(err)
}
if err := mt.kvDB.Put(
sqlbase.MakeDescMetadataKey(mt.tableDesc.ID),
sqlbase.WrapDescriptor(mt.tableDesc),
); err != nil {
mt.Fatal(err)
}
}
示例3: AcquireLease
// AcquireLease acquires a schema change lease on the table if
// an unexpired lease doesn't exist. It returns the lease.
func (sc *SchemaChanger) AcquireLease() (sqlbase.TableDescriptor_SchemaChangeLease, error) {
var lease sqlbase.TableDescriptor_SchemaChangeLease
err := sc.db.Txn(func(txn *client.Txn) error {
txn.SetSystemConfigTrigger()
tableDesc, err := getTableDescFromID(txn, sc.tableID)
if err != nil {
return err
}
// A second to deal with the time uncertainty across nodes.
// It is perfectly valid for two or more goroutines to hold a valid
// lease and execute a schema change in parallel, because schema
// changes are executed using transactions that run sequentially.
// This just reduces the probability of a write collision.
expirationTimeUncertainty := time.Second
if tableDesc.Lease != nil {
if time.Unix(0, tableDesc.Lease.ExpirationTime).Add(expirationTimeUncertainty).After(timeutil.Now()) {
return errExistingSchemaChangeLease
}
log.Infof("Overriding existing expired lease %v", tableDesc.Lease)
}
lease = sc.createSchemaChangeLease()
tableDesc.Lease = &lease
return txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
return lease, err
}
示例4: createDescriptorWithID
func (p *planner) createDescriptorWithID(
idKey roachpb.Key, id sqlbase.ID, descriptor sqlbase.DescriptorProto,
) (bool, error) {
descriptor.SetID(id)
// TODO(pmattis): The error currently returned below is likely going to be
// difficult to interpret.
//
// TODO(pmattis): Need to handle if-not-exists here as well.
//
// TODO(pmattis): This is writing the namespace and descriptor table entries,
// but not going through the normal INSERT logic and not performing a precise
// mimicry. In particular, we're only writing a single key per table, while
// perfect mimicry would involve writing a sentinel key for each row as well.
descKey := sqlbase.MakeDescMetadataKey(descriptor.GetID())
b := &client.Batch{}
descID := descriptor.GetID()
descDesc := sqlbase.WrapDescriptor(descriptor)
if log.V(2) {
log.Infof(p.ctx(), "CPut %s -> %d", idKey, descID)
log.Infof(p.ctx(), "CPut %s -> %s", descKey, descDesc)
}
b.CPut(idKey, descID, nil)
b.CPut(descKey, descDesc, nil)
p.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, idKey, descID); err != nil {
return err
}
return expectDescriptor(systemConfig, descKey, descDesc)
})
return true, p.txn.Run(b)
}
示例5: writeTableDesc
// writeTableDesc implements the SchemaAccessor interface.
func (p *planner) writeTableDesc(tableDesc *sqlbase.TableDescriptor) error {
if isVirtualDescriptor(tableDesc) {
panic(fmt.Sprintf("Virtual Descriptors cannot be stored, found: %v", tableDesc))
}
return p.txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.GetID()),
sqlbase.WrapDescriptor(tableDesc))
}
示例6: writeMutation
// writeMutation writes the mutation to the table descriptor. If the
// State or the Direction is undefined, these values are populated via
// picking random values before the mutation is written.
func (mt mutationTest) writeMutation(m sqlbase.DescriptorMutation) {
if m.Direction == sqlbase.DescriptorMutation_NONE {
// randomly pick ADD/DROP mutation if this is the first mutation, or
// pick the direction already chosen for the first mutation.
if len(mt.tableDesc.Mutations) > 0 {
m.Direction = mt.tableDesc.Mutations[0].Direction
} else {
m.Direction = sqlbase.DescriptorMutation_DROP
if rand.Intn(2) == 0 {
m.Direction = sqlbase.DescriptorMutation_ADD
}
}
}
if m.State == sqlbase.DescriptorMutation_UNKNOWN {
// randomly pick DELETE_ONLY/WRITE_ONLY state.
r := rand.Intn(2)
if r == 0 {
m.State = sqlbase.DescriptorMutation_DELETE_ONLY
} else {
m.State = sqlbase.DescriptorMutation_WRITE_ONLY
}
}
mt.tableDesc.Mutations = append(mt.tableDesc.Mutations, m)
if err := mt.tableDesc.Validate(); err != nil {
mt.Fatal(err)
}
if err := mt.kvDB.Put(
sqlbase.MakeDescMetadataKey(mt.tableDesc.ID),
sqlbase.WrapDescriptor(mt.tableDesc),
); err != nil {
mt.Fatal(err)
}
}
示例7: waitForOneVersion
// waitForOneVersion returns once there are no unexpired leases on the
// previous version of the table descriptor. It returns the current version.
// After returning there can only be versions of the descriptor >= to the
// returned version. Lease acquisition (see acquire()) maintains the
// invariant that no new leases for desc.Version-1 will be granted once
// desc.Version exists.
func (s LeaseStore) waitForOneVersion(tableID sqlbase.ID, retryOpts retry.Options) (
sqlbase.DescriptorVersion, error,
) {
desc := &sqlbase.Descriptor{}
descKey := sqlbase.MakeDescMetadataKey(tableID)
var tableDesc *sqlbase.TableDescriptor
for r := retry.Start(retryOpts); r.Next(); {
// Get the current version of the table descriptor non-transactionally.
//
// TODO(pmattis): Do an inconsistent read here?
if err := s.db.GetProto(descKey, desc); err != nil {
return 0, err
}
tableDesc = desc.GetTable()
if tableDesc == nil {
return 0, errors.Errorf("ID %d is not a table", tableID)
}
// Check to see if there are any leases that still exist on the previous
// version of the descriptor.
now := s.clock.Now()
count, err := s.countLeases(tableDesc.ID, tableDesc.Version-1, now.GoTime())
if err != nil {
return 0, err
}
if count == 0 {
break
}
log.Infof(context.TODO(), "publish (count leases): descID=%d name=%s version=%d count=%d",
tableDesc.ID, tableDesc.Name, tableDesc.Version-1, count)
}
return tableDesc.Version, nil
}
示例8: GetZoneConfig
// GetZoneConfig returns the zone config for the object with 'id'.
func GetZoneConfig(cfg config.SystemConfig, id uint32) (config.ZoneConfig, bool, error) {
// Look in the zones table.
if zoneVal := cfg.GetValue(sqlbase.MakeZoneKey(sqlbase.ID(id))); zoneVal != nil {
var zone config.ZoneConfig
// We're done.
return zone, true, zoneVal.GetProto(&zone)
}
// No zone config for this ID. We need to figure out if it's a database
// or table. Lookup its descriptor.
if descVal := cfg.GetValue(sqlbase.MakeDescMetadataKey(sqlbase.ID(id))); descVal != nil {
// Determine whether this is a database or table.
var desc sqlbase.Descriptor
if err := descVal.GetProto(&desc); err != nil {
return config.ZoneConfig{}, false, err
}
if tableDesc := desc.GetTable(); tableDesc != nil {
// This is a table descriptor. Lookup its parent database zone config.
return GetZoneConfig(cfg, uint32(tableDesc.ParentID))
}
}
// Retrieve the default zone config, but only as long as that wasn't the ID
// we were trying to retrieve (avoid infinite recursion).
if id != keys.RootNamespaceID {
return GetZoneConfig(cfg, keys.RootNamespaceID)
}
// No descriptor or not a table.
return config.ZoneConfig{}, false, nil
}
示例9: getTableDescriptor
func getTableDescriptor(db *client.DB, database string, table string) *sqlbase.TableDescriptor {
dbNameKey := sqlbase.MakeNameMetadataKey(keys.RootNamespaceID, database)
gr, err := db.Get(dbNameKey)
if err != nil {
panic(err)
}
if !gr.Exists() {
panic("database missing")
}
dbDescID := sqlbase.ID(gr.ValueInt())
tableNameKey := sqlbase.MakeNameMetadataKey(dbDescID, table)
gr, err = db.Get(tableNameKey)
if err != nil {
panic(err)
}
if !gr.Exists() {
panic("table missing")
}
descKey := sqlbase.MakeDescMetadataKey(sqlbase.ID(gr.ValueInt()))
desc := &sqlbase.Descriptor{}
if err := db.GetProto(descKey, desc); err != nil {
panic("proto missing")
}
return desc.GetTable()
}
示例10: RenameDatabase
// RenameDatabase renames the database.
// Privileges: security.RootUser user.
// Notes: postgres requires superuser, db owner, or "CREATEDB".
// mysql >= 5.1.23 does not allow database renames.
func (p *planner) RenameDatabase(n *parser.RenameDatabase) (planNode, error) {
if n.Name == "" || n.NewName == "" {
return nil, errEmptyDatabaseName
}
if p.session.User != security.RootUser {
return nil, fmt.Errorf("only %s is allowed to rename databases", security.RootUser)
}
dbDesc, err := p.getDatabaseDesc(string(n.Name))
if err != nil {
return nil, err
}
if dbDesc == nil {
return nil, databaseDoesNotExistError(string(n.Name))
}
if n.Name == n.NewName {
// Noop.
return &emptyNode{}, nil
}
// Now update the nameMetadataKey and the descriptor.
descKey := sqlbase.MakeDescMetadataKey(dbDesc.GetID())
dbDesc.SetName(string(n.NewName))
if err := dbDesc.Validate(); err != nil {
return nil, err
}
newKey := databaseKey{string(n.NewName)}.Key()
oldKey := databaseKey{string(n.Name)}.Key()
descID := dbDesc.GetID()
descDesc := sqlbase.WrapDescriptor(dbDesc)
b := client.Batch{}
b.CPut(newKey, descID, nil)
b.Put(descKey, descDesc)
b.Del(oldKey)
if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*roachpb.ConditionFailedError); ok {
return nil, fmt.Errorf("the new database name %q already exists", string(n.NewName))
}
return nil, err
}
p.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, newKey, descID); err != nil {
return err
}
if err := expectDescriptor(systemConfig, descKey, descDesc); err != nil {
return err
}
return expectDeleted(systemConfig, oldKey)
})
return &emptyNode{}, nil
}
示例11: RenameIndex
// RenameIndex renames the index.
// Privileges: CREATE on table.
// notes: postgres requires CREATE on the table.
// mysql requires ALTER, CREATE, INSERT on the table.
func (p *planner) RenameIndex(n *parser.RenameIndex) (planNode, error) {
tn, err := n.Index.Table.NormalizeWithDatabaseName(p.session.Database)
if err != nil {
return nil, err
}
tableDesc, err := p.mustGetTableDesc(tn)
if err != nil {
return nil, err
}
normIdxName := sqlbase.NormalizeName(n.Index.Index)
status, i, err := tableDesc.FindIndexByNormalizedName(normIdxName)
if err != nil {
if n.IfExists {
// Noop.
return &emptyNode{}, nil
}
// Index does not exist, but we want it to: error out.
return nil, err
}
if err := p.checkPrivilege(tableDesc, privilege.CREATE); err != nil {
return nil, err
}
if n.NewName == "" {
return nil, errEmptyIndexName
}
normNewIdxName := sqlbase.NormalizeName(n.NewName)
if normIdxName == normNewIdxName {
// Noop.
return &emptyNode{}, nil
}
if _, _, err := tableDesc.FindIndexByNormalizedName(normNewIdxName); err == nil {
return nil, fmt.Errorf("index name %q already exists", n.NewName)
}
if status == sqlbase.DescriptorActive {
tableDesc.Indexes[i].Name = normNewIdxName
} else {
tableDesc.Mutations[i].GetIndex().Name = normNewIdxName
}
if err := tableDesc.SetUpVersion(); err != nil {
return nil, err
}
descKey := sqlbase.MakeDescMetadataKey(tableDesc.GetID())
if err := tableDesc.Validate(p.txn); err != nil {
return nil, err
}
if err := p.txn.Put(descKey, sqlbase.WrapDescriptor(tableDesc)); err != nil {
return nil, err
}
p.notifySchemaChange(tableDesc.ID, sqlbase.InvalidMutationID)
return &emptyNode{}, nil
}
示例12: getKeysForTableDescriptor
// 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 = sqlbase.MakeZoneKey(tableDesc.ID)
nameKey = sqlbase.MakeNameMetadataKey(tableDesc.ParentID, tableDesc.GetName())
descKey = sqlbase.MakeDescMetadataKey(tableDesc.ID)
return
}
示例13: getKeysForDatabaseDescriptor
func getKeysForDatabaseDescriptor(
dbDesc *sqlbase.DatabaseDescriptor,
) (zoneKey roachpb.Key, nameKey roachpb.Key, descKey roachpb.Key) {
zoneKey = sqlbase.MakeZoneKey(dbDesc.ID)
nameKey = sqlbase.MakeNameMetadataKey(keys.RootNamespaceID, dbDesc.GetName())
descKey = sqlbase.MakeDescMetadataKey(dbDesc.ID)
return
}
示例14: GetTableDesc
// GetTableDesc returns the table descriptor for the table with 'id'.
// Returns nil if the descriptor is not present, or is present but is not a
// table.
func GetTableDesc(cfg config.SystemConfig, id sqlbase.ID) (*sqlbase.TableDescriptor, error) {
if descVal := cfg.GetValue(sqlbase.MakeDescMetadataKey(id)); descVal != nil {
desc := &sqlbase.Descriptor{}
if err := descVal.GetProto(desc); err != nil {
return nil, err
}
return desc.GetTable(), nil
}
return nil, nil
}
示例15: createDescriptor
// createDescriptor implements the DescriptorAccessor interface.
func (p *planner) createDescriptor(plainKey sqlbase.DescriptorKey, descriptor sqlbase.DescriptorProto, ifNotExists bool) (bool, error) {
idKey := plainKey.Key()
// Check whether idKey exists.
gr, err := p.txn.Get(idKey)
if err != nil {
return false, err
}
if gr.Exists() {
if ifNotExists {
// Noop.
return false, nil
}
// Key exists, but we don't want it to: error out.
return false, fmt.Errorf("%s %q already exists", descriptor.TypeName(), plainKey.Name())
}
// Increment unique descriptor counter.
if ir, err := p.txn.Inc(keys.DescIDGenerator, 1); err == nil {
descriptor.SetID(sqlbase.ID(ir.ValueInt() - 1))
} else {
return false, err
}
// TODO(pmattis): The error currently returned below is likely going to be
// difficult to interpret.
//
// TODO(pmattis): Need to handle if-not-exists here as well.
//
// TODO(pmattis): This is writing the namespace and descriptor table entries,
// but not going through the normal INSERT logic and not performing a precise
// mimicry. In particular, we're only writing a single key per table, while
// perfect mimicry would involve writing a sentinel key for each row as well.
descKey := sqlbase.MakeDescMetadataKey(descriptor.GetID())
b := client.Batch{}
descID := descriptor.GetID()
descDesc := sqlbase.WrapDescriptor(descriptor)
if log.V(2) {
log.Infof("CPut %s -> %d", idKey, descID)
log.Infof("CPut %s -> %s", descKey, descDesc)
}
b.CPut(idKey, descID, nil)
b.CPut(descKey, descDesc, nil)
p.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, idKey, descID); err != nil {
return err
}
return expectDescriptor(systemConfig, descKey, descDesc)
})
return true, p.txn.Run(&b)
}