You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by el...@apache.org on 2018/07/06 18:30:30 UTC

[trafficcontrol] branch master updated: remove nested sql call in cachegroup read

This is an automated email from the ASF dual-hosted git repository.

elsloo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new b4b0102  remove nested sql call in cachegroup read
b4b0102 is described below

commit b4b010206c0cbaa67704e3afb3ca439ff68b4caf
Author: Dylan Volz <Dy...@comcast.com>
AuthorDate: Thu Jul 5 13:59:37 2018 -0600

    remove nested sql call in cachegroup read
---
 lib/go-tc/v13/cachegroups.go                       | 10 +++---
 .../traffic_ops_golang/cachegroup/cachegroups.go   | 39 +++-------------------
 2 files changed, 10 insertions(+), 39 deletions(-)

diff --git a/lib/go-tc/v13/cachegroups.go b/lib/go-tc/v13/cachegroups.go
index df1562f..d053db4 100644
--- a/lib/go-tc/v13/cachegroups.go
+++ b/lib/go-tc/v13/cachegroups.go
@@ -36,11 +36,11 @@ type CacheGroup struct {
 	ShortName                   string       `json:"shortName" db:"short_name"`
 	Latitude                    float64      `json:"latitude" db:"latitude"`
 	Longitude                   float64      `json:"longitude" db:"longitude"`
-	ParentName                  string       `json:"parentCachegroupName"`
+	ParentName                  string       `json:"parentCachegroupName" db:"parent_cachegroup_name"`
 	ParentCachegroupID          int          `json:"parentCachegroupId" db:"parent_cachegroup_id"`
-	SecondaryParentName         string       `json:"secondaryParentCachegroupName"`
+	SecondaryParentName         string       `json:"secondaryParentCachegroupName" db:"secondary_parent_cachegroup_name"`
 	SecondaryParentCachegroupID int          `json:"secondaryParentCachegroupId" db:"secondary_parent_cachegroup_id"`
-	FallbackToClosest           bool         `json:"fallbackToClosest" db:"fallback_to_closest`
+	FallbackToClosest           bool         `json:"fallbackToClosest" db:"fallback_to_closest"`
 	Type                        string       `json:"typeName" db:"type_name"` // aliased to type_name to disambiguate struct scans due to join on 'type' table
 	TypeID                      int          `json:"typeId" db:"type_id"`     // aliased to type_id to disambiguate struct scans due join on 'type' table
 	LastUpdated                 tc.TimeNoMod `json:"lastUpdated" db:"last_updated"`
@@ -52,9 +52,9 @@ type CacheGroupNullable struct {
 	ShortName                   *string       `json:"shortName" db:"short_name"`
 	Latitude                    *float64      `json:"latitude" db:"latitude"`
 	Longitude                   *float64      `json:"longitude"db:"longitude"`
-	ParentName                  *string       `json:"parentCachegroupName"`
+	ParentName                  *string       `json:"parentCachegroupName" db:"parent_cachegroup_name"`
 	ParentCachegroupID          *int          `json:"parentCachegroupId" db:"parent_cachegroup_id"`
-	SecondaryParentName         *string       `json:"secondaryParentCachegroupName"`
+	SecondaryParentName         *string       `json:"secondaryParentCachegroupName" db:"secondary_parent_cachegroup_name"`
 	SecondaryParentCachegroupID *int          `json:"secondaryParentCachegroupId" db:"secondary_parent_cachegroup_id"`
 	FallbackToClosest           *bool         `json:"fallbackToClosest" db:"fallback_to_closest"`
 	Type                        *string       `json:"typeName" db:"type_name"` // aliased to type_name to disambiguate struct scans due to join on 'type' table
diff --git a/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go b/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
index c84e329..4c53856 100644
--- a/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
+++ b/traffic_ops/traffic_ops_golang/cachegroup/cachegroups.go
@@ -201,38 +201,6 @@ func getParentCachegroupIDs(tx *sqlx.Tx, cachegroup *TOCacheGroup) error {
 	return nil
 }
 
-// looks up the parent and secondary cachegroup names by cachegroup ID.
-//  the names are set on the struct.
-//
-// used by Read()
-func getParentCacheGroupNames(tx *sqlx.Tx, cachegroup *TOCacheGroup) error {
-	query1 := `SELECT name FROM cachegroup where id=$1`
-	var primaryName string
-	var secondaryName string
-
-	// primary parent lookup
-	if cachegroup.ParentCachegroupID != nil {
-		err := tx.QueryRow(query1, *cachegroup.ParentCachegroupID).Scan(&primaryName)
-		if err != nil {
-			log.Errorf("received error: %++v from query execution", err)
-			return err
-		}
-		cachegroup.ParentName = &primaryName
-	}
-
-	// secondary parent lookup
-	if cachegroup.SecondaryParentCachegroupID != nil {
-		err := tx.QueryRow(query1, *cachegroup.SecondaryParentCachegroupID).Scan(&secondaryName)
-		if err != nil {
-			log.Errorf("received error: %++v from query execution", err)
-			return err
-		}
-		cachegroup.SecondaryParentName = &secondaryName
-	}
-
-	return nil
-}
-
 //The TOCacheGroup implementation of the Creator interface
 //all implementations of Creator should use transactions and return the proper errorType
 //ParsePQUniqueConstraintError is used to determine if a cachegroup with conflicting values exists
@@ -317,7 +285,6 @@ func (cg *TOCacheGroup) Read(parameters map[string]string) ([]interface{}, []err
 			log.Errorf("error parsing CacheGroup rows: %v", err)
 			return nil, []error{tc.DBError}, tc.SystemError
 		}
-		getParentCacheGroupNames(cg.ReqInfo.Tx, &s)
 		CacheGroups = append(CacheGroups, s)
 	}
 
@@ -445,12 +412,16 @@ cachegroup.short_name,
 cachegroup.latitude,
 cachegroup.longitude,
 cachegroup.parent_cachegroup_id,
+cgp.name AS parent_cachegroup_name,
 cachegroup.secondary_parent_cachegroup_id,
+cgs.name AS secondary_parent_cachegroup_name,
 type.name AS type_name,
 cachegroup.type AS type_id,
 cachegroup.last_updated
 FROM cachegroup
-INNER JOIN type ON cachegroup.type = type.id`
+INNER JOIN type ON cachegroup.type = type.id
+LEFT JOIN cachegroup AS cgp ON cachegroup.parent_cachegroup_id = cgp.id
+LEFT JOIN cachegroup AS cgs ON cachegroup.secondary_parent_cachegroup_id = cgs.id`
 	return query
 }