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 <lirenzuo@shein.com>
This commit is contained in:
匠心零度 2022-01-28 18:10:47 +08:00 committed by GitHub
parent 069880fd3c
commit a1083d0531
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 28 deletions

View file

@ -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

View file

@ -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

View file

@ -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
}

View file

@ -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