Batch some of the new queries in Db

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-11-16 17:19:06 +01:00
parent faf265328e
commit adf43c87dd

View file

@ -1354,7 +1354,7 @@ where
.bind(room_id) .bind(room_id)
.fetch(&mut *tx); .fetch(&mut *tx);
let mut participants = Vec::new(); let mut participants = HashMap::default();
let mut pending_participants = Vec::new(); let mut pending_participants = Vec::new();
while let Some(participant) = db_participants.next().await { while let Some(participant) = db_participants.next().await {
let ( let (
@ -1381,12 +1381,15 @@ where
Default::default(), Default::default(),
)), )),
}; };
participants.push(proto::Participant { participants.insert(
user_id: user_id.to_proto(), answering_connection_id,
peer_id: answering_connection_id as u32, proto::Participant {
projects: Default::default(), user_id: user_id.to_proto(),
location: Some(proto::ParticipantLocation { variant: location }), peer_id: answering_connection_id as u32,
}); projects: Default::default(),
location: Some(proto::ParticipantLocation { variant: location }),
},
);
} else { } else {
pending_participants.push(proto::PendingParticipant { pending_participants.push(proto::PendingParticipant {
user_id: user_id.to_proto(), user_id: user_id.to_proto(),
@ -1397,41 +1400,42 @@ where
} }
drop(db_participants); drop(db_participants);
for participant in &mut participants { let mut rows = sqlx::query_as::<_, (i32, ProjectId, Option<String>)>(
let mut entries = sqlx::query_as::<_, (ProjectId, String)>( "
" SELECT host_connection_id, projects.id, worktrees.root_name
SELECT projects.id, worktrees.root_name FROM projects
FROM projects LEFT JOIN worktrees ON projects.id = worktrees.project_id
LEFT JOIN worktrees ON projects.id = worktrees.project_id WHERE room_id = $1
WHERE room_id = $1 AND host_connection_id = $2 ",
", )
) .bind(room_id)
.bind(room_id) .fetch(&mut *tx);
.bind(participant.peer_id as i32)
.fetch(&mut *tx);
let mut projects = HashMap::default(); while let Some(row) = rows.next().await {
while let Some(entry) = entries.next().await { let (connection_id, project_id, worktree_root_name) = row?;
let (project_id, worktree_root_name) = entry?; if let Some(participant) = participants.get_mut(&connection_id) {
let participant_project = let project = if let Some(project) = participant
projects .projects
.entry(project_id) .iter_mut()
.or_insert(proto::ParticipantProject { .find(|project| project.id == project_id.to_proto())
id: project_id.to_proto(), {
worktree_root_names: Default::default(), project
}); } else {
participant_project participant.projects.push(proto::ParticipantProject {
.worktree_root_names id: project_id.to_proto(),
.push(worktree_root_name); worktree_root_names: Default::default(),
});
participant.projects.last_mut().unwrap()
};
project.worktree_root_names.extend(worktree_root_name);
} }
participant.projects = projects.into_values().collect();
} }
Ok(proto::Room { Ok(proto::Room {
id: room.id.to_proto(), id: room.id.to_proto(),
version: room.version as u64, version: room.version as u64,
live_kit_room: room.live_kit_room, live_kit_room: room.live_kit_room,
participants, participants: participants.into_values().collect(),
pending_participants, pending_participants,
}) })
} }
@ -1472,22 +1476,36 @@ where
.fetch_one(&mut tx) .fetch_one(&mut tx)
.await?; .await?;
for worktree in worktrees { if !worktrees.is_empty() {
sqlx::query( let mut params = "(?, ?, ?, ?, ?, ?, ?),".repeat(worktrees.len());
params.pop();
let query = format!(
" "
INSERT INTO worktrees (project_id, id, root_name, abs_path, visible, scan_id, is_complete) INSERT INTO worktrees (
VALUES ($1, $2, $3, $4, $5, $6, $7) project_id,
", id,
) root_name,
.bind(project_id) abs_path,
.bind(worktree.id as i32) visible,
.bind(&worktree.root_name) scan_id,
.bind(&worktree.abs_path) is_complete
.bind(worktree.visible) )
.bind(0) VALUES {params}
.bind(false) "
.execute(&mut tx) );
.await?;
let mut query = sqlx::query(&query);
for worktree in worktrees {
query = query
.bind(project_id)
.bind(worktree.id as i32)
.bind(&worktree.root_name)
.bind(&worktree.abs_path)
.bind(worktree.visible)
.bind(0)
.bind(false);
}
query.execute(&mut tx).await?;
} }
sqlx::query( sqlx::query(
@ -1535,23 +1553,37 @@ where
.fetch_one(&mut tx) .fetch_one(&mut tx)
.await?; .await?;
for worktree in worktrees { if !worktrees.is_empty() {
sqlx::query( let mut params = "(?, ?, ?, ?, ?, ?, ?),".repeat(worktrees.len());
params.pop();
let query = format!(
" "
INSERT INTO worktrees (project_id, id, root_name, abs_path, visible, scan_id, is_complete) INSERT INTO worktrees (
VALUES ($1, $2, $3, $4, $5, $6, $7) project_id,
id,
root_name,
abs_path,
visible,
scan_id,
is_complete
)
VALUES ${params}
ON CONFLICT (project_id, id) DO UPDATE SET root_name = excluded.root_name ON CONFLICT (project_id, id) DO UPDATE SET root_name = excluded.root_name
", "
) );
.bind(project_id)
.bind(worktree.id as i32) let mut query = sqlx::query(&query);
.bind(&worktree.root_name) for worktree in worktrees {
.bind(&worktree.abs_path) query = query
.bind(worktree.visible) .bind(project_id)
.bind(0) .bind(worktree.id as i32)
.bind(false) .bind(&worktree.root_name)
.execute(&mut tx) .bind(&worktree.abs_path)
.await?; .bind(worktree.visible)
.bind(0)
.bind(false)
}
query.execute(&mut tx).await?;
} }
let mut params = "?,".repeat(worktrees.len()); let mut params = "?,".repeat(worktrees.len());