khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2015 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 "encoding/binary" |
| 18 | |
| 19 | // revBytesLen is the byte length of a normal revision. |
| 20 | // First 8 bytes is the revision.main in big-endian format. The 9th byte |
| 21 | // is a '_'. The last 8 bytes is the revision.sub in big-endian format. |
| 22 | const revBytesLen = 8 + 1 + 8 |
| 23 | |
| 24 | // A revision indicates modification of the key-value space. |
| 25 | // The set of changes that share same main revision changes the key-value space atomically. |
| 26 | type revision struct { |
| 27 | // main is the main revision of a set of changes that happen atomically. |
| 28 | main int64 |
| 29 | |
| 30 | // sub is the sub revision of a change in a set of changes that happen |
| 31 | // atomically. Each change has different increasing sub revision in that |
| 32 | // set. |
| 33 | sub int64 |
| 34 | } |
| 35 | |
| 36 | func (a revision) GreaterThan(b revision) bool { |
| 37 | if a.main > b.main { |
| 38 | return true |
| 39 | } |
| 40 | if a.main < b.main { |
| 41 | return false |
| 42 | } |
| 43 | return a.sub > b.sub |
| 44 | } |
| 45 | |
| 46 | func newRevBytes() []byte { |
| 47 | return make([]byte, revBytesLen, markedRevBytesLen) |
| 48 | } |
| 49 | |
| 50 | func revToBytes(rev revision, bytes []byte) { |
| 51 | binary.BigEndian.PutUint64(bytes, uint64(rev.main)) |
| 52 | bytes[8] = '_' |
| 53 | binary.BigEndian.PutUint64(bytes[9:], uint64(rev.sub)) |
| 54 | } |
| 55 | |
| 56 | func bytesToRev(bytes []byte) revision { |
| 57 | return revision{ |
| 58 | main: int64(binary.BigEndian.Uint64(bytes[0:8])), |
| 59 | sub: int64(binary.BigEndian.Uint64(bytes[9:])), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | type revisions []revision |
| 64 | |
| 65 | func (a revisions) Len() int { return len(a) } |
| 66 | func (a revisions) Less(i, j int) bool { return a[j].GreaterThan(a[i]) } |
| 67 | func (a revisions) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |