From d77dc0f0aeee2cfdacae02347c807eaa2e17ca0b Mon Sep 17 00:00:00 2001 From: Ales Raszka Date: Fri, 8 Mar 2019 09:51:40 +0100 Subject: [PATCH] Use struct as a map key instead of string String was used when Feature contains PotentialNamespace. Since it was moved to LayerFeature we can use struct as map key instead of string. --- database/pgsql/feature.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/database/pgsql/feature.go b/database/pgsql/feature.go index 58f12dbd..66b47c50 100644 --- a/database/pgsql/feature.go +++ b/database/pgsql/feature.go @@ -17,7 +17,6 @@ package pgsql import ( "database/sql" "sort" - "strconv" "github.com/lib/pq" log "github.com/sirupsen/logrus" @@ -312,7 +311,7 @@ func (tx *pgSession) findNamespacedFeatureIDs(nfs []database.NamespacedFeature) return nil, nil } - nfsMap := map[string]int64{} + nfsMap := map[database.NamespacedFeature]int64{} keys := make([]interface{}, 0, len(nfs)*5) for _, nf := range nfs { keys = append(keys, nf.Name, nf.Version, nf.VersionFormat, nf.Type, nf.Namespace.Name) @@ -335,12 +334,12 @@ func (tx *pgSession) findNamespacedFeatureIDs(nfs []database.NamespacedFeature) if err != nil { return nil, handleError("searchNamespacedFeature", err) } - nfsMap[nf.Key()] = id + nfsMap[nf] = id } ids := make([]sql.NullInt64, len(nfs)) for i, nf := range nfs { - if id, ok := nfsMap[nf.Key()]; ok { + if id, ok := nfsMap[nf]; ok { ids[i] = sql.NullInt64{id, true} } else { ids[i] = sql.NullInt64{} @@ -360,14 +359,13 @@ func (tx *pgSession) findFeatureIDs(fs []database.Feature) ([]sql.NullInt64, err return nil, err } - fMap := map[string]sql.NullInt64{} + fMap := map[database.Feature]sql.NullInt64{} keys := make([]interface{}, 0, len(fs)*4) for _, f := range fs { typeID := types.byName[f.Type] keys = append(keys, f.Name, f.Version, f.VersionFormat, typeID) - mapKey := f.Name + f.Version + f.VersionFormat + strconv.Itoa(typeID) - fMap[mapKey] = sql.NullInt64{} + fMap[f] = sql.NullInt64{} } rows, err := tx.Query(querySearchFeatureID(len(fs)), keys...) @@ -388,15 +386,12 @@ func (tx *pgSession) findFeatureIDs(fs []database.Feature) ([]sql.NullInt64, err } f.Type = types.byID[typeID] - mapKey := f.Name + f.Version + f.VersionFormat + strconv.Itoa(typeID) - fMap[mapKey] = id + fMap[f] = id } ids := make([]sql.NullInt64, len(fs)) for i, f := range fs { - typeID := types.byName[f.Type] - mapKey := f.Name + f.Version + f.VersionFormat + strconv.Itoa(typeID) - ids[i] = fMap[mapKey] + ids[i] = fMap[f] } return ids, nil