本文整理汇总了Golang中github.com/cockroachdb/cockroach/structured.TableDescriptor.AllocateIDs方法的典型用法代码示例。如果您正苦于以下问题:Golang TableDescriptor.AllocateIDs方法的具体用法?Golang TableDescriptor.AllocateIDs怎么用?Golang TableDescriptor.AllocateIDs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cockroachdb/cockroach/structured.TableDescriptor
的用法示例。
在下文中一共展示了TableDescriptor.AllocateIDs方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateTable
// CreateTable creates a table from the specified schema. Table creation will
// fail if the table name is already in use. The table name is required to have
// the form "<namespace>.<table>".
func (db *DB) CreateTable(desc *structured.TableDescriptor) error {
desc.Name = strings.ToLower(desc.Name)
if err := desc.AllocateIDs(); err != nil {
return err
}
nsID, name, err := db.lookupTable(desc.Name)
if err != nil {
return err
}
if name == "" {
return fmt.Errorf("empty table name: %s", desc.Name)
}
nameKey := keys.MakeNameMetadataKey(nsID, name)
// This isn't strictly necessary as the conditional put below will fail if
// the key already exists, but it seems good to avoid the table ID allocation
// in most cases when the table already exists.
if gr, err := db.Get(nameKey); err != nil {
return err
} else if gr.Exists() {
return fmt.Errorf("table \"%s\" already exists", desc.Name)
}
ir, err := db.Inc(keys.DescIDGenerator, 1)
if err != nil {
return err
}
desc.ID = uint32(ir.ValueInt() - 1)
// TODO(pmattis): Be cognizant of error messages when this is ported to the
// server. The error currently returned below is likely going to be difficult
// to interpret.
return db.Txn(func(txn *Txn) error {
descKey := keys.MakeDescMetadataKey(desc.ID)
b := &Batch{}
b.CPut(nameKey, descKey, nil)
b.Put(descKey, desc)
return txn.Commit(b)
})
}