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