Skip to content

Fix panic in filecache #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions pkg/fsutils/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@ package fsutils
import (
"fmt"
"io/ioutil"
"sync"

"github.com/golangci/golangci-lint/pkg/logutils"

"github.com/pkg/errors"
)

type FileCache struct {
files map[string][]byte
files sync.Map
}

func NewFileCache() *FileCache {
return &FileCache{
files: map[string][]byte{},
}
return &FileCache{}
}

func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
cachedBytes := fc.files[filePath]
if cachedBytes != nil {
return cachedBytes, nil
cachedBytes, ok := fc.files.Load(filePath)
if ok {
return cachedBytes.([]byte), nil
}

fileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, errors.Wrapf(err, "can't read file %s", filePath)
}

fc.files[filePath] = fileBytes
fc.files.Store(filePath, fileBytes)
return fileBytes, nil
}

Expand All @@ -56,9 +55,13 @@ func prettifyBytesCount(n int) string {

func (fc *FileCache) PrintStats(log logutils.Log) {
var size int
for _, fileBytes := range fc.files {
size += len(fileBytes)
}
var mapLen int
fc.files.Range(func(_, fileBytes interface{}) bool {
mapLen++
size += len(fileBytes.([]byte))

return true
})

log.Infof("File cache stats: %d entries of total size %s", len(fc.files), prettifyBytesCount(size))
log.Infof("File cache stats: %d entries of total size %s", mapLen, prettifyBytesCount(size))
}
14 changes: 7 additions & 7 deletions pkg/fsutils/linecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package fsutils
import (
"bytes"
"fmt"
"sync"

"github.com/pkg/errors"
)

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,
}
}
Expand Down Expand Up @@ -53,17 +53,17 @@ 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)
if err != nil {
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
}