當前位置: 首頁>>代碼示例>>Golang>>正文


Golang TableDescriptor.AllocateIDs方法代碼示例

本文整理匯總了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)
	})
}
開發者ID:mingpengxiao,項目名稱:cockroach,代碼行數:44,代碼來源:table.go


注:本文中的github.com/cockroachdb/cockroach/structured.TableDescriptor.AllocateIDs方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。