本文整理汇总了Golang中github.com/cockroachdb/cockroach/pkg/sql/sqlbase.MakeDescMetadataKey函数的典型用法代码示例。如果您正苦于以下问题:Golang MakeDescMetadataKey函数的具体用法?Golang MakeDescMetadataKey怎么用?Golang MakeDescMetadataKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MakeDescMetadataKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: restoreTableDesc
func restoreTableDesc(
ctx context.Context,
txn *client.Txn,
database sqlbase.DatabaseDescriptor,
table sqlbase.TableDescriptor,
) error {
// Run getDescriptorID again to make sure the database hasn't been dropped
// while we were importing.
var err error
if table.ParentID, err = getDescriptorID(txn, tableKey{name: database.Name}); err != nil {
return err
}
tableIDKey := tableKey{parentID: table.ParentID, name: table.Name}.Key()
tableDescKey := sqlbase.MakeDescMetadataKey(table.ID)
// Check for an existing table.
var existingDesc sqlbase.Descriptor
existingIDKV, err := txn.Get(tableIDKey)
if err != nil {
return err
}
if existingIDKV.Value != nil {
existingID, err := existingIDKV.Value.GetInt()
if err != nil {
return err
}
existingDescKV, err := txn.Get(sqlbase.MakeDescMetadataKey(sqlbase.ID(existingID)))
if err != nil {
return err
}
if err := existingDescKV.Value.GetProto(&existingDesc); err != nil {
return err
}
}
// Write the new descriptors. First the ID -> TableDescriptor for the new
// table, then flip (or initialize) the name -> ID entry so any new queries
// will use the new one. If there was an existing table, it can now be
// cleaned up.
b := txn.NewBatch()
b.CPut(tableDescKey, sqlbase.WrapDescriptor(&table), nil)
if existingTable := existingDesc.GetTable(); existingTable == nil {
b.CPut(tableIDKey, table.ID, nil)
} else {
existingIDKV.Value.ClearChecksum()
b.CPut(tableIDKey, table.ID, existingIDKV.Value)
// TODO(dan): This doesn't work for interleaved tables. Fix it when we
// fix the empty range interleaved table TODO below.
existingDataPrefix := roachpb.Key(keys.MakeTablePrefix(uint32(existingTable.ID)))
b.DelRange(existingDataPrefix, existingDataPrefix.PrefixEnd(), false)
zoneKey, _, descKey := GetKeysForTableDescriptor(existingTable)
// Delete the desc and zone entries. Leave the name because the new
// table is using it.
b.Del(descKey)
b.Del(zoneKey)
}
return txn.Run(b)
}
示例2: 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.ValidateTable(); err != nil {
mt.Fatal(err)
}
if err := mt.kvDB.Put(
context.TODO(),
sqlbase.MakeDescMetadataKey(mt.tableDesc.ID),
sqlbase.WrapDescriptor(mt.tableDesc),
); err != nil {
mt.Fatal(err)
}
}
示例3: 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.ValidateTable(); err != nil {
mt.Fatal(err)
}
if err := mt.kvDB.Put(
context.TODO(),
sqlbase.MakeDescMetadataKey(mt.tableDesc.ID),
sqlbase.WrapDescriptor(mt.tableDesc),
); err != nil {
mt.Fatal(err)
}
}
示例4: 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 {
// We're done.
zone, err := config.MigrateZoneConfig(zoneVal)
return zone, true, err
}
// 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
}
示例5: 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(context.TODO(), 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
}
示例6: getCachedDatabaseDesc
// getCachedDatabaseDesc implements the DatabaseAccessor interface.
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, errors.Errorf("%q is not a database", name)
}
return database, database.Validate()
}
示例7: 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))
}
示例8: createDescriptorWithID
func (p *planner) createDescriptorWithID(
idKey roachpb.Key, id sqlbase.ID, descriptor sqlbase.DescriptorProto,
) 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 p.txn.Run(b)
}
示例9: 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(context.TODO(), func(txn *client.Txn) error {
txn.SetSystemConfigTrigger()
tableDesc, err := sqlbase.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(txn.Context, "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
}
示例10: TestRenameTable
// TestRenameTable tests the table descriptor changes during
// a rename operation.
func TestRenameTable(t *testing.T) {
defer leaktest.AfterTest(t)()
s, db, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
counter := int64(keys.MaxReservedDescID)
counter++
oldDBID := sqlbase.ID(counter)
if _, err := db.Exec(`CREATE DATABASE test`); err != nil {
t.Fatal(err)
}
// Create table in 'test'.
counter++
oldName := "foo"
if _, err := db.Exec(`CREATE TABLE test.foo (k INT PRIMARY KEY, v int)`); err != nil {
t.Fatal(err)
}
// Check the table descriptor.
desc := &sqlbase.Descriptor{}
tableDescKey := sqlbase.MakeDescMetadataKey(sqlbase.ID(counter))
if err := kvDB.GetProto(context.TODO(), tableDescKey, desc); err != nil {
t.Fatal(err)
}
tableDesc := desc.GetTable()
if tableDesc.Name != oldName {
t.Fatalf("Wrong table name, expected %s, got: %+v", oldName, tableDesc)
}
if tableDesc.ParentID != oldDBID {
t.Fatalf("Wrong parent ID on table, expected %d, got: %+v", oldDBID, tableDesc)
}
// Create database test2.
counter++
newDBID := sqlbase.ID(counter)
if _, err := db.Exec(`CREATE DATABASE test2`); err != nil {
t.Fatal(err)
}
// Move table to test2 and change its name as well.
newName := "bar"
if _, err := db.Exec(`ALTER TABLE test.foo RENAME TO test2.bar`); err != nil {
t.Fatal(err)
}
// Check the table descriptor again.
if err := kvDB.GetProto(context.TODO(), tableDescKey, desc); err != nil {
t.Fatal(err)
}
tableDesc = desc.GetTable()
if tableDesc.Name != newName {
t.Fatalf("Wrong table name, expected %s, got: %+v", newName, tableDesc)
}
if tableDesc.ParentID != newDBID {
t.Fatalf("Wrong parent ID on table, expected %d, got: %+v", newDBID, tableDesc)
}
}
示例11: 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
}
示例12: getKeysForDatabaseDescriptor
// getKeysForDatabaseDescriptor retrieves the KV keys corresponding to
// the zone, name and descriptor of a database.
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
}
示例13: 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
}
示例14: ReleaseLease
// ReleaseLease releases the table lease if it is the one registered with
// the table descriptor.
func (sc *SchemaChanger) ReleaseLease(lease sqlbase.TableDescriptor_SchemaChangeLease) error {
err := sc.db.Txn(context.TODO(), func(txn *client.Txn) error {
tableDesc, err := sc.findTableWithLease(txn, lease)
if err != nil {
return err
}
tableDesc.Lease = nil
txn.SetSystemConfigTrigger()
return txn.Put(sqlbase.MakeDescMetadataKey(tableDesc.ID), sqlbase.WrapDescriptor(tableDesc))
})
return err
}
示例15: isDeleted
func isDeleted(tableID sqlbase.ID, cfg config.SystemConfig) bool {
descKey := sqlbase.MakeDescMetadataKey(tableID)
val := cfg.GetValue(descKey)
if val == nil {
return false
}
var descriptor sqlbase.Descriptor
if err := val.GetProto(&descriptor); err != nil {
panic("unable to unmarshal table descriptor")
}
table := descriptor.GetTable()
return table.Dropped()
}