From 648d6a192d20ec4e24cbec22d3565469d01f1328 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 31 Jul 2015 14:59:36 +0900 Subject: [PATCH] Simplification --- server.go | 25 ++++++++++++------------- types.go | 11 +++++------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/server.go b/server.go index f840ceb..dc0c204 100644 --- a/server.go +++ b/server.go @@ -32,6 +32,7 @@ import ( "net/http" "runtime" "strings" + "sync" "time" "github.com/GaryBoone/GoStats/stats" @@ -40,15 +41,14 @@ import ( var db *sql.DB -func prepareColumn(request jsonQueryRequest, entries, foundEntries records, features featureMap, modes modeMap, name string, value float64, columns chan jsonColumn) { +func prepareColumn(request jsonQueryRequest, entries, foundEntries records, features featureMap, modes modeMap, name string, column *jsonColumn, wg *sync.WaitGroup) { mode := modes[name] - column := jsonColumn{ + *column = jsonColumn{ Bracket: jsonBracket{Max: -1.0, Min: 1.0}, Mode: mode.String(), Steps: request.Resolution, - Value: value, - name: name} + Value: features[name]} hints := project( entries, @@ -82,7 +82,7 @@ func prepareColumn(request jsonQueryRequest, entries, foundEntries records, feat column.Bracket.Min = math.Max(mean-dev, d.Min()) } - columns <- column + wg.Done() } func executeQuery(rw http.ResponseWriter, req *http.Request) { @@ -108,19 +108,18 @@ func executeQuery(rw http.ResponseWriter, req *http.Request) { sorter.sort() response := jsonQueryResponse{ - Columns: make(map[string]jsonColumn), + Columns: make(map[string]*jsonColumn), Count: len(foundEntries), MinScore: request.MinScore, Records: make([]jsonRecord, 0)} - columns := make(chan jsonColumn, len(features)) - for name, value := range features { - go prepareColumn(request, entries, foundEntries, features, modes, name, value, columns) - } + var wg sync.WaitGroup + wg.Add(len(features)) - for i := 0; i < len(features); i++ { - column := <-columns - response.Columns[column.name] = column + for name := range features { + column := &jsonColumn{} + prepareColumn(request, entries, foundEntries, features, modes, name, column, &wg) + response.Columns[name] = column } for index, record := range foundEntries { diff --git a/types.go b/types.go index aeba57b..d4e6983 100644 --- a/types.go +++ b/types.go @@ -66,7 +66,6 @@ type jsonColumn struct { Mode string `json:"mode"` Steps int `json:"steps"` Value float64 `json:"value"` - name string } type jsonProjection struct { @@ -93,11 +92,11 @@ type jsonBracket struct { } type jsonQueryResponse struct { - Columns map[string]jsonColumn `json:"columns"` - Count int `json:"count"` - MinScore float64 `json:"minScore"` - Records []jsonRecord `json:"records"` - ElapsedTime int64 `json:"elapsedTime"` + Columns map[string]*jsonColumn `json:"columns"` + Count int `json:"count"` + MinScore float64 `json:"minScore"` + Records []jsonRecord `json:"records"` + ElapsedTime int64 `json:"elapsedTime"` } type jsonCategory struct {