github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/batcheval/cmd_delete_range.go (about)

     1  // Copyright 2014 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package batcheval
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/batcheval/result"
    17  	"github.com/cockroachdb/cockroach/pkg/kv/kvserver/spanset"
    18  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    19  	"github.com/cockroachdb/cockroach/pkg/storage"
    20  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    21  )
    22  
    23  func init() {
    24  	RegisterReadWriteCommand(roachpb.DeleteRange, declareKeysDeleteRange, DeleteRange)
    25  }
    26  
    27  func declareKeysDeleteRange(
    28  	desc *roachpb.RangeDescriptor,
    29  	header roachpb.Header,
    30  	req roachpb.Request,
    31  	latchSpans, lockSpans *spanset.SpanSet,
    32  ) {
    33  	args := req.(*roachpb.DeleteRangeRequest)
    34  	if args.Inline {
    35  		DefaultDeclareKeys(desc, header, req, latchSpans, lockSpans)
    36  	} else {
    37  		DefaultDeclareIsolatedKeys(desc, header, req, latchSpans, lockSpans)
    38  	}
    39  }
    40  
    41  // DeleteRange deletes the range of key/value pairs specified by
    42  // start and end keys.
    43  func DeleteRange(
    44  	ctx context.Context, readWriter storage.ReadWriter, cArgs CommandArgs, resp roachpb.Response,
    45  ) (result.Result, error) {
    46  	args := cArgs.Args.(*roachpb.DeleteRangeRequest)
    47  	h := cArgs.Header
    48  	reply := resp.(*roachpb.DeleteRangeResponse)
    49  
    50  	var timestamp hlc.Timestamp
    51  	if !args.Inline {
    52  		timestamp = h.Timestamp
    53  	}
    54  	// NB: Even if args.ReturnKeys is false, we want to know which intents were
    55  	// written if we're evaluating the DeleteRange for a transaction so that we
    56  	// can update the Result's AcquiredLocks field.
    57  	returnKeys := args.ReturnKeys || h.Txn != nil
    58  	deleted, resumeSpan, num, err := storage.MVCCDeleteRange(
    59  		ctx, readWriter, cArgs.Stats, args.Key, args.EndKey, h.MaxSpanRequestKeys, timestamp, h.Txn, returnKeys,
    60  	)
    61  	if err == nil && args.ReturnKeys {
    62  		reply.Keys = deleted
    63  	}
    64  	reply.NumKeys = num
    65  	if resumeSpan != nil {
    66  		reply.ResumeSpan = resumeSpan
    67  		reply.ResumeReason = roachpb.RESUME_KEY_LIMIT
    68  	}
    69  	// NB: even if MVCC returns an error, it may still have written an intent
    70  	// into the batch. This allows callers to consume errors like WriteTooOld
    71  	// without re-evaluating the batch. This behavior isn't particularly
    72  	// desirable, but while it remains, we need to assume that an intent could
    73  	// have been written even when an error is returned. This is harmless if the
    74  	// error is not consumed by the caller because the result will be discarded.
    75  	return result.FromAcquiredLocks(h.Txn, deleted...), err
    76  }