Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Parallelized map and optimize Database search API #2669

Merged
merged 10 commits into from
Oct 7, 2024
10 changes: 10 additions & 0 deletions engine/src/main/java/com/google/android/fhir/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Date
import java.util.Locale
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import org.hl7.fhir.r4.model.OperationOutcome
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
Expand Down Expand Up @@ -69,6 +73,12 @@ internal fun Resource.isUploadSuccess(): Boolean {
outcome.issue.all { it.severity.equals(OperationOutcome.IssueSeverity.INFORMATION) }
}

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

internal class OffsetDateTimeTypeAdapter : TypeAdapter<OffsetDateTime>() {
override fun write(out: JsonWriter, value: OffsetDateTime) {
out.value(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.annotation.VisibleForTesting
import androidx.room.Room
import androidx.room.withTransaction
import androidx.sqlite.db.SimpleSQLiteQuery
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.parser.IParser
import ca.uhn.fhir.util.FhirTerser
import com.google.android.fhir.DatabaseErrorStrategy
Expand All @@ -37,11 +38,13 @@ import com.google.android.fhir.db.impl.entities.LocalChangeEntity
import com.google.android.fhir.db.impl.entities.ResourceEntity
import com.google.android.fhir.index.ResourceIndexer
import com.google.android.fhir.logicalId
import com.google.android.fhir.pmap
import com.google.android.fhir.search.SearchQuery
import com.google.android.fhir.toLocalChange
import com.google.android.fhir.updateMeta
import java.time.Instant
import java.util.UUID
import kotlinx.coroutines.Dispatchers
import org.hl7.fhir.r4.model.IdType
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType
Expand Down Expand Up @@ -227,8 +230,13 @@ internal class DatabaseImpl(
query: SearchQuery,
): List<ResourceWithUUID<R>> {
return db.withTransaction {
resourceDao.getResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray())).map {
ResourceWithUUID(it.uuid, iParser.parseResource(it.serializedResource) as R)
resourceDao.getResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray())).pmap(
Dispatchers.Default,
) {
ResourceWithUUID(
it.uuid,
FhirContext.forR4Cached().newJsonParser().parseResource(it.serializedResource) as R,
)
}
}
}
Expand All @@ -239,11 +247,12 @@ internal class DatabaseImpl(
return db.withTransaction {
resourceDao
.getForwardReferencedResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray()))
.map {
.pmap(Dispatchers.Default) {
ForwardIncludeSearchResult(
it.matchingIndex,
it.baseResourceUUID,
iParser.parseResource(it.serializedResource) as Resource,
FhirContext.forR4Cached().newJsonParser().parseResource(it.serializedResource)
as Resource,
)
}
}
Expand All @@ -255,11 +264,12 @@ internal class DatabaseImpl(
return db.withTransaction {
resourceDao
.getReverseReferencedResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray()))
.map {
.pmap(Dispatchers.Default) {
ReverseIncludeSearchResult(
it.matchingIndex,
it.baseResourceTypeAndId,
iParser.parseResource(it.serializedResource) as Resource,
FhirContext.forR4Cached().newJsonParser().parseResource(it.serializedResource)
as Resource,
)
}
}
Expand Down
Loading