1

Make compatibility computation happen in go routine

This commit is contained in:
Alex Yatskov 2015-07-31 14:41:44 +09:00
parent 30ca2305b1
commit 9b771dc68e

34
util.go
View File

@ -26,6 +26,7 @@ import (
"log" "log"
"math" "math"
"strconv" "strconv"
"sync"
"github.com/kellydunn/golang-geo" "github.com/kellydunn/golang-geo"
) )
@ -159,7 +160,7 @@ func project(entries records, features featureMap, modes modeMap, featureName st
return projection return projection
} }
func computeRecordGeo(entries records, context queryContext) { func computeRecordsGeo(entries records, context queryContext) {
distUserMin := math.MaxFloat64 distUserMin := math.MaxFloat64
distUserMax := 0.0 distUserMax := 0.0
@ -195,10 +196,7 @@ func computeRecordGeo(entries records, context queryContext) {
} }
} }
func computeRecordCompat(entries records, context queryContext) { func computeRecordCompat(entry *record, context queryContext, wg *sync.WaitGroup) {
for index := range entries {
entry := &entries[index]
historyRows, err := db.Query("SELECT id FROM history WHERE reviewId = (?)", entry.id) historyRows, err := db.Query("SELECT id FROM history WHERE reviewId = (?)", entry.id)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -245,6 +243,28 @@ func computeRecordCompat(entries records, context queryContext) {
if groupCount > 0 { if groupCount > 0 {
entry.compatibility = groupSum / float64(groupCount) entry.compatibility = groupSum / float64(groupCount)
} }
wg.Done()
}
func computeRecordsCompat(entries records, context queryContext) {
count := len(entries)
limit := 32
for i := 0; i < count; i += limit {
batch := count - i
if batch > limit {
batch = limit
}
var wg sync.WaitGroup
wg.Add(batch)
for j := 0; j < batch; j++ {
go computeRecordCompat(&entries[i+j], context, &wg)
}
wg.Wait()
} }
} }
@ -296,8 +316,8 @@ func getRecords(context queryContext) records {
log.Fatal(err) log.Fatal(err)
} }
computeRecordCompat(entries, context) computeRecordsCompat(entries, context)
computeRecordGeo(entries, context) computeRecordsGeo(entries, context)
return entries return entries
} }