feat(db): add similarity lookup queries (ListActiveContextualLikesForUser, GetCurrentSessionVectorForUser)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: albums.sql
|
// source: albums.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: artists.sql
|
// source: artists.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: contextual_likes.sql
|
// source: contextual_likes.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
@@ -33,6 +33,43 @@ func (q *Queries) InsertContextualLike(ctx context.Context, arg InsertContextual
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const listActiveContextualLikesForUser = `-- name: ListActiveContextualLikesForUser :many
|
||||||
|
SELECT track_id, session_vector
|
||||||
|
FROM contextual_likes
|
||||||
|
WHERE user_id = $1
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
AND session_vector IS NOT NULL
|
||||||
|
`
|
||||||
|
|
||||||
|
type ListActiveContextualLikesForUserRow struct {
|
||||||
|
TrackID pgtype.UUID
|
||||||
|
SessionVector []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns all the user's active (non-soft-deleted) contextual_likes with
|
||||||
|
// non-null vectors. Cardinality is bounded by the user's actual like-while-
|
||||||
|
// playing history — typically tens to low hundreds. Used by the engine to
|
||||||
|
// compute contextual_match_score for the candidate pool.
|
||||||
|
func (q *Queries) ListActiveContextualLikesForUser(ctx context.Context, userID pgtype.UUID) ([]ListActiveContextualLikesForUserRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listActiveContextualLikesForUser, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []ListActiveContextualLikesForUserRow
|
||||||
|
for rows.Next() {
|
||||||
|
var i ListActiveContextualLikesForUserRow
|
||||||
|
if err := rows.Scan(&i.TrackID, &i.SessionVector); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const softDeleteContextualLikesForUserTrack = `-- name: SoftDeleteContextualLikesForUserTrack :exec
|
const softDeleteContextualLikesForUserTrack = `-- name: SoftDeleteContextualLikesForUserTrack :exec
|
||||||
UPDATE contextual_likes
|
UPDATE contextual_likes
|
||||||
SET deleted_at = now()
|
SET deleted_at = now()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: events.sql
|
// source: events.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
@@ -11,6 +11,26 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const getCurrentSessionVectorForUser = `-- name: GetCurrentSessionVectorForUser :one
|
||||||
|
SELECT pe.session_vector_at_play
|
||||||
|
FROM play_events pe
|
||||||
|
JOIN play_sessions s ON s.id = pe.session_id
|
||||||
|
WHERE pe.user_id = $1
|
||||||
|
AND s.ended_at IS NULL
|
||||||
|
ORDER BY pe.started_at DESC
|
||||||
|
LIMIT 1
|
||||||
|
`
|
||||||
|
|
||||||
|
// Returns the session_vector_at_play of the user's most recent play_event
|
||||||
|
// in a still-active (un-timed-out) session. NoRows means no current vector.
|
||||||
|
// Joined with play_sessions so closed sessions don't leak stale vectors.
|
||||||
|
func (q *Queries) GetCurrentSessionVectorForUser(ctx context.Context, userID pgtype.UUID) ([]byte, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getCurrentSessionVectorForUser, userID)
|
||||||
|
var session_vector_at_play []byte
|
||||||
|
err := row.Scan(&session_vector_at_play)
|
||||||
|
return session_vector_at_play, err
|
||||||
|
}
|
||||||
|
|
||||||
const getMostRecentPlaySessionForUser = `-- name: GetMostRecentPlaySessionForUser :one
|
const getMostRecentPlaySessionForUser = `-- name: GetMostRecentPlaySessionForUser :one
|
||||||
SELECT id, user_id, started_at, ended_at, last_event_at, track_count, client_id FROM play_sessions
|
SELECT id, user_id, started_at, ended_at, last_event_at, track_count, client_id FROM play_sessions
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: likes.sql
|
// source: likes.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: recommendation.sql
|
// source: recommendation.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: sessions.sql
|
// source: sessions.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: tracks.sql
|
// source: tracks.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by sqlc. DO NOT EDIT.
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// sqlc v1.31.1
|
// sqlc v1.27.0
|
||||||
// source: users.sql
|
// source: users.sql
|
||||||
|
|
||||||
package dbq
|
package dbq
|
||||||
|
|||||||
@@ -8,3 +8,14 @@ VALUES ($1, $2, $3, $4);
|
|||||||
UPDATE contextual_likes
|
UPDATE contextual_likes
|
||||||
SET deleted_at = now()
|
SET deleted_at = now()
|
||||||
WHERE user_id = $1 AND track_id = $2 AND deleted_at IS NULL;
|
WHERE user_id = $1 AND track_id = $2 AND deleted_at IS NULL;
|
||||||
|
|
||||||
|
-- name: ListActiveContextualLikesForUser :many
|
||||||
|
-- Returns all the user's active (non-soft-deleted) contextual_likes with
|
||||||
|
-- non-null vectors. Cardinality is bounded by the user's actual like-while-
|
||||||
|
-- playing history — typically tens to low hundreds. Used by the engine to
|
||||||
|
-- compute contextual_match_score for the candidate pool.
|
||||||
|
SELECT track_id, session_vector
|
||||||
|
FROM contextual_likes
|
||||||
|
WHERE user_id = $1
|
||||||
|
AND deleted_at IS NULL
|
||||||
|
AND session_vector IS NOT NULL;
|
||||||
|
|||||||
@@ -66,3 +66,15 @@ LIMIT $3;
|
|||||||
UPDATE play_events
|
UPDATE play_events
|
||||||
SET session_vector_at_play = $2
|
SET session_vector_at_play = $2
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: GetCurrentSessionVectorForUser :one
|
||||||
|
-- Returns the session_vector_at_play of the user's most recent play_event
|
||||||
|
-- in a still-active (un-timed-out) session. NoRows means no current vector.
|
||||||
|
-- Joined with play_sessions so closed sessions don't leak stale vectors.
|
||||||
|
SELECT pe.session_vector_at_play
|
||||||
|
FROM play_events pe
|
||||||
|
JOIN play_sessions s ON s.id = pe.session_id
|
||||||
|
WHERE pe.user_id = $1
|
||||||
|
AND s.ended_at IS NULL
|
||||||
|
ORDER BY pe.started_at DESC
|
||||||
|
LIMIT 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user