From 2283cd5f430c2e958a33c001d7ebc27d3f617b32 Mon Sep 17 00:00:00 2001 From: thomas Date: Mon, 7 Apr 2025 13:45:23 -0700 Subject: [PATCH] Fix compilation on 32 bit architectures --- cmd/zoekt-sourcegraph-indexserver/index.go | 2 +- index/bits_test.go | 8 +++---- index/hititer.go | 25 +++++++++++----------- index/indexdata.go | 5 ++--- index/indexfile.go | 3 ++- index/matchiter.go | 7 +++--- index/matchtree.go | 13 +++++------ index/matchtree_test.go | 9 ++++---- 8 files changed, 38 insertions(+), 34 deletions(-) diff --git a/cmd/zoekt-sourcegraph-indexserver/index.go b/cmd/zoekt-sourcegraph-indexserver/index.go index cbd09de2a..71e5d4682 100644 --- a/cmd/zoekt-sourcegraph-indexserver/index.go +++ b/cmd/zoekt-sourcegraph-indexserver/index.go @@ -115,7 +115,7 @@ func (o *indexArgs) BuildOptions() *index.Options { // nothing needs to be done. RepositoryDescription: zoekt.Repository{ TenantID: o.TenantID, - ID: uint32(o.IndexOptions.RepoID), + ID: o.IndexOptions.RepoID, Name: o.Name, Branches: o.Branches, RawConfig: map[string]string{ diff --git a/index/bits_test.go b/index/bits_test.go index 2337fd819..fc759dc4a 100644 --- a/index/bits_test.go +++ b/index/bits_test.go @@ -17,14 +17,14 @@ package index import ( "encoding/binary" "log" + "math" "math/rand" "reflect" + "slices" "strconv" "testing" "testing/quick" - "slices" - "github.com/google/go-cmp/cmp" ) @@ -99,7 +99,7 @@ func TestNextFileIndex(t *testing.T) { ends []uint32 want uint32 }{ - {maxUInt32, 0, []uint32{34}, 1}, + {math.MaxUint32, 0, []uint32{34}, 1}, {9, 0, []uint32{34}, 0}, {450, 0, []uint32{100, 200, 300, 400, 500, 600}, 4}, } { @@ -150,7 +150,7 @@ func TestCompressedPostingIterator(t *testing.T) { var nums []uint32 i := newCompressedPostingIterator(data, stringToNGram("abc")) - for i.first() != maxUInt32 { + for i.first() != math.MaxUint32 { nums = append(nums, i.first()) i.next(i.first()) } diff --git a/index/hititer.go b/index/hititer.go index 7129ce0f2..1d63fc797 100644 --- a/index/hititer.go +++ b/index/hititer.go @@ -17,6 +17,7 @@ package index import ( "encoding/binary" "fmt" + "math" "github.com/sourcegraph/zoekt" ) @@ -24,10 +25,10 @@ import ( // hitIterator finds potential search matches, measured in offsets of // the concatenation of all documents. type hitIterator interface { - // Return the first hit, or maxUInt32 if none. + // Return the first hit, or math.MaxUint32 if none. first() uint32 - // Skip until past limit. The argument maxUInt32 should be + // Skip until past limit. The argument math.MaxUint32 should be // treated specially. next(limit uint32) @@ -52,8 +53,8 @@ func (i *distanceHitIterator) findNext() { var p1, p2 uint32 p1 = i.i1.first() p2 = i.i2.first() - if p1 == maxUInt32 || p2 == maxUInt32 { - i.i1.next(maxUInt32) + if p1 == math.MaxUint32 || p2 == math.MaxUint32 { + i.i1.next(math.MaxUint32) break } @@ -85,7 +86,7 @@ func (i *distanceHitIterator) next(limit uint32) { l2 := limit + i.distance if l2 < limit { // overflow. - l2 = maxUInt32 + l2 = math.MaxUint32 } i.i2.next(l2) i.findNext() @@ -158,14 +159,14 @@ func (i *inMemoryIterator) first() uint32 { if len(i.postings) > 0 { return i.postings[0] } - return maxUInt32 + return math.MaxUint32 } -func (i *inMemoryIterator) updateStats(s *zoekt.Stats) { +func (i *inMemoryIterator) updateStats(_ *zoekt.Stats) { } func (i *inMemoryIterator) next(limit uint32) { - if limit == maxUInt32 { + if limit == math.MaxUint32 { i.postings = nil } @@ -203,9 +204,9 @@ func (i *compressedPostingIterator) first() uint32 { } func (i *compressedPostingIterator) next(limit uint32) { - if limit == maxUInt32 { + if limit == math.MaxUint32 { i.blob = nil - i._first = maxUInt32 + i._first = math.MaxUint32 return } @@ -217,7 +218,7 @@ func (i *compressedPostingIterator) next(limit uint32) { } if i._first <= limit && len(i.blob) == 0 { - i._first = maxUInt32 + i._first = math.MaxUint32 } } @@ -248,7 +249,7 @@ func (i *mergingIterator) updateStats(s *zoekt.Stats) { } func (i *mergingIterator) first() uint32 { - r := uint32(maxUInt32) + r := uint32(math.MaxUint32) for _, j := range i.iters { f := j.first() if f < r { diff --git a/index/indexdata.go b/index/indexdata.go index 1bc7fa2f6..6793ec377 100644 --- a/index/indexdata.go +++ b/index/indexdata.go @@ -20,6 +20,7 @@ import ( "fmt" "hash/crc64" "log" + "math" "math/bits" "slices" "unicode/utf8" @@ -354,12 +355,10 @@ func findSelectiveNgrams(ngramOffs []runeNgramOff, indexMap []int, frequencies [ return } -const maxUInt32 = 0xffffffff - func minFrequencyNgramOffsets(ngramOffs []runeNgramOff, frequencies []uint32) (first, last runeNgramOff) { // Find the two lowest frequency ngrams. idx0, idx1 := 0, 0 - min0, min1 := uint32(maxUInt32), uint32(maxUInt32) + min0, min1 := uint32(math.MaxUint32), uint32(math.MaxUint32) for i, x := range frequencies { if x <= min0 { idx0, idx1 = i, idx0 diff --git a/index/indexfile.go b/index/indexfile.go index 5771ff54f..e9ddb3b5d 100644 --- a/index/indexfile.go +++ b/index/indexfile.go @@ -19,6 +19,7 @@ package index import ( "fmt" "log" + "math" "os" "golang.org/x/sys/unix" @@ -62,7 +63,7 @@ func NewIndexFile(f *os.File) (IndexFile, error) { } sz := fi.Size() - if sz >= maxUInt32 { + if sz >= math.MaxUint32 { return nil, fmt.Errorf("file %s too large: %d", f.Name(), sz) } r := &mmapedIndexFile{ diff --git a/index/matchiter.go b/index/matchiter.go index 982677eb5..df75410b5 100644 --- a/index/matchiter.go +++ b/index/matchiter.go @@ -17,6 +17,7 @@ package index import ( "bytes" "fmt" + "math" "github.com/sourcegraph/zoekt" ) @@ -95,7 +96,7 @@ func (t *noMatchTree) candidates() []*candidateMatch { } func (t *noMatchTree) nextDoc() uint32 { - return maxUInt32 + return math.MaxUint32 } func (t *noMatchTree) prepare(uint32) {} @@ -148,7 +149,7 @@ func nextFileIndex(offset, f uint32, ends []uint32) uint32 { func (i *ngramDocIterator) nextDoc() uint32 { i.fileIdx = nextFileIndex(i.iter.first(), i.fileIdx, i.ends) if i.fileIdx >= uint32(len(i.ends)) { - return maxUInt32 + return math.MaxUint32 } return i.fileIdx } @@ -190,7 +191,7 @@ func (i *ngramDocIterator) candidates() []*candidateMatch { var candidates []*candidateMatch for { p1 := i.iter.first() - if p1 == maxUInt32 || p1 >= i.ends[i.fileIdx] { + if p1 == math.MaxUint32 || p1 >= i.ends[i.fileIdx] { break } i.iter.next(p1) diff --git a/index/matchtree.go b/index/matchtree.go index 6fda311b1..6929b350c 100644 --- a/index/matchtree.go +++ b/index/matchtree.go @@ -18,6 +18,7 @@ import ( "bytes" "fmt" "log" + "math" "regexp/syntax" "strings" "unicode/utf8" @@ -442,7 +443,7 @@ func (t *docMatchTree) nextDoc() uint32 { return i } } - return maxUInt32 + return math.MaxUint32 } func (t *bruteForceMatchTree) nextDoc() uint32 { @@ -464,7 +465,7 @@ func (t *andMatchTree) nextDoc() uint32 { } func (t *orMatchTree) nextDoc() uint32 { - min := uint32(maxUInt32) + min := uint32(math.MaxUint32) for _, c := range t.children { m := c.nextDoc() if m < min { @@ -497,7 +498,7 @@ func (t *branchQueryMatchTree) nextDoc() uint32 { return i } } - return maxUInt32 + return math.MaxUint32 } // all String methods @@ -672,7 +673,7 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc // can return MatchesFound. // find child with fewest candidates - min := maxUInt32 + minCount := math.MaxInt fewestChildren := 0 for ix, child := range t.children { v, ok := child.(*substrMatchTree) @@ -681,8 +682,8 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc if !ok || v.fileName { return matchesFound } - if len(v.current) < min { - min = len(v.current) + if len(v.current) < minCount { + minCount = len(v.current) fewestChildren = ix } } diff --git a/index/matchtree_test.go b/index/matchtree_test.go index 4361bb9b8..cedc211b3 100644 --- a/index/matchtree_test.go +++ b/index/matchtree_test.go @@ -15,6 +15,7 @@ package index import ( + "math" "reflect" "regexp/syntax" "testing" @@ -304,7 +305,7 @@ func TestRepoSet(t *testing.T) { } mt.prepare(nextDoc) } - if mt.nextDoc() != maxUInt32 { + if mt.nextDoc() != math.MaxUint32 { t.Fatalf("expected %d document, but got at least 1 more", len(want)) } } @@ -327,7 +328,7 @@ func TestRepo(t *testing.T) { } mt.prepare(nextDoc) } - if mt.nextDoc() != maxUInt32 { + if mt.nextDoc() != math.MaxUint32 { t.Fatalf("expect %d documents, but got at least 1 more", len(want)) } } @@ -360,7 +361,7 @@ func TestBranchesRepos(t *testing.T) { mt.prepare(nextDoc) } - if mt.nextDoc() != maxUInt32 { + if mt.nextDoc() != math.MaxUint32 { t.Fatalf("expect %d documents, but got at least 1 more", len(want)) } } @@ -383,7 +384,7 @@ func TestRepoIDs(t *testing.T) { } mt.prepare(nextDoc) } - if mt.nextDoc() != maxUInt32 { + if mt.nextDoc() != math.MaxUint32 { t.Fatalf("expected %d document, but got at least 1 more", len(want)) } }