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.
This commit is contained in:
Ales Raszka 2019-03-08 09:51:40 +01:00
parent a8a91379d9
commit d77dc0f0ae

View File

@ -17,7 +17,6 @@ package pgsql
import ( import (
"database/sql" "database/sql"
"sort" "sort"
"strconv"
"github.com/lib/pq" "github.com/lib/pq"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -312,7 +311,7 @@ func (tx *pgSession) findNamespacedFeatureIDs(nfs []database.NamespacedFeature)
return nil, nil return nil, nil
} }
nfsMap := map[string]int64{} nfsMap := map[database.NamespacedFeature]int64{}
keys := make([]interface{}, 0, len(nfs)*5) keys := make([]interface{}, 0, len(nfs)*5)
for _, nf := range nfs { for _, nf := range nfs {
keys = append(keys, nf.Name, nf.Version, nf.VersionFormat, nf.Type, nf.Namespace.Name) 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 { if err != nil {
return nil, handleError("searchNamespacedFeature", err) return nil, handleError("searchNamespacedFeature", err)
} }
nfsMap[nf.Key()] = id nfsMap[nf] = id
} }
ids := make([]sql.NullInt64, len(nfs)) ids := make([]sql.NullInt64, len(nfs))
for i, nf := range 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} ids[i] = sql.NullInt64{id, true}
} else { } else {
ids[i] = sql.NullInt64{} ids[i] = sql.NullInt64{}
@ -360,14 +359,13 @@ func (tx *pgSession) findFeatureIDs(fs []database.Feature) ([]sql.NullInt64, err
return nil, err return nil, err
} }
fMap := map[string]sql.NullInt64{} fMap := map[database.Feature]sql.NullInt64{}
keys := make([]interface{}, 0, len(fs)*4) keys := make([]interface{}, 0, len(fs)*4)
for _, f := range fs { for _, f := range fs {
typeID := types.byName[f.Type] typeID := types.byName[f.Type]
keys = append(keys, f.Name, f.Version, f.VersionFormat, typeID) keys = append(keys, f.Name, f.Version, f.VersionFormat, typeID)
mapKey := f.Name + f.Version + f.VersionFormat + strconv.Itoa(typeID) fMap[f] = sql.NullInt64{}
fMap[mapKey] = sql.NullInt64{}
} }
rows, err := tx.Query(querySearchFeatureID(len(fs)), keys...) 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] f.Type = types.byID[typeID]
mapKey := f.Name + f.Version + f.VersionFormat + strconv.Itoa(typeID) fMap[f] = id
fMap[mapKey] = id
} }
ids := make([]sql.NullInt64, len(fs)) ids := make([]sql.NullInt64, len(fs))
for i, f := range fs { for i, f := range fs {
typeID := types.byName[f.Type] ids[i] = fMap[f]
mapKey := f.Name + f.Version + f.VersionFormat + strconv.Itoa(typeID)
ids[i] = fMap[mapKey]
} }
return ids, nil return ids, nil