Skip to content

Commit

Permalink
improve Analyzer forEach error
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Mar 1, 2024
1 parent f7e3eff commit 4ebd4ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
46 changes: 43 additions & 3 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"sync"
"time"
)

type SolutionPossibility struct {
Expand All @@ -24,6 +25,10 @@ var (

type Analyzer struct {
DB ErrorDB

errMux sync.RWMutex
lastUpdateErr time.Time
cachedErrors []*ErrorDesc
}

func NewAnalyzer(db ErrorDB) (a *Analyzer) {
Expand All @@ -32,10 +37,46 @@ func NewAnalyzer(db ErrorDB) (a *Analyzer) {
}
}

func (a *Analyzer) UpdateErrors() (err error) {
a.errMux.Lock()
defer a.errMux.Unlock()
return a.updateErrorsLocked()
}

func (a *Analyzer) updateErrorsLocked() (err error) {
errors := make([]*ErrorDesc, 0, 64)
if err = a.DB.ForEachErrors(func(e *ErrorDesc) error {
errors = append(errors, e)
return nil
}); err != nil {
return
}
a.lastUpdateErr = time.Now()
a.cachedErrors = errors
return
}

func (a *Analyzer) getErrors() []*ErrorDesc {
{
a.errMux.RLock()
needUpdate := a.lastUpdateErr.IsZero() || time.Now().After(a.lastUpdateErr.Add(time.Hour))
a.errMux.RUnlock()
if needUpdate {
a.errMux.Lock()
if a.lastUpdateErr.IsZero() || time.Now().After(a.lastUpdateErr.Add(time.Hour)) {
a.updateErrorsLocked()
}
a.errMux.Unlock()
}
}
return a.cachedErrors
}

func (a *Analyzer) DoError(jerr *JavaError) (matched []SolutionPossibility, err error) {
errors := a.getErrors()
for jerr != nil {
epkg, ecls := rsplit(jerr.Class, '.')
a.DB.ForEachErrors(func(e *ErrorDesc) (err error) {
for _, e := range errors {
sol := SolutionPossibility{
ErrorDesc: e,
}
Expand All @@ -62,8 +103,7 @@ func (a *Analyzer) DoError(jerr *JavaError) (matched []SolutionPossibility, err
if sol.Match != 0 { // have any matches
matched = append(matched, sol)
}
return
})
}
jerr = jerr.CausedBy
}
if matched == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mcla_wasm/errdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *JsStorageCache) Clear() {
s.storage.Call("removeItem", key)
}
}
}else if keysFn := s.storage.Get("keys"); keysFn.Type() == js.TypeFunction{
} else if keysFn := s.storage.Get("keys"); keysFn.Type() == js.TypeFunction {
keys, _ := awaitPromise(keysFn.Invoke())
if keys.InstanceOf(Array) {
leng := keys.Length()
Expand Down

0 comments on commit 4ebd4ef

Please sign in to comment.