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 Aliaksandr Valialkin
parent 97947c5fcf
commit 1999bbfe82
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
4 changed files with 11 additions and 28 deletions

View file

@ -89,24 +89,20 @@ func (ts *TableSearch) Seek(k []byte) {
ts.err = nil ts.err = nil
// Initialize the psHeap. // Initialize the psHeap.
var errors []error
ts.psHeap = ts.psHeap[:0] ts.psHeap = ts.psHeap[:0]
for i := range ts.psPool { for i := range ts.psPool {
ps := &ts.psPool[i] ps := &ts.psPool[i]
ps.Seek(k) ps.Seek(k)
if !ps.NextItem() { if !ps.NextItem() {
if err := ps.Error(); err != nil { 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 continue
} }
ts.psHeap = append(ts.psHeap, ps) 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 { if len(ts.psHeap) == 0 {
ts.err = io.EOF ts.err = io.EOF
return return

View file

@ -92,23 +92,19 @@ func (pts *partitionSearch) Init(pt *partition, tsids []TSID, tr TimeRange) {
} }
// Initialize the psHeap. // Initialize the psHeap.
var errors []error
pts.psHeap = pts.psHeap[:0] pts.psHeap = pts.psHeap[:0]
for i := range pts.psPool { for i := range pts.psPool {
ps := &pts.psPool[i] ps := &pts.psPool[i]
if !ps.NextBlock() { if !ps.NextBlock() {
if err := ps.Error(); err != nil { 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 continue
} }
pts.psHeap = append(pts.psHeap, ps) 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 { if len(pts.psHeap) == 0 {
pts.err = io.EOF pts.err = io.EOF
return return

View file

@ -338,7 +338,6 @@ func (tb *table) AddRows(rows []rawRow) error {
// Do this under tb.ptwsLock. // Do this under tb.ptwsLock.
minTimestamp, maxTimestamp := tb.getMinMaxTimestamps() minTimestamp, maxTimestamp := tb.getMinMaxTimestamps()
tb.ptwsLock.Lock() tb.ptwsLock.Lock()
var errors []error
for i := range missingRows { for i := range missingRows {
r := &missingRows[i] 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) pt, err := createPartition(r.Timestamp, tb.smallPartitionsPath, tb.bigPartitionsPath, tb.getDeletedMetricIDs, tb.retentionMsecs)
if err != nil { if err != nil {
errors = append(errors, err) // Return only the first error, since it has no sense in returning all errors.
continue return fmt.Errorf("errors while adding rows to table %q: %w", tb.path, err)
} }
pt.AddRows(missingRows[i : i+1]) pt.AddRows(missingRows[i : i+1])
tb.addPartitionNolock(pt) tb.addPartitionNolock(pt)
} }
tb.ptwsLock.Unlock() 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 return nil
} }

View file

@ -93,23 +93,19 @@ func (ts *tableSearch) Init(tb *table, tsids []TSID, tr TimeRange) {
} }
// Initialize the ptsHeap. // Initialize the ptsHeap.
var errors []error
ts.ptsHeap = ts.ptsHeap[:0] ts.ptsHeap = ts.ptsHeap[:0]
for i := range ts.ptsPool { for i := range ts.ptsPool {
pts := &ts.ptsPool[i] pts := &ts.ptsPool[i]
if !pts.NextBlock() { if !pts.NextBlock() {
if err := pts.Error(); err != nil { 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 continue
} }
ts.ptsHeap = append(ts.ptsHeap, pts) 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 { if len(ts.ptsHeap) == 0 {
ts.err = io.EOF ts.err = io.EOF
return return