mirror of
https://github.com/btcsuite/btcd.git
synced 2025-02-23 22:47:01 +01:00
ffldb: refactor out file close code into its own method
The existing file close code is refactored out into it's own method closeFile() so that it can be reused elsewhere.
This commit is contained in:
parent
8b5f2aa6f2
commit
8ed8ef1340
1 changed files with 21 additions and 11 deletions
|
@ -281,17 +281,7 @@ func (s *blockStore) openFile(fileNum uint32) (*lockableFile, error) {
|
||||||
lruList := s.openBlocksLRU
|
lruList := s.openBlocksLRU
|
||||||
if lruList.Len() >= maxOpenFiles {
|
if lruList.Len() >= maxOpenFiles {
|
||||||
lruFileNum := lruList.Remove(lruList.Back()).(uint32)
|
lruFileNum := lruList.Remove(lruList.Back()).(uint32)
|
||||||
oldBlockFile := s.openBlockFiles[lruFileNum]
|
s.closeFile(lruFileNum)
|
||||||
|
|
||||||
// Close the old file under the write lock for the file in case
|
|
||||||
// any readers are currently reading from it so it's not closed
|
|
||||||
// out from under them.
|
|
||||||
oldBlockFile.Lock()
|
|
||||||
_ = oldBlockFile.file.Close()
|
|
||||||
oldBlockFile.Unlock()
|
|
||||||
|
|
||||||
delete(s.openBlockFiles, lruFileNum)
|
|
||||||
delete(s.fileNumToLRUElem, lruFileNum)
|
|
||||||
}
|
}
|
||||||
s.fileNumToLRUElem[fileNum] = lruList.PushFront(fileNum)
|
s.fileNumToLRUElem[fileNum] = lruList.PushFront(fileNum)
|
||||||
s.lruMutex.Unlock()
|
s.lruMutex.Unlock()
|
||||||
|
@ -302,6 +292,26 @@ func (s *blockStore) openFile(fileNum uint32) (*lockableFile, error) {
|
||||||
return blockFile, nil
|
return blockFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// closeFile checks that the file corresponding to the file number is open and
|
||||||
|
// if it is, it closes it in a concurrency safe manner and cleans up associated
|
||||||
|
// data in the blockstore struct.
|
||||||
|
func (s *blockStore) closeFile(fileNum uint32) {
|
||||||
|
blockFile := s.openBlockFiles[fileNum]
|
||||||
|
if blockFile == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the old file under the write lock for the file in case
|
||||||
|
// any readers are currently reading from it so it's not closed
|
||||||
|
// out from under them.
|
||||||
|
blockFile.Lock()
|
||||||
|
_ = blockFile.file.Close()
|
||||||
|
blockFile.Unlock()
|
||||||
|
|
||||||
|
delete(s.openBlockFiles, fileNum)
|
||||||
|
delete(s.fileNumToLRUElem, fileNum)
|
||||||
|
}
|
||||||
|
|
||||||
// deleteFile removes the block file for the passed flat file number. The file
|
// deleteFile removes the block file for the passed flat file number. The file
|
||||||
// must already be closed and it is the responsibility of the caller to do any
|
// must already be closed and it is the responsibility of the caller to do any
|
||||||
// other state cleanup necessary.
|
// other state cleanup necessary.
|
||||||
|
|
Loading…
Add table
Reference in a new issue