Skip to content

Commit fb1af05

Browse files
committed
refactor: store data in a map vs a slice
1 parent 40d2454 commit fb1af05

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

daemon/internal/newrelic/php_packages.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type PhpPackagesKey struct {
2323
// process to be reported to the backend.
2424
type PhpPackages struct {
2525
numSeen int
26-
data []PhpPackagesKey
26+
data map[PhpPackagesKey]struct{}
2727
filteredPkgs []PhpPackagesKey
2828
}
2929

@@ -38,7 +38,7 @@ func (packages *PhpPackages) NumSaved() float64 {
3838
func NewPhpPackages() *PhpPackages {
3939
p := &PhpPackages{
4040
numSeen: 0,
41-
data: nil,
41+
data: make(map[PhpPackagesKey]struct{}),
4242
filteredPkgs: nil,
4343
}
4444

@@ -62,11 +62,11 @@ func NewPhpPackages() *PhpPackages {
6262
// key does not exist, add it to the map and the slice of filteredPkgs to be
6363
// sent in the current harvest.
6464
func (packages *PhpPackages) Filter(pkgHistory map[PhpPackagesKey]struct{}) {
65-
if packages == nil || packages.data == nil {
65+
if packages == nil || len(packages.data) == 0 {
6666
return
6767
}
6868

69-
for _, pkgKey := range packages.data {
69+
for pkgKey := range packages.data {
7070
_, ok := pkgHistory[pkgKey]
7171
if !ok {
7272
pkgHistory[pkgKey] = struct{}{}
@@ -107,7 +107,11 @@ func (packages *PhpPackages) AddPhpPackagesFromData(data []byte) error {
107107
return fmt.Errorf("unable to parse package version")
108108
}
109109

110-
packages.data = append(packages.data, PhpPackagesKey{name, version})
110+
pkgKey := PhpPackagesKey{name, version}
111+
_, ok = packages.data[pkgKey]
112+
if !ok {
113+
packages.data[pkgKey] = struct{}{}
114+
}
111115
}
112116

113117
packages.numSeen = 1
@@ -158,7 +162,7 @@ func (packages *PhpPackages) FailedHarvest(newHarvest *Harvest) {
158162

159163
// Empty returns true if the collection is empty.
160164
func (packages *PhpPackages) Empty() bool {
161-
return nil == packages || nil == packages.data || 0 == packages.numSeen
165+
return nil == packages || len(packages.data) == 0 || 0 == packages.numSeen
162166
}
163167

164168
// Data marshals the collection to JSON according to the schema expected

daemon/internal/newrelic/php_packages_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestNewPhpPackages(t *testing.T) {
1818
if 0 != pkg.NumSaved() {
1919
t.Fatalf("Expected 0, got %f", pkg.NumSaved())
2020
}
21-
if nil != pkg.data {
21+
if len(pkg.data) != 0 {
2222
t.Fatalf("Expected nil, got %v", pkg.data)
2323
}
2424
}

0 commit comments

Comments
 (0)