github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bithash/stats.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 bithash
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"sync/atomic"
    21  
    22  	"github.com/zuoyebang/bitalosdb/internal/utils"
    23  )
    24  
    25  type Stats struct {
    26  	FileTotal   atomic.Uint32
    27  	KeyTotal    atomic.Uint64
    28  	DelKeyTotal atomic.Uint64
    29  }
    30  
    31  func (s *Stats) String() string {
    32  	return fmt.Sprintf("fileTotal:%d keyTotal:%d delKeyTotal:%d",
    33  		s.FileTotal.Load(), s.KeyTotal.Load(), s.DelKeyTotal.Load())
    34  }
    35  
    36  func (b *Bithash) StatsAddKeyTotal(n int) {
    37  	b.stats.KeyTotal.Add(uint64(n))
    38  }
    39  
    40  func (b *Bithash) StatsSubKeyTotal(n int) {
    41  	b.stats.KeyTotal.Add(^uint64(n - 1))
    42  }
    43  
    44  func (b *Bithash) StatsGetDelKeyTotal() uint64 {
    45  	return b.stats.DelKeyTotal.Load()
    46  }
    47  
    48  func (b *Bithash) StatsSubDelKeyTotal(n int) {
    49  	b.stats.DelKeyTotal.Add(^uint64(n - 1))
    50  }
    51  
    52  func (b *Bithash) StatsSetDelKeyTotal(n int) {
    53  	b.stats.DelKeyTotal.Store(uint64(n))
    54  }
    55  
    56  func (b *Bithash) Stats() *Stats {
    57  	return b.stats
    58  }
    59  
    60  func (b *Bithash) StatsToString() string {
    61  	return b.stats.String()
    62  }
    63  
    64  func (b *Bithash) DebugInfo(dataType string) string {
    65  	b.meta.mu.RLock()
    66  	defer b.meta.mu.RUnlock()
    67  
    68  	buf := new(bytes.Buffer)
    69  	fmt.Fprintf(buf, "%s-bithash%d:dirname=%s dirSize=%s files=%d\n",
    70  		dataType, b.index, b.dirname,
    71  		utils.FmtSize(uint64(utils.GetDirSize(b.dirname))),
    72  		len(b.meta.mu.filesMeta))
    73  
    74  	for fn, fileMeta := range b.meta.mu.filesMeta {
    75  		if fileMeta.state == fileMetaStateImmutable {
    76  			var delPercent float64
    77  			if fileMeta.keyNum == 0 {
    78  				delPercent = 0
    79  			} else {
    80  				delPercent = float64(fileMeta.delKeyNum) / float64(fileMeta.keyNum)
    81  			}
    82  			fmt.Fprintf(buf, "%s-bithash%d-%d.bht:%s delPercent=%.2f\n", dataType, b.index, fn, fileMeta.String(), delPercent)
    83  		} else {
    84  			fmt.Fprintf(buf, "%s-bithash%d-%d.bht:%s\n", dataType, b.index, fn, fileMeta.String())
    85  		}
    86  	}
    87  
    88  	return buf.String()
    89  }