本文整理汇总了Golang中github.com/cockroachdb/cockroach/pkg/sql/sqlbase.ID函数的典型用法代码示例。如果您正苦于以下问题:Golang ID函数的具体用法?Golang ID怎么用?Golang ID使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ID函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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)
}
}
示例3: getTableID
// getTableID retrieves the table ID for the specified table.
func getTableID(p *planner, tn *parser.TableName) (sqlbase.ID, error) {
if err := tn.QualifyWithDatabase(p.session.Database); err != nil {
return 0, err
}
virtual, err := p.session.virtualSchemas.getVirtualTableDesc(tn)
if err != nil {
return 0, err
}
if virtual != nil {
return virtual.GetID(), nil
}
dbID, err := p.getDatabaseID(tn.Database())
if err != nil {
return 0, err
}
nameKey := tableKey{dbID, tn.Table()}
key := nameKey.Key()
gr, err := p.txn.Get(key)
if err != nil {
return 0, err
}
if !gr.Exists() {
return 0, sqlbase.NewUndefinedTableError(parser.AsString(tn))
}
return sqlbase.ID(gr.ValueInt()), nil
}
示例4: queryZones
func queryZones(conn *sqlConn) (map[sqlbase.ID]config.ZoneConfig, error) {
rows, err := makeQuery(`SELECT * FROM system.zones`)(conn)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
vals := make([]driver.Value, len(rows.Columns()))
zones := make(map[sqlbase.ID]config.ZoneConfig)
for {
if err := rows.Next(vals); err != nil {
if err == io.EOF {
break
}
return nil, err
}
id, ok := vals[0].(int64)
if !ok {
return nil, fmt.Errorf("unexpected value: %T", vals[0])
}
zone := config.ZoneConfig{}
if err := unmarshalProto(vals[1], &zone); err != nil {
return nil, err
}
zones[sqlbase.ID(id)] = zone
}
return zones, nil
}
示例5: queryNamespace
func queryNamespace(conn *sqlConn, parentID sqlbase.ID, name string) (sqlbase.ID, error) {
rows, err := makeQuery(
`SELECT id FROM system.namespace WHERE parentID = $1 AND name = $2`,
parentID, parser.Name(name).Normalize())(conn)
if err != nil {
return 0, err
}
defer func() { _ = rows.Close() }()
if err != nil {
return 0, fmt.Errorf("%s not found: %v", name, err)
}
if len(rows.Columns()) != 1 {
return 0, fmt.Errorf("unexpected result columns: %d", len(rows.Columns()))
}
vals := make([]driver.Value, 1)
if err := rows.Next(vals); err != nil {
return 0, err
}
switch t := vals[0].(type) {
case int64:
return sqlbase.ID(t), nil
default:
return 0, fmt.Errorf("unexpected result type: %T", vals[0])
}
}
示例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: GenerateUniqueDescID
// GenerateUniqueDescID returns the next available Descriptor ID and increments
// the counter.
func GenerateUniqueDescID(txn *client.Txn) (sqlbase.ID, error) {
// Increment unique descriptor counter.
ir, err := txn.Inc(keys.DescIDGenerator, 1)
if err != nil {
return 0, err
}
return sqlbase.ID(ir.ValueInt() - 1), nil
}
示例8: 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)
}
示例9: getDescriptorID
func getDescriptorID(txn *client.Txn, key sqlbase.DescriptorKey) (sqlbase.ID, error) {
// TODO(dan): Share this with (*planner).getDescriptor.
idValue, err := txn.Get(key.Key())
if err != nil {
return 0, err
}
if !idValue.Exists() {
return 0, errors.Errorf("no descriptor for key: %s", key)
}
return sqlbase.ID(idValue.ValueInt()), nil
}
示例10: resolveName
// resolveName resolves a table name to a descriptor ID by looking in the
// database. If the mapping is not found, sqlbase.ErrDescriptorNotFound is returned.
func (m *LeaseManager) resolveName(
txn *client.Txn, dbID sqlbase.ID, tableName string,
) (sqlbase.ID, error) {
nameKey := tableKey{dbID, tableName}
key := nameKey.Key()
gr, err := txn.Get(key)
if err != nil {
return 0, err
}
if !gr.Exists() {
return 0, sqlbase.ErrDescriptorNotFound
}
return sqlbase.ID(gr.ValueInt()), nil
}
示例11: expectDescriptorID
func expectDescriptorID(systemConfig config.SystemConfig, idKey roachpb.Key, id sqlbase.ID) error {
idValue := systemConfig.GetValue(idKey)
if idValue == nil {
return errStaleMetadata
}
cachedID, err := idValue.GetInt()
if err != nil {
return err
}
if sqlbase.ID(cachedID) != id {
return errStaleMetadata
}
return nil
}
示例12: getDescriptor
func getDescriptor(
txn *client.Txn, plainKey sqlbase.DescriptorKey, descriptor sqlbase.DescriptorProto,
) (bool, error) {
gr, err := txn.Get(plainKey.Key())
if err != nil {
return false, err
}
if !gr.Exists() {
return false, nil
}
descKey := sqlbase.MakeDescMetadataKey(sqlbase.ID(gr.ValueInt()))
desc := &sqlbase.Descriptor{}
if err := txn.GetProto(descKey, desc); err != nil {
return false, err
}
switch t := descriptor.(type) {
case *sqlbase.TableDescriptor:
table := desc.GetTable()
if table == nil {
return false, errors.Errorf("%q is not a table", plainKey.Name())
}
table.MaybeUpgradeFormatVersion()
// TODO(dan): Write the upgraded TableDescriptor back to kv. This will break
// the ability to use a previous version of cockroach with the on-disk data,
// but it's worth it to avoid having to do the upgrade every time the
// descriptor is fetched. Our current test for this enforces compatibility
// backward and forward, so that'll have to be extended before this is done.
if err := table.Validate(txn); err != nil {
return false, err
}
*t = *table
case *sqlbase.DatabaseDescriptor:
database := desc.GetDatabase()
if database == nil {
return false, errors.Errorf("%q is not a database", plainKey.Name())
}
if err := database.Validate(); err != nil {
return false, err
}
*t = *database
}
return true, nil
}
示例13: TestSchemaChangeLease
func TestSchemaChangeLease(t *testing.T) {
defer leaktest.AfterTest(t)()
params, _ := createTestServerParams()
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
// Set MinSchemaChangeLeaseDuration to always expire the lease.
minLeaseDuration := csql.MinSchemaChangeLeaseDuration
csql.MinSchemaChangeLeaseDuration = 2 * csql.SchemaChangeLeaseDuration
defer func() {
csql.MinSchemaChangeLeaseDuration = minLeaseDuration
}()
if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k CHAR PRIMARY KEY, v CHAR);
`); err != nil {
t.Fatal(err)
}
var lease sqlbase.TableDescriptor_SchemaChangeLease
var id = sqlbase.ID(keys.MaxReservedDescID + 2)
var node = roachpb.NodeID(2)
changer := csql.NewSchemaChangerForTesting(id, 0, node, *kvDB, nil)
// Acquire a lease.
lease, err := changer.AcquireLease()
if err != nil {
t.Fatal(err)
}
if !validExpirationTime(lease.ExpirationTime) {
t.Fatalf("invalid expiration time: %s", time.Unix(0, lease.ExpirationTime))
}
// Acquiring another lease will fail.
if _, err := changer.AcquireLease(); !testutils.IsError(
err, "an outstanding schema change lease exists",
) {
t.Fatal(err)
}
// Extend the lease.
newLease, err := changer.ExtendLease(lease)
if err != nil {
t.Fatal(err)
}
if !validExpirationTime(newLease.ExpirationTime) {
t.Fatalf("invalid expiration time: %s", time.Unix(0, newLease.ExpirationTime))
}
// The new lease is a brand new lease.
if newLease == lease {
t.Fatalf("lease was not extended: %v", lease)
}
// Extending an old lease fails.
if _, err := changer.ExtendLease(lease); !testutils.IsError(err, "table: .* has lease") {
t.Fatal(err)
}
// Releasing an old lease fails.
err = changer.ReleaseLease(lease)
if err == nil {
t.Fatal("releasing a old lease succeeded")
}
// Release lease.
err = changer.ReleaseLease(newLease)
if err != nil {
t.Fatal(err)
}
// Extending the lease fails.
_, err = changer.ExtendLease(newLease)
if err == nil {
t.Fatalf("was able to extend an already released lease: %d, %v", id, lease)
}
// acquiring the lease succeeds
lease, err = changer.AcquireLease()
if err != nil {
t.Fatal(err)
}
// Set MinSchemaChangeLeaseDuration to not expire the lease.
csql.MinSchemaChangeLeaseDuration = minLeaseDuration
newLease, err = changer.ExtendLease(lease)
if err != nil {
t.Fatal(err)
}
// The old lease is renewed.
if newLease != lease {
t.Fatalf("acquired new lease: %v, old lease: %v", newLease, lease)
}
}
示例14: TestSchemaChangeProcess
func TestSchemaChangeProcess(t *testing.T) {
defer leaktest.AfterTest(t)()
// The descriptor changes made must have an immediate effect
// so disable leases on tables.
defer csql.TestDisableTableLeases()()
params, _ := createTestServerParams()
// Disable external processing of mutations.
params.Knobs.SQLSchemaChanger = &csql.SchemaChangerTestingKnobs{
AsyncExecNotification: asyncSchemaChangerDisabled,
}
s, sqlDB, kvDB := serverutils.StartServer(t, params)
defer s.Stopper().Stop()
var id = sqlbase.ID(keys.MaxReservedDescID + 2)
var node = roachpb.NodeID(2)
stopper := stop.NewStopper()
leaseMgr := csql.NewLeaseManager(
&base.NodeIDContainer{},
*kvDB,
hlc.NewClock(hlc.UnixNano, time.Nanosecond),
csql.LeaseManagerTestingKnobs{},
stopper,
&csql.MemoryMetrics{},
)
defer stopper.Stop()
changer := csql.NewSchemaChangerForTesting(id, 0, node, *kvDB, leaseMgr)
if _, err := sqlDB.Exec(`
CREATE DATABASE t;
CREATE TABLE t.test (k CHAR PRIMARY KEY, v CHAR, INDEX foo(v));
INSERT INTO t.test VALUES ('a', 'b'), ('c', 'd');
`); err != nil {
t.Fatal(err)
}
// Read table descriptor for version.
tableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
expectedVersion := tableDesc.Version
desc, err := changer.MaybeIncrementVersion()
if err != nil {
t.Fatal(err)
}
tableDesc = desc.GetTable()
newVersion := tableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version; e = %d, v = %d", expectedVersion, newVersion)
}
isDone, err := changer.IsDone()
if err != nil {
t.Fatal(err)
}
if !isDone {
t.Fatalf("table expected to not have an outstanding schema change: %v", tableDesc)
}
// Check that MaybeIncrementVersion increments the version
// correctly.
expectedVersion++
tableDesc.UpVersion = true
if err := kvDB.Put(
context.TODO(),
sqlbase.MakeDescMetadataKey(tableDesc.ID),
sqlbase.WrapDescriptor(tableDesc),
); err != nil {
t.Fatal(err)
}
isDone, err = changer.IsDone()
if err != nil {
t.Fatal(err)
}
if isDone {
t.Fatalf("table expected to have an outstanding schema change: %v", desc.GetTable())
}
desc, err = changer.MaybeIncrementVersion()
if err != nil {
t.Fatal(err)
}
tableDesc = desc.GetTable()
savedTableDesc := sqlbase.GetTableDescriptor(kvDB, "t", "test")
newVersion = tableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version in returned desc; e = %d, v = %d", expectedVersion, newVersion)
}
newVersion = savedTableDesc.Version
if newVersion != expectedVersion {
t.Fatalf("bad version in saved desc; e = %d, v = %d", expectedVersion, newVersion)
}
isDone, err = changer.IsDone()
if err != nil {
t.Fatal(err)
}
if !isDone {
t.Fatalf("table expected to not have an outstanding schema change: %v", tableDesc)
}
// Check that RunStateMachineBeforeBackfill doesn't do anything
// if there are no mutations queued.
//.........这里部分代码省略.........
示例15: TestSystemConfigGossip
func TestSystemConfigGossip(t *testing.T) {
defer leaktest.AfterTest(t)()
t.Skip("#12351")
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
ts := s.(*TestServer)
ctx := context.TODO()
key := sqlbase.MakeDescMetadataKey(keys.MaxReservedDescID)
valAt := func(i int) *sqlbase.DatabaseDescriptor {
return &sqlbase.DatabaseDescriptor{Name: "foo", ID: sqlbase.ID(i)}
}
// Register a callback for gossip updates.
resultChan := ts.Gossip().RegisterSystemConfigChannel()
// The span gets gossiped when it first shows up.
select {
case <-resultChan:
case <-time.After(500 * time.Millisecond):
t.Fatal("did not receive gossip message")
}
// Try a plain KV write first.
if err := kvDB.Put(ctx, key, valAt(0)); err != nil {
t.Fatal(err)
}
// Now do it as part of a transaction, but without the trigger set.
if err := kvDB.Txn(ctx, func(txn *client.Txn) error {
return txn.Put(key, valAt(1))
}); err != nil {
t.Fatal(err)
}
// Gossip channel should be dormant.
// TODO(tschottdorf): This test is likely flaky. Why can't some other
// process trigger gossip? It seems that a new range lease being
// acquired will gossip a new system config since the hash changed and fail
// the test (seen in practice during some buggy WIP).
var systemConfig config.SystemConfig
select {
case <-resultChan:
systemConfig, _ = ts.gossip.GetSystemConfig()
t.Fatalf("unexpected message received on gossip channel: %v", systemConfig)
case <-time.After(50 * time.Millisecond):
}
// This time mark the transaction as having a Gossip trigger.
if err := kvDB.Txn(ctx, func(txn *client.Txn) error {
txn.SetSystemConfigTrigger()
return txn.Put(key, valAt(2))
}); err != nil {
t.Fatal(err)
}
// New system config received.
select {
case <-resultChan:
systemConfig, _ = ts.gossip.GetSystemConfig()
case <-time.After(500 * time.Millisecond):
t.Fatal("did not receive gossip message")
}
// Now check the new config.
var val *roachpb.Value
for _, kv := range systemConfig.Values {
if bytes.Equal(key, kv.Key) {
val = &kv.Value
break
}
}
if val == nil {
t.Fatal("key not found in gossiped info")
}
// Make sure the returned value is valAt(2).
got := new(sqlbase.DatabaseDescriptor)
if err := val.GetProto(got); err != nil {
t.Fatal(err)
}
if expected := valAt(2); !reflect.DeepEqual(got, expected) {
t.Fatalf("mismatch: expected %+v, got %+v", *expected, *got)
}
}