util.go 1.4 KB

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