...

Source file src/go.etcd.io/etcd/server/v3/mvcc/util.go

Documentation: go.etcd.io/etcd/server/v3/mvcc

     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  	"fmt"
    19  
    20  	"go.etcd.io/etcd/api/v3/mvccpb"
    21  	"go.etcd.io/etcd/server/v3/mvcc/backend"
    22  	"go.etcd.io/etcd/server/v3/mvcc/buckets"
    23  )
    24  
    25  func WriteKV(be backend.Backend, kv mvccpb.KeyValue) {
    26  	ibytes := newRevBytes()
    27  	revToBytes(revision{main: kv.ModRevision}, ibytes)
    28  
    29  	d, err := kv.Marshal()
    30  	if err != nil {
    31  		panic(fmt.Errorf("cannot marshal event: %v", err))
    32  	}
    33  
    34  	be.BatchTx().LockOutsideApply()
    35  	be.BatchTx().UnsafePut(buckets.Key, ibytes, d)
    36  	be.BatchTx().Unlock()
    37  }
    38  
    39  func UnsafeSetScheduledCompact(tx backend.BatchTx, value int64) {
    40  	rbytes := newRevBytes()
    41  	revToBytes(revision{main: value}, rbytes)
    42  	tx.UnsafePut(buckets.Meta, scheduledCompactKeyName, rbytes)
    43  }
    44  
    45  func UnsafeReadFinishedCompact(tx backend.ReadTx) (int64, bool) {
    46  	_, finishedCompactBytes := tx.UnsafeRange(buckets.Meta, finishedCompactKeyName, nil, 0)
    47  	if len(finishedCompactBytes) != 0 {
    48  		return bytesToRev(finishedCompactBytes[0]).main, true
    49  	}
    50  	return 0, false
    51  }
    52  
    53  func UnsafeReadScheduledCompact(tx backend.ReadTx) (int64, bool) {
    54  	_, scheduledCompactBytes := tx.UnsafeRange(buckets.Meta, scheduledCompactKeyName, nil, 0)
    55  	if len(scheduledCompactBytes) != 0 {
    56  		return bytesToRev(scheduledCompactBytes[0]).main, true
    57  	}
    58  	return 0, false
    59  }
    60  

View as plain text