diff --git a/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt b/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt index cf8ab22c07..0fff78d839 100644 --- a/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt +++ b/engine/src/main/java/com/google/android/fhir/db/impl/DatabaseImpl.kt @@ -38,6 +38,9 @@ import com.google.android.fhir.index.ResourceIndexer import com.google.android.fhir.logicalId import com.google.android.fhir.search.SearchQuery import com.google.android.fhir.toLocalChange +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope import java.time.Instant import java.util.UUID import org.hl7.fhir.r4.model.Resource @@ -205,7 +208,7 @@ internal class DatabaseImpl( query: SearchQuery, ): List> { return db.withTransaction { - resourceDao.getResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray())).map { + resourceDao.getResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray())).pmap { ResourceWithUUID(it.uuid, iParser.parseResource(it.serializedResource) as R) } } @@ -217,7 +220,7 @@ internal class DatabaseImpl( return db.withTransaction { resourceDao .getForwardReferencedResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray())) - .map { + .pmap { ForwardIncludeSearchResult( it.matchingIndex, it.baseResourceUUID, @@ -233,7 +236,7 @@ internal class DatabaseImpl( return db.withTransaction { resourceDao .getReverseReferencedResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray())) - .map { + .pmap { ReverseIncludeSearchResult( it.matchingIndex, it.baseResourceTypeAndId, @@ -413,6 +416,13 @@ internal class DatabaseImpl( } } + /** + * Implementation of a parallelized map + */ + suspend fun Iterable.pmap(f: suspend (A) -> B): List = coroutineScope { + map { async { f(it) } }.awaitAll() + } + companion object { /** * The name for unencrypted database.