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 10, 2024
1 parent 5787c7f commit d871fa3
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import com.google.android.fhir.search.SearchQuery
import com.google.android.fhir.toLocalChange
import java.time.Instant
import java.util.UUID
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType

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,11 @@ 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 d871fa3

Please sign in to comment.