Skip to content

Commit

Permalink
Parallelize DB resource deserialization to Optimize Database API
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Sep 4, 2024
1 parent bcf9262 commit 04cb554
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -205,7 +208,7 @@ internal class DatabaseImpl(
query: SearchQuery,
): List<ResourceWithUUID<R>> {
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)
}
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -413,6 +416,13 @@ internal class DatabaseImpl(
}
}

/**
* Implementation of a parallelized map
*/
suspend fun <A, B> Iterable<A>.pmap(f: suspend (A) -> B): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll()
}

companion object {
/**
* The name for unencrypted database.
Expand Down

0 comments on commit 04cb554

Please sign in to comment.