github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/storage/kv/version.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     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  // Copyright 2015 PingCAP, Inc.
    16  //
    17  // Licensed under the Apache License, Version 2.0 (the "License");
    18  // you may not use this file except in compliance with the License.
    19  // You may obtain a copy of the License at
    20  //
    21  //     http://www.apache.org/licenses/LICENSE-2.0
    22  //
    23  // Unless required by applicable law or agreed to in writing, software
    24  // distributed under the License is distributed on an "AS IS" BASIS,
    25  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    26  // See the License for the specific language governing permissions and
    27  // limitations under the License.
    28  
    29  package kv
    30  
    31  import "math"
    32  
    33  // VersionProvider provides increasing IDs.
    34  type VersionProvider interface {
    35  	CurrentVersion() Version
    36  }
    37  
    38  // Version is the wrapper of KV's version.
    39  type Version uint64
    40  
    41  type VersionPair struct {
    42  	StartVer  Version
    43  	CommitVer Version
    44  }
    45  
    46  var (
    47  	// MaxVersion is the maximum version, notice that it's not a valid version.
    48  	MaxVersion = Version(math.MaxUint64)
    49  	// MinVersion is the minimum version, it's not a valid version, too.
    50  	MinVersion = Version(0)
    51  )
    52  
    53  // NewVersion creates a new Version struct.
    54  func NewVersion(v uint64) Version {
    55  	return Version(v)
    56  }
    57  
    58  // Cmp returns the comparison result of two versions.
    59  // The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
    60  func (v Version) Cmp(another Version) int {
    61  	if v > another {
    62  		return 1
    63  	} else if v < another {
    64  		return -1
    65  	}
    66  	return 0
    67  }
    68  
    69  // Max returns the larger version between a and b
    70  func Max(a, b Version) Version {
    71  	if a > b {
    72  		return a
    73  	}
    74  	return b
    75  }