From 1999bbfe82a7e73b43c4d84e12ae423ff3b155c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8C=A0=E5=BF=83=E9=9B=B6=E5=BA=A6?= <97019752@qq.com> Date: Fri, 28 Jan 2022 18:10:47 +0800 Subject: [PATCH] optimized code (#2103) * optimized code ,because only the first error,so no need var errors []error * optimized code ,because only the first error,so no need var errors []error Co-authored-by: lirenzuo --- lib/mergeset/table_search.go | 10 +++------- lib/storage/partition_search.go | 10 +++------- lib/storage/table.go | 9 ++------- lib/storage/table_search.go | 10 +++------- 4 files changed, 11 insertions(+), 28 deletions(-) diff --git a/lib/mergeset/table_search.go b/lib/mergeset/table_search.go index afdc053412..fdd03d5867 100644 --- a/lib/mergeset/table_search.go +++ b/lib/mergeset/table_search.go @@ -89,24 +89,20 @@ func (ts *TableSearch) Seek(k []byte) { ts.err = nil // Initialize the psHeap. - var errors []error ts.psHeap = ts.psHeap[:0] for i := range ts.psPool { ps := &ts.psPool[i] ps.Seek(k) if !ps.NextItem() { if err := ps.Error(); err != nil { - errors = append(errors, err) + // Return only the first error, since it has no sense in returning all errors. + ts.err = fmt.Errorf("cannot seek %q: %w", k, err) + return } continue } ts.psHeap = append(ts.psHeap, ps) } - if len(errors) > 0 { - // Return only the first error, since it has no sense in returning all errors. - ts.err = fmt.Errorf("cannot seek %q: %w", k, errors[0]) - return - } if len(ts.psHeap) == 0 { ts.err = io.EOF return diff --git a/lib/storage/partition_search.go b/lib/storage/partition_search.go index 1639102be2..ace5902845 100644 --- a/lib/storage/partition_search.go +++ b/lib/storage/partition_search.go @@ -92,23 +92,19 @@ func (pts *partitionSearch) Init(pt *partition, tsids []TSID, tr TimeRange) { } // Initialize the psHeap. - var errors []error pts.psHeap = pts.psHeap[:0] for i := range pts.psPool { ps := &pts.psPool[i] if !ps.NextBlock() { if err := ps.Error(); err != nil { - errors = append(errors, err) + // Return only the first error, since it has no sense in returning all errors. + pts.err = fmt.Errorf("cannot initialize partition search: %w", err) + return } continue } pts.psHeap = append(pts.psHeap, ps) } - if len(errors) > 0 { - // Return only the first error, since it has no sense in returning all errors. - pts.err = fmt.Errorf("cannot initialize partition search: %w", errors[0]) - return - } if len(pts.psHeap) == 0 { pts.err = io.EOF return diff --git a/lib/storage/table.go b/lib/storage/table.go index c85bbbfb01..80f35449ce 100644 --- a/lib/storage/table.go +++ b/lib/storage/table.go @@ -338,7 +338,6 @@ func (tb *table) AddRows(rows []rawRow) error { // Do this under tb.ptwsLock. minTimestamp, maxTimestamp := tb.getMinMaxTimestamps() tb.ptwsLock.Lock() - var errors []error for i := range missingRows { r := &missingRows[i] @@ -362,18 +361,14 @@ func (tb *table) AddRows(rows []rawRow) error { pt, err := createPartition(r.Timestamp, tb.smallPartitionsPath, tb.bigPartitionsPath, tb.getDeletedMetricIDs, tb.retentionMsecs) if err != nil { - errors = append(errors, err) - continue + // Return only the first error, since it has no sense in returning all errors. + return fmt.Errorf("errors while adding rows to table %q: %w", tb.path, err) } pt.AddRows(missingRows[i : i+1]) tb.addPartitionNolock(pt) } tb.ptwsLock.Unlock() - if len(errors) > 0 { - // Return only the first error, since it has no sense in returning all errors. - return fmt.Errorf("errors while adding rows to table %q: %w", tb.path, errors[0]) - } return nil } diff --git a/lib/storage/table_search.go b/lib/storage/table_search.go index f81dc2136f..f344da06ab 100644 --- a/lib/storage/table_search.go +++ b/lib/storage/table_search.go @@ -93,23 +93,19 @@ func (ts *tableSearch) Init(tb *table, tsids []TSID, tr TimeRange) { } // Initialize the ptsHeap. - var errors []error ts.ptsHeap = ts.ptsHeap[:0] for i := range ts.ptsPool { pts := &ts.ptsPool[i] if !pts.NextBlock() { if err := pts.Error(); err != nil { - errors = append(errors, err) + // Return only the first error, since it has no sense in returning all errors. + ts.err = fmt.Errorf("cannot initialize table search: %w", err) + return } continue } ts.ptsHeap = append(ts.ptsHeap, pts) } - if len(errors) > 0 { - // Return only the first error, since it has no sense in returning all errors. - ts.err = fmt.Errorf("cannot initialize table search: %w", errors[0]) - return - } if len(ts.ptsHeap) == 0 { ts.err = io.EOF return