From 6f61e9d49d3604a7960cd637666853c34a93debd Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 20 Sep 2024 11:57:24 +0200 Subject: [PATCH] lib/storage: simplify indexDB.doExtDB() usage by removing the returned value Previously indexDB.doExtDB() was returning boolean value, which was indicating whether f callback was called. There is no need in returning this boolean value, since the f callback can determine on itself whether it was called. This simplifies the code a bit. While at it, document indexDB.doExtDB(). --- lib/storage/index_db.go | 18 ++++++++++-------- lib/storage/storage.go | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/storage/index_db.go b/lib/storage/index_db.go index 0e6ea2a92..4d9ec3cd5 100644 --- a/lib/storage/index_db.go +++ b/lib/storage/index_db.go @@ -258,19 +258,20 @@ func (db *indexDB) UpdateMetrics(m *IndexDBMetrics) { }) } -func (db *indexDB) doExtDB(f func(extDB *indexDB)) bool { +// doExtDB calls f for non-nil db.extDB. +// +// f isn't called if db.extDB is nil. +func (db *indexDB) doExtDB(f func(extDB *indexDB)) { db.extDBLock.Lock() extDB := db.extDB if extDB != nil { extDB.incRef() } db.extDBLock.Unlock() - if extDB == nil { - return false + if extDB != nil { + f(extDB) + extDB.decRef() } - f(extDB) - extDB.decRef() - return true } // SetExtDB sets external db to search. @@ -1505,7 +1506,7 @@ func (db *indexDB) searchMetricNameWithCache(dst []byte, metricID uint64) ([]byt } // Try searching in the external indexDB. - if db.doExtDB(func(extDB *indexDB) { + db.doExtDB(func(extDB *indexDB) { is := extDB.getIndexSearch(noDeadline) dst, ok = is.searchMetricName(dst, metricID) extDB.putIndexSearch(is) @@ -1514,7 +1515,8 @@ func (db *indexDB) searchMetricNameWithCache(dst []byte, metricID uint64) ([]byt // since the filtering must be performed before calling this func. extDB.putMetricNameToCache(metricID, dst) } - }) && ok { + }) + if ok { return dst, true } diff --git a/lib/storage/storage.go b/lib/storage/storage.go index c3996f137..bf0e3a233 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -389,11 +389,11 @@ func (s *Storage) CreateSnapshot() (string, error) { dirsToRemoveOnError = append(dirsToRemoveOnError, idbSnapshot) var err error - ok := idb.doExtDB(func(extDB *indexDB) { + idb.doExtDB(func(extDB *indexDB) { prevSnapshot := filepath.Join(idbSnapshot, extDB.name) err = extDB.tb.CreateSnapshotAt(prevSnapshot) }) - if ok && err != nil { + if err != nil { return "", fmt.Errorf("cannot create prev indexDB snapshot: %w", err) } dstIdbDir := filepath.Join(dstDir, indexdbDirname)