本文整理汇总了Golang中github.com/cockroachdb/cockroach/client.Batch.Del方法的典型用法代码示例。如果您正苦于以下问题:Golang Batch.Del方法的具体用法?Golang Batch.Del怎么用?Golang Batch.Del使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/cockroachdb/cockroach/client.Batch
的用法示例。
在下文中一共展示了Batch.Del方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: runDel
func runDel(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
return
}
count := len(args)
keys := make([]string, 0, count)
for i := 0; i < count; i++ {
keys = append(keys, unquoteArg(args[i], true /* disallow system keys */))
}
kvDB := makeDBClient()
if kvDB == nil {
return
}
var b client.Batch
for i := 0; i < count; i++ {
b.Del(keys[i])
}
if err := kvDB.Run(&b); err != nil {
fmt.Fprintf(osStderr, "delete failed: %s\n", err)
osExit(1)
return
}
}
示例2: deleteRow
// deleteRow adds to the batch the kv operations necessary to delete a table row
// with the given values.
func (rd *rowDeleter) deleteRow(b *client.Batch, values []parser.Datum) error {
if err := rd.fks.checkAll(values); err != nil {
return err
}
primaryIndexKey, secondaryIndexEntries, err := rd.helper.encodeIndexes(rd.fetchColIDtoRowIndex, values)
if err != nil {
return err
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof("Del %s", secondaryIndexEntry.Key)
}
b.Del(secondaryIndexEntry.Key)
}
// Delete the row.
rd.startKey = roachpb.Key(primaryIndexKey)
rd.endKey = rd.startKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %s - %s", rd.startKey, rd.endKey)
}
b.DelRange(&rd.startKey, &rd.endKey, false)
rd.startKey, rd.endKey = nil, nil
return nil
}
示例3: 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, databaseDoesNotExistError(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
}
示例4: 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)
}
}
}
}
示例5: runDel
func runDel(cmd *cobra.Command, args []string) {
if len(args) == 0 {
mustUsage(cmd)
return
}
var b client.Batch
for _, arg := range args {
b.Del(unquoteArg(arg, true /* disallow system keys */))
}
kvDB, stopper := makeDBClient()
defer stopper.Stop()
if err := kvDB.Run(&b); err != nil {
panicf("delete failed: %s", err)
}
}
示例6: RenameDatabase
// RenameDatabase renames the database.
// Privileges: "root" 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.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 n.Name == n.NewName {
// Noop.
return &valuesNode{}, nil
}
// Now update the nameMetadataKey and the descriptor.
descKey := MakeDescMetadataKey(dbDesc.GetID())
dbDesc.SetName(string(n.NewName))
if err := dbDesc.Validate(); err != nil {
return nil, err
}
b := client.Batch{}
b.CPut(databaseKey{string(n.NewName)}.Key(), dbDesc.GetID(), nil)
b.Put(descKey, dbDesc)
b.Del(databaseKey{string(n.Name)}.Key())
// Mark transaction as operating on the system DB.
p.txn.SetSystemDBTrigger()
if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*proto.ConditionFailedError); ok {
return nil, fmt.Errorf("the new database name %q already exists", string(n.NewName))
}
return nil, err
}
return &valuesNode{}, nil
}
示例7: runDel
func runDel(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
return
}
var b client.Batch
for _, arg := range args {
b.Del(unquoteArg(arg, true /* disallow system keys */))
}
kvDB, stopper := makeDBClient()
defer stopper.Stop()
if err := kvDB.Run(&b); err != nil {
fmt.Fprintf(osStderr, "delete failed: %s\n", err)
osExit(1)
return
}
}
示例8: RenameTable
// RenameTable renames the table.
// Privileges: WRITE on 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 n.NewName == "" {
return nil, errEmptyTableName
}
if err := n.Name.NormalizeTableName(p.session.Database); err != nil {
return nil, err
}
if n.Name.Table() == string(n.NewName) {
// Noop.
return &valuesNode{}, nil
}
dbDesc, err := p.getDatabaseDesc(n.Name.Database())
if err != nil {
return nil, err
}
tbKey := tableKey{dbDesc.ID, string(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 &valuesNode{}, nil
}
// Key does not exist, but we want it to: error out.
return nil, fmt.Errorf("table %q does not exist", n.Name.Table())
}
if err := p.checkPrivilege(dbDesc, privilege.WRITE); err != nil {
return nil, err
}
tableDesc, err := p.getTableDesc(n.Name)
if err != nil {
return nil, err
}
tableDesc.SetName(string(n.NewName))
newTbKey := tableKey{dbDesc.ID, string(n.NewName)}.Key()
descKey := MakeDescMetadataKey(tableDesc.GetID())
b := client.Batch{}
b.Put(descKey, tableDesc)
b.CPut(newTbKey, descKey, nil)
b.Del(tbKey)
if err := p.txn.Run(&b); err != nil {
if _, ok := err.(*proto.ConditionFailedError); ok {
return nil, fmt.Errorf("table name %q already exists", n.NewName)
}
return nil, err
}
return &valuesNode{}, nil
}
示例9: 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, *roachpb.Error) {
if err := n.NewName.NormalizeTableName(p.session.Database); err != nil {
return nil, roachpb.NewError(err)
}
if n.NewName.Table() == "" {
return nil, roachpb.NewError(errEmptyTableName)
}
if err := n.Name.NormalizeTableName(p.session.Database); err != nil {
return nil, roachpb.NewError(err)
}
dbDesc, pErr := p.getDatabaseDesc(n.Name.Database())
if pErr != nil {
return nil, pErr
}
tbKey := tableKey{dbDesc.ID, n.Name.Table()}.Key()
// Check if table exists.
gr, pErr := p.txn.Get(tbKey)
if pErr != nil {
return nil, pErr
}
if !gr.Exists() {
if n.IfExists {
// Noop.
return &valuesNode{}, nil
}
// Key does not exist, but we want it to: error out.
return nil, roachpb.NewUErrorf("table %q does not exist", n.Name.Table())
}
targetDbDesc, pErr := p.getDatabaseDesc(n.NewName.Database())
if pErr != nil {
return nil, pErr
}
if pErr := p.checkPrivilege(targetDbDesc, privilege.CREATE); pErr != nil {
return nil, pErr
}
if n.Name.Database() == n.NewName.Database() && n.Name.Table() == n.NewName.Table() {
// Noop.
return &valuesNode{}, nil
}
tableDesc, pErr := p.getTableDesc(n.Name)
if pErr != nil {
return nil, pErr
}
if pErr := p.checkPrivilege(tableDesc, privilege.DROP); pErr != nil {
return nil, pErr
}
tableDesc.SetName(n.NewName.Table())
tableDesc.ParentID = targetDbDesc.ID
descKey := MakeDescMetadataKey(tableDesc.GetID())
newTbKey := tableKey{targetDbDesc.ID, n.NewName.Table()}.Key()
if err := tableDesc.Validate(); err != nil {
return nil, roachpb.NewError(err)
}
descID := tableDesc.GetID()
descDesc := wrapDescriptor(tableDesc)
b := client.Batch{}
b.Put(descKey, descDesc)
b.CPut(newTbKey, descID, nil)
b.Del(tbKey)
if pErr := p.txn.Run(&b); pErr != nil {
if _, ok := pErr.GoError().(*roachpb.ConditionFailedError); ok {
return nil, roachpb.NewUErrorf("table name %q already exists", n.NewName.Table())
}
return nil, pErr
}
p.testingVerifyMetadata = func(systemConfig config.SystemConfig) error {
if err := expectDescriptorID(systemConfig, newTbKey, descID); err != nil {
return err
}
if err := expectDescriptor(systemConfig, descKey, descDesc); err != nil {
return err
}
return expectDeleted(systemConfig, tbKey)
}
return &valuesNode{}, nil
}
示例10: Delete
// Delete deletes rows from a table.
// Privileges: WRITE and READ on table. We currently always use a SELECT statement.
// Notes: postgres requires DELETE. Also requires SELECT for "USING" and "WHERE" with tables.
// mysql requires DELETE. Also requires SELECT if a table is used in the "WHERE" clause.
func (p *planner) Delete(n *parser.Delete) (planNode, error) {
tableDesc, err := p.getAliasedTableDesc(n.Table)
if err != nil {
return nil, err
}
if !tableDesc.HasPrivilege(p.user, parser.PrivilegeWrite) {
return nil, fmt.Errorf("user %s does not have %s privilege on table %s",
p.user, parser.PrivilegeWrite, tableDesc.Name)
}
// TODO(tamird,pmattis): avoid going through Select to avoid encoding
// and decoding keys. Also, avoiding Select may provide more
// convenient access to index keys which we are not currently
// deleting.
node, err := p.Select(&parser.Select{
Exprs: parser.SelectExprs{
&parser.StarExpr{TableName: &parser.QualifiedName{Base: parser.Name(tableDesc.Name)}},
},
From: parser.TableExprs{n.Table},
Where: n.Where,
})
if err != nil {
return nil, err
}
// Construct a map from column ID to the index the value appears at within a
// row.
colIDtoRowIndex := map[structured.ID]int{}
for i, name := range node.Columns() {
c, err := tableDesc.FindColumnByName(name)
if err != nil {
return nil, err
}
colIDtoRowIndex[c.ID] = i
}
primaryIndex := tableDesc.PrimaryIndex
primaryIndexKeyPrefix := structured.MakeIndexKeyPrefix(tableDesc.ID, primaryIndex.ID)
b := client.Batch{}
for node.Next() {
if err := node.Err(); err != nil {
return nil, err
}
values := node.Values()
primaryIndexKeySuffix, _, err := encodeIndexKey(primaryIndex.ColumnIDs, colIDtoRowIndex, values, nil)
if err != nil {
return nil, err
}
primaryIndexKey := bytes.Join([][]byte{primaryIndexKeyPrefix, primaryIndexKeySuffix}, nil)
// Delete the secondary indexes.
secondaryIndexEntries, err := encodeSecondaryIndexes(tableDesc.ID, tableDesc.Indexes, colIDtoRowIndex, values, primaryIndexKeySuffix)
if err != nil {
return nil, err
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof("Del %q", secondaryIndexEntry.key)
}
b.Del(secondaryIndexEntry.key)
}
// Delete the row.
rowStartKey := proto.Key(primaryIndexKey)
rowEndKey := rowStartKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %q - %q", rowStartKey, rowEndKey)
}
b.DelRange(rowStartKey, rowEndKey)
}
if err := p.db.Run(&b); err != nil {
return nil, err
}
// TODO(tamird/pmattis): return the number of affected rows
return &valuesNode{}, nil
}
示例11: Update
//.........这里部分代码省略.........
// Secondary indexes needing updating.
var indexes []IndexDescriptor
for _, index := range tableDesc.Indexes {
for _, id := range index.ColumnIDs {
if _, ok := colIDSet[id]; ok {
indexes = append(indexes, index)
break
}
}
}
// Update all the rows.
var b client.Batch
for rows.Next() {
rowVals := rows.Values()
primaryIndexKey, _, err := encodeIndexKey(
primaryIndex.ColumnIDs, colIDtoRowIndex, rowVals, primaryIndexKeyPrefix)
if err != nil {
return nil, err
}
// Compute the current secondary index key:value pairs for this row.
secondaryIndexEntries, err := encodeSecondaryIndexes(
tableDesc.ID, indexes, colIDtoRowIndex, rowVals)
if err != nil {
return nil, err
}
// Our updated value expressions occur immediately after the plain
// columns in the output.
newVals := rowVals[len(tableDesc.Columns):]
// Update the row values.
for i, col := range cols {
val := newVals[i]
if !col.Nullable && val == parser.DNull {
return nil, fmt.Errorf("null value in column %q violates not-null constraint", col.Name)
}
rowVals[colIDtoRowIndex[col.ID]] = val
}
// Compute the new secondary index key:value pairs for this row.
newSecondaryIndexEntries, err := encodeSecondaryIndexes(
tableDesc.ID, indexes, colIDtoRowIndex, rowVals)
if err != nil {
return nil, err
}
// Update secondary indexes.
for i, newSecondaryIndexEntry := range newSecondaryIndexEntries {
secondaryIndexEntry := secondaryIndexEntries[i]
if !bytes.Equal(newSecondaryIndexEntry.key, secondaryIndexEntry.key) {
if log.V(2) {
log.Infof("CPut %q -> %v", newSecondaryIndexEntry.key, newSecondaryIndexEntry.value)
}
b.CPut(newSecondaryIndexEntry.key, newSecondaryIndexEntry.value, nil)
if log.V(2) {
log.Infof("Del %q", secondaryIndexEntry.key)
}
b.Del(secondaryIndexEntry.key)
}
}
// Add the new values.
for i, val := range newVals {
col := cols[i]
primitive, err := convertDatum(col, val)
if err != nil {
return nil, err
}
key := MakeColumnKey(col.ID, primaryIndexKey)
if primitive != nil {
// We only output non-NULL values. Non-existent column keys are
// considered NULL during scanning and the row sentinel ensures we know
// the row exists.
if log.V(2) {
log.Infof("Put %q -> %v", key, val)
}
b.Put(key, primitive)
} else {
// The column might have already existed but is being set to NULL, so
// delete it.
if log.V(2) {
log.Infof("Del %q", key)
}
b.Del(key)
}
}
}
if err := rows.Err(); err != nil {
return nil, err
}
if err := p.txn.Run(&b); err != nil {
return nil, convertBatchError(tableDesc, b, err)
}
// TODO(tamird/pmattis): return the number of affected rows.
return &valuesNode{}, nil
}
示例12: backfillBatch
func (p *planner) backfillBatch(b *client.Batch, oldTableDesc *TableDescriptor, mutationID MutationID) *roachpb.Error {
var droppedColumnDescs []ColumnDescriptor
var droppedIndexDescs []IndexDescriptor
var newIndexDescs []IndexDescriptor
// Collect the elements that are part of the mutation.
for _, m := range oldTableDesc.Mutations {
if m.MutationID != mutationID {
// Mutations are applied in a FIFO order. Only apply the first set of
// mutations if they have the mutation ID we're looking for.
break
}
switch m.Direction {
case DescriptorMutation_ADD:
switch t := m.Descriptor_.(type) {
case *DescriptorMutation_Column:
// TODO(vivek): Add column to new columns and use it
// to fill in default values.
case *DescriptorMutation_Index:
newIndexDescs = append(newIndexDescs, *t.Index)
}
case DescriptorMutation_DROP:
switch t := m.Descriptor_.(type) {
case *DescriptorMutation_Column:
droppedColumnDescs = append(droppedColumnDescs, *t.Column)
case *DescriptorMutation_Index:
droppedIndexDescs = append(droppedIndexDescs, *t.Index)
}
}
}
// TODO(vivek): Break these backfill operations into chunks. All of them
// will fail on big tables (see #3274).
// Delete the entire dropped columns.
// This used to use SQL UPDATE in the past to update the dropped
// column to NULL; but a column in the process of being
// dropped is placed in the table descriptor mutations, and
// a SQL UPDATE of a column in mutations will fail.
if len(droppedColumnDescs) > 0 {
// Run a scan across the table using the primary key.
start := roachpb.Key(MakeIndexKeyPrefix(oldTableDesc.ID, oldTableDesc.PrimaryIndex.ID))
// Use a different batch to perform the scan.
batch := &client.Batch{}
batch.Scan(start, start.PrefixEnd(), 0)
if pErr := p.txn.Run(batch); pErr != nil {
return pErr
}
for _, result := range batch.Results {
var sentinelKey roachpb.Key
for _, kv := range result.Rows {
if sentinelKey == nil || !bytes.HasPrefix(kv.Key, sentinelKey) {
// Sentinel keys have a 0 suffix indicating 0 bytes of column
// ID. Strip off that suffix to determine the prefix shared with the
// other keys for the row.
sentinelKey = stripColumnIDLength(kv.Key)
for _, columnDesc := range droppedColumnDescs {
// Delete the dropped column.
colKey := keys.MakeColumnKey(sentinelKey, uint32(columnDesc.ID))
if log.V(2) {
log.Infof("Del %s", colKey)
}
b.Del(colKey)
}
}
}
}
}
for _, indexDescriptor := range droppedIndexDescs {
indexPrefix := MakeIndexKeyPrefix(oldTableDesc.ID, indexDescriptor.ID)
// Delete the index.
indexStartKey := roachpb.Key(indexPrefix)
indexEndKey := indexStartKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %s - %s", indexStartKey, indexEndKey)
}
b.DelRange(indexStartKey, indexEndKey)
}
if len(newIndexDescs) > 0 {
// Get all the rows affected.
// TODO(vivek): Avoid going through Select.
// TODO(tamird): Support partial indexes?
// Use a scanNode with SELECT to pass in a TableDescriptor
// to the SELECT without needing to use a parser.QualifiedName,
// because we want to run schema changes from a gossip feed of
// table IDs.
scan := &scanNode{
planner: p,
txn: p.txn,
desc: oldTableDesc,
}
scan.initDescDefaults()
rows, pErr := p.selectIndex(&selectNode{}, scan, nil, false)
if pErr != nil {
return pErr
//.........这里部分代码省略.........
示例13: updateRow
// updateRow adds to the batch the kv operations necessary to update a table row
// with the given values.
//
// The row corresponding to oldValues is updated with the ones in updateValues.
// Note that updateValues only contains the ones that are changing.
//
// The return value is only good until the next call to UpdateRow.
func (ru *rowUpdater) updateRow(
b *client.Batch,
oldValues []parser.Datum,
updateValues []parser.Datum,
) ([]parser.Datum, error) {
if len(oldValues) != len(ru.fetchCols) {
return nil, util.Errorf("got %d values but expected %d", len(oldValues), len(ru.fetchCols))
}
if len(updateValues) != len(ru.updateCols) {
return nil, util.Errorf("got %d values but expected %d", len(updateValues), len(ru.updateCols))
}
primaryIndexKey, secondaryIndexEntries, err := ru.helper.encodeIndexes(ru.fetchColIDtoRowIndex, oldValues)
if err != nil {
return nil, err
}
// Check that the new value types match the column types. This needs to
// happen before index encoding because certain datum types (i.e. tuple)
// cannot be used as index values.
for i, val := range updateValues {
if ru.marshalled[i], err = sqlbase.MarshalColumnValue(ru.updateCols[i], val); err != nil {
return nil, err
}
}
// Update the row values.
copy(ru.newValues, oldValues)
for i, updateCol := range ru.updateCols {
ru.newValues[ru.fetchColIDtoRowIndex[updateCol.ID]] = updateValues[i]
}
newPrimaryIndexKey := primaryIndexKey
rowPrimaryKeyChanged := false
var newSecondaryIndexEntries []sqlbase.IndexEntry
if ru.primaryKeyColChange {
newPrimaryIndexKey, newSecondaryIndexEntries, err = ru.helper.encodeIndexes(ru.fetchColIDtoRowIndex, ru.newValues)
if err != nil {
return nil, err
}
rowPrimaryKeyChanged = !bytes.Equal(primaryIndexKey, newPrimaryIndexKey)
} else {
newSecondaryIndexEntries, err = sqlbase.EncodeSecondaryIndexes(
ru.helper.tableDesc.ID, ru.helper.indexes, ru.fetchColIDtoRowIndex, ru.newValues)
if err != nil {
return nil, err
}
}
if rowPrimaryKeyChanged {
err := ru.rd.deleteRow(b, oldValues)
if err != nil {
return nil, err
}
err = ru.ri.insertRow(b, ru.newValues)
return ru.newValues, err
}
// Update secondary indexes.
for i, newSecondaryIndexEntry := range newSecondaryIndexEntries {
secondaryIndexEntry := secondaryIndexEntries[i]
secondaryKeyChanged := !bytes.Equal(newSecondaryIndexEntry.Key, secondaryIndexEntry.Key)
if secondaryKeyChanged {
if log.V(2) {
log.Infof("Del %s", secondaryIndexEntry.Key)
}
b.Del(secondaryIndexEntry.Key)
// Do not update Indexes in the DELETE_ONLY state.
if _, ok := ru.deleteOnlyIndex[i]; !ok {
if log.V(2) {
log.Infof("CPut %s -> %v", newSecondaryIndexEntry.Key, newSecondaryIndexEntry.Value)
}
b.CPut(newSecondaryIndexEntry.Key, newSecondaryIndexEntry.Value, nil)
}
}
}
// Add the new values.
for i, val := range updateValues {
col := ru.updateCols[i]
if ru.helper.columnInPK(col.ID) {
// Skip primary key columns as their values are encoded in the row
// sentinel key which is guaranteed to exist for as long as the row
// exists.
continue
}
ru.key = keys.MakeColumnKey(newPrimaryIndexKey, uint32(col.ID))
if ru.marshalled[i].RawBytes != nil {
// We only output non-NULL values. Non-existent column keys are
// considered NULL during scanning and the row sentinel ensures we know
// the row exists.
//.........这里部分代码省略.........
示例14: Delete
// Delete deletes rows from a table.
// Privileges: DELETE and SELECT on table. We currently always use a SELECT statement.
// Notes: postgres requires DELETE. Also requires SELECT for "USING" and "WHERE" with tables.
// mysql requires DELETE. Also requires SELECT if a table is used in the "WHERE" clause.
func (p *planner) Delete(n *parser.Delete) (planNode, error) {
tableDesc, err := p.getAliasedTableDesc(n.Table, false /* !allowCache */)
if err != nil {
return nil, err
}
if err := p.checkPrivilege(tableDesc, privilege.DELETE); err != nil {
return nil, err
}
// TODO(tamird,pmattis): avoid going through Select to avoid encoding
// and decoding keys. Also, avoiding Select may provide more
// convenient access to index keys which we are not currently
// deleting.
rows, err := p.Select(&parser.Select{
Exprs: parser.SelectExprs{parser.StarSelectExpr()},
From: parser.TableExprs{n.Table},
Where: n.Where,
})
if err != nil {
return nil, err
}
// Construct a map from column ID to the index the value appears at within a
// row.
colIDtoRowIndex := map[ColumnID]int{}
for i, name := range rows.Columns() {
c, err := tableDesc.FindColumnByName(name)
if err != nil {
return nil, err
}
colIDtoRowIndex[c.ID] = i
}
primaryIndex := tableDesc.PrimaryIndex
primaryIndexKeyPrefix := MakeIndexKeyPrefix(tableDesc.ID, primaryIndex.ID)
b := client.Batch{}
result := &valuesNode{}
for rows.Next() {
rowVals := rows.Values()
result.rows = append(result.rows, parser.DTuple(nil))
primaryIndexKey, _, err := encodeIndexKey(
primaryIndex.ColumnIDs, colIDtoRowIndex, rowVals, primaryIndexKeyPrefix)
if err != nil {
return nil, err
}
// Delete the secondary indexes.
secondaryIndexEntries, err := encodeSecondaryIndexes(
tableDesc.ID, tableDesc.Indexes, colIDtoRowIndex, rowVals)
if err != nil {
return nil, err
}
for _, secondaryIndexEntry := range secondaryIndexEntries {
if log.V(2) {
log.Infof("Del %s", prettyKey(secondaryIndexEntry.key, 0))
}
b.Del(secondaryIndexEntry.key)
}
// Delete the row.
rowStartKey := roachpb.Key(primaryIndexKey)
rowEndKey := rowStartKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %s - %s", prettyKey(rowStartKey, 0), prettyKey(rowEndKey, 0))
}
b.DelRange(rowStartKey, rowEndKey)
}
if err := rows.Err(); err != nil {
return nil, err
}
if err := p.txn.Run(&b); err != nil {
return nil, err
}
return result, nil
}
示例15: backfillBatch
func (p *planner) backfillBatch(b *client.Batch, oldTableDesc, newTableDesc *TableDescriptor) error {
var droppedColumnDescs []ColumnDescriptor
var droppedIndexDescs []IndexDescriptor
var newIndexDescs []IndexDescriptor
for _, m := range oldTableDesc.Mutations {
switch m.Direction {
case DescriptorMutation_ADD:
switch t := m.Descriptor_.(type) {
case *DescriptorMutation_Column:
// TODO(vivek): Add column to new columns and use it
// to fill in default values.
case *DescriptorMutation_Index:
newIndexDescs = append(newIndexDescs, *t.Index)
}
case DescriptorMutation_DROP:
switch t := m.Descriptor_.(type) {
case *DescriptorMutation_Column:
droppedColumnDescs = append(droppedColumnDescs, *t.Column)
case *DescriptorMutation_Index:
droppedIndexDescs = append(droppedIndexDescs, *t.Index)
}
}
}
// TODO(vivek): Break these backfill operations into chunks. All of them
// will fail on big tables.
// Delete the entire dropped columns.
// This used to use SQL UPDATE in the past to update the dropped
// column to NULL; but a column in the process of being
// dropped is placed in the table descriptor mutations, and
// a SQL UPDATE of a column in mutations will fail.
if len(droppedColumnDescs) > 0 {
// Run a scan across the table using the primary key.
start := roachpb.Key(MakeIndexKeyPrefix(newTableDesc.ID, newTableDesc.PrimaryIndex.ID))
// Use a different batch to perform the scan.
batch := &client.Batch{}
batch.Scan(start, start.PrefixEnd(), 0)
if err := p.txn.Run(batch); err != nil {
return err
}
for _, result := range batch.Results {
var sentinelKey roachpb.Key
for _, kv := range result.Rows {
if sentinelKey == nil || !bytes.HasPrefix(kv.Key, sentinelKey) {
// Sentinel keys have a 0 suffix indicating 0 bytes of column
// ID. Strip off that suffix to determine the prefix shared with the
// other keys for the row.
sentinelKey = stripColumnIDLength(kv.Key)
for _, columnDesc := range droppedColumnDescs {
// Delete the dropped column.
colKey := keys.MakeColumnKey(sentinelKey, uint32(columnDesc.ID))
if log.V(2) {
log.Infof("Del %s", colKey)
}
b.Del(colKey)
}
}
}
}
}
for _, indexDescriptor := range droppedIndexDescs {
indexPrefix := MakeIndexKeyPrefix(newTableDesc.ID, indexDescriptor.ID)
// Delete the index.
indexStartKey := roachpb.Key(indexPrefix)
indexEndKey := indexStartKey.PrefixEnd()
if log.V(2) {
log.Infof("DelRange %s - %s", indexStartKey, indexEndKey)
}
b.DelRange(indexStartKey, indexEndKey)
}
if len(newIndexDescs) > 0 {
// Get all the rows affected.
// TODO(vivek): Avoid going through Select.
// TODO(tamird): Support partial indexes?
// Use a scanNode with SELECT to pass in a TableDescriptor
// to the SELECT without needing to use a parser.QualifiedName,
// because we want to run schema changes from a gossip feed of
// table IDs.
scan := &scanNode{
planner: p,
txn: p.txn,
desc: oldTableDesc,
}
scan.initDescDefaults()
rows, err := p.selectWithScan(scan, &parser.Select{Exprs: oldTableDesc.allColumnsSelector()})
if err != nil {
return err
}
// Construct a map from column ID to the index the value appears at within a
// row.
colIDtoRowIndex, err := makeColIDtoRowIndex(rows, oldTableDesc)
if err != nil {
//.........这里部分代码省略.........