本文整理汇总了Golang中github.com/cockroachdb/cockroach/internal/client.Batch.Put方法的典型用法代码示例。如果您正苦于以下问题:Golang Batch.Put方法的具体用法?Golang Batch.Put怎么用?Golang Batch.Put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cockroachdb/cockroach/internal/client.Batch
的用法示例。
在下文中一共展示了Batch.Put方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAuthentication
// TestAuthentication tests authentication for the KV endpoint.
func TestAuthentication(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
var b1 client.Batch
b1.Put("a", "b")
// Create a node user client and call Run() on it which lets us build our own
// request, specifying the user.
db1 := createTestClientForUser(t, s.Stopper(), s.ServingAddr(), security.NodeUser)
if err := db1.Run(&b1); err != nil {
t.Fatal(err)
}
var b2 client.Batch
b2.Put("c", "d")
// Try again, but this time with certs for a non-node user (even the root
// user has no KV permissions).
db2 := createTestClientForUser(t, s.Stopper(), s.ServingAddr(), security.RootUser)
if err := db2.Run(&b2); !testutils.IsError(err, "is not allowed") {
t.Fatal(err)
}
}
示例2: RenameDatabase
// RenameDatabase renames the database.
// Privileges: security.RootUser user.
// Notes: postgres requires superuser, db owner, or "CREATEDB".
// mysql >= 5.1.23 does not allow database renames.
func (p *planner) RenameDatabase(n *parser.RenameDatabase) (planNode, error) {
if n.Name == "" || n.NewName == "" {
return nil, errEmptyDatabaseName
}
if p.session.User != security.RootUser {
return nil, fmt.Errorf("only %s is allowed to rename databases", security.RootUser)
}
dbDesc, err := p.getDatabaseDesc(string(n.Name))
if err != nil {
return nil, err
}
if dbDesc == nil {
return nil, sqlbase.NewUndefinedDatabaseError(string(n.Name))
}
if n.Name == n.NewName {
// Noop.
return &emptyNode{}, nil
}
// Now update the nameMetadataKey and the descriptor.
descKey := sqlbase.MakeDescMetadataKey(dbDesc.GetID())
dbDesc.SetName(string(n.NewName))
if err := dbDesc.Validate(); err != nil {
return nil, err
}
newKey := databaseKey{string(n.NewName)}.Key()
oldKey := databaseKey{string(n.Name)}.Key()
descID := dbDesc.GetID()
descDesc := sqlbase.WrapDescriptor(dbDesc)
b := client.Batch{}
b.CPut(newKey, descID, nil)
b.Put(descKey, descDesc)
b.Del(oldKey)
if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*roachpb.ConditionFailedError); ok {
return nil, fmt.Errorf("the new database name %q already exists", string(n.NewName))
}
return nil, err
}
p.setTestingVerifyMetadata(func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, newKey, descID); err != nil {
return err
}
if err := expectDescriptor(systemConfig, descKey, descDesc); err != nil {
return err
}
return expectDeleted(systemConfig, oldKey)
})
return &emptyNode{}, nil
}
示例3: flush
// flush writes all dirty nodes and the tree to the transaction.
func (tc *treeContext) flush(b *client.Batch) {
if tc.dirty {
b.Put(keys.RangeTreeRoot, tc.tree)
}
for key, cachedNode := range tc.nodes {
if cachedNode.dirty {
if cachedNode.node == nil {
b.Del(keys.RangeTreeNodeKey(roachpb.RKey(key)))
} else {
b.Put(keys.RangeTreeNodeKey(roachpb.RKey(key)), cachedNode.node)
}
}
}
}
示例4: putMeta
func putMeta(b *client.Batch, key roachpb.Key, desc *roachpb.RangeDescriptor) {
b.Put(key, desc)
}
示例5: updateRow
//.........这里部分代码省略.........
// TODO(dan): This has gotten very similar to the loop in insertRow, see if
// they can be DRY'd. Ideally, this would also work for
// truncateAndBackfillColumnsChunk, which is currently abusing rowUdpdater.
for i, family := range ru.helper.tableDesc.Families {
update := false
for _, colID := range family.ColumnIDs {
if _, ok := ru.updateColIDtoRowIndex[colID]; ok {
update = true
break
}
}
if !update {
continue
}
if i > 0 {
// HACK: MakeFamilyKey appends to its argument, so on every loop iteration
// after the first, trim primaryIndexKey so nothing gets overwritten.
// TODO(dan): Instead of this, use something like engine.ChunkAllocator.
primaryIndexKey = primaryIndexKey[:len(primaryIndexKey):len(primaryIndexKey)]
}
if len(family.ColumnIDs) == 1 && family.ColumnIDs[0] == family.DefaultColumnID {
// Storage optimization to store DefaultColumnID directly as a value. Also
// backwards compatible with the original BaseFormatVersion.
idx, ok := ru.updateColIDtoRowIndex[family.DefaultColumnID]
if !ok {
continue
}
ru.key = keys.MakeFamilyKey(primaryIndexKey, uint32(family.ID))
if log.V(2) {
log.Infof("Put %s -> %v", ru.key, ru.marshalled[idx].PrettyPrint())
}
b.Put(&ru.key, &ru.marshalled[idx])
ru.key = nil
continue
}
ru.key = keys.MakeFamilyKey(primaryIndexKey, uint32(family.ID))
ru.valueBuf = ru.valueBuf[:0]
var lastColID sqlbase.ColumnID
familySortedColumnIDs, ok := ru.helper.sortedColumnFamily(family.ID)
if !ok {
panic("invalid family sorted column id map")
}
for _, colID := range familySortedColumnIDs {
if ru.helper.columnInPK(colID) {
if family.ID != 0 {
return nil, errors.Errorf("primary index column %d must be in family 0, was %d", colID, family.ID)
}
// Skip primary key columns as their values are encoded in the key of
// each family. Family 0 is guaranteed to exist and acts as a sentinel.
continue
}
idx, ok := ru.fetchColIDtoRowIndex[colID]
if !ok {
return nil, errors.Errorf("column %d was expected to be fetched, but wasn't", colID)
}
col := ru.fetchCols[idx]
if ru.newValues[idx].Compare(parser.DNull) == 0 {
示例6: insertPutFn
// insertPutFn is used by insertRow when conflicts should be ignored.
// logValue is used for pretty printing.
func insertPutFn(b *client.Batch, key *roachpb.Key, value *roachpb.Value) {
if log.V(2) {
log.InfofDepth(1, "Put %s -> %s", *key, value.PrettyPrint())
}
b.Put(key, value)
}
示例7: RenameTable
// RenameTable renames the table.
// Privileges: DROP on source table, CREATE on destination database.
// Notes: postgres requires the table owner.
// mysql requires ALTER, DROP on the original table, and CREATE, INSERT
// on the new table (and does not copy privileges over).
func (p *planner) RenameTable(n *parser.RenameTable) (planNode, error) {
if err := n.NewName.NormalizeTableName(p.session.Database); err != nil {
return nil, err
}
if n.NewName.Table() == "" {
return nil, errEmptyTableName
}
if err := n.Name.NormalizeTableName(p.session.Database); err != nil {
return nil, err
}
dbDesc, err := p.getDatabaseDesc(n.Name.Database())
if err != nil {
return nil, err
}
if dbDesc == nil {
return nil, sqlbase.NewUndefinedDatabaseError(n.Name.Database())
}
tbKey := tableKey{dbDesc.ID, n.Name.Table()}.Key()
// Check if table exists.
gr, err := p.txn.Get(tbKey)
if err != nil {
return nil, err
}
if !gr.Exists() {
if n.IfExists {
// Noop.
return &emptyNode{}, nil
}
// Key does not exist, but we want it to: error out.
return nil, fmt.Errorf("table %q does not exist", n.Name.Table())
}
targetDbDesc, err := p.getDatabaseDesc(n.NewName.Database())
if err != nil {
return nil, err
}
if targetDbDesc == nil {
return nil, sqlbase.NewUndefinedDatabaseError(n.NewName.Database())
}
if err := p.checkPrivilege(targetDbDesc, privilege.CREATE); err != nil {
return nil, err
}
if n.Name.Database() == n.NewName.Database() && n.Name.Table() == n.NewName.Table() {
// Noop.
return &emptyNode{}, nil
}
tableDesc, err := p.getTableDesc(n.Name)
if err != nil {
return nil, err
}
if tableDesc == nil || tableDesc.State != sqlbase.TableDescriptor_PUBLIC {
return nil, sqlbase.NewUndefinedTableError(n.Name.String())
}
if err := p.checkPrivilege(tableDesc, privilege.DROP); err != nil {
return nil, err
}
tableDesc.SetName(n.NewName.Table())
tableDesc.ParentID = targetDbDesc.ID
descKey := sqlbase.MakeDescMetadataKey(tableDesc.GetID())
newTbKey := tableKey{targetDbDesc.ID, n.NewName.Table()}.Key()
if err := tableDesc.Validate(); err != nil {
return nil, err
}
descID := tableDesc.GetID()
descDesc := sqlbase.WrapDescriptor(tableDesc)
if err := tableDesc.SetUpVersion(); err != nil {
return nil, err
}
renameDetails := sqlbase.TableDescriptor_RenameInfo{
OldParentID: uint32(dbDesc.ID),
OldName: n.Name.Table()}
tableDesc.Renames = append(tableDesc.Renames, renameDetails)
if err := p.writeTableDesc(tableDesc); err != nil {
return nil, err
}
// We update the descriptor to the new name, but also leave the mapping of the
// old name to the id, so that the name is not reused until the schema changer
// has made sure it's not in use any more.
b := client.Batch{}
b.Put(descKey, descDesc)
//.........这里部分代码省略.........