Skip to content

Commit

Permalink
Fix errors caused by empty lists
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Feb 22, 2024
1 parent c72f352 commit 8575c09
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/fpstore/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (c *RedisFingerprintCache) cacheKey(id uint64) string {
}

func (c *RedisFingerprintCache) GetMulti(ctx context.Context, ids []uint64) (map[uint64]*pb.Fingerprint, error) {
if len(ids) == 0 {
return nil, nil
}

keys := make([]string, len(ids))
for i, id := range ids {
keys[i] = c.cacheKey(id)
Expand Down
8 changes: 8 additions & 0 deletions pkg/fpstore/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (s *FingerprintStoreService) getFingerprint(ctx context.Context, id uint64)
func (s *FingerprintStoreService) getFingerprints(ctx context.Context, ids []uint64) (map[uint64]*pb.Fingerprint, error) {
logger := zerolog.Ctx(ctx)

if len(ids) == 0 {
return nil, nil
}

cachedFingerprints, err := s.cache.GetMulti(ctx, ids)
if err != nil {
logger.Err(err).Msg("failed to get fingerprints from cache")
Expand Down Expand Up @@ -233,6 +237,10 @@ func (s *FingerprintStoreService) Search(ctx context.Context, req *pb.SearchFing

logger.Debug().Int("candidates", len(candidateIds)).Msg("received candidates")

if len(candidateIds) == 0 {
return &pb.SearchFingerprintResponse{}, nil
}

results, err := s.compareFingerprints(ctx, req.Fingerprint, candidateIds, req.MinScore)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions pkg/fpstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func (s *PostgresFingerprintStore) Get(ctx context.Context, id uint64) (*pb.Fing
}

func (s *PostgresFingerprintStore) GetMulti(ctx context.Context, ids []uint64) (map[uint64]*pb.Fingerprint, error) {
if len(ids) == 0 {
return nil, nil
}
fps := make(map[uint64]*pb.Fingerprint, len(ids))
for _, id := range ids {
fp, err := s.Get(ctx, id)
Expand Down

0 comments on commit 8575c09

Please sign in to comment.