go.temporal.io/server@v1.23.0/common/collection/interface.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 collection
    26  
    27  type (
    28  	// Queue is the interface for queue
    29  	Queue[T any] interface {
    30  		// Peek returns the first item of the queue
    31  		Peek() T
    32  		// Add push an item to the queue
    33  		Add(item T)
    34  		// Remove pop an item from the queue
    35  		Remove() T
    36  		// IsEmpty indicate if the queue is empty
    37  		IsEmpty() bool
    38  		// Len return the size of the queue
    39  		Len() int
    40  	}
    41  
    42  	// HashFunc represents a hash function for string
    43  	HashFunc func(interface{}) uint32
    44  
    45  	// ActionFunc take a key and value, do calculation and return err
    46  	ActionFunc func(key interface{}, value interface{}) error
    47  	// PredicateFunc take a key and value, do calculation and return boolean
    48  	PredicateFunc func(key interface{}, value interface{}) bool
    49  
    50  	// ConcurrentTxMap is a generic interface for any implementation of a dictionary
    51  	// or a key value lookup table that is thread safe, and providing functionality
    52  	// to modify key / value pair inside within a transaction
    53  	ConcurrentTxMap interface {
    54  		// Get returns the value for the given key
    55  		Get(key interface{}) (interface{}, bool)
    56  		// Contains returns true if the key exist and false otherwise
    57  		Contains(key interface{}) bool
    58  		// Put records the mapping from given key to value
    59  		Put(key interface{}, value interface{})
    60  		// PutIfNotExist records the key value mapping only
    61  		// if the mapping does not already exist
    62  		PutIfNotExist(key interface{}, value interface{}) bool
    63  		// Remove deletes the key from the map
    64  		Remove(key interface{})
    65  		// GetAndDo returns the value corresponding to the key, and apply fn to key value before return value
    66  		// return (value, value exist or not, error when evaluation fn)
    67  		GetAndDo(key interface{}, fn ActionFunc) (interface{}, bool, error)
    68  		// PutOrDo put the key value in the map, if key does not exists, otherwise, call fn with existing key and value
    69  		// return (value, fn evaluated or not, error when evaluation fn)
    70  		PutOrDo(key interface{}, value interface{}, fn ActionFunc) (interface{}, bool, error)
    71  		// RemoveIf deletes the given key from the map if fn return true
    72  		// return whether the key is removed or not
    73  		RemoveIf(key interface{}, fn PredicateFunc) bool
    74  		// Iter returns an iterator to the map
    75  		Iter() MapIterator
    76  		// Len returns the number of items in the map
    77  		Len() int
    78  	}
    79  
    80  	// MapIterator represents the interface for map iterators
    81  	MapIterator interface {
    82  		// Close closes the iterator
    83  		// and releases any allocated resources
    84  		Close()
    85  		// Entries returns a channel of MapEntry
    86  		// objects that can be used in a range loop
    87  		Entries() <-chan *MapEntry
    88  	}
    89  
    90  	// MapEntry represents a key-value entry within the map
    91  	MapEntry struct {
    92  		// Key represents the key
    93  		Key interface{}
    94  		// Value represents the value
    95  		Value interface{}
    96  	}
    97  )
    98  
    99  const (
   100  	// UUIDStringLength is the length of an UUID represented as a hex string
   101  	UUIDStringLength = 36 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
   102  )