github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/mockstore/mocktikv/utils.go (about)

     1  // Copyright 2021 TiKV Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/mockstore/mocktikv/utils.go
    19  //
    20  
    21  // Copyright 2021 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package mocktikv
    36  
    37  import "github.com/pingcap/kvproto/pkg/kvrpcpb"
    38  
    39  // PutMutations is exported for testing.
    40  var PutMutations = putMutations
    41  
    42  func putMutations(kvpairs ...string) []*kvrpcpb.Mutation {
    43  	var mutations []*kvrpcpb.Mutation
    44  	for i := 0; i < len(kvpairs); i += 2 {
    45  		mutations = append(mutations, &kvrpcpb.Mutation{
    46  			Op:    kvrpcpb.Op_Put,
    47  			Key:   []byte(kvpairs[i]),
    48  			Value: []byte(kvpairs[i+1]),
    49  		})
    50  	}
    51  	return mutations
    52  }
    53  
    54  // MustPrewrite write mutations to mvcc store.
    55  func MustPrewrite(store MVCCStore, mutations []*kvrpcpb.Mutation, primary string, startTS uint64, ttl uint64) bool {
    56  	return mustPrewriteWithTTL(store, mutations, primary, startTS, ttl)
    57  }
    58  
    59  func mustPrewriteWithTTL(store MVCCStore, mutations []*kvrpcpb.Mutation, primary string, startTS uint64, ttl uint64) bool {
    60  	req := &kvrpcpb.PrewriteRequest{
    61  		Mutations:    mutations,
    62  		PrimaryLock:  []byte(primary),
    63  		StartVersion: startTS,
    64  		LockTtl:      ttl,
    65  		MinCommitTs:  startTS + 1,
    66  	}
    67  	errs := store.Prewrite(req)
    68  	for _, err := range errs {
    69  		if err != nil {
    70  			return false
    71  		}
    72  	}
    73  	return true
    74  }