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().
This commit is contained in:
Aliaksandr Valialkin 2024-09-20 11:57:24 +02:00
parent 218c533874
commit 6f61e9d49d
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB
2 changed files with 12 additions and 10 deletions

View file

@ -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() db.extDBLock.Lock()
extDB := db.extDB extDB := db.extDB
if extDB != nil { if extDB != nil {
extDB.incRef() extDB.incRef()
} }
db.extDBLock.Unlock() db.extDBLock.Unlock()
if extDB == nil { if extDB != nil {
return false f(extDB)
extDB.decRef()
} }
f(extDB)
extDB.decRef()
return true
} }
// SetExtDB sets external db to search. // 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. // Try searching in the external indexDB.
if db.doExtDB(func(extDB *indexDB) { db.doExtDB(func(extDB *indexDB) {
is := extDB.getIndexSearch(noDeadline) is := extDB.getIndexSearch(noDeadline)
dst, ok = is.searchMetricName(dst, metricID) dst, ok = is.searchMetricName(dst, metricID)
extDB.putIndexSearch(is) 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. // since the filtering must be performed before calling this func.
extDB.putMetricNameToCache(metricID, dst) extDB.putMetricNameToCache(metricID, dst)
} }
}) && ok { })
if ok {
return dst, true return dst, true
} }

View file

@ -389,11 +389,11 @@ func (s *Storage) CreateSnapshot() (string, error) {
dirsToRemoveOnError = append(dirsToRemoveOnError, idbSnapshot) dirsToRemoveOnError = append(dirsToRemoveOnError, idbSnapshot)
var err error var err error
ok := idb.doExtDB(func(extDB *indexDB) { idb.doExtDB(func(extDB *indexDB) {
prevSnapshot := filepath.Join(idbSnapshot, extDB.name) prevSnapshot := filepath.Join(idbSnapshot, extDB.name)
err = extDB.tb.CreateSnapshotAt(prevSnapshot) err = extDB.tb.CreateSnapshotAt(prevSnapshot)
}) })
if ok && err != nil { if err != nil {
return "", fmt.Errorf("cannot create prev indexDB snapshot: %w", err) return "", fmt.Errorf("cannot create prev indexDB snapshot: %w", err)
} }
dstIdbDir := filepath.Join(dstDir, indexdbDirname) dstIdbDir := filepath.Join(dstDir, indexdbDirname)