Skip to content

Commit

Permalink
refactor: Statistics check for nil in its methods (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
luohaha authored Aug 1, 2022
1 parent f4b1998 commit c9026df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
16 changes: 4 additions & 12 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,11 @@ func (ndb *nodeDB) GetNode(hash []byte) (*Node, error) {

// Check the cache.
if cachedNode := ndb.nodeCache.Get(hash); cachedNode != nil {
if ndb.opts.Stat != nil {
ndb.opts.Stat.IncCacheHitCnt()
}
ndb.opts.Stat.IncCacheHitCnt()
return cachedNode.(*Node), nil
}

if ndb.opts.Stat != nil {
ndb.opts.Stat.IncCacheMissCnt()
}
ndb.opts.Stat.IncCacheMissCnt()

// Doesn't exist, load.
buf, err := ndb.db.Get(ndb.nodeKey(hash))
Expand Down Expand Up @@ -160,15 +156,11 @@ func (ndb *nodeDB) GetFastNode(key []byte) (*FastNode, error) {
}

if cachedFastNode := ndb.fastNodeCache.Get(key); cachedFastNode != nil {
if ndb.opts.Stat != nil {
ndb.opts.Stat.IncFastCacheHitCnt()
}
ndb.opts.Stat.IncFastCacheHitCnt()
return cachedFastNode.(*FastNode), nil
}

if ndb.opts.Stat != nil {
ndb.opts.Stat.IncFastCacheMissCnt()
}
ndb.opts.Stat.IncFastCacheMissCnt()

// Doesn't exist, load.
buf, err := ndb.db.Get(ndb.fastNodeKey(key))
Expand Down
12 changes: 12 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@ type Statistics struct {
}

func (stat *Statistics) IncCacheHitCnt() {
if stat == nil {
return
}
atomic.AddUint64(&stat.cacheHitCnt, 1)
}

func (stat *Statistics) IncCacheMissCnt() {
if stat == nil {
return
}
atomic.AddUint64(&stat.cacheMissCnt, 1)
}

func (stat *Statistics) IncFastCacheHitCnt() {
if stat == nil {
return
}
atomic.AddUint64(&stat.fastCacheHitCnt, 1)
}

func (stat *Statistics) IncFastCacheMissCnt() {
if stat == nil {
return
}
atomic.AddUint64(&stat.fastCacheMissCnt, 1)
}

Expand Down

0 comments on commit c9026df

Please sign in to comment.