Skip to content

Commit deb5230

Browse files
authored
fix: golangci-lint findings, third delivery (#2167)
Since I started using this project on my production workloads, I would like to contribute to the project. This third delivery is still fixing easy stuff like: - `golangci-lint run ./... | grep "S1001:"` - search/searcher/search_conjunction.go:53:2: S1001: should use copy(to, from) instead of a loop (gosimple) - search/searcher/search_disjunction_slice.go:82:3: S1001: should use copy(to, from) instead of a loop (gosimple) - `golangci-lint run ./... | grep "ineffectual assignment"`-> - search/sort.go:49:14: ineffectual assignment to ok (ineffassign) - index/scorch/reader_test.go:72:2: ineffectual assignment to expectedCount (ineffassign) - index/upsidedown/row.go:645:7: ineffectual assignment to arrayPositionsLen (ineffassign) - `golangci-lint run ./... | grep "S1017:"` - search/query/regexp.go:72:2: S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix` (gosimple) - added a new file `search/sort_test.go` to test a change before and after it - minor formatting changes
1 parent 5e2cca2 commit deb5230

File tree

8 files changed

+372
-30
lines changed

8 files changed

+372
-30
lines changed

analysis/token/keyword/keyword.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ func NewKeyWordMarkerFilter(keyWords analysis.TokenMap) *KeyWordMarkerFilter {
3535

3636
func (f *KeyWordMarkerFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
3737
for _, token := range input {
38-
word := string(token.Term)
39-
_, isKeyWord := f.keyWords[word]
38+
_, isKeyWord := f.keyWords[string(token.Term)]
4039
if isKeyWord {
4140
token.KeyWord = true
4241
}

index/scorch/reader_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func TestIndexReader(t *testing.T) {
101101
t.Errorf("Error accessing term field reader: %v", err)
102102
}
103103

104-
expectedCount = 2
105104
count = reader.Count()
106105
if count != expectedCount {
107106
t.Errorf("Expected doc count to be: %d got: %d", expectedCount, count)

index/upsidedown/row.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import (
2626
"github.com/golang/protobuf/proto"
2727
)
2828

29-
var reflectStaticSizeTermFrequencyRow int
30-
var reflectStaticSizeTermVector int
29+
var (
30+
reflectStaticSizeTermFrequencyRow int
31+
reflectStaticSizeTermVector int
32+
)
3133

3234
func init() {
3335
var tfr TermFrequencyRow
@@ -322,7 +324,6 @@ func NewDictionaryRowKV(key, value []byte) (*DictionaryRow, error) {
322324
return nil, err
323325
}
324326
return rv, nil
325-
326327
}
327328

328329
func NewDictionaryRowK(key []byte) (*DictionaryRow, error) {
@@ -642,7 +643,7 @@ func (tfr *TermFrequencyRow) parseV(value []byte, includeTermVectors bool) error
642643
}
643644
currOffset += bytesRead
644645

645-
var arrayPositionsLen uint64 = 0
646+
var arrayPositionsLen uint64
646647
arrayPositionsLen, bytesRead = binary.Uvarint(value[currOffset:])
647648
if bytesRead <= 0 {
648649
return fmt.Errorf("invalid term frequency value, vector contains no arrayPositionLen")
@@ -682,7 +683,6 @@ func NewTermFrequencyRowKV(key, value []byte) (*TermFrequencyRow, error) {
682683
return nil, err
683684
}
684685
return rv, nil
685-
686686
}
687687

688688
type BackIndexRow struct {
@@ -1029,7 +1029,7 @@ func visitBackIndexRow(data []byte, callback backIndexFieldTermVisitor) error {
10291029
return io.ErrUnexpectedEOF
10301030
}
10311031
// don't track unrecognized data
1032-
//m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...)
1032+
// m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...)
10331033
iNdEx += skippy
10341034
}
10351035
}
@@ -1109,7 +1109,7 @@ func visitBackIndexRowFieldTerms(data []byte, callback backIndexFieldTermVisitor
11091109
if postIndex > l {
11101110
return io.ErrUnexpectedEOF
11111111
}
1112-
//m.Terms = append(m.Terms, string(data[iNdEx:postIndex]))
1112+
// m.Terms = append(m.Terms, string(data[iNdEx:postIndex]))
11131113
callback(theField, data[iNdEx:postIndex])
11141114
iNdEx = postIndex
11151115
default:
@@ -1132,7 +1132,7 @@ func visitBackIndexRowFieldTerms(data []byte, callback backIndexFieldTermVisitor
11321132
if (iNdEx + skippy) > l {
11331133
return io.ErrUnexpectedEOF
11341134
}
1135-
//m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...)
1135+
// m.XXX_unrecognized = append(m.XXX_unrecognized, data[iNdEx:iNdEx+skippy]...)
11361136
iNdEx += skippy
11371137
}
11381138
}

search/query/regexp.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ func (q *RegexpQuery) Searcher(ctx context.Context, i index.IndexReader, m mappi
6969
// known to interfere with LiteralPrefix() the way ^ does
7070
// and removing $ introduces possible ambiguities with escaped \$, \\$, etc
7171
actualRegexp := q.Regexp
72-
if strings.HasPrefix(actualRegexp, "^") {
73-
actualRegexp = actualRegexp[1:] // remove leading ^
74-
}
72+
actualRegexp = strings.TrimPrefix(actualRegexp, "^") // remove leading ^ if it exists
7573

76-
return searcher.NewRegexpStringSearcher(ctx, i, actualRegexp, field,
77-
q.BoostVal.Value(), options)
74+
return searcher.NewRegexpStringSearcher(ctx, i, actualRegexp, field, q.BoostVal.Value(), options)
7875
}
7976

8077
func (q *RegexpQuery) Validate() error {

search/searcher/search_conjunction.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ type ConjunctionSearcher struct {
4747

4848
func NewConjunctionSearcher(ctx context.Context, indexReader index.IndexReader,
4949
qsearchers []search.Searcher, options search.SearcherOptions) (
50-
search.Searcher, error) {
50+
search.Searcher, error,
51+
) {
5152
// build the sorted downstream searchers
5253
searchers := make(OrderedSearcherList, len(qsearchers))
53-
for i, searcher := range qsearchers {
54-
searchers[i] = searcher
55-
}
54+
copy(searchers, qsearchers)
5655
sort.Sort(searchers)
5756

5857
// attempt the "unadorned" conjunction optimization only when we

search/searcher/search_disjunction_slice.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ type DisjunctionSliceSearcher struct {
5252
func newDisjunctionSliceSearcher(ctx context.Context, indexReader index.IndexReader,
5353
qsearchers []search.Searcher, min float64, options search.SearcherOptions,
5454
limit bool) (
55-
*DisjunctionSliceSearcher, error) {
55+
*DisjunctionSliceSearcher, error,
56+
) {
5657
if limit && tooManyClauses(len(qsearchers)) {
5758
return nil, tooManyClausesErr("", len(qsearchers))
5859
}
@@ -79,9 +80,7 @@ func newDisjunctionSliceSearcher(ctx context.Context, indexReader index.IndexRea
7980
originalPos = sortedSearchers.index
8081
} else {
8182
searchers = make(OrderedSearcherList, len(qsearchers))
82-
for i, searcher := range qsearchers {
83-
searchers[i] = searcher
84-
}
83+
copy(searchers, qsearchers)
8584
sort.Sort(searchers)
8685
}
8786

@@ -210,7 +209,8 @@ func (s *DisjunctionSliceSearcher) SetQueryNorm(qnorm float64) {
210209
}
211210

212211
func (s *DisjunctionSliceSearcher) Next(ctx *search.SearchContext) (
213-
*search.DocumentMatch, error) {
212+
*search.DocumentMatch, error,
213+
) {
214214
if !s.initialized {
215215
err := s.initSearchers(ctx)
216216
if err != nil {
@@ -255,7 +255,8 @@ func (s *DisjunctionSliceSearcher) Next(ctx *search.SearchContext) (
255255
}
256256

257257
func (s *DisjunctionSliceSearcher) Advance(ctx *search.SearchContext,
258-
ID index.IndexInternalID) (*search.DocumentMatch, error) {
258+
ID index.IndexInternalID,
259+
) (*search.DocumentMatch, error) {
259260
if !s.initialized {
260261
err := s.initSearchers(ctx)
261262
if err != nil {
@@ -320,7 +321,8 @@ func (s *DisjunctionSliceSearcher) DocumentMatchPoolSize() int {
320321
// but only activates on an edge case where the disjunction is a
321322
// wrapper around a single Optimizable child searcher
322323
func (s *DisjunctionSliceSearcher) Optimize(kind string, octx index.OptimizableContext) (
323-
index.OptimizableContext, error) {
324+
index.OptimizableContext, error,
325+
) {
324326
if len(s.searchers) == 1 {
325327
o, ok := s.searchers[0].(index.Optimizable)
326328
if ok {

search/sort.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import (
2828
"github.com/blevesearch/bleve/v2/util"
2929
)
3030

31-
var HighTerm = strings.Repeat(string(utf8.MaxRune), 3)
32-
var LowTerm = string([]byte{0x00})
31+
var (
32+
HighTerm = strings.Repeat(string(utf8.MaxRune), 3)
33+
LowTerm = string([]byte{0x00})
34+
)
3335

3436
type SearchSort interface {
3537
UpdateVisitor(field string, term []byte)
@@ -47,10 +49,15 @@ type SearchSort interface {
4749

4850
func ParseSearchSortObj(input map[string]interface{}) (SearchSort, error) {
4951
descending, ok := input["desc"].(bool)
52+
if !ok {
53+
descending = false
54+
}
55+
5056
by, ok := input["by"].(string)
5157
if !ok {
5258
return nil, fmt.Errorf("search sort must specify by")
5359
}
60+
5461
switch by {
5562
case "id":
5663
return &SortDocID{
@@ -612,7 +619,8 @@ var maxDistance = string(numeric.MustNewPrefixCodedInt64(math.MaxInt64, 0))
612619
// NewSortGeoDistance creates SearchSort instance for sorting documents by
613620
// their distance from the specified point.
614621
func NewSortGeoDistance(field, unit string, lon, lat float64, desc bool) (
615-
*SortGeoDistance, error) {
622+
*SortGeoDistance, error,
623+
) {
616624
rv := &SortGeoDistance{
617625
Field: field,
618626
Desc: desc,

0 commit comments

Comments
 (0)