go.etcd.io/etcd@v3.3.27+incompatible/mvcc/util.go (about)

     1  // Copyright 2016 The etcd 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  package mvcc
    16  
    17  import (
    18  	"encoding/binary"
    19  
    20  	"github.com/coreos/etcd/mvcc/backend"
    21  	"github.com/coreos/etcd/mvcc/mvccpb"
    22  )
    23  
    24  func UpdateConsistentIndex(be backend.Backend, index uint64) {
    25  	tx := be.BatchTx()
    26  	tx.Lock()
    27  	defer tx.Unlock()
    28  
    29  	var oldi uint64
    30  	_, vs := tx.UnsafeRange(metaBucketName, consistentIndexKeyName, nil, 0)
    31  	if len(vs) != 0 {
    32  		oldi = binary.BigEndian.Uint64(vs[0])
    33  	}
    34  
    35  	if index <= oldi {
    36  		return
    37  	}
    38  
    39  	bs := make([]byte, 8)
    40  	binary.BigEndian.PutUint64(bs, index)
    41  	tx.UnsafePut(metaBucketName, consistentIndexKeyName, bs)
    42  }
    43  
    44  func WriteKV(be backend.Backend, kv mvccpb.KeyValue) {
    45  	ibytes := newRevBytes()
    46  	revToBytes(revision{main: kv.ModRevision}, ibytes)
    47  
    48  	d, err := kv.Marshal()
    49  	if err != nil {
    50  		plog.Fatalf("cannot marshal event: %v", err)
    51  	}
    52  
    53  	be.BatchTx().Lock()
    54  	be.BatchTx().UnsafePut(keyBucketName, ibytes, d)
    55  	be.BatchTx().Unlock()
    56  }