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(
answering_connection_id,
proto::Participant {
user_id: user_id.to_proto(), user_id: user_id.to_proto(),
peer_id: answering_connection_id as u32, peer_id: answering_connection_id as u32,
projects: Default::default(), projects: Default::default(),
location: Some(proto::ParticipantLocation { variant: location }), 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 projects.id, worktrees.root_name SELECT host_connection_id, 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 AND host_connection_id = $2 WHERE room_id = $1
", ",
) )
.bind(room_id) .bind(room_id)
.bind(participant.peer_id as i32)
.fetch(&mut *tx); .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())
{
project
} else {
participant.projects.push(proto::ParticipantProject {
id: project_id.to_proto(), id: project_id.to_proto(),
worktree_root_names: Default::default(), worktree_root_names: Default::default(),
}); });
participant_project participant.projects.last_mut().unwrap()
.worktree_root_names };
.push(worktree_root_name); 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,
abs_path,
visible,
scan_id,
is_complete
) )
VALUES {params}
"
);
let mut query = sqlx::query(&query);
for worktree in worktrees {
query = query
.bind(project_id) .bind(project_id)
.bind(worktree.id as i32) .bind(worktree.id as i32)
.bind(&worktree.root_name) .bind(&worktree.root_name)
.bind(&worktree.abs_path) .bind(&worktree.abs_path)
.bind(worktree.visible) .bind(worktree.visible)
.bind(0) .bind(0)
.bind(false) .bind(false);
.execute(&mut tx) }
.await?; query.execute(&mut tx).await?;
} }
sqlx::query( sqlx::query(
@ -1535,14 +1553,28 @@ 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,
ON CONFLICT (project_id, id) DO UPDATE SET root_name = excluded.root_name 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
"
);
let mut query = sqlx::query(&query);
for worktree in worktrees {
query = query
.bind(project_id) .bind(project_id)
.bind(worktree.id as i32) .bind(worktree.id as i32)
.bind(&worktree.root_name) .bind(&worktree.root_name)
@ -1550,8 +1582,8 @@ where
.bind(worktree.visible) .bind(worktree.visible)
.bind(0) .bind(0)
.bind(false) .bind(false)
.execute(&mut tx) }
.await?; query.execute(&mut tx).await?;
} }
let mut params = "?,".repeat(worktrees.len()); let mut params = "?,".repeat(worktrees.len());