本文整理汇总了Golang中github.com/cockroachdb/cockroach/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 {
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
}
示例2: getTableID
// getTableID retrieves the table ID for the specified table. It uses the
// descriptor cache to perform lookups, falling back to the KV store when
// necessary.
func (p *planner) getTableID(qname *parser.QualifiedName) (sqlbase.ID, error) {
if err := qname.NormalizeTableName(p.session.Database); err != nil {
return 0, err
}
dbID, err := p.getDatabaseID(qname.Database())
if err != nil {
return 0, err
}
// Lookup the ID of the table in the cache. The use of the cache might cause
// the usage of a recently renamed table, but that's a race that could occur
// anyways.
// TODO(andrei): remove the used of p.systemConfig as a cache for table names,
// replace it with using the leases for resolving names, and do away with any
// races due to renames. We'll probably have to rewrite renames to perform
// an async schema change.
nameKey := tableKey{dbID, qname.Table()}
key := nameKey.Key()
if nameVal := p.systemConfig.GetValue(key); nameVal != nil {
id, err := nameVal.GetInt()
return sqlbase.ID(id), err
}
gr, err := p.txn.Get(key)
if err != nil {
return 0, err
}
if !gr.Exists() {
return 0, tableDoesNotExistError(qname.String())
}
return sqlbase.ID(gr.ValueInt()), nil
}
示例3: 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()
}
示例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: 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 := 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
}
示例6: 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, sqlbase.NormalizeName(name))(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])
}
}
示例7: queryNamespaceID
// queryNamespaceID queries for the ID of the namespace with the given name and
// parent ID.
func (s *adminServer) queryNamespaceID(
session *sql.Session, parentID sqlbase.ID, name string,
) (sqlbase.ID, error) {
const query = `SELECT id FROM system.namespace WHERE parentID = $1 AND name = $2`
params := parser.NewPlaceholderInfo()
params.SetValue(`1`, parser.NewDInt(parser.DInt(parentID)))
params.SetValue(`2`, parser.NewDString(name))
r := s.server.sqlExecutor.ExecuteStatements(session, query, params)
if err := s.checkQueryResults(r.ResultList, 1); err != nil {
return 0, err
}
result := r.ResultList[0]
if len(result.Rows) == 0 {
return 0, errors.Errorf("namespace %s with ParentID %d not found", name, parentID)
}
var id int64
scanner := resultScanner{}
err := scanner.ScanIndex(result.Rows[0], 0, &id)
if err != nil {
return 0, err
}
return sqlbase.ID(id), nil
}
示例8: 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()
}
示例9: generateUniqueDescID
func (p *planner) generateUniqueDescID() (sqlbase.ID, error) {
// Increment unique descriptor counter.
ir, err := p.txn.Inc(keys.DescIDGenerator, 1)
if err != nil {
return 0, err
}
return sqlbase.ID(ir.ValueInt() - 1), nil
}
示例10: 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)
}
示例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: 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
}
示例13: TestStoreRangeSplitAtTablePrefix
// TestStoreRangeSplitAtTablePrefix verifies a range can be split at
// UserTableDataMin and still gossip the SystemConfig properly.
func TestStoreRangeSplitAtTablePrefix(t *testing.T) {
defer leaktest.AfterTest(t)()
sCtx := storage.TestStoreContext()
sCtx.TestingKnobs.DisableSplitQueue = true
store, stopper, _ := createTestStoreWithContext(t, sCtx)
defer stopper.Stop()
key := keys.MakeRowSentinelKey(append([]byte(nil), keys.UserTableDataMin...))
args := adminSplitArgs(key, key)
if _, pErr := client.SendWrapped(rg1(store), nil, &args); pErr != nil {
t.Fatalf("%q: split unexpected error: %s", key, pErr)
}
var desc sqlbase.TableDescriptor
descBytes, err := protoutil.Marshal(&desc)
if err != nil {
t.Fatal(err)
}
// Update SystemConfig to trigger gossip.
if err := store.DB().Txn(func(txn *client.Txn) error {
txn.SetSystemConfigTrigger()
// We don't care about the values, just the keys.
k := sqlbase.MakeDescMetadataKey(sqlbase.ID(keys.MaxReservedDescID + 1))
return txn.Put(k, &desc)
}); err != nil {
t.Fatal(err)
}
successChan := make(chan struct{}, 1)
store.Gossip().RegisterCallback(gossip.KeySystemConfig, func(_ string, content roachpb.Value) {
contentBytes, err := content.GetBytes()
if err != nil {
t.Fatal(err)
}
if bytes.Contains(contentBytes, descBytes) {
select {
case successChan <- struct{}{}:
default:
}
}
})
select {
case <-time.After(time.Second):
t.Errorf("expected a schema gossip containing %q, but did not see one", descBytes)
case <-successChan:
}
}
示例14: getDescriptor
// getDescriptor implements the DescriptorAccessor interface.
func (p *planner) getDescriptor(plainKey sqlbase.DescriptorKey, descriptor sqlbase.DescriptorProto,
) (bool, error) {
gr, err := p.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 := p.txn.GetProto(descKey, desc); err != nil {
return false, err
}
switch t := descriptor.(type) {
case *sqlbase.TableDescriptor:
table := desc.GetTable()
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 table == nil {
return false, errors.Errorf("%q is not a table", plainKey.Name())
}
if err := table.Validate(p.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
}
示例15: getTableID
// getTableID retrieves the table ID for the specified table.
func getTableID(p *planner, qname *parser.QualifiedName) (sqlbase.ID, error) {
if err := qname.NormalizeTableName(p.session.Database); err != nil {
return 0, err
}
dbID, err := p.getDatabaseID(qname.Database())
if err != nil {
return 0, err
}
nameKey := tableKey{dbID, qname.Table()}
key := nameKey.Key()
gr, err := p.txn.Get(key)
if err != nil {
return 0, err
}
if !gr.Exists() {
return 0, sqlbase.NewUndefinedTableError(qname.String())
}
return sqlbase.ID(gr.ValueInt()), nil
}