Issue 71761531 Add indexes (#34)

* [ISSUE-71761531]  Add KMS indexes

* [ISSUE-71761531] add comments
diff --git a/common/data.go b/common/data.go
index f7c9ba6..8f409ea 100644
--- a/common/data.go
+++ b/common/data.go
@@ -117,6 +117,44 @@
 	return
 }
 
+func AddIndexes(version string) error {
+	db, err := services.Data().DBVersion(version)
+	if err != nil {
+		log.Errorf("Unable to access database: %v", err)
+		return err
+	}
+	log.Debugf("adding indexes to sqlite file")
+	tx, err := db.Begin()
+	if err != nil {
+		log.Errorf("AddIndexes: Unable to get DB tx Err: {%v}", err)
+		return err
+	}
+	defer tx.Rollback()
+	_, err = tx.Exec(`
+	CREATE INDEX IF NOT EXISTS mp_appcred_id on KMS_APP_CREDENTIAL_APIPRODUCT_MAPPER (appcred_id);
+	CREATE INDEX IF NOT EXISTS mp_app_id on KMS_APP_CREDENTIAL_APIPRODUCT_MAPPER (app_id);
+	CREATE INDEX IF NOT EXISTS mp_apiprdt_id on KMS_APP_CREDENTIAL_APIPRODUCT_MAPPER (apiprdt_id);
+	CREATE INDEX IF NOT EXISTS app_name on KMS_APP (name);
+	CREATE INDEX IF NOT EXISTS app_company_id on KMS_APP (company_id);
+	CREATE INDEX IF NOT EXISTS app_developer_id on KMS_APP (developer_id);
+	CREATE INDEX IF NOT EXISTS dev_email on KMS_DEVELOPER (email);
+	CREATE INDEX IF NOT EXISTS com_name on KMS_COMPANY (name);
+	CREATE INDEX IF NOT EXISTS com_dev_com_id on KMS_COMPANY_DEVELOPER (company_id);
+	CREATE INDEX IF NOT EXISTS com_dev_dev_id on KMS_COMPANY_DEVELOPER (developer_id);
+	CREATE INDEX IF NOT EXISTS cred_app_id on KMS_APP_CREDENTIAL (app_id);
+	CREATE INDEX IF NOT EXISTS org_tenant_id on KMS_ORGANIZATION (tenant_id);
+	CREATE INDEX IF NOT EXISTS org_name on KMS_ORGANIZATION (name);
+	`)
+	if err != nil {
+		log.Errorf("AddIndexes: Tx Exec Err: {%v}", err)
+		return err
+	}
+	if err = tx.Commit(); err != nil {
+		log.Errorf("Commit error in AddIndexes: %v", err)
+	}
+	return err
+}
+
 func JsonToStringArray(fjson string) []string {
 	var array []string
 	if err := json.Unmarshal([]byte(fjson), &array); err == nil {
diff --git a/common/data_test.go b/common/data_test.go
index 7070890..0a29c46 100644
--- a/common/data_test.go
+++ b/common/data_test.go
@@ -61,6 +61,10 @@
 			Expect(orgs).Should(Equal([]string{"apid-haoming", "apid-test"}))
 		})
 
+		It("Add indexes", func() {
+			Expect(AddIndexes(testDbMan.GetDbVersion())).Should(Succeed())
+		})
+
 	})
 
 	Context("Validate common.JsonToStringArray", func() {
diff --git a/common/data_test.sql b/common/data_test.sql
index 1b5349b..6681d40 100644
--- a/common/data_test.sql
+++ b/common/data_test.sql
@@ -29,4 +29,11 @@
 CREATE TABLE edgex_data_scope (id text,apid_cluster_id text,scope text,org text,env text,created blob,created_by text,updated blob,updated_by text,_change_selector text,org_scope text,env_scope text, primary key (id));
 INSERT INTO "edgex_data_scope" VALUES('cc066263-6355-416d-9d59-7f3135d64953','543230f1-8c41-4bf5-94a3-f10c104ff5d4','155211e9','apid-haoming','test','2017-08-27 22:53:33.859+00:00','foobar@google.com','2017-08-27 22:53:33.859+00:00','foobar@google.com','543230f1-8c41-4bf5-94a3-f10c104ff5d4','12344caf-40d6-4ecb-8149-ed32d04184b2','1234203e-ba88-4cd5-967d-4caa88f64909');
 INSERT INTO "edgex_data_scope" VALUES('08c81eeb-57ec-43fe-8fed-cdff5494406f','123430f1-8c41-4bf5-94a3-f10c104ff5d4','165211e9','apid-test','prod','2017-08-29 02:39:34.093+00:00','foobar@google.com','2017-08-29 02:39:34.093+00:00','foobar@google.com','123430f1-8c41-4bf5-94a3-f10c104ff5d4','43214caf-40d6-4ecb-8149-ed32d04184b2','43211cae-f2a6-4663-9f36-eb17d76e6c32');
+CREATE TABLE kms_company_developer (tenant_id text,company_id text,developer_id text,roles text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (tenant_id,company_id,developer_id));
+CREATE TABLE kms_developer (id text,tenant_id text,username text,first_name text,last_name text,password text,email text,status text,encrypted_password text,salt text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));
+CREATE TABLE kms_api_product (id text,tenant_id text,name text,display_name text,description text,api_resources text,approval_type text,scopes text,proxies text,environments text,quota text,quota_time_unit text,quota_interval integer,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));
+CREATE TABLE kms_app (id text,tenant_id text,name text,display_name text,access_type text,callback_url text,status text,app_family text,company_id text,developer_id text,parent_id text,type text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));
+CREATE TABLE kms_app_credential_apiproduct_mapper (tenant_id text,appcred_id text,app_id text,apiprdt_id text,status text,_change_selector text, primary key (tenant_id,appcred_id,app_id,apiprdt_id));
+CREATE TABLE kms_company (id text,tenant_id text,name text,display_name text,status text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));
+CREATE TABLE kms_app_credential (id text,tenant_id text,consumer_secret text,app_id text,method_type text,status text,issued_at blob,expires_at blob,app_status text,scopes text,created_at blob,created_by text,updated_at blob,updated_by text,_change_selector text, primary key (id,tenant_id));
 COMMIT;
diff --git a/listener.go b/listener.go
index 4253cfc..97fdb9d 100644
--- a/listener.go
+++ b/listener.go
@@ -44,6 +44,11 @@
 	for _, dbMan := range h.dbMans {
 		dbMan.SetDbVersion(snapshot.SnapshotInfo)
 	}
+	// add indexes
+	if err := common.AddIndexes(snapshot.SnapshotInfo); err != nil {
+		// not on critical path, continue in case of error
+		log.Errorf("Failed to add KMS indexes: %v", err)
+	}
 	// retrieve encryption keys
 	orgs, err := h.dbMans[0].GetOrgs()
 	if err != nil {