fix possible race in line cache
This commit is contained in:
parent
c5b0f95dac
commit
e17582581d
@ -3,6 +3,7 @@ package fsutils
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -10,13 +11,12 @@ import (
|
||||
type fileLinesCache [][]byte
|
||||
|
||||
type LineCache struct {
|
||||
files map[string]fileLinesCache
|
||||
files sync.Map
|
||||
fileCache *FileCache
|
||||
}
|
||||
|
||||
func NewLineCache(fc *FileCache) *LineCache {
|
||||
return &LineCache{
|
||||
files: map[string]fileLinesCache{},
|
||||
fileCache: fc,
|
||||
}
|
||||
}
|
||||
@ -53,9 +53,9 @@ func (lc *LineCache) getRawLine(filePath string, index0 int) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) {
|
||||
fc := lc.files[filePath]
|
||||
if fc != nil {
|
||||
return fc, nil
|
||||
loadedFc, ok := lc.files.Load(filePath)
|
||||
if ok {
|
||||
return loadedFc.(fileLinesCache), nil
|
||||
}
|
||||
|
||||
fileBytes, err := lc.fileCache.GetFileBytes(filePath)
|
||||
@ -63,7 +63,7 @@ func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) {
|
||||
return nil, errors.Wrapf(err, "can't get file %s bytes from cache", filePath)
|
||||
}
|
||||
|
||||
fc = bytes.Split(fileBytes, []byte("\n"))
|
||||
lc.files[filePath] = fc
|
||||
fc := bytes.Split(fileBytes, []byte("\n"))
|
||||
lc.files.Store(filePath, fileLinesCache(fc))
|
||||
return fc, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user