package writecachebadger import ( "fmt" "math" "sync/atomic" "github.com/dgraph-io/badger/v4" ) func (c *cache) estimateCacheSize() uint64 { onDiskSize, _ := c.db.EstimateSize(nil) c.metrics.SetEstimateSize(onDiskSize, 0) return onDiskSize } func (c *cache) incSizeDB(sz uint64) uint64 { return sz + c.maxObjectSize } type counters struct { cDB atomic.Uint64 } func (x *counters) IncDB() { x.cDB.Add(1) } func (x *counters) DecDB() { x.cDB.Add(math.MaxUint64) } func (x *counters) DB() uint64 { return x.cDB.Load() } func (c *cache) initCounters() error { var inDB uint64 err := c.db.View(func(tx *badger.Txn) error { opts := badger.DefaultIteratorOptions opts.PrefetchValues = false it := tx.NewIterator(opts) defer it.Close() for it.Rewind(); it.Valid(); it.Next() { inDB++ } return nil }) if err != nil { return fmt.Errorf("could not read write-cache DB counter: %w", err) } c.objCounters.cDB.Store(inDB) c.metrics.SetActualCounters(inDB, 0) return nil } func (c *cache) incDB() { c.objCounters.IncDB() c.metrics.SetActualCounters(c.objCounters.DB(), 0) } func (c *cache) decDB() { c.objCounters.DecDB() c.metrics.SetActualCounters(c.objCounters.DB(), 0) }