util.go 1.4 KB

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