Refactor domain-match lookup to idiomatic Load + filter

Replace the cross-entity JOIN in
DetectedTrackers.LoadCommonThirdPartyIDByDomainMatch with two
idiomatic coredata calls: LoadInitiatorDomainsByTrackerPatternID
on DetectedTrackers, then a new CommonThirdPartyDomains.Load with
a CommonThirdPartyDomainFilter. Each entity now queries only its
own table, and the caller orchestrates the lookup.

Document the Load vs LoadAll naming convention and the no
cross-entity JOINs rule in contrib/claude/coredata.md.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-19 11:52:57 +04:00
parent 370b593217
commit 10adf2bd4b
5 changed files with 128 additions and 36 deletions

View File

@@ -16,7 +16,6 @@ package coredata
import (
"context"
"errors"
"fmt"
"maps"
"time"
@@ -152,47 +151,24 @@ WHERE
return count, nil
}
func (dts *DetectedTrackers) LoadCommonThirdPartyIDByDomainMatch(
ctx context.Context,
conn pg.Querier,
trackerPatternID gid.GID,
) (*gid.GID, error) {
q := `
SELECT DISTINCT ctpd.common_third_party_id
FROM detected_trackers dt
JOIN common_third_party_domains ctpd ON ctpd.domain = dt.initiator_domain
WHERE dt.tracker_pattern_id = @tracker_pattern_id
AND dt.initiator_domain IS NOT NULL
LIMIT 1;
`
args := pgx.StrictNamedArgs{"tracker_pattern_id": trackerPatternID}
var commonThirdPartyID gid.GID
if err := conn.QueryRow(ctx, q, args).Scan(&commonThirdPartyID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, fmt.Errorf("cannot load common third party ID by tracker pattern: %w", err)
}
return &commonThirdPartyID, nil
}
func (dts *DetectedTrackers) LoadInitiatorDomainsByTrackerPatternID(
ctx context.Context,
conn pg.Querier,
trackerPatternID gid.GID,
limit int,
) ([]string, error) {
q := `
SELECT DISTINCT initiator_domain
FROM detected_trackers
WHERE tracker_pattern_id = @tracker_pattern_id
AND initiator_domain IS NOT NULL
LIMIT 5;
LIMIT @limit;
`
args := pgx.StrictNamedArgs{"tracker_pattern_id": trackerPatternID}
args := pgx.StrictNamedArgs{
"tracker_pattern_id": trackerPatternID,
"limit": limit,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {