go.temporal.io/server@v1.23.0/common/persistence/versionhistory/version_history_item.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package versionhistory
    26  
    27  import (
    28  	"fmt"
    29  
    30  	historyspb "go.temporal.io/server/api/history/v1"
    31  )
    32  
    33  // NewVersionHistoryItem create a new instance of VersionHistoryItem.
    34  func NewVersionHistoryItem(eventID int64, version int64) *historyspb.VersionHistoryItem {
    35  	if eventID < 0 || version < 0 {
    36  		panic(fmt.Sprintf("invalid version history item event ID: %v, version: %v", eventID, version))
    37  	}
    38  
    39  	return &historyspb.VersionHistoryItem{EventId: eventID, Version: version}
    40  }
    41  
    42  // CopyVersionHistoryItem create a new instance of VersionHistoryItem.
    43  func CopyVersionHistoryItem(item *historyspb.VersionHistoryItem) *historyspb.VersionHistoryItem {
    44  	return NewVersionHistoryItem(item.EventId, item.Version)
    45  }
    46  
    47  // IsEqualVersionHistoryItem checks whether version history items are equal
    48  func IsEqualVersionHistoryItem(item1 *historyspb.VersionHistoryItem, item2 *historyspb.VersionHistoryItem) bool {
    49  	return item1.EventId == item2.EventId && item1.Version == item2.Version
    50  }
    51  
    52  // IsEqualVersionHistoryItems checks whether version history items are equal
    53  func IsEqualVersionHistoryItems(items1 []*historyspb.VersionHistoryItem, items2 []*historyspb.VersionHistoryItem) bool {
    54  	if len(items1) != len(items2) {
    55  		return false
    56  	}
    57  	for i := 0; i < len(items1); i++ {
    58  		if !IsEqualVersionHistoryItem(items1[i], items2[i]) {
    59  			return false
    60  		}
    61  	}
    62  	return true
    63  }
    64  
    65  // CompareVersionHistoryItem compares 2 version history items
    66  func CompareVersionHistoryItem(item1 *historyspb.VersionHistoryItem, item2 *historyspb.VersionHistoryItem) int {
    67  	if item1.Version < item2.Version {
    68  		return -1
    69  	}
    70  	if item1.Version > item2.Version {
    71  		return 1
    72  	}
    73  
    74  	// item1.Version == item2.Version
    75  	if item1.EventId < item2.EventId {
    76  		return -1
    77  	}
    78  	if item1.EventId > item2.EventId {
    79  		return 1
    80  	}
    81  	return 0
    82  }