Skip to content

Conversation

@stereosteve
Copy link
Contributor

@stereosteve stereosteve commented Apr 8, 2025

This was annoying. The options considered:

  • Make two queries for each track ID to get followee_favorites + followee_reposts. These queries are simple and can be issued in parallel with the GetTracks query. But it does mean getting 10 tracks incurs 20 additional queries for these fields.
  • Make two bulk queries with a list of track IDs. One for followee_favorites and one for followee_reposts. Use window function to get the relevant rows grouped by each track ID. Elemental does something similar in this query.

With both of these you do the familiar pattern of:

  • collect base item ids
  • issue queries
  • key the results by the base item id.
  • attach to the relevant results to proper base item.

Which is all fine I guess.

Anyway in this PR I tried the 3rd approach which is to use postgres features to build up a json result in the base query itself. I hit a few sqlc snags initially but adding ::jsonb to the column type mostly made things work.

And I could have called it done with just a query change were it not for the mf hashids. So then I defined struct to match json and unmarshal and loop over and encode the hashids.

I spent too long trying to write a generic recursive hash ID encoder that would just modify the JSON on the way out. Stupid hash IDs. Anyway me and ChatGPT got pretty close but I gave up on that for now. Might revisit later.

@stereosteve stereosteve merged commit db5a712 into main Apr 8, 2025
2 checks passed
json_build_object(
'user_id', r.user_id::text,
'favorite_item_id', r.save_item_id::text, -- this is redundant
'favorite_type', 'SaveType.track', -- some sqlalchemy bs
Copy link
Contributor

Choose a reason for hiding this comment

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

I love these comments LOOOL

'user_id', r.user_id::text,
'favorite_item_id', r.save_item_id::text, -- this is redundant
'favorite_type', 'SaveType.track', -- some sqlalchemy bs
'created_at', r.created_at -- this is not actually present in python response?
Copy link
Contributor

Choose a reason for hiding this comment

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

interesting.. maybe it gets marshalled away?

@stereosteve stereosteve deleted the followee_reposts_take_2 branch April 14, 2025 14:48
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.

3 participants