Skip to content

Commit

Permalink
refactor results calculator to object
Browse files Browse the repository at this point in the history
  • Loading branch information
d-led committed Dec 31, 2023
1 parent bea62ad commit 3fae27c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
41 changes: 29 additions & 12 deletions common/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,46 @@ type ResultRow struct {
CandidateSources []string `json:"candidate_sources,omitempty"`
}

func CalculateResults(fs Filesystem, source ValueSource) ([]ResultRow, error) {
values := source.Values()
type positionLookup multimap.MultiMap[string, int]

func duplicatePredicate(a, b int) bool { return true }

type ResultsCalculator struct {
fs Filesystem
source ValueSource
pathsLookup positionLookup
}

func NewResultsCalculator(fs Filesystem, source ValueSource) *ResultsCalculator {
return &ResultsCalculator{
fs: fs,
source: source,
pathsLookup: multimap.NewMapSet[string](duplicatePredicate),
}
}

func (r *ResultsCalculator) CalculateResults() ([]ResultRow, error) {
values := r.source.Values()

if len(values) == 0 {
return nil, errors.New(source.Source() + " is empty")
return nil, errors.New(r.source.Source() + " is empty")
}

duplicatePredicate := func(a, b int) bool { return true }
pathsLookup := multimap.NewMapSet[string](duplicatePredicate)
for i, path := range values {
pathKey := fs.GetAbsolutePath(path)
pathsLookup.Put(pathKey, i)
pathKey := r.fs.GetAbsolutePath(path)
r.pathsLookup.Put(pathKey, i)
}

return calculateResultRows(fs, values, pathsLookup), nil
return r.calculateResultRows(), nil
}

func calculateResultRows(fs Filesystem, paths []string, pathsLookup multimap.MultiMap[string, int]) []ResultRow {
func (r *ResultsCalculator) calculateResultRows() []ResultRow {
paths := r.source.Values()
res := []ResultRow{}
for index, path := range paths {
pathKey := fs.GetAbsolutePath(path)
dup := getDuplicatesOf(pathsLookup, pathKey, index)
exists, isdir := fs.PathStatus(pathKey)
pathKey := r.fs.GetAbsolutePath(path)
dup := getDuplicatesOf(r.pathsLookup, pathKey, index)
exists, isdir := r.fs.PathStatus(pathKey)
res = append(res, ResultRow{
Id: index + 1,
Path: path,
Expand Down
4 changes: 3 additions & 1 deletion view/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ func getResults() []common.ResultRow {

fs := &common.OsFilesystem{}

results, err := common.CalculateResults(fs, source)
resultsCalculator := common.NewResultsCalculator(fs, source)

results, err := resultsCalculator.CalculateResults()
if err != nil {
common.FailWith(err.Error())
}
Expand Down

0 comments on commit 3fae27c

Please sign in to comment.