Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use std slices, maps package #278

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions db/arch.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package db

Check failure on line 1 in db/arch.go

View workflow job for this annotation

GitHub Actions / Build

should have a package comment https://revive.run/r#package-comments

import (
"errors"
"fmt"
"io"
"os"
"slices"

"github.com/cheggaaa/pb/v3"
"github.com/spf13/viper"
Expand Down Expand Up @@ -82,11 +83,11 @@
return fmt.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
}

for idx := range chunkSlice(len(advs), batchSize) {
if err = tx.Create(advs[idx.From:idx.To]).Error; err != nil {
for chunk := range slices.Chunk(advs, batchSize) {
if err = tx.Create(chunk).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand Down
23 changes: 0 additions & 23 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,26 +91,3 @@ func newDB(dbType string) (DB, error) {
}
return nil, fmt.Errorf("Invalid database dialect. dbType: %s", dbType)
}

// 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
}
7 changes: 4 additions & 3 deletions db/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"slices"

"github.com/cheggaaa/pb/v3"
"github.com/spf13/viper"
Expand Down Expand Up @@ -90,11 +91,11 @@ func (r *RDBDriver) deleteAndInsertDebian(cves []models.DebianCVE) (err error) {
return fmt.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
}

for idx := range chunkSlice(len(cves), batchSize) {
if err = tx.Create(cves[idx.From:idx.To]).Error; err != nil {
for chunk := range slices.Chunk(cves, batchSize) {
if err = tx.Create(chunk).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand Down
22 changes: 11 additions & 11 deletions db/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"errors"
"fmt"
"io"
"maps"
"os"
"slices"
"strconv"
"strings"

"github.com/cheggaaa/pb/v3"
"github.com/inconshreveable/log15"
"github.com/spf13/viper"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"gorm.io/gorm"

Expand Down Expand Up @@ -69,7 +69,7 @@ func (r *RDBDriver) GetExpandKB(applied []string, unapplied []string) ([]string,
uniqUnappliedKBIDs[kbID] = struct{}{}
delete(uniqAppliedKBIDs, kbID)
}
applied = maps.Keys(uniqAppliedKBIDs)
applied = slices.Collect(maps.Keys(uniqAppliedKBIDs))

if len(applied) > 0 {
relations := []models.MicrosoftKBRelation{}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (r *RDBDriver) GetExpandKB(applied []string, unapplied []string) ([]string,

if err := r.conn.
Preload("SupersededBy").
Where("kb_id IN ?", maps.Keys(uniqUnappliedKBIDs)).
Where("kb_id IN ?", slices.Collect(maps.Keys(uniqUnappliedKBIDs))).
Find(&relations).Error; err != nil {
return nil, nil, xerrors.Errorf("Failed to get KB Relation by unapplied KBID: %q. err: %w", unapplied, err)
}
Expand All @@ -114,7 +114,7 @@ func (r *RDBDriver) GetExpandKB(applied []string, unapplied []string) ([]string,
}
}

return applied, maps.Keys(uniqUnappliedKBIDs), nil
return applied, slices.Collect(maps.Keys(uniqUnappliedKBIDs)), nil
}

// GetRelatedProducts :
Expand Down Expand Up @@ -284,11 +284,11 @@ func (r *RDBDriver) deleteAndInsertMicrosoft(cves []models.MicrosoftCVE) (err er
return fmt.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
}

for idx := range chunkSlice(len(cves), batchSize) {
if err = tx.Create(cves[idx.From:idx.To]).Error; err != nil {
for chunk := range slices.Chunk(cves, batchSize) {
if err = tx.Create(chunk).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand Down Expand Up @@ -324,11 +324,11 @@ func (r *RDBDriver) deleteAndInsertMicrosoftKBRelation(kbs []models.MicrosoftKBR
return fmt.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
}

for idx := range chunkSlice(len(kbs), batchSize) {
if err = tx.Create(kbs[idx.From:idx.To]).Error; err != nil {
for chunk := range slices.Chunk(kbs, batchSize) {
if err = tx.Create(chunk).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()
return nil
Expand Down
4 changes: 1 addition & 3 deletions db/rdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ func (r *RDBDriver) MigrateDB() error {
}
}
case dialectMysql, dialectPostgreSQL:
if err != nil {
return xerrors.Errorf("Failed to migrate. err: %w", err)
}
return xerrors.Errorf("Failed to migrate. err: %w", err)
default:
return xerrors.Errorf("Not Supported DB dialects. r.name: %s", r.name)
}
Expand Down
7 changes: 4 additions & 3 deletions db/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -222,11 +223,11 @@ func (r *RDBDriver) deleteAndInsertRedhat(cves []models.RedhatCVE) (err error) {
return fmt.Errorf("Failed to set batch-size. err: batch-size option is not set properly")
}

for idx := range chunkSlice(len(cves), batchSize) {
if err = tx.Create(cves[idx.From:idx.To]).Error; err != nil {
for chunk := range slices.Chunk(cves, batchSize) {
if err = tx.Create(chunk).Error; err != nil {
return xerrors.Errorf("Failed to insert. err: %w", err)
}
bar.Add(idx.To - idx.From)
bar.Add(len(chunk))
}
bar.Finish()

Expand Down
Loading
Loading