Skip to content

Commit

Permalink
chore(repo): use go 1.22 int range loop
Browse files Browse the repository at this point in the history
  • Loading branch information
xqqp committed Oct 3, 2024
1 parent 345c438 commit e90fbde
Show file tree
Hide file tree
Showing 84 changed files with 211 additions and 215 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ linters:
- staticcheck
- unconvert
- unused
- intrange
2 changes: 1 addition & 1 deletion algo/packed.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func IndexOfPacked(u *pb.UidPack, uid uint64) int {
}

index += uidx
for i := 0; i < decoder.BlockIdx(); i++ {
for i := range decoder.BlockIdx() {
index += int(u.Blocks[i].GetNumUids())
}

Expand Down
6 changes: 3 additions & 3 deletions algo/packed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,20 @@ func TestSubSorted6Packed(t *testing.T) {

func TestIndexOfPacked1(t *testing.T) {
encoder := codec.Encoder{BlockSize: 10}
for i := 0; i < 1000; i++ {
for i := range 1000 {
encoder.Add(uint64(i))
}
pack := encoder.Done()

for i := 0; i < 1000; i++ {
for i := range 1000 {
require.Equal(t, i, IndexOfPacked(pack, uint64(i)))
}
require.Equal(t, -1, IndexOfPacked(pack, 1000))
}

func TestIndexOfPacked2(t *testing.T) {
encoder := codec.Encoder{BlockSize: 10}
for i := 0; i < 100; i++ {
for i := range 100 {
encoder.Add(uint64(i))
}
pack := encoder.Done()
Expand Down
14 changes: 7 additions & 7 deletions algo/uidlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func BenchmarkListIntersectRandom(b *testing.B) {
randomTests := func(arrSz int, overlap float64) {
limit := int64(float64(arrSz) / overlap)
u1, v1 := make([]uint64, arrSz), make([]uint64, arrSz)
for i := 0; i < arrSz; i++ {
for i := range arrSz {
u1[i] = uint64(rand.Int63n(limit))
v1[i] = uint64(rand.Int63n(limit))
}
Expand Down Expand Up @@ -379,10 +379,10 @@ func BenchmarkListIntersectCompressBin(b *testing.B) {

u1, v1 := make([]uint64, sz1), make([]uint64, sz2)
limit := int64(float64(sz) / overlap)
for i := 0; i < sz1; i++ {
for i := range sz1 {
u1[i] = uint64(rand.Int63n(limit))
}
for i := 0; i < sz2; i++ {
for i := range sz2 {
v1[i] = uint64(rand.Int63n(limit))
}
sort.Slice(u1, func(i, j int) bool { return u1[i] < u1[j] })
Expand Down Expand Up @@ -434,10 +434,10 @@ func BenchmarkListIntersectRatio(b *testing.B) {

u1, v1 := make([]uint64, sz1), make([]uint64, sz2)
limit := int64(float64(sz) / overlap)
for i := 0; i < sz1; i++ {
for i := range sz1 {
u1[i] = uint64(rand.Int63n(limit))
}
for i := 0; i < sz2; i++ {
for i := range sz2 {
v1[i] = uint64(rand.Int63n(limit))
}
sort.Slice(u1, func(i, j int) bool { return u1[i] < u1[j] })
Expand Down Expand Up @@ -510,7 +510,7 @@ func fillNumsDiff(N1, N2, N3 int) ([]uint64, []uint64, []uint64) {
otherNums := make([]uint64, N1+N3)
allC := make(map[uint64]bool)

for i := 0; i < N1; i++ {
for i := range N1 {
val := rand.Uint64() % 1000
commonNums[i] = val
blockNums[i] = val
Expand Down Expand Up @@ -546,7 +546,7 @@ func fillNums(N1, N2 int) ([]uint64, []uint64, []uint64) {
blockNums := make([]uint64, N1+N2)
otherNums := make([]uint64, N1+N2)

for i := 0; i < N1; i++ {
for i := range N1 {
val := rand.Uint64()
commonNums[i] = val
blockNums[i] = val
Expand Down
2 changes: 1 addition & 1 deletion chunker/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewChunker(inputFormat InputFormat, batchSize int) Chunker {
func (*rdfChunker) Chunk(r *bufio.Reader) (*bytes.Buffer, error) {
batch := new(bytes.Buffer)
batch.Grow(1 << 20)
for lineCount := 0; lineCount < 1e5; lineCount++ {
for range 100000 {
slc, err := r.ReadSlice('\n')
if err == io.EOF {
if _, err := batch.Write(slc); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion codec/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func benchmarkUnpack(trials int, chunks *chunks) int {
for i, c := range chunks.data {
out := codec.Decode(packed[i], 0)

for j := 0; j < len(c); j++ {
for j := range c {
if c[j] != out[j] {
x.Fatalf("Something wrong %+v \n%+v\n %+v\n", len(c), len(out), j)
}
Expand Down
4 changes: 2 additions & 2 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (e *Encoder) packBlock() {
buf := make([]byte, 17)
tmpUids := make([]uint32, 4)
for {
for i := 0; i < 4; i++ {
for i := range 4 {
if i >= len(e.uids) {
// Padding with '0' because Encode4 encodes only in batch of 4.
tmpUids[i] = 0
Expand Down Expand Up @@ -199,7 +199,7 @@ func (d *Decoder) UnpackBlock() []uint64 {

groupvarint.Decode4(tmpUids, encData)
encData = encData[groupvarint.BytesUsed[encData[0]]:]
for i := 0; i < 4; i++ {
for i := range 4 {
sum = last + uint64(tmpUids[i])
d.uids = append(d.uids, sum)
last = sum
Expand Down
6 changes: 3 additions & 3 deletions codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestUidPack(t *testing.T) {
require.Equal(t, 0, ApproxLen(&pb.UidPack{}))
require.Equal(t, 0, len(Decode(&pb.UidPack{}, 0)))

for i := 0; i < 13; i++ {
for range 13 {
size := rand.Intn(10e6)
if size < 0 {
size = 1e6
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestBufferUidPack(t *testing.T) {
require.Equal(t, 0, buf.LenNoPadding())
require.NoError(t, buf.Release())

for i := 0; i < 13; i++ {
for range 13 {
size := rand.Intn(10e6)
if size < 0 {
size = 1e6
Expand Down Expand Up @@ -335,7 +335,7 @@ func TestEncoding(t *testing.T) {
rand.Seed(time.Now().UnixNano())
var lengths = []int{0, 1, 2, 3, 5, 13, 18, 100, 99, 98}

for tc := 0; tc < len(lengths); tc++ {
for tc := range lengths {
ints := make([]uint64, lengths[tc])

for i := 0; i < 50 && i < lengths[tc]; i++ {
Expand Down
2 changes: 1 addition & 1 deletion conn/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestProposal(t *testing.T) {
wg.Add(loop)
go n.run(&wg)

for i := 0; i < loop; i++ {
for i := range loop {
data := []byte(fmt.Sprintf("hey-%d", i))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
2 changes: 1 addition & 1 deletion contrib/integration/acctupsert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func doUpserts(c *dgo.Dgraph) {
var wg sync.WaitGroup
wg.Add(len(accounts) * *concurr)
for _, acct := range accounts {
for i := 0; i < *concurr; i++ {
for range *concurr {
go func(acct account) {
upsert(c, acct)
wg.Done()
Expand Down
2 changes: 1 addition & 1 deletion contrib/integration/bank/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func main() {
s.createAccounts(clients[0])

var wg sync.WaitGroup
for i := 0; i < *conc; i++ {
for range *conc {
for _, dg := range clients {
wg.Add(1)
go s.loop(dg, &wg)
Expand Down
4 changes: 2 additions & 2 deletions contrib/integration/bigdata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
case "mutate":
var errCount int64
var mutateCount int64
for i := 0; i < *conc; i++ {
for range *conc {
go func() {
for {
err := mutate(c)
Expand All @@ -94,7 +94,7 @@ func main() {
case "query":
var errCount int64
var queryCount int64
for i := 0; i < *conc; i++ {
for range *conc {
go func() {
for {
err := showNode(c)
Expand Down
6 changes: 3 additions & 3 deletions contrib/integration/swap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func main() {
done := make(chan struct{})
go func() {
pending := make(chan struct{}, *concurr)
for i := 0; i < *numSwaps; i++ {
for range *numSwaps {
pending <- struct{}{}
go func() {
swapSentences(c,
Expand All @@ -114,7 +114,7 @@ func main() {
<-pending
}()
}
for i := 0; i < *concurr; i++ {
for range *concurr {
pending <- struct{}{}
}
close(done)
Expand Down Expand Up @@ -343,7 +343,7 @@ func checkInvariants(c *dgo.Dgraph, uids []string, sentences []string) error {
}
}
sort.Strings(gotSentences)
for i := 0; i < len(sentences); i++ {
for i := range sentences {
if sentences[i] != gotSentences[i] {
fmt.Printf("Sentence doesn't match. Wanted: %q. Got: %q\n", sentences[i], gotSentences[i])
fmt.Printf("All sentences: %v\n", sentences)
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/bulk/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func newLoader(opt *options) *loader {
mappers: make([]*mapper, opt.NumGoroutines),
zero: zero,
}
for i := 0; i < opt.NumGoroutines; i++ {
for i := range opt.NumGoroutines {
ld.mappers[i] = newMapper(st)
}
go ld.prog.report()
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/bulk/merge_shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func mergeMapShardsIntoReduceShards(opt *options) {
sortBySize(shardDirs)

var reduceShards []string
for i := 0; i < opt.ReduceShards; i++ {
for i := range opt.ReduceShards {
shardDir := filepath.Join(opt.TmpDir, reduceShardDir, fmt.Sprintf("shard_%d", i))
x.Check(os.MkdirAll(shardDir, 0750))
reduceShards = append(reduceShards, shardDir)
Expand Down
6 changes: 3 additions & 3 deletions dgraph/cmd/bulk/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (r *reducer) run() error {
x.AssertTrue(len(r.opt.shardOutputDirs) == r.opt.ReduceShards)

thr := y.NewThrottle(r.opt.NumReducers)
for i := 0; i < r.opt.ReduceShards; i++ {
for i := range r.opt.ReduceShards {
if err := thr.Do(); err != nil {
return err
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func (r *reducer) reduce(partitionKeys [][]byte, mapItrs []*mapIterator, ci *cou
encoderCh := make(chan *encodeRequest, 2*cpu)
writerCh := make(chan *encodeRequest, 2*cpu)
encoderCloser := z.NewCloser(cpu)
for i := 0; i < cpu; i++ {
for range cpu {
// Start listening to encode entries
// For time being let's lease 100 stream id for each encoder.
go r.encode(encoderCh, encoderCloser)
Expand Down Expand Up @@ -500,7 +500,7 @@ func (r *reducer) reduce(partitionKeys [][]byte, mapItrs []*mapIterator, ci *cou
// Append nil for the last entries.
partitionKeys = append(partitionKeys, nil)

for i := 0; i < len(partitionKeys); i++ {
for i := range partitionKeys {
pkey := partitionKeys[i]
for _, itr := range mapItrs {
itr.Next(cbuf, pkey)
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/bulk/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func run() {

// Delete and recreate the output dirs to ensure they are empty.
x.Check(os.RemoveAll(opt.OutDir))
for i := 0; i < opt.ReduceShards; i++ {
for i := range opt.ReduceShards {
dir := filepath.Join(opt.OutDir, strconv.Itoa(i), "p")
x.Check(os.MkdirAll(dir, 0700))
opt.shardOutputDirs = append(opt.shardOutputDirs, dir)
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/cert/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func createCAPair(opt *options) error {
// which case the path must already exist and be writable.
// Returns nil on success, or an error otherwise.
func createNodePair(opt *options) error {
if opt.nodes == nil || len(opt.nodes) == 0 {
if len(opt.nodes) == 0 {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/debug/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func jepsen(db *badger.DB) {
showAllPostingsAt(db, ts)
seekTotal(db, ts-1)

for i := 0; i < 5; i++ {
for range 5 {
// Get a few previous commits.
_, ts = getMinMax(db, ts-1)
showAllPostingsAt(db, ts)
Expand Down
4 changes: 2 additions & 2 deletions dgraph/cmd/increment/increment.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func run(conf *viper.Viper) {

// Run things serially first, if conc > 1.
if conc > 1 {
for i := 0; i < conc; i++ {
for range conc {
err := processOne(0)
x.Check(err)
num--
Expand All @@ -241,7 +241,7 @@ func run(conf *viper.Viper) {
}
}

for i := 0; i < conc; i++ {
for i := range conc {
wg.Add(1)
go f(i + 1)
}
Expand Down
4 changes: 2 additions & 2 deletions dgraph/cmd/live/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func setup(opts batchMutationOptions, dc *dgo.Dgraph, conf *viper.Viper) *loader
}

l.requestsWg.Add(opts.Pending)
for i := 0; i < opts.Pending; i++ {
for range opts.Pending {
go l.makeRequests()
}

Expand Down Expand Up @@ -850,7 +850,7 @@ func run() error {
go l.printCounters()
}

for i := 0; i < totalFiles; i++ {
for range totalFiles {
if err := <-errCh; err != nil {
fmt.Printf("Error while processing data file %s\n", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/migrate/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func getColumnValues(columns []string, dataTypes []dataType,
}

valuePtrs := make([]interface{}, 0, len(columns))
for i := 0; i < len(columns); i++ {
for i := range columns {
switch dataTypes[i] {
case stringType:
valuePtrs = append(valuePtrs, new([]byte)) // the value can be nil
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/zero/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (n *node) applyProposal(e raftpb.Entry) (uint64, error) {
}
}

if p.Tablets != nil && len(p.Tablets) > 0 {
if len(p.Tablets) > 0 {
if err := n.handleBulkTabletProposal(p.Tablets); err != nil {
span.Annotatef(nil, "While applying bulk tablet proposal: %v", err)
glog.Errorf("While applying bulk tablet proposal: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion dgraphapi/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ loop:
// and the "loop" will not be able to detect this case. The "loop2" below ensures
// by looking into the logs that the restore process has in fact been started and completed.
loop2:
for i := 0; i < 60; i++ {
for range 60 {
time.Sleep(waitDurBeforeRetry)

alphasLogs, err := c.AlphasLogs()
Expand Down
2 changes: 1 addition & 1 deletion dgraphapi/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

func GenerateRandomVector(size int) []float32 {
vector := make([]float32, size)
for i := 0; i < size; i++ {
for i := range size {
vector[i] = rand.Float32() * 10
}
return vector
Expand Down
2 changes: 1 addition & 1 deletion dgraphtest/dcloud_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c1 *DCloudCluster) AssignUids(client *dgo.Dgraph, num uint64) error {
genData := func() []byte {
var rdfs bytes.Buffer
_, _ = rdfs.WriteString("_:root <test_cloud> \"root\" .\n")
for i := 0; i < 1000; i++ {
for i := range 1000 {
rdfs.WriteString(fmt.Sprintf("_:%v <test_cloud> \"0\" .\n", i))
}
return rdfs.Bytes()
Expand Down
Loading

0 comments on commit e90fbde

Please sign in to comment.