Skip to content

Commit

Permalink
[FAB-8033] Optimize DB queries
Browse files Browse the repository at this point in the history
Database queries optimized to make fewer calls to the
database.

Change-Id: I84b1c4780007283654003eb9bd677d3e226e5aaa
Signed-off-by: Saad Karim <skarim@us.ibm.com>
  • Loading branch information
Saad Karim authored and mastersingh24 committed Jul 8, 2018
1 parent 9e11a65 commit ae7a91a
Showing 1 changed file with 33 additions and 49 deletions.
82 changes: 33 additions & 49 deletions lib/dbaccessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,26 @@ INSERT INTO affiliations (name, prekey, level)
DELETE FROM affiliations
WHERE (name = ?)`

deleteAffAndSubAff = `
DELETE FROM affiliations
WHERE (name = ? OR name LIKE ?)`

getAffiliationQuery = `
SELECT * FROM affiliations
WHERE (name = ?)`

getAllAffiliationsQuery = `
SELECT * FROM affiliations
WHERE ((name = ?) OR (name LIKE ?))`
WHERE (name = ? OR name LIKE ?)`

getIDsWithAffiliation = `
SELECT * FROM users
WHERE (affiliation = ?)`

updateAffiliation = `
UPDATE affiliations
SET name = ?, prekey = ?
WHERE (name = ?)`
)

// UserRecord defines the properties of a user
Expand Down Expand Up @@ -358,22 +371,14 @@ func (d *Accessor) deleteAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
identityRemoval := args[2].(bool)
isRegistar := args[3].(bool)

query := "SELECT * FROM users WHERE (affiliation = ?)"
ids := []UserRecord{}
err = tx.Select(&ids, tx.Rebind(query), name)
if err != nil {
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to select users with affiliation '%s': %s", name, err)
}

subAffName := name + ".%"
query = "SELECT * FROM users WHERE (affiliation LIKE ?)"
subAffIds := []UserRecord{}
err = tx.Select(&subAffIds, tx.Rebind(query), subAffName)
query := "SELECT * FROM users WHERE (affiliation = ? OR affiliation LIKE ?)"
ids := []UserRecord{}
err = tx.Select(&ids, tx.Rebind(query), name, subAffName)
if err != nil {
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to select users with sub-affiliation of '%s': %s", name, err)
}

ids = append(ids, subAffIds...)
idNames := []string{}
for _, id := range ids {
idNames = append(idNames, id.Name)
Expand All @@ -394,25 +399,24 @@ func (d *Accessor) deleteAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
}
}

aff := AffiliationRecord{}
err = tx.Get(&aff, tx.Rebind(getAffiliationQuery), name)
allAffs := []AffiliationRecord{}
err = tx.Select(&allAffs, tx.Rebind(getAllAffiliationsQuery), name, subAffName)
if err != nil {
return nil, getError(err, "Affiliation")
}
// Getting all the sub-affiliations that are going to be deleted
allAffs := []AffiliationRecord{}
err = tx.Select(&allAffs, tx.Rebind("Select * FROM affiliations where (name LIKE ?)"), subAffName)
if err != nil {
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to select sub-affiliations of '%s': %s", allAffs, err)

affNames := []string{}
for _, aff := range allAffs {
affNames = append(affNames, aff.Name)
}
affNamesStr := strings.Join(affNames, ",")

if len(allAffs) > 0 {
if len(allAffs) > 1 {
if !force {
// If force option is not specified, only delete affiliation if there are no sub-affiliations
return nil, newAuthErr(ErrUpdateConfigRemoveAff, "Cannot delete affiliation '%s'. The affiliation has the following sub-affiliations: %s. Need to use 'force' to remove affiliation and sub-affiliations", name, allAffs)
return nil, newAuthErr(ErrUpdateConfigRemoveAff, "Cannot delete affiliation '%s'. The affiliation has the following sub-affiliations: %s. Need to use 'force' to remove affiliation and sub-affiliations", name, affNamesStr)
}
}
allAffs = append(allAffs, aff)

// Now proceed with deletion

Expand Down Expand Up @@ -445,19 +449,12 @@ func (d *Accessor) deleteAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf

log.Debugf("All affiliations to be removed: %s", allAffs)

// Delete the requested affiliation
_, err = tx.Exec(tx.Rebind(deleteAffiliation), name)
// Delete the requested affiliation and it's subaffiliations
_, err = tx.Exec(tx.Rebind(deleteAffAndSubAff), name, subAffName)
if err != nil {
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to delete affiliation '%s': %s", name, err)
}

if len(allAffs) > 1 {
// Delete all the sub-affiliations
_, err = tx.Exec(tx.Rebind("DELETE FROM affiliations where (name LIKE ?)"), subAffName)
if err != nil {
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to delete affiliations: %s", err)
}
}
// Return the identities and affiliations that were removed
result := d.getResult(ids, allAffs)

Expand Down Expand Up @@ -712,24 +709,13 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
force := args[2].(bool)
isRegistar := args[3].(bool)

// Get the affiliation record
query := "SELECT name, prekey FROM affiliations WHERE (name = ?)"
var oldAffiliationRecord AffiliationRecord
err := tx.Get(&oldAffiliationRecord, tx.Rebind(query), oldAffiliation)
if err != nil {
return nil, err
}

// Get the affiliation records for all sub affiliations
query = "SELECT name, prekey FROM affiliations WHERE (name LIKE ?)"
// Get the affiliation records including all sub affiliations
var allOldAffiliations []AffiliationRecord
err = tx.Select(&allOldAffiliations, tx.Rebind(query), oldAffiliation+".%")
err := tx.Select(&allOldAffiliations, tx.Rebind(getAllAffiliationsQuery), oldAffiliation, oldAffiliation+".%")
if err != nil {
return nil, err
}

allOldAffiliations = append(allOldAffiliations, oldAffiliationRecord)

log.Debugf("Affiliations to be modified %+v", allOldAffiliations)

// Iterate through all the affiliations found and update to use new affiliation path
Expand All @@ -743,8 +729,7 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
log.Debugf("oldPath: %s, newPath: %s, oldParentPath: %s, newParentPath: %s", oldPath, newPath, oldParentPath, newParentPath)

// Select all users that are using the old affiliation
query = "SELECT * FROM users WHERE (affiliation = ?)"
err = tx.Select(&idsWithOldAff, tx.Rebind(query), oldPath)
err = tx.Select(&idsWithOldAff, tx.Rebind(getIDsWithAffiliation), oldPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -820,8 +805,7 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
}

// Update the affiliation record in the database to use new affiliation path
query = "Update affiliations SET name = ?, prekey = ? WHERE (name = ?)"
res := tx.MustExec(tx.Rebind(query), newPath, newParentPath, oldPath)
res := tx.MustExec(tx.Rebind(updateAffiliation), newPath, newParentPath, oldPath)
numRowsAffected, err := res.RowsAffected()
if err != nil {
return nil, errors.Errorf("Failed to get number of rows affected")
Expand All @@ -834,7 +818,7 @@ func (d *Accessor) modifyAffiliationTx(tx *sqlx.Tx, args ...interface{}) (interf
// Generate the result set that has all identities with their new affiliation and all renamed affiliations
var idsWithNewAff []UserRecord
if len(idsUpdated) > 0 {
query = "Select * FROM users WHERE (id IN (?))"
query := "Select * FROM users WHERE (id IN (?))"
inQuery, args, err := sqlx.In(query, idsUpdated)
if err != nil {
return nil, errors.Wrapf(err, "Failed to construct query '%s'", query)
Expand Down

0 comments on commit ae7a91a

Please sign in to comment.