go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/common/tryjob.go (about)

     1  // Copyright 2021 The LUCI 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 common
    16  
    17  import (
    18  	"slices"
    19  )
    20  
    21  // TryjobID is a unique ID of a Tryjob used internally in CV.
    22  //
    23  // This ID is not a Buildbucket Build ID.
    24  // See also tryjob.Tryjob type.
    25  type TryjobID int64
    26  
    27  // TryjobIDs is a convenience type to facilitate handling of a slice of
    28  // TryjobID.
    29  type TryjobIDs []TryjobID
    30  
    31  // Dedupe removes duplicates in place and sorts the slice.
    32  //
    33  // Note: Does not preserve original order.
    34  func (p *TryjobIDs) Dedupe() {
    35  	ids := *p
    36  	if len(ids) <= 1 {
    37  		return
    38  	}
    39  	slices.Sort(ids)
    40  	n, prev, skipped := 0, ids[0], false
    41  	for _, id := range ids[1:] {
    42  		if id == prev {
    43  			skipped = true
    44  			continue
    45  		}
    46  		n++
    47  		if skipped {
    48  			ids[n] = id
    49  		}
    50  		prev = id
    51  	}
    52  	*p = ids[:n+1]
    53  }
    54  
    55  // ToInt64 returns a slice that contains all Tryjobs in int64 type.
    56  func (ids TryjobIDs) ToInt64() []int64 {
    57  	if ids == nil {
    58  		return nil
    59  	}
    60  	ret := make([]int64, len(ids))
    61  	for i, id := range ids {
    62  		ret[i] = int64(id)
    63  	}
    64  	return ret
    65  }
    66  
    67  // MakeTryjobIDs returns TryjobIDs from list of TryjobID in int64.
    68  func MakeTryjobIDs(ids ...int64) TryjobIDs {
    69  	if ids == nil {
    70  		return nil
    71  	}
    72  	ret := make(TryjobIDs, len(ids))
    73  	for i, id := range ids {
    74  		ret[i] = TryjobID(id)
    75  	}
    76  	return ret
    77  }
    78  
    79  // TryjobIDSet is convenience type to reduce the boilerplate.
    80  type TryjobIDSet map[TryjobID]struct{}
    81  
    82  // Add adds the provided Tryjob ID to the set.
    83  func (s TryjobIDSet) Add(tjID TryjobID) {
    84  	s[tjID] = struct{}{}
    85  }
    86  
    87  // Has returns true if the provided Tryjob ID is in the set.
    88  //
    89  // Otherwise, returns false.
    90  func (s TryjobIDSet) Has(tjID TryjobID) bool {
    91  	_, exists := s[tjID]
    92  	return exists
    93  }