2023-08-11 08:32:43 +00:00
|
|
|
package fstree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FileCounter used to count files in FSTree. The implementation must be thread-safe.
|
|
|
|
type FileCounter interface {
|
|
|
|
Set(v uint64)
|
|
|
|
Inc()
|
|
|
|
Dec()
|
|
|
|
}
|
|
|
|
|
|
|
|
type noopCounter struct{}
|
|
|
|
|
|
|
|
func (c *noopCounter) Set(uint64) {}
|
|
|
|
func (c *noopCounter) Inc() {}
|
|
|
|
func (c *noopCounter) Dec() {}
|
|
|
|
|
2024-02-08 15:04:18 +00:00
|
|
|
func counterEnabled(c FileCounter) bool {
|
|
|
|
_, noop := c.(*noopCounter)
|
|
|
|
return !noop
|
|
|
|
}
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
type SimpleCounter struct {
|
|
|
|
v atomic.Uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimpleCounter() *SimpleCounter {
|
|
|
|
return &SimpleCounter{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SimpleCounter) Set(v uint64) { c.v.Store(v) }
|
|
|
|
func (c *SimpleCounter) Inc() { c.v.Add(1) }
|
|
|
|
func (c *SimpleCounter) Dec() { c.v.Add(math.MaxUint64) }
|
|
|
|
func (c *SimpleCounter) Value() uint64 { return c.v.Load() }
|