File tree 1 file changed +29
-0
lines changed
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ package bitcask
3
3
import (
4
4
"bitcask/data"
5
5
"bitcask/index"
6
+ "bitcask/utils"
6
7
"errors"
8
+ "fmt"
7
9
"io"
8
10
"os"
9
11
"path/filepath"
@@ -30,6 +32,14 @@ type DB struct {
30
32
isInitiated bool // 是否是第一次初始化数据目录
31
33
}
32
34
35
+ // Stat 存储引擎统计信息
36
+ type Stat struct {
37
+ KeyNum uint // key 的总数量
38
+ DataFileNum uint // 数据文件的数量
39
+ ReclaimableSize int64 // 可以进行 merge 回收的数据量,字节为单位
40
+ DiskSize int64 // 数据目录所占磁盘空间大小
41
+ }
42
+
33
43
// Open 打开bitcask存储引擎示例
34
44
func Open (cfg DBConfig ) (* DB , error ) {
35
45
// 对传入配置进行校验
@@ -552,3 +562,22 @@ func (db *DB) loadSeqNo() error {
552
562
db .seqNoFileExist = true
553
563
return nil
554
564
}
565
+ func (db * DB ) Stat () * Stat {
566
+ db .mu .RLock ()
567
+ defer db .mu .RUnlock ()
568
+
569
+ var dataFiles = uint (len (db .oldFile ))
570
+ if db .activeFile != nil {
571
+ dataFiles += 1
572
+ }
573
+
574
+ dirSize , err := utils .DirSize (db .cfg .DirPath )
575
+ if err != nil {
576
+ panic (fmt .Sprintf ("failed to get dir size : %v" , err ))
577
+ }
578
+ return & Stat {
579
+ KeyNum : uint (db .index .Size ()),
580
+ DataFileNum : dataFiles ,
581
+ DiskSize : dirSize ,
582
+ }
583
+ }
You can’t perform that action at this time.
0 commit comments