-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor common revision code to pkg
Signed-off-by: Allen Ray <[email protected]>
- Loading branch information
Showing
19 changed files
with
479 additions
and
528 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2015 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package revision | ||
|
||
import "encoding/binary" | ||
|
||
// RevBytesLen is the byte length of a normal revision. | ||
// First 8 bytes is the revision.main in big-endian format. The 9th byte | ||
// is a '_'. The last 8 bytes is the revision.sub in big-endian format. | ||
const RevBytesLen = 8 + 1 + 8 | ||
const MarkedRevBytesLen = RevBytesLen + 1 | ||
|
||
// Revision indicates modification of the key-value space. | ||
// The set of changes that share same main Revision changes the key-value space atomically. | ||
type Revision struct { | ||
// Main is the Main revision of a set of changes that happen atomically. | ||
Main int64 | ||
|
||
// Sub is the Sub revision of a change in a set of changes that happen | ||
// atomically. Each change has different increasing Sub revision in that | ||
// set. | ||
Sub int64 | ||
} | ||
|
||
func (a Revision) GreaterThan(b Revision) bool { | ||
if a.Main > b.Main { | ||
return true | ||
} | ||
if a.Main < b.Main { | ||
return false | ||
} | ||
return a.Sub > b.Sub | ||
} | ||
|
||
func NewRevBytes() []byte { | ||
return make([]byte, RevBytesLen, MarkedRevBytesLen) | ||
} | ||
|
||
func RevToBytes(rev Revision, bytes []byte) { | ||
binary.BigEndian.PutUint64(bytes, uint64(rev.Main)) | ||
bytes[8] = '_' | ||
binary.BigEndian.PutUint64(bytes[9:], uint64(rev.Sub)) | ||
} | ||
|
||
func BytesToRev(bytes []byte) Revision { | ||
return Revision{ | ||
Main: int64(binary.BigEndian.Uint64(bytes[0:8])), | ||
Sub: int64(binary.BigEndian.Uint64(bytes[9:])), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.