github.com/matrixorigin/matrixone@v1.2.0/pkg/util/metric/stats/counter.go (about)

     1  // Copyright 2023 Matrix Origin
     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 stats
    16  
    17  import "sync/atomic"
    18  
    19  // Counter represents a combination of global & current_window counter.
    20  type Counter struct {
    21  	window atomic.Int64
    22  	global atomic.Int64
    23  }
    24  
    25  // Add adds to global and window counter
    26  func (c *Counter) Add(delta int64) (new int64) {
    27  	c.window.Add(delta)
    28  	return c.global.Add(delta)
    29  }
    30  
    31  // Set swaps the global counter value
    32  func (c *Counter) Swap(n int64) int64 {
    33  	return c.global.Swap(n)
    34  }
    35  
    36  // Load return the global counter value
    37  func (c *Counter) Load() int64 {
    38  	return c.global.Load()
    39  }
    40  
    41  // SwapW swaps current window counter with new
    42  func (c *Counter) SwapW(new int64) int64 {
    43  	return c.window.Swap(new)
    44  }
    45  
    46  // LoadW returns current window value
    47  func (c *Counter) LoadW() int64 {
    48  	return c.window.Load()
    49  }
    50  
    51  // Reset reset the global and window counter to 0
    52  func (c *Counter) Reset() {
    53  	c.global.Store(0)
    54  	c.window.Store(0)
    55  }