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

feat!(db/redis): use CPE#Titles instead of CPE#Cache#Titles #187

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

MaineK00n
Copy link
Collaborator

@MaineK00n MaineK00n commented Jun 25, 2024

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 save CPE#Titles as a String in the first place.

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How Has This Been Tested?

Checklist:

You don't have to satisfy all of the following.

  • Write tests
  • Write documentation
  • Check that there aren't other open pull requests for the same issue/feature
  • Format your source code by make fmt
  • Pass the test by make test
  • Provide verification config / commands
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES

Reference

@MaineK00n MaineK00n self-assigned this Jun 25, 2024
@MaineK00n
Copy link
Collaborator Author

@shino
When should we set the deadline? (1 week ?, 1 month ?

@shino
Copy link
Contributor

shino commented Jun 25, 2024

How about 2weeks? (geometric mean of the two)

@MaineK00n MaineK00n requested a review from shino October 30, 2024 15:17
@shino
Copy link
Contributor

shino commented Oct 31, 2024

Diff looks nice!

One thing I don't understand yet is...

titleListKey = "CPE#Titles"

This key is only used in InsertCpes() . Can we remove it completely?

@MaineK00n
Copy link
Collaborator Author

@shino
SET CPE#Titles cannot be deleted because it is necessary to create the StringCPE#Cache#Titles.

@MaineK00n
Copy link
Collaborator Author

MaineK00n commented Nov 1, 2024

: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)

db/redis.go Outdated Show resolved Hide resolved
db/redis.go Outdated Show resolved Hide resolved
@MaineK00n MaineK00n changed the title feat(db/redis): remove backward compatibility code feat(db/redis): use CPE#Titles instead of CPE#Cache#Titles Nov 5, 2024
@MaineK00n MaineK00n changed the title feat(db/redis): use CPE#Titles instead of CPE#Cache#Titles feat!(db/redis): use CPE#Titles instead of CPE#Cache#Titles Nov 5, 2024
@MaineK00n MaineK00n force-pushed the MaineK00n/patch-1 branch 3 times, most recently from 78a0ee5 to 2abe218 Compare November 5, 2024 22:09
@MaineK00n MaineK00n changed the title feat!(db/redis): use CPE#Titles instead of CPE#Cache#Titles feat(db/redis)!: remove backward compatibility code Nov 5, 2024
@MaineK00n MaineK00n changed the title feat(db/redis)!: remove backward compatibility code feat!(db/redis): use CPE#Titles instead of CPE#Cache#Titles Nov 5, 2024
@MaineK00n MaineK00n requested a review from shino November 5, 2024 22:35
db/redis.go Show resolved Hide resolved
@MaineK00n MaineK00n force-pushed the MaineK00n/patch-1 branch 2 times, most recently from 11a8f71 to 8531a70 Compare November 7, 2024 08:24
@MaineK00n MaineK00n requested a review from shino November 7, 2024 08:31
Copy link
Contributor

@shino shino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fantastic!

@MaineK00n MaineK00n merged commit 04a5dde into master Nov 8, 2024
4 of 5 checks passed
@MaineK00n MaineK00n deleted the MaineK00n/patch-1 branch November 8, 2024 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants