Skip to content

Commit

Permalink
feat(db/redis): remove backward compatibility code
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n committed Nov 1, 2024
1 parent 6920f82 commit 82b51ab
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions db/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"maps"
"os"
"slices"
"strconv"
Expand All @@ -29,7 +30,7 @@ import (
│NO │ KEY │ VALUE │ PURPOSE │
└───┴──────────────────┴──────────┴──────────────────────────────────────────────────┘
┌───┬──────────────────┬──────────┬──────────────────────────────────────────────────┐
│ 1 │ CPE#Cache#Titles │ JSON │ TO CACHE CPE#Titles │
│ 1 │ CPE#Titles │ JSON │ Get ALL Titles
└───┴──────────────────┴──────────┴──────────────────────────────────────────────────┘
- Sets
Expand All @@ -45,8 +46,6 @@ import (
├─────────────────────────────┼───────────────────────┼────────────────────────────────────┤
│ CPE#DeprecatedCPEs │ CPEURI │ Get DeprecatedCPEs │
├─────────────────────────────┼───────────────────────┼────────────────────────────────────┤
│ CPE#Titles │ Title │ Get ALL Titles │
├─────────────────────────────┼───────────────────────┼────────────────────────────────────┤
│ CPE#Title#{title} │ CPEURI │ Get CPEURI by title │
└─────────────────────────────┴───────────────────────┴────────────────────────────────────┘
Expand Down Expand Up @@ -256,20 +255,31 @@ func (r *RedisDriver) GetSimilarCpesByTitle(query string, n int, algorithm edlib
}

ctx := context.Background()
t, err := r.conn.Type(ctx, titleListKey).Result()
if err != nil {
return nil, xerrors.Errorf("Failed to TYPE CPE#Titles. err: %w", err)
}

var ts []string
bs, err := r.conn.Get(ctx, titleListCacheKey).Bytes()
if err == nil {
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)
}
} 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()
case "set":
bs, err := r.conn.Get(ctx, titleListCacheKey).Bytes()
if err != nil {
return nil, xerrors.Errorf("Failed to SMembers Titles. err: %w", err)
return nil, xerrors.Errorf("Failed to Get Titles Cache. err: %w", err)
}
if err := json.Unmarshal(bs, &ts); err != nil {
return nil, xerrors.Errorf("Failed to Unmarshal JSON. err: %w", err)
}
default:
return nil, xerrors.Errorf("unexpected CPE#Titles type. expected: %q, actual: %q", []string{"string", "set"}, t)
}

if len(ts) < n {
Expand Down Expand Up @@ -323,7 +333,6 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
"VendorProducts": {},
"DeprecatedVendorProducts": {},
"DeprecatedCPEs": {},
"Titles": {},
"Title": {}
}`
}
Expand Down Expand Up @@ -379,9 +388,7 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
delete(oldDeps["VP"], vendorProductStr)
}
}
_ = pipe.SAdd(ctx, titleListKey, c.Title)
newDeps["Titles"][c.Title] = map[string]struct{}{}
delete(oldDeps["Titles"], c.Title)

_ = pipe.SAdd(ctx, fmt.Sprintf(titleKeyFormat, c.Title), c.CpeURI)
if _, ok := newDeps["Title"][c.Title]; !ok {
Expand All @@ -402,16 +409,14 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
}
}

ts, err := r.conn.SMembers(ctx, titleListKey).Result()
if err != nil {
return xerrors.Errorf("Failed to SMembers Titles. err: %w", err)
}
ts := slices.Collect(maps.Keys(newDeps["Titles"]))
delete(newDeps, "Titles")
bs, err := json.Marshal(ts)
if err != nil {
return xerrors.Errorf("Failed to Marshal JSON. err: %w", err)
}
if err := r.conn.Set(ctx, titleListCacheKey, string(bs), 0).Err(); err != nil {
return xerrors.Errorf("Failed to SET Titles Cache. err: %w", err)
if err := r.conn.Set(ctx, titleListKey, string(bs), 0).Err(); err != nil {
return xerrors.Errorf("Failed to SET Titles. err: %w", err)
}
bar.Increment()

Expand All @@ -433,9 +438,7 @@ func (r *RedisDriver) InsertCpes(fetchType models.FetchType, cpes models.Fetched
for cpeURI := range oldDeps["DeprecatedCPEs"] {
_ = pipe.SRem(ctx, deprecatedCPEsKey, cpeURI)
}
for title := range oldDeps["Titles"] {
_ = pipe.SRem(ctx, titleListKey, title)
}
_ = pipe.Del(ctx, titleListCacheKey)
for title, cpeURIs := range oldDeps["Title"] {
for cpeURI := range cpeURIs {
_ = pipe.SRem(ctx, fmt.Sprintf(titleKeyFormat, title), cpeURI)
Expand Down

0 comments on commit 82b51ab

Please sign in to comment.