Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous query optimizations on appview #1766

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export const skeleton = async (

if (filter === 'posts_with_media') {
feedItemsQb = feedItemsQb
// and only your own posts/reposts
.where('post.creator', '=', actorDid)
// only your own posts
.where('type', '=', 'post')
// only posts with media
.whereExists((qb) =>
qb
Expand Down
55 changes: 33 additions & 22 deletions packages/bsky/src/api/app/bsky/notification/listNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ const skeleton = async (
}
let notifBuilder = db.db
.selectFrom('notification as notif')
.innerJoin('record', 'record.uri', 'notif.recordUri')
.innerJoin('actor as author', 'author.did', 'notif.author')
.where(notSoftDeletedClause(ref('record')))
.where(notSoftDeletedClause(ref('author')))
.where('notif.did', '=', viewer)
.where((clause) =>
clause
Expand All @@ -69,23 +65,20 @@ const skeleton = async (
),
)
.select([
'notif.author as authorDid',
'notif.recordUri as uri',
'notif.recordCid as cid',
'author.did as authorDid',
'author.handle as authorHandle',
'author.indexedAt as authorIndexedAt',
'author.takedownId as authorTakedownId',
'notif.reason as reason',
'notif.reasonSubject as reasonSubject',
'notif.sortAt as indexedAt',
'record.json as recordJson',
])

const keyset = new NotifsKeyset(ref('notif.sortAt'), ref('notif.recordCid'))
notifBuilder = paginate(notifBuilder, {
cursor,
limit,
keyset,
tryIndex: true,
})

const actorStateQuery = db.db
Expand All @@ -107,17 +100,18 @@ const skeleton = async (
}

const hydration = async (state: SkeletonState, ctx: Context) => {
const { graphService, actorService, labelService } = ctx
const { graphService, actorService, labelService, db } = ctx
const { params, notifs } = state
const { viewer } = params
const dids = notifs.map((notif) => notif.authorDid)
const uris = notifs.map((notif) => notif.uri)
const [actors, labels, bam] = await Promise.all([
const [actors, records, labels, bam] = await Promise.all([
actorService.views.profiles(dids, viewer),
getRecordMap(db, uris),
labelService.getLabelsForUris(uris),
graphService.getBlockAndMuteState(dids.map((did) => [viewer, did])),
])
return { ...state, actors, labels, bam }
return { ...state, actors, records, labels, bam }
}

const noBlockOrMutes = (state: HydrationState) => {
Expand All @@ -131,11 +125,11 @@ const noBlockOrMutes = (state: HydrationState) => {
}

const presentation = (state: HydrationState) => {
const { notifs, cursor, actors, labels, lastSeenNotifs } = state
const { notifs, cursor, actors, records, labels, lastSeenNotifs } = state
const notifications = mapDefined(notifs, (notif) => {
const author = actors[notif.authorDid]
if (!author) return undefined
const record = jsonStringToLex(notif.recordJson) as Record<string, unknown>
const record = records[notif.uri]
if (!author || !record) return undefined
const recordLabels = labels[notif.uri] ?? []
const recordSelfLabels = getSelfLabels({
uri: notif.uri,
Expand All @@ -157,6 +151,24 @@ const presentation = (state: HydrationState) => {
return { notifications, cursor }
}

const getRecordMap = async (
db: Database,
uris: string[],
): Promise<RecordMap> => {
if (!uris.length) return {}
const { ref } = db.db.dynamic
const recordRows = await db.db
.selectFrom('record')
.select(['uri', 'json'])
.where('uri', 'in', uris)
.where(notSoftDeletedClause(ref('record')))
.execute()
return recordRows.reduce((acc, { uri, json }) => {
acc[uri] = jsonStringToLex(json) as Record<string, unknown>
return acc
}, {} as RecordMap)
}

type Context = {
db: Database
actorService: ActorService
Expand All @@ -178,20 +190,19 @@ type SkeletonState = {
type HydrationState = SkeletonState & {
bam: BlockAndMuteState
actors: ActorInfoMap
records: RecordMap
labels: Labels
}

type RecordMap = { [uri: string]: Record<string, unknown> }

type NotifRow = {
indexedAt: string
cid: string
uri: string
authorDid: string
authorHandle: string | null
authorIndexedAt: string
authorTakedownId: number | null
uri: string
cid: string
reason: string
reasonSubject: string | null
recordJson: string
indexedAt: string
}

class NotifsKeyset extends TimeCidKeyset<NotifRow> {
Expand Down
8 changes: 7 additions & 1 deletion packages/bsky/src/services/actor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ export class ActorService {
qb = qb.orWhere('actor.did', 'in', dids)
}
if (handles.length) {
qb = qb.orWhere('actor.handle', 'in', handles)
qb = qb.orWhere(
'actor.handle',
'in',
handles.length === 1
? [handles[0], handles[0]] // a silly (but worthwhile) optimization to avoid usage of actor_handle_tgrm_idx
: handles,
)
}
return qb
})
Expand Down