Skip to content

ref/#2540-Move Search Logic from Search.execute() to FhirEngine.search() #2541

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import com.google.android.fhir.db.Database
import com.google.android.fhir.logicalId
import com.google.android.fhir.search.Search
import com.google.android.fhir.search.count
import com.google.android.fhir.search.execute
import com.google.android.fhir.search.getIncludeQuery
import com.google.android.fhir.search.getQuery
import com.google.android.fhir.search.getRevIncludeQuery
import com.google.android.fhir.sync.ConflictResolver
import com.google.android.fhir.sync.Resolved
import com.google.android.fhir.sync.upload.DefaultResourceConsolidator
Expand Down Expand Up @@ -61,7 +63,44 @@ internal class FhirEngineImpl(private val database: Database, private val contex
}

override suspend fun <R : Resource> search(search: Search): List<SearchResult<R>> {
return search.execute(database)
val baseResources = database.search<R>(search.getQuery())
val includedResources =
if (search.forwardIncludes.isEmpty() || baseResources.isEmpty()) {
null
} else {
database.searchForwardReferencedResources(
search.getIncludeQuery(includeIds = baseResources.map { it.uuid }),
)
}
val revIncludedResources =
if (search.revIncludes.isEmpty() || baseResources.isEmpty()) {
null
} else {
database.searchReverseReferencedResources(
search.getRevIncludeQuery(
includeIds =
baseResources.map { "${it.resource.resourceType}/${it.resource.logicalId}" },
),
)
}

return baseResources.map { (uuid, baseResource) ->
SearchResult(
baseResource,
included =
includedResources
?.asSequence()
?.filter { it.baseResourceUUID == uuid }
?.groupBy({ it.searchIndex }, { it.resource }),
revIncluded =
revIncludedResources
?.asSequence()
?.filter {
it.baseResourceTypeWithId == "${baseResource.fhirType()}/${baseResource.logicalId}"
}
?.groupBy({ it.resource.resourceType to it.searchIndex }, { it.resource }),
)
}
}

override suspend fun count(search: Search): Long {
Expand Down
Loading