-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
feat!(db/redis): use CPE#Titles instead of CPE#Cache#Titles #187
Conversation
@shino |
How about 2weeks? (geometric mean of the two) |
10c1ae4
to
db5bcfb
Compare
Diff looks nice! One thing I don't understand yet is...
This key is only used in InsertCpes() . Can we remove it completely? |
@shino |
db5bcfb
to
82b51ab
Compare
:100644 100644 7832f41 0000000 M db/redis.go
diff --git a/db/redis.go b/db/redis.go
index 7832f41..70e7f26 100644
--- a/db/redis.go
+++ b/db/redis.go
@@ -72,7 +72,6 @@ const (
vpSeparator = "##"
deprecatedCPEsKey = "CPE#DeprecatedCPEs"
titleListKey = "CPE#Titles"
- titleListCacheKey = "CPE#Cache#Titles"
titleKeyFormat = "CPE#Title#%s"
depKey = "CPE#DEP"
fetchMetaKey = "CPE#FETCHMETA"
@@ -255,38 +254,13 @@ func (r *RedisDriver) GetSimilarCpesByTitle(query string, n int, algorithm edlib
}
ctx := context.Background()
- t, err := r.conn.Type(ctx, titleListKey).Result()
+ var ts []string
+ bs, err := r.conn.Get(ctx, titleListKey).Bytes()
if err != nil {
- return nil, xerrors.Errorf("Failed to TYPE CPE#Titles. err: %w", err)
+ return nil, xerrors.Errorf("Failed to Get Titles. err: %w", err)
}
-
- var ts []string
- switch t {
- case "string":
- bs, err := r.conn.Get(ctx, titleListKey).Bytes()
- if err != nil {
- return nil, xerrors.Errorf("Failed to Get Titles. err: %w", err)
- }
- if err := json.Unmarshal(bs, &ts); err != nil {
- return nil, xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
- }
- case "set": // backward compatibility: https://github.com/vulsio/go-cpe-dictionary/pull/186
- bs, err := r.conn.Get(ctx, titleListCacheKey).Bytes()
- if err == nil {
- if err := json.Unmarshal(bs, &ts); err != nil {
- return nil, xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
- }
- } else {
- if !xerrors.Is(err, redis.Nil) {
- return nil, xerrors.Errorf("Failed to Get Titles. err: %w", err)
- }
- ts, err = r.conn.SMembers(ctx, titleListKey).Result()
- if err != nil {
- return nil, xerrors.Errorf("Failed to SMembers Titles. err: %w", err)
- }
- }
- default:
- return nil, xerrors.Errorf("unexpected CPE#Titles type. expected: %q, actual: %q", []string{"string", "set"}, t)
+ if err := json.Unmarshal(bs, &ts); err != nil {
+ return nil, xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
}
if len(ts) < n {
@@ -353,38 +327,13 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
return xerrors.Errorf("Failed to Exists CPE#Titles. err: %w", err)
}
if exists > 0 {
- t, err := r.conn.Type(ctx, titleListKey).Result()
+ var ts []string
+ bs, err := r.conn.Get(ctx, titleListKey).Bytes()
if err != nil {
- return xerrors.Errorf("Failed to TYPE CPE#Titles. err: %w", err)
+ return xerrors.Errorf("Failed to Get Titles. err: %w", err)
}
-
- var ts []string
- switch t {
- case "string":
- bs, err := r.conn.Get(ctx, titleListKey).Bytes()
- if err != nil {
- return xerrors.Errorf("Failed to Get Titles. err: %w", err)
- }
- if err := json.Unmarshal(bs, &ts); err != nil {
- return xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
- }
- case "set": // backward compatibility: https://github.com/vulsio/go-cpe-dictionary/pull/186
- bs, err := r.conn.Get(ctx, titleListCacheKey).Bytes()
- if err == nil {
- if err := json.Unmarshal(bs, &ts); err != nil {
- return xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
- }
- } else {
- if !xerrors.Is(err, redis.Nil) {
- return xerrors.Errorf("Failed to Get Titles. err: %w", err)
- }
- ts, err = r.conn.SMembers(ctx, titleListKey).Result()
- if err != nil {
- return xerrors.Errorf("Failed to SMembers Titles. err: %w", err)
- }
- }
- default:
- return xerrors.Errorf("unexpected CPE#Titles type. expected: %q, actual: %q", []string{"string", "set"}, t)
+ if err := json.Unmarshal(bs, &ts); err != nil {
+ return xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
}
for _, t := range ts {
titles[t] = struct{}{}
@@ -488,7 +437,6 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
for cpeURI := range oldDeps["DeprecatedCPEs"] {
_ = pipe.SRem(ctx, deprecatedCPEsKey, cpeURI)
}
- _ = pipe.Del(ctx, titleListCacheKey)
for title, cpeURIs := range oldDeps["Title"] {
for cpeURI := range cpeURIs {
_ = pipe.SRem(ctx, fmt.Sprintf(titleKeyFormat, title), cpeURI)
|
82b51ab
to
a1b7351
Compare
a1b7351
to
431f6db
Compare
78a0ee5
to
2abe218
Compare
2abe218
to
d29d544
Compare
d29d544
to
c8a882d
Compare
c8a882d
to
515af55
Compare
11a8f71
to
8531a70
Compare
8531a70
to
a1f2f5d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fantastic!
If this Pull Request is work in progress, Add a prefix of “[WIP]” in the title.
What did you implement:
#186 created a new key (
CPE#Cache#Titles
) when fetched, and changed to use it.However, there was no need to prepare
CPE#Cache#Titles
; it was enough to simply saveCPE#Titles
as a String in the first place.Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Checklist:
You don't have to satisfy all of the following.
make fmt
make test
Is this ready for review?: YES
Reference