Skip to content

Commit

Permalink
fix: index out of range error of BatchSave (apache#5561)
Browse files Browse the repository at this point in the history
  • Loading branch information
mindlesscloud committed Jun 25, 2023
1 parent d3bc54b commit ee73505
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backend/helpers/pluginhelper/api/batch_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"

"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/dal"
Expand All @@ -42,6 +43,7 @@ type BatchSave struct {
valueIndex map[string]int
primaryKey []reflect.StructField
tableName string
mutex sync.Mutex
}

// NewBatchSave creates a new BatchSave instance
Expand Down Expand Up @@ -86,7 +88,8 @@ func (c *BatchSave) Add(slot interface{}) errors.Error {
stripZeroByte(slot)
// deduplication
key := getKeyValue(slot, c.primaryKey)

c.mutex.Lock()
defer c.mutex.Unlock()
if key != "" {
if index, ok := c.valueIndex[key]; !ok {
c.valueIndex[key] = c.current
Expand All @@ -108,6 +111,8 @@ func (c *BatchSave) Add(slot interface{}) errors.Error {

// Flush save cached records into database
func (c *BatchSave) Flush() errors.Error {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.current == 0 {
return nil
}
Expand Down

0 comments on commit ee73505

Please sign in to comment.