From 33f1286e82f049d69b3eb07816595b14c33a8e91 Mon Sep 17 00:00:00 2001 From: oleksandrkravchuk87 Date: Thu, 11 Jan 2018 02:44:22 +0200 Subject: [PATCH 1/3] changed Recipes function (using concurrency) --- server/database/postgres.go | 72 +++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/server/database/postgres.go b/server/database/postgres.go index 6de0614..05653fd 100644 --- a/server/database/postgres.go +++ b/server/database/postgres.go @@ -6,6 +6,8 @@ import ( "strings" "time" + "sync" + "github.com/ASeegull/SmartFridge/server/config" "github.com/jinzhu/gorm" _ "github.com/lib/pq" @@ -260,37 +262,61 @@ func Recipes(foodInfoSlice []FoodInfo) ([]Recepie, error) { return nil, err } - var name, unit string - var amount int copyRec := make([]Recepie, 0, len(recipes)) + dat := make(chan Recepie, len(recipes)) + mu := sync.Mutex{} + wg := &sync.WaitGroup{} -OUTER: for key, recipe := range recipes { - rows, err := db.Table("recepies").Select("ingridients.amount, m_units.unit, products.name"). - Joins("LEFT JOIN ingridients on ingridients.recipe_id = recepies.id"). - Joins("JOIN products on ingridients.product_id = products.id"). - Joins("JOIN m_units on m_units.id = products.units"). - Where("recepies.id=?", recipe.ID). - Rows() - if err != nil { - rows.Close() - return nil, err - } - for rows.Next() { - err := rows.Scan(&amount, &unit, &name) + wg.Add(1) + go func(wg *sync.WaitGroup, index int, recipe Recepie) { + + var name, unit string + var amount int + + defer wg.Done() + rows, err := db.Table("recepies").Select("ingridients.amount, m_units.unit, products.name"). + Joins("LEFT JOIN ingridients on ingridients.recipe_id = recepies.id"). + Joins("JOIN products on ingridients.product_id = products.id"). + Joins("JOIN m_units on m_units.id = products.units"). + Where("recepies.id=?", recipe.ID). + Rows() if err != nil { rows.Close() - return nil, err + return } - if contains(productNameSlice, name) && amount <= productMap[name] { - recipe.Ingred = append(recipe.Ingred, strconv.Itoa(amount)+" "+unit+" "+name) - recipes[key] = recipe - } else { - rows.Close() - continue OUTER + for rows.Next() { + err := rows.Scan(&amount, &unit, &name) + if err != nil { + rows.Close() + return + } + if contains(productNameSlice, name) && amount <= productMap[name] { + mu.Lock() + recipe.Ingred = append(recipe.Ingred, strconv.Itoa(amount)+" "+unit+" "+name) + recipes[key] = recipe + mu.Unlock() + } else { + rows.Close() + return + } } + mu.Lock() + dat <- recipes[key] + mu.Unlock() + }(wg, key, recipe) + } + wg.Wait() + + for i := 0; i < len(recipes); i++ { + select { + case d := <-dat: + copyRec = append(copyRec, d) + default: } - copyRec = append(copyRec, recipes[key]) + } + if len(copyRec) == 0 { + return []Recepie{{RecName: "Sorry but you do not have enough food"}}, nil } return copyRec, nil } From 3ffe17c6c2098f582bc4f825cf10246fae944adb Mon Sep 17 00:00:00 2001 From: oleksandrkravchuk87 Date: Thu, 11 Jan 2018 02:47:18 +0200 Subject: [PATCH 2/3] changed Recipes function (using concurrency) --- server/database/postgres.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/database/postgres.go b/server/database/postgres.go index 05653fd..97e2538 100644 --- a/server/database/postgres.go +++ b/server/database/postgres.go @@ -235,7 +235,7 @@ func GetAllProductsNames() ([]string, error) { return productNames, nil } -//Recipes takes the slice of FoodInfo strucktures, representing all available products in all agents and return all recepies, which can be offered +//Recipes takes the slice of FoodInfo structures representing all available products in all agents and return all recepies, which can be offered func Recipes(foodInfoSlice []FoodInfo) ([]Recepie, error) { var err error productNameSlice := make([]string, 0, avgNumrOfIngInRecepie) From 1030cd5ad88216e38b3dceb0740106d04673bc76 Mon Sep 17 00:00:00 2001 From: oleksandrkravchuk87 Date: Fri, 12 Jan 2018 02:30:18 +0200 Subject: [PATCH 3/3] changed Recipes function (using concurrency) --- server/database/postgres.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/server/database/postgres.go b/server/database/postgres.go index 97e2538..1976dfb 100644 --- a/server/database/postgres.go +++ b/server/database/postgres.go @@ -270,7 +270,6 @@ func Recipes(foodInfoSlice []FoodInfo) ([]Recepie, error) { for key, recipe := range recipes { wg.Add(1) go func(wg *sync.WaitGroup, index int, recipe Recepie) { - var name, unit string var amount int @@ -307,13 +306,9 @@ func Recipes(foodInfoSlice []FoodInfo) ([]Recepie, error) { }(wg, key, recipe) } wg.Wait() - - for i := 0; i < len(recipes); i++ { - select { - case d := <-dat: - copyRec = append(copyRec, d) - default: - } + close(dat) + for d := range dat { + copyRec = append(copyRec, d) } if len(copyRec) == 0 { return []Recepie{{RecName: "Sorry but you do not have enough food"}}, nil