github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/batcheval/cmd_put.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.Put, declareKeysPut, Put) 25 } 26 27 func declareKeysPut( 28 desc *roachpb.RangeDescriptor, 29 header roachpb.Header, 30 req roachpb.Request, 31 latchSpans, lockSpans *spanset.SpanSet, 32 ) { 33 args := req.(*roachpb.PutRequest) 34 if args.Inline { 35 DefaultDeclareKeys(desc, header, req, latchSpans, lockSpans) 36 } else { 37 DefaultDeclareIsolatedKeys(desc, header, req, latchSpans, lockSpans) 38 } 39 } 40 41 // Put sets the value for a specified key. 42 func Put( 43 ctx context.Context, readWriter storage.ReadWriter, cArgs CommandArgs, resp roachpb.Response, 44 ) (result.Result, error) { 45 args := cArgs.Args.(*roachpb.PutRequest) 46 h := cArgs.Header 47 ms := cArgs.Stats 48 49 var ts hlc.Timestamp 50 if !args.Inline { 51 ts = h.Timestamp 52 } 53 if h.DistinctSpans { 54 if b, ok := readWriter.(storage.Batch); ok { 55 // Use the distinct batch for both blind and normal ops so that we don't 56 // accidentally flush mutations to make them visible to the distinct 57 // batch. 58 readWriter = b.Distinct() 59 defer readWriter.Close() 60 } 61 } 62 var err error 63 if args.Blind { 64 err = storage.MVCCBlindPut(ctx, readWriter, ms, args.Key, ts, args.Value, h.Txn) 65 } else { 66 err = storage.MVCCPut(ctx, readWriter, ms, args.Key, ts, args.Value, h.Txn) 67 } 68 // NB: even if MVCC returns an error, it may still have written an intent 69 // into the batch. This allows callers to consume errors like WriteTooOld 70 // without re-evaluating the batch. This behavior isn't particularly 71 // desirable, but while it remains, we need to assume that an intent could 72 // have been written even when an error is returned. This is harmless if the 73 // error is not consumed by the caller because the result will be discarded. 74 return result.FromAcquiredLocks(h.Txn, args.Key), err 75 }