Feature: replace arrays with slices

The current code is much cleaner because it doesn't use indexes + it
should be more memory efficient.
This commit is contained in:
Ales Raszka 2019-01-02 12:57:45 +01:00
parent 3947073b9e
commit 90f5592095

View File

@ -76,11 +76,9 @@ func (tx *pgSession) PersistFeatures(features []database.Feature) error {
}) })
// TODO(Sida): A better interface for bulk insertion is needed. // TODO(Sida): A better interface for bulk insertion is needed.
keys := make([]interface{}, len(features)*3) keys := make([]interface{}, 0, len(features)*3)
for i, f := range features { for _, f := range features {
keys[i*3] = f.Name keys = append(keys, f.Name, f.Version, f.VersionFormat)
keys[i*3+1] = f.Version
keys[i*3+2] = f.VersionFormat
if f.Name == "" || f.Version == "" || f.VersionFormat == "" { if f.Name == "" || f.Version == "" || f.VersionFormat == "" {
return commonerr.NewBadRequestError("Empty feature name, version or version format is not allowed") return commonerr.NewBadRequestError("Empty feature name, version or version format is not allowed")
} }
@ -160,11 +158,9 @@ func (tx *pgSession) CacheAffectedNamespacedFeatures(features []database.Namespa
cache, err := tx.searchAffectingVulnerabilities(features) cache, err := tx.searchAffectingVulnerabilities(features)
keys := make([]interface{}, len(cache)*3) keys := make([]interface{}, 0, len(cache)*3)
for i, c := range cache { for _, c := range cache {
keys[i*3] = c.vulnID keys = append(keys, c.vulnID, c.nsFeatureID, c.vulnAffectingID)
keys[i*3+1] = c.nsFeatureID
keys[i*3+2] = c.vulnAffectingID
} }
if len(cache) == 0 { if len(cache) == 0 {
@ -231,10 +227,9 @@ func (tx *pgSession) PersistNamespacedFeatures(features []database.NamespacedFea
return err return err
} }
keys := make([]interface{}, len(features)*2) keys := make([]interface{}, 0, len(features)*2)
for i, f := range features { for _, f := range features {
keys[i*2] = fIDs[f.Feature] keys = append(keys, fIDs[f.Feature], nsIDs[f.Namespace])
keys[i*2+1] = nsIDs[f.Namespace]
} }
_, err := tx.Exec(queryPersistNamespacedFeature(len(features)), keys...) _, err := tx.Exec(queryPersistNamespacedFeature(len(features)), keys...)
@ -329,12 +324,9 @@ func (tx *pgSession) findNamespacedFeatureIDs(nfs []database.NamespacedFeature)
} }
nfsMap := map[database.NamespacedFeature]sql.NullInt64{} nfsMap := map[database.NamespacedFeature]sql.NullInt64{}
keys := make([]interface{}, len(nfs)*4) keys := make([]interface{}, 0, len(nfs)*4)
for i, nf := range nfs { for _, nf := range nfs {
keys[i*4] = nfs[i].Name keys = append(keys, nf.Name, nf.Version, nf.VersionFormat, nf.Namespace.Name)
keys[i*4+1] = nfs[i].Version
keys[i*4+2] = nfs[i].VersionFormat
keys[i*4+3] = nfs[i].Namespace.Name
nfsMap[nf] = sql.NullInt64{} nfsMap[nf] = sql.NullInt64{}
} }
@ -373,11 +365,9 @@ func (tx *pgSession) findFeatureIDs(fs []database.Feature) ([]sql.NullInt64, err
fMap := map[database.Feature]sql.NullInt64{} fMap := map[database.Feature]sql.NullInt64{}
keys := make([]interface{}, len(fs)*3) keys := make([]interface{}, 0, len(fs)*3)
for i, f := range fs { for _, f := range fs {
keys[i*3] = f.Name keys = append(keys, f.Name, f.Version, f.VersionFormat)
keys[i*3+1] = f.Version
keys[i*3+2] = f.VersionFormat
fMap[f] = sql.NullInt64{} fMap[f] = sql.NullInt64{}
} }