Skip to content

Commit 273713b

Browse files
emmaling27Convex, Inc.
authored andcommitted
Make VirtualSystemDocMapper trait async (#43536)
Makes `VirtualSystemDocMapper` trait async, which we will need to await `get_document` inside the implementation. GitOrigin-RevId: 4238f6f8aa25bcd92fa3aa1704b282630e870b32
1 parent 3202780 commit 273713b

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

crates/common/src/virtual_system_mapping.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::{
44
};
55

66
use anyhow::Context;
7+
use async_trait::async_trait;
78
use imbl::OrdMap;
89
use semver::Version;
9-
use tonic::async_trait;
1010
use value::{
1111
DeveloperDocumentId,
1212
NamespacedTableMapping,
@@ -25,8 +25,9 @@ use crate::{
2525
types::IndexName,
2626
};
2727

28+
#[async_trait]
2829
pub trait VirtualSystemDocMapper: Send + Sync {
29-
fn system_to_virtual_doc(
30+
async fn system_to_virtual_doc(
3031
&self,
3132
tx: &mut dyn GetDocument,
3233
virtual_system_mapping: &VirtualSystemMapping,
@@ -52,6 +53,7 @@ pub struct NoopDocMapper;
5253

5354
#[cfg(any(test, feature = "testing"))]
5455
pub mod test_virtual_system_mapping {
56+
use async_trait::async_trait;
5557
use value::TableMapping;
5658

5759
use super::NoopDocMapper;
@@ -68,8 +70,9 @@ pub mod test_virtual_system_mapping {
6870
},
6971
};
7072

73+
#[async_trait]
7174
impl VirtualSystemDocMapper for NoopDocMapper {
72-
fn system_to_virtual_doc(
75+
async fn system_to_virtual_doc(
7376
&self,
7477
_tx: &mut dyn GetDocument,
7578
_virtual_system_mapping: &VirtualSystemMapping,

crates/database/src/bootstrap_model/user_facing.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -409,26 +409,29 @@ pub async fn index_range_batch<RT: Runtime>(
409409

410410
for (&batch_key, fetch_result) in fetch_requests.keys().zip(fetch_results) {
411411
let virtual_table_version = virtual_table_versions.get(&batch_key).cloned();
412-
let result = fetch_result.and_then(|IndexRangeResponse { page, cursor }| {
412+
let result = try {
413+
let IndexRangeResponse { page, cursor } = fetch_result?;
413414
let developer_results = match virtual_table_version {
414-
Some(version) => page
415-
.into_iter()
416-
.map(|(key, doc, ts)| {
417-
let doc =
418-
VirtualTable::new(tx).system_to_virtual_doc(doc, version.clone())?;
419-
anyhow::Ok((key, doc, ts))
420-
})
421-
.try_collect()?,
415+
Some(version) => {
416+
let mut converted_documents = vec![];
417+
for (key, doc, ts) in page {
418+
let doc = VirtualTable::new(tx)
419+
.system_to_virtual_doc(doc, version.clone())
420+
.await?;
421+
converted_documents.push((key, doc, ts));
422+
}
423+
converted_documents
424+
},
422425
None => page
423426
.into_iter()
424427
.map(|(key, doc, ts)| (key, doc.to_developer(), ts))
425428
.collect(),
426429
};
427-
anyhow::Ok(DeveloperIndexRangeResponse {
430+
DeveloperIndexRangeResponse {
428431
page: developer_results,
429432
cursor,
430-
})
431-
});
433+
}
434+
};
432435
results.insert(batch_key, result);
433436
}
434437
assert_eq!(results.len(), batch_size);

crates/database/src/virtual_tables/mod.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ impl<'a, RT: Runtime> VirtualTable<'a, RT> {
6262
let result = self.tx.get_inner(id_, system_table_name).await?;
6363
match result {
6464
Some((doc, ts)) => {
65-
let doc = self.system_to_virtual_doc(doc, version)?;
65+
let doc = self.system_to_virtual_doc(doc, version).await?;
6666
Ok(Some((doc, ts)))
6767
},
6868
None => Ok(None),
6969
}
7070
}
7171

72-
pub fn system_to_virtual_doc(
72+
pub async fn system_to_virtual_doc(
7373
&mut self,
7474
doc: ResolvedDocument,
7575
version: Option<Version>,
@@ -91,12 +91,14 @@ impl<'a, RT: Runtime> VirtualTable<'a, RT> {
9191
else {
9292
anyhow::bail!("System document cannot be converted to a virtual document")
9393
};
94-
mapper.system_to_virtual_doc(
95-
self.tx,
96-
&virtual_system_mapping,
97-
doc,
98-
&table_mapping,
99-
version,
100-
)
94+
mapper
95+
.system_to_virtual_doc(
96+
self.tx,
97+
&virtual_system_mapping,
98+
doc,
99+
&table_mapping,
100+
version,
101+
)
102+
.await
101103
}
102104
}

crates/model/src/file_storage/virtual_table.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{
33
sync::LazyLock,
44
};
55

6+
use async_trait::async_trait;
67
use common::{
78
document::{
89
DeveloperDocument,
@@ -42,8 +43,9 @@ static MIN_NPM_VERSION_FILE_STORAGE_V2: LazyLock<Version> =
4243

4344
pub struct FileStorageDocMapper;
4445

46+
#[async_trait]
4547
impl VirtualSystemDocMapper for FileStorageDocMapper {
46-
fn system_to_virtual_doc(
48+
async fn system_to_virtual_doc(
4749
&self,
4850
_tx: &mut dyn GetDocument,
4951
virtual_system_mapping: &VirtualSystemMapping,

crates/model/src/scheduled_jobs/virtual_table.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::{
44
sync::LazyLock,
55
};
66

7+
use async_trait::async_trait;
78
use common::{
89
document::{
910
timestamp_to_ms,
@@ -43,8 +44,9 @@ static MIN_NPM_VERSION_SCHEDULED_JOBS_V1: LazyLock<Version> =
4344

4445
pub struct ScheduledJobsDocMapper;
4546

47+
#[async_trait]
4648
impl VirtualSystemDocMapper for ScheduledJobsDocMapper {
47-
fn system_to_virtual_doc(
49+
async fn system_to_virtual_doc(
4850
&self,
4951
_tx: &mut dyn GetDocument,
5052
virtual_system_mapping: &VirtualSystemMapping,

0 commit comments

Comments
 (0)