Skip to content

Commit ae7a91a

Browse files
Saad Karimmastersingh24
authored andcommitted
[FAB-8033] Optimize DB queries
Database queries optimized to make fewer calls to the database. Change-Id: I84b1c4780007283654003eb9bd677d3e226e5aaa Signed-off-by: Saad Karim <skarim@us.ibm.com>
1 parent 9e11a65 commit ae7a91a

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

lib/dbaccessor.go

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,26 @@ INSERT INTO affiliations (name, prekey, level)
6767
DELETE FROM affiliations
6868
WHERE (name = ?)`
6969

70+
deleteAffAndSubAff = `
71+
DELETE FROM affiliations
72+
WHERE (name = ? OR name LIKE ?)`
73+
7074
getAffiliationQuery = `
7175
SELECT * FROM affiliations
7276
WHERE (name = ?)`
7377

7478
getAllAffiliationsQuery = `
7579
SELECT * FROM affiliations
76-
WHERE ((name = ?) OR (name LIKE ?))`
80+
WHERE (name = ? OR name LIKE ?)`
81+
82+
getIDsWithAffiliation = `
83+
SELECT * FROM users
84+
WHERE (affiliation = ?)`
85+
86+
updateAffiliation = `
87+
UPDATE affiliations
88+
SET name = ?, prekey = ?
89+
WHERE (name = ?)`
7790
)
7891

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

361-
query := "SELECT * FROM users WHERE (affiliation = ?)"
362-
ids := []UserRecord{}
363-
err = tx.Select(&ids, tx.Rebind(query), name)
364-
if err != nil {
365-
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to select users with affiliation '%s': %s", name, err)
366-
}
367-
368374
subAffName := name + ".%"
369-
query = "SELECT * FROM users WHERE (affiliation LIKE ?)"
370-
subAffIds := []UserRecord{}
371-
err = tx.Select(&subAffIds, tx.Rebind(query), subAffName)
375+
query := "SELECT * FROM users WHERE (affiliation = ? OR affiliation LIKE ?)"
376+
ids := []UserRecord{}
377+
err = tx.Select(&ids, tx.Rebind(query), name, subAffName)
372378
if err != nil {
373379
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to select users with sub-affiliation of '%s': %s", name, err)
374380
}
375381

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

397-
aff := AffiliationRecord{}
398-
err = tx.Get(&aff, tx.Rebind(getAffiliationQuery), name)
402+
allAffs := []AffiliationRecord{}
403+
err = tx.Select(&allAffs, tx.Rebind(getAllAffiliationsQuery), name, subAffName)
399404
if err != nil {
400405
return nil, getError(err, "Affiliation")
401406
}
402-
// Getting all the sub-affiliations that are going to be deleted
403-
allAffs := []AffiliationRecord{}
404-
err = tx.Select(&allAffs, tx.Rebind("Select * FROM affiliations where (name LIKE ?)"), subAffName)
405-
if err != nil {
406-
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to select sub-affiliations of '%s': %s", allAffs, err)
407+
408+
affNames := []string{}
409+
for _, aff := range allAffs {
410+
affNames = append(affNames, aff.Name)
407411
}
412+
affNamesStr := strings.Join(affNames, ",")
408413

409-
if len(allAffs) > 0 {
414+
if len(allAffs) > 1 {
410415
if !force {
411416
// If force option is not specified, only delete affiliation if there are no sub-affiliations
412-
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)
417+
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)
413418
}
414419
}
415-
allAffs = append(allAffs, aff)
416420

417421
// Now proceed with deletion
418422

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

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

448-
// Delete the requested affiliation
449-
_, err = tx.Exec(tx.Rebind(deleteAffiliation), name)
452+
// Delete the requested affiliation and it's subaffiliations
453+
_, err = tx.Exec(tx.Rebind(deleteAffAndSubAff), name, subAffName)
450454
if err != nil {
451455
return nil, newHTTPErr(500, ErrRemoveAffDB, "Failed to delete affiliation '%s': %s", name, err)
452456
}
453457

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

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

715-
// Get the affiliation record
716-
query := "SELECT name, prekey FROM affiliations WHERE (name = ?)"
717-
var oldAffiliationRecord AffiliationRecord
718-
err := tx.Get(&oldAffiliationRecord, tx.Rebind(query), oldAffiliation)
719-
if err != nil {
720-
return nil, err
721-
}
722-
723-
// Get the affiliation records for all sub affiliations
724-
query = "SELECT name, prekey FROM affiliations WHERE (name LIKE ?)"
712+
// Get the affiliation records including all sub affiliations
725713
var allOldAffiliations []AffiliationRecord
726-
err = tx.Select(&allOldAffiliations, tx.Rebind(query), oldAffiliation+".%")
714+
err := tx.Select(&allOldAffiliations, tx.Rebind(getAllAffiliationsQuery), oldAffiliation, oldAffiliation+".%")
727715
if err != nil {
728716
return nil, err
729717
}
730718

731-
allOldAffiliations = append(allOldAffiliations, oldAffiliationRecord)
732-
733719
log.Debugf("Affiliations to be modified %+v", allOldAffiliations)
734720

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

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

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

0 commit comments

Comments
 (0)