Skip to content

Commit

Permalink
refactor: use std slices, maps package (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n authored Sep 27, 2024
1 parent 06d27b8 commit 6da4beb
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 57 deletions.
2 changes: 1 addition & 1 deletion commands/fetch-oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package commands

import (
"encoding/xml"
"slices"
"strings"
"time"

"github.com/inconshreveable/log15"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

c "github.com/vulsio/goval-dictionary/config"
Expand Down
23 changes: 0 additions & 23 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,6 @@ func getAmazonLinuxVer(osVersion string) string {
return "1"
}

// IndexChunk has a starting point and an ending point for Chunk
type IndexChunk struct {
From, To int
}

func chunkSlice(length int, chunkSize int) <-chan IndexChunk {
ch := make(chan IndexChunk)

go func() {
defer close(ch)

for i := 0; i < length; i += chunkSize {
idx := IndexChunk{i, i + chunkSize}
if length < idx.To {
idx.To = length
}
ch <- idx
}
}()

return ch
}

func filterByRedHatMajor(packs []models.Package, majorVer string) (filtered []models.Package) {
for _, p := range packs {
if p.NotFixedYet ||
Expand Down
37 changes: 19 additions & 18 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"fmt"
"io"
"log"
"maps"
"os"
"slices"
"strings"
"time"

"github.com/cheggaaa/pb/v3"
"github.com/glebarez/sqlite"
"github.com/inconshreveable/log15"
"github.com/spf13/viper"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
Expand Down Expand Up @@ -237,7 +238,7 @@ func (r *RDBDriver) GetByPackName(family, osVer, packName, arch string) ([]model
for _, d := range defs {
m[d.DefinitionID] = d
}
return maps.Values(m), nil
return slices.Collect(maps.Values(m)), nil
default:
return defs, nil
}
Expand Down Expand Up @@ -292,7 +293,7 @@ func (r *RDBDriver) GetByCveID(family, osVer, cveID, arch string) ([]models.Defi
for _, d := range defs {
m[d.DefinitionID] = d
}
return maps.Values(m), nil
return slices.Collect(maps.Values(m)), nil
default:
return defs, nil
}
Expand Down Expand Up @@ -437,25 +438,25 @@ func (r *RDBDriver) InsertOval(root *models.Root) error {
}
return os.Stderr
}())
for idx := range chunkSlice(len(defs), 998) {
for chunk := range slices.Chunk(defs, 998) {
var advs []models.Advisory
if err := tx.Model(defs[idx.From:idx.To]).Association("Advisory").Find(&advs); err != nil {
if err := tx.Model(chunk).Association("Advisory").Find(&advs); err != nil {
tx.Rollback()
return xerrors.Errorf("Failed to delete: %w", err)
}

for idx2 := range chunkSlice(len(advs), 998) {
if err := tx.Select(clause.Associations).Unscoped().Delete(advs[idx2.From:idx2.To]).Error; err != nil {
for chunk2 := range slices.Chunk(advs, 998) {
if err := tx.Select(clause.Associations).Unscoped().Delete(chunk2).Error; err != nil {
tx.Rollback()
return xerrors.Errorf("Failed to delete: %w", err)
}
}

if err := tx.Select(clause.Associations).Unscoped().Delete(defs[idx.From:idx.To]).Error; err != nil {
if err := tx.Select(clause.Associations).Unscoped().Delete(chunk).Error; err != nil {
tx.Rollback()
return xerrors.Errorf("Failed to delete: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
if err := tx.Unscoped().Where("id = ?", old.ID).Delete(&models.Root{}).Error; err != nil {
tx.Rollback()
Expand All @@ -480,25 +481,25 @@ func (r *RDBDriver) InsertOval(root *models.Root) error {
root.Definitions[i].RootID = root.ID
}

for idx := range chunkSlice(len(root.Definitions), batchSize) {
if err := tx.Omit("AffectedPacks").Create(root.Definitions[idx.From:idx.To]).Error; err != nil {
for chunk := range slices.Chunk(root.Definitions, batchSize) {
if err := tx.Omit("AffectedPacks").Create(chunk).Error; err != nil {
tx.Rollback()
return xerrors.Errorf("Failed to insert Definitions. err: %w", err)
}

for _, d := range root.Definitions[idx.From:idx.To] {
for idx2 := range chunkSlice(len(d.AffectedPacks), batchSize) {
for i := range d.AffectedPacks[idx2.From:idx2.To] {
d.AffectedPacks[idx2.From+i].DefinitionID = d.ID
}
if err := tx.Create(d.AffectedPacks[idx2.From:idx2.To]).Error; err != nil {
for _, d := range chunk {
for i := range d.AffectedPacks {
d.AffectedPacks[i].DefinitionID = d.ID
}
for chunk2 := range slices.Chunk(d.AffectedPacks, batchSize) {
if err := tx.Create(chunk2).Error; err != nil {
tx.Rollback()
return xerrors.Errorf("Failed to insert AffectedPacks. err: %w", err)
}
}
}

bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand Down
18 changes: 10 additions & 8 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"errors"
"fmt"
"io"
"maps"
"os"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -15,7 +17,6 @@ import (
"github.com/go-redis/redis/v8"
"github.com/inconshreveable/log15"
"github.com/spf13/viper"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"

c "github.com/vulsio/goval-dictionary/config"
Expand Down Expand Up @@ -458,9 +459,9 @@ func (r *RedisDriver) InsertOval(root *models.Root) (err error) {
}
return os.Stderr
}())
for idx := range chunkSlice(len(root.Definitions), batchSize) {
for chunk := range slices.Chunk(root.Definitions, batchSize) {
pipe := r.conn.Pipeline()
for _, def := range root.Definitions[idx.From:idx.To] {
for _, def := range chunk {
var dj []byte
if dj, err = json.Marshal(def); err != nil {
return xerrors.Errorf("Failed to marshal json. err: %w", err)
Expand Down Expand Up @@ -510,7 +511,7 @@ func (r *RedisDriver) InsertOval(root *models.Root) (err error) {
if _, err = pipe.Exec(ctx); err != nil {
return xerrors.Errorf("Failed to exec pipeline. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand All @@ -521,10 +522,11 @@ func (r *RedisDriver) InsertOval(root *models.Root) (err error) {
}
return os.Stderr
}())
keys := maps.Keys(advs)
for idx := range chunkSlice(len(keys), batchSize) {

keys := slices.Collect(maps.Keys(advs))
for chunk := range slices.Chunk(keys, batchSize) {
pipe := r.conn.Pipeline()
for _, adv := range keys[idx.From:idx.To] {
for _, adv := range chunk {
var aj []byte
if aj, err = json.Marshal(advs[adv]); err != nil {
return xerrors.Errorf("Failed to marshal json. err: %w", err)
Expand All @@ -539,7 +541,7 @@ func (r *RedisDriver) InsertOval(root *models.Root) (err error) {
if _, err = pipe.Exec(ctx); err != nil {
return xerrors.Errorf("Failed to exec pipeline. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand Down
2 changes: 1 addition & 1 deletion fetcher/redhat/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"bytes"
"fmt"
"io"
"slices"
"strconv"
"strings"

"github.com/inconshreveable/log15"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"github.com/vulsio/goval-dictionary/fetcher/util"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/ulikunitz/xz v0.5.12
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.29.0
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -67,6 +66,7 @@ require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
Expand All @@ -33,6 +35,7 @@ github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzq
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
Expand Down Expand Up @@ -62,7 +65,9 @@ github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3t
github.com/knqyf263/go-rpm-version v0.0.0-20220614171824-631e686d1075 h1:aC6MEAs3PE3lWD7lqrJfDxHd6hcced9R4JTZu85cJwU=
github.com/knqyf263/go-rpm-version v0.0.0-20220614171824-631e686d1075/go.mod h1:i4sF0l1fFnY1aiw08QQSwVAFxHEm311Me3WsU/X7nL0=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand All @@ -81,18 +86,23 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand Down Expand Up @@ -155,9 +165,11 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3j
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
7 changes: 4 additions & 3 deletions models/redhat/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package redhat

import (
"fmt"
"maps"
"slices"
"strings"
"time"

version "github.com/knqyf263/go-rpm-version"
"github.com/spf13/viper"
"golang.org/x/exp/maps"

"github.com/vulsio/goval-dictionary/models"
"github.com/vulsio/goval-dictionary/models/util"
Expand Down Expand Up @@ -112,7 +113,7 @@ func ConvertToModel(v string, roots []Root) []models.Definition {
}
}
}
return maps.Values(defs)
return slices.Collect(maps.Values(defs))
}

func collectRedHatPacks(v string, cri Criteria) []models.Package {
Expand Down Expand Up @@ -145,7 +146,7 @@ func collectRedHatPacks(v string, cri Criteria) []models.Package {

pkgs[n] = p
}
return maps.Values(pkgs)
return slices.Collect(maps.Values(pkgs))
}

func walkRedHat(cri Criteria, acc []models.Package, label string) []models.Package {
Expand Down
7 changes: 5 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package util

import "golang.org/x/exp/maps"
import (
"maps"
"slices"
)

// Unique return unique elements
func Unique[T comparable](s []T) []T {
m := map[T]struct{}{}
for _, v := range s {
m[v] = struct{}{}
}
return maps.Keys(m)
return slices.Collect(maps.Keys(m))
}

0 comments on commit 6da4beb

Please sign in to comment.