github.com/kaydxh/golang@v0.0.131/pkg/pool/taskqueue/tasker_syncmap.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package taskqueue
    23  
    24  import (
    25  	"sync" // Used by sync.Map.
    26  )
    27  
    28  // Generate code that will fail if the constants change value.
    29  func _() {
    30  	// An "cannot convert TaskerMap literal (type TaskerMap) to type sync.Map" compiler error signifies that the base type have changed.
    31  	// Re-run the go-syncmap command to generate them again.
    32  	_ = (sync.Map)(TaskerMap{})
    33  }
    34  
    35  var _nil_TaskerMap_Tasker_value = func() (val Tasker) { return }()
    36  
    37  // Load returns the value stored in the map for a key, or nil if no
    38  // value is present.
    39  // The ok result indicates whether value was found in the map.
    40  func (m *TaskerMap) Load(key string) (Tasker, bool) {
    41  	value, ok := (*sync.Map)(m).Load(key)
    42  	if value == nil {
    43  		return _nil_TaskerMap_Tasker_value, ok
    44  	}
    45  	return value.(Tasker), ok
    46  }
    47  
    48  // Store sets the value for a key.
    49  func (m *TaskerMap) Store(key string, value Tasker) {
    50  	(*sync.Map)(m).Store(key, value)
    51  }
    52  
    53  // LoadOrStore returns the existing value for the key if present.
    54  // Otherwise, it stores and returns the given value.
    55  // The loaded result is true if the value was loaded, false if stored.
    56  func (m *TaskerMap) LoadOrStore(key string, value Tasker) (Tasker, bool) {
    57  	actual, loaded := (*sync.Map)(m).LoadOrStore(key, value)
    58  	if actual == nil {
    59  		return _nil_TaskerMap_Tasker_value, loaded
    60  	}
    61  	return actual.(Tasker), loaded
    62  }
    63  
    64  // LoadAndDelete deletes the value for a key, returning the previous value if any.
    65  // The loaded result reports whether the key was present.
    66  func (m *TaskerMap) LoadAndDelete(key string) (value Tasker, loaded bool) {
    67  	actual, loaded := (*sync.Map)(m).LoadAndDelete(key)
    68  	if actual == nil {
    69  		return _nil_TaskerMap_Tasker_value, loaded
    70  	}
    71  	return actual.(Tasker), loaded
    72  }
    73  
    74  // Delete deletes the value for a key.
    75  func (m *TaskerMap) Delete(key string) {
    76  	(*sync.Map)(m).Delete(key)
    77  }
    78  
    79  // Range calls f sequentially for each key and value present in the map.
    80  // If f returns false, range stops the iteration.
    81  //
    82  // Range does not necessarily correspond to any consistent snapshot of the Map's
    83  // contents: no key will be visited more than once, but if the value for any key
    84  // is stored or deleted concurrently, Range may reflect any mapping for that key
    85  // from any point during the Range call.
    86  //
    87  // Range may be O(N) with the number of elements in the map even if f returns
    88  // false after a constant number of calls.
    89  func (m *TaskerMap) Range(f func(key string, value Tasker) bool) {
    90  	(*sync.Map)(m).Range(func(key, value interface{}) bool {
    91  		return f(key.(string), value.(Tasker))
    92  	})
    93  }