Skip to content

Commit

Permalink
update go-redis (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
sadayuki-matsuno authored Dec 4, 2020
1 parent 29ba79a commit 0d3ca57
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
20 changes: 13 additions & 7 deletions db/redis.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package db

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
"github.com/inconshreveable/log15"
c "github.com/kotakanbe/goval-dictionary/config"
"github.com/kotakanbe/goval-dictionary/models"
Expand Down Expand Up @@ -101,13 +102,14 @@ func (d *RedisDriver) Name() string {

// OpenDB opens Database
func (d *RedisDriver) OpenDB(dbType, dbPath string, debugSQL bool) (err error) {
ctx := context.Background()
var option *redis.Options
if option, err = redis.ParseURL(dbPath); err != nil {
log15.Error("Failed to parse url", "err", err)
return fmt.Errorf("Failed to Parse Redis URL. dbpath: %s, err: %s", dbPath, err)
}
d.conn = redis.NewClient(option)
if err = d.conn.Ping().Err(); err != nil {
if err = d.conn.Ping(ctx).Err(); err != nil {
return fmt.Errorf("Failed to open DB. dbtype: %s, dbpath: %s, err: %s", dbType, dbPath, err)
}
return nil
Expand All @@ -124,6 +126,7 @@ func (d *RedisDriver) CloseDB() (err error) {

// GetByPackName select OVAL definition related to OS Family, osVer, packName, arch
func (d *RedisDriver) GetByPackName(family, osVer, packName, arch string) ([]models.Definition, error) {
ctx := context.Background()
switch family {
case c.CentOS:
family = c.RedHat
Expand All @@ -146,7 +149,7 @@ func (d *RedisDriver) GetByPackName(family, osVer, packName, arch string) ([]mod
}

var result *redis.StringSliceCmd
if result = d.conn.ZRange(zkey, 0, -1); result.Err() != nil {
if result = d.conn.ZRange(ctx, zkey, 0, -1); result.Err() != nil {
log15.Error("Failed to get definition from package", "err", result.Err())
return nil, result.Err()
}
Expand Down Expand Up @@ -180,6 +183,7 @@ func (d *RedisDriver) GetByCveID(family, osVer, cveID string) ([]models.Definiti

// InsertOval inserts OVAL
func (d *RedisDriver) InsertOval(family string, root *models.Root, meta models.FetchMeta) (err error) {
ctx := context.Background()
definitions := aggregateAffectedPackages(root.Definitions)
total := map[string]struct{}{}
for chunked := range chunkSlice(definitions, 10) {
Expand All @@ -205,7 +209,7 @@ func (d *RedisDriver) InsertOval(family string, root *models.Root, meta models.F
}
for cveID := range cveIDs {
hashKey := getHashKey(root.Family, root.OSVersion, cveID)
if result := pipe.HSet(hashKey, def.DefinitionID, string(dj)); result.Err() != nil {
if result := pipe.HSet(ctx, hashKey, def.DefinitionID, string(dj)); result.Err() != nil {
return fmt.Errorf("Failed to HSet Definition. err: %s", result.Err())
}
for _, pack := range def.AffectedPacks {
Expand All @@ -215,8 +219,9 @@ func (d *RedisDriver) InsertOval(family string, root *models.Root, meta models.F
zkey = hashKeyPrefix + pack.Name + hashKeySeparator + pack.Arch
}
if result := pipe.ZAdd(
ctx,
zkey,
redis.Z{
&redis.Z{
Score: 0,
Member: hashKey,
}); result.Err() != nil {
Expand All @@ -226,7 +231,7 @@ func (d *RedisDriver) InsertOval(family string, root *models.Root, meta models.F
total[cveID] = struct{}{}
}
}
if _, err = pipe.Exec(); err != nil {
if _, err = pipe.Exec(ctx); err != nil {
return fmt.Errorf("Failed to exec pipeline. err: %s", err)
}
}
Expand Down Expand Up @@ -279,7 +284,8 @@ func chunkSlice(l []models.Definition, n int) chan []models.Definition {
}

func getByHashKey(hashKey string, driver *redis.Client) ([]models.Definition, error) {
result := driver.HGetAll(hashKey)
ctx := context.Background()
result := driver.HGetAll(ctx, hashKey)
if result.Err() != nil {
log15.Error("Failed to get definition.", "err", result.Err())
return nil, result.Err()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.12
require (
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-redis/redis v6.15.2+incompatible
github.com/go-redis/redis/v8 v8.4.0
github.com/google/subcommands v1.0.1
github.com/htcat/htcat v1.0.2
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec
Expand All @@ -17,5 +17,5 @@ require (
github.com/labstack/gommon v0.2.9 // indirect
github.com/mattn/go-sqlite3 v1.11.0
github.com/ymomoi/goval-parser v0.0.0-20170813122243-0a0be1dd9d08
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v2 v2.3.0
)
Loading

0 comments on commit 0d3ca57

Please sign in to comment.