Skip to content

Commit

Permalink
Merge pull request #12 from tomtwinkle/fix/async-load-or-store
Browse files Browse the repository at this point in the history
fix: sync.Map use LoadOrStore
  • Loading branch information
tomtwinkle authored Aug 24, 2022
2 parents b51ddfe + 05a105d commit 6c67f60
Showing 1 changed file with 40 additions and 65 deletions.
105 changes: 40 additions & 65 deletions excelizeam.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,14 @@ func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, sty
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)

if cached, ok := e.cellStore.Load(key); ok {
styleID, err := e.getStyleID(style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: value,
}); ok {
cell := cached.(*Cell)
if cell.Value != nil && value != nil && !overrideValue {
return ErrOverrideCellValue
Expand All @@ -200,31 +207,14 @@ func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, sty
if !overrideStyle {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(cell.StyleID, *style)
styleID, err = e.overrideStyle(cell.StyleID, *style)
if err != nil {
return err
}
cell.StyleID = styleID
return nil
}
styleID, err := e.getStyleID(style)
if err != nil {
return err
}
cell.StyleID = styleID
return nil
}
return nil
}

styleID, err := e.getStyleID(style)
if err != nil {
return err
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: value,
})
return nil
}

Expand All @@ -245,29 +235,28 @@ func (e *excelizeam) SetStyleCell(colIndex, rowIndex int, style excelize.Style,
func (e *excelizeam) setStyleCell(colIndex, rowIndex int, style excelize.Style, override bool) error {
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)
if cached, ok := e.cellStore.Load(key); ok {

styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(c.StyleID, style)
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
return nil
}

styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
return nil
}

Expand All @@ -290,34 +279,27 @@ func (e *excelizeam) setStyleCellRange(startColIndex, startRowIndex, endColIndex
for rowIdx := startRowIndex; rowIdx <= endRowIndex; rowIdx++ {
for colIdx := startColIndex; colIdx <= endColIndex; colIdx++ {
key := e.getCacheKey(colIdx, rowIdx)
if cached, ok := e.cellStore.Load(key); ok {

styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
} else {
styleID, err := e.getStyleID(&style)
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
continue
}
styleID, err := e.getStyleID(&style)
if err != nil {
return err
c.StyleID = styleID
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
}
}
return nil
Expand Down Expand Up @@ -501,34 +483,27 @@ func (e *excelizeam) setBorderRange(startColIndex, startRowIndex, endColIndex, e
}
style := excelize.Style{Border: borderStyles}

if cached, ok := e.cellStore.Load(key); ok {
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err := e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
} else {
styleID, err := e.getStyleID(&style)
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
c.StyleID = styleID
continue
}
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
e.cellStore.Store(key, &Cell{
StyleID: styleID,
Value: nil,
})
}
}
return nil
Expand Down

0 comments on commit 6c67f60

Please sign in to comment.