khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 | "fmt" |
| 20 | |
| 21 | "go.etcd.io/etcd/mvcc/backend" |
| 22 | "go.etcd.io/etcd/mvcc/mvccpb" |
| 23 | ) |
| 24 | |
| 25 | func UpdateConsistentIndex(be backend.Backend, index uint64) { |
| 26 | tx := be.BatchTx() |
| 27 | tx.Lock() |
| 28 | defer tx.Unlock() |
| 29 | |
| 30 | var oldi uint64 |
| 31 | _, vs := tx.UnsafeRange(metaBucketName, consistentIndexKeyName, nil, 0) |
| 32 | if len(vs) != 0 { |
| 33 | oldi = binary.BigEndian.Uint64(vs[0]) |
| 34 | } |
| 35 | |
| 36 | if index <= oldi { |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | bs := make([]byte, 8) |
| 41 | binary.BigEndian.PutUint64(bs, index) |
| 42 | tx.UnsafePut(metaBucketName, consistentIndexKeyName, bs) |
| 43 | } |
| 44 | |
| 45 | func WriteKV(be backend.Backend, kv mvccpb.KeyValue) { |
| 46 | ibytes := newRevBytes() |
| 47 | revToBytes(revision{main: kv.ModRevision}, ibytes) |
| 48 | |
| 49 | d, err := kv.Marshal() |
| 50 | if err != nil { |
| 51 | panic(fmt.Errorf("cannot marshal event: %v", err)) |
| 52 | } |
| 53 | |
| 54 | be.BatchTx().Lock() |
| 55 | be.BatchTx().UnsafePut(keyBucketName, ibytes, d) |
| 56 | be.BatchTx().Unlock() |
| 57 | } |