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

Allow users to request referenced resources along with the searched via inline params. #1978

Merged
merged 46 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
f33cc64
Inital changes to support inline resources via revInclude
aditya-07 Apr 13, 2023
54e4cbc
Merge branch 'master' into ak/revinclude
aditya-07 Apr 18, 2023
b0cd819
Merge branch 'master' into ak/revinclude
aditya-07 Apr 24, 2023
df863ac
Updated test and added doc
aditya-07 Apr 24, 2023
4239de9
Merge branch 'master' into ak/revinclude
aditya-07 Apr 27, 2023
e0f3c0f
RevInclude to return mapped response
aditya-07 Apr 28, 2023
1ccf108
Test code for forward include
aditya-07 May 4, 2023
d14e52a
Merge branch 'master' into ak/revinclude
aditya-07 May 5, 2023
543478c
Fixed include query
aditya-07 May 5, 2023
1818b80
Merge branch 'master' into ak/revinclude
aditya-07 Jun 6, 2023
724fb9d
Merge branch 'master' into ak/revinclude
aditya-07 Jun 28, 2023
379ed16
Unify search api to include include and revInclude functionality
aditya-07 Jun 29, 2023
c6f3682
Merge branch 'master' into ak/revinclude
aditya-07 Jun 29, 2023
75fac23
Workflow library: Incorporated changes related to FhirEngine.search
aditya-07 Jun 29, 2023
8ceb736
Merge branch 'ak/revinclude' of github.com:aditya-07/android-fhir int…
aditya-07 Jun 29, 2023
8756138
Merge branch 'master' into ak/revinclude
aditya-07 Jun 30, 2023
30d48a6
Merge branch 'master' into ak/revinclude
aditya-07 Jul 4, 2023
7acc7bb
Merge branch 'master' into ak/revinclude
aditya-07 Jul 5, 2023
4e50b9e
Merge branch 'master' into ak/revinclude
aditya-07 Jul 11, 2023
ced4b08
Merge branch 'master' into ak/revinclude
aditya-07 Jul 19, 2023
fe10fa0
Review comments: Changed the search result type to include the search…
aditya-07 Jul 20, 2023
5dcb352
Merge branch 'master' into ak/revinclude
aditya-07 Jul 20, 2023
7b55c7e
Updated docs
aditya-07 Jul 20, 2023
3db1d92
Added spotless toggle to skip indentation for specific code portions
aditya-07 Jul 20, 2023
e28c098
Merge branch 'master' into ak/revinclude
aditya-07 Jul 21, 2023
f74707e
Merge branch 'master' into ak/revinclude
aditya-07 Jul 24, 2023
35bf026
Added individual tests for include and revInclude
aditya-07 Jul 25, 2023
48df351
Merge branch 'master' into ak/revinclude
aditya-07 Jul 25, 2023
41d090c
Merge branch 'master' into ak/revinclude
aditya-07 Jul 26, 2023
b79bb28
Merge branch 'master' into ak/revinclude
aditya-07 Jul 26, 2023
a5ef271
Merge branch 'master' into ak/revinclude
aditya-07 Jul 27, 2023
286b58a
Merge branch 'master' into ak/revinclude
aditya-07 Jul 27, 2023
8efbdc2
Review Comments: Updated kdocs and refactored function name
aditya-07 Jul 27, 2023
af7768e
Merge branch 'master' into ak/revinclude
aditya-07 Jul 31, 2023
cfe459f
Review comments: refactored type of revInclude
aditya-07 Aug 1, 2023
31834c6
Fixed failing workflow compilation as workflow doesn't depends on eng…
aditya-07 Aug 1, 2023
9f169f3
Resolved compilation errors
aditya-07 Aug 1, 2023
fd22291
Merge branch 'master' into ak/revinclude
aditya-07 Aug 3, 2023
d5d0fe0
Merge branch 'master' into ak/revinclude
aditya-07 Aug 3, 2023
ed8a645
Merge branch 'master' into ak/revinclude
aditya-07 Aug 4, 2023
1228211
Merge branch 'master' into ak/revinclude
aditya-07 Aug 10, 2023
14ffa49
Merge branch 'master' into ak/revinclude
aditya-07 Aug 11, 2023
1dbe6be
Reverted the engine changes
aditya-07 Aug 11, 2023
530a5e1
Merge branch 'master' into ak/revinclude
aditya-07 Aug 11, 2023
8740ff4
Merge branch 'master' into ak/revinclude
omarismail94 Aug 21, 2023
00b1956
Merge branch 'master' into ak/revinclude
omarismail94 Aug 21, 2023
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
Prev Previous commit
Next Next commit
RevInclude to return mapped response
  • Loading branch information
aditya-07 committed Apr 28, 2023
commit e0f3c0f8131b3d159aa19948989485b3f7567fd8
4 changes: 4 additions & 0 deletions engine/src/main/java/com/google/android/fhir/FhirEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ interface FhirEngine {
*/
suspend fun <R : Resource> search(search: Search): List<R>

suspend fun <R : Resource> searchWithRevInclude(
search: Search
): Map<R, Map<ResourceType, List<Resource>>>

/**
* Synchronizes the [upload] result in the database. [upload] operation may result in multiple
* calls to the server to upload the data. Result of each call will be emitted by [upload] and the
Expand Down
3 changes: 3 additions & 0 deletions engine/src/main/java/com/google/android/fhir/db/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.android.fhir.db

import com.google.android.fhir.db.impl.dao.IndexedIdAndResource
import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.dao.SquashedLocalChange
import com.google.android.fhir.db.impl.entities.LocalChangeEntity
Expand Down Expand Up @@ -94,6 +95,8 @@ internal interface Database {

suspend fun <R : Resource> search(query: SearchQuery): List<R>

suspend fun <R : Resource> searchRev(query: SearchQuery): List<IndexedIdAndResource>

suspend fun count(query: SearchQuery): Long

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ca.uhn.fhir.parser.IParser
import com.google.android.fhir.DatabaseErrorStrategy
import com.google.android.fhir.db.ResourceNotFoundException
import com.google.android.fhir.db.impl.DatabaseImpl.Companion.UNENCRYPTED_DATABASE_NAME
import com.google.android.fhir.db.impl.dao.IndexedIdAndResource
import com.google.android.fhir.db.impl.dao.LocalChangeToken
import com.google.android.fhir.db.impl.dao.LocalChangeUtils
import com.google.android.fhir.db.impl.dao.SquashedLocalChange
Expand Down Expand Up @@ -186,6 +187,17 @@ internal class DatabaseImpl(
}
}

override suspend fun <R : Resource> searchRev(query: SearchQuery): List<IndexedIdAndResource> {
return db.withTransaction {
resourceDao.getResourcesRev(SimpleSQLiteQuery(query.query, query.args.toTypedArray())).map {
IndexedIdAndResource(
it.idOfBaseResourceOnWhichThisMatched,
iParser.parseResource(it.serializedResource) as Resource
)
}
}
}

override suspend fun count(query: SearchQuery): Long {
return db.withTransaction {
resourceDao.countResources(SimpleSQLiteQuery(query.query, query.args.toTypedArray()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.fhir.db.impl.dao

import androidx.annotation.VisibleForTesting
import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
Expand Down Expand Up @@ -150,6 +151,11 @@ internal abstract class ResourceDao {

@RawQuery abstract suspend fun getResources(query: SupportSQLiteQuery): List<String>

@RawQuery
abstract suspend fun getResourcesRev(
query: SupportSQLiteQuery
): List<IndexedIdAndSerializedResource>

@RawQuery abstract suspend fun countResources(query: SupportSQLiteQuery): Long

private suspend fun insertResource(resource: Resource): String {
Expand Down Expand Up @@ -279,3 +285,13 @@ internal abstract class ResourceDao {
}
}
}

internal data class IndexedIdAndSerializedResource(
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
@ColumnInfo(name = "index_value") val idOfBaseResourceOnWhichThisMatched: String,
val serializedResource: String
)

internal data class IndexedIdAndResource(
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
val idOfBaseResourceOnWhichThisMatched: String,
val resource: Resource
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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.sync.ConflictResolver
import com.google.android.fhir.sync.Resolved
import java.time.OffsetDateTime
Expand Down Expand Up @@ -60,6 +62,29 @@ internal class FhirEngineImpl(private val database: Database, private val contex
return search.execute(database)
}

override suspend fun <R : Resource> searchWithRevInclude(
search: Search
): Map<R, Map<ResourceType, List<Resource>>> {
val baseResources = database.search<R>(search.getQuery()) // .subList(0,2)
val includedResources =
database.searchRev<R>(
search.getIncludeQuery(
includeIds = baseResources.map { "\'${it.resourceType}/${it.logicalId}\'" }
)
)
val resultMap = mutableMapOf<R, Map<ResourceType, List<Resource>>>()
baseResources.forEach { patient ->
resultMap[patient] =
includedResources
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
.filter {
it.idOfBaseResourceOnWhichThisMatched == "${patient.fhirType()}/${patient.logicalId}"
}
.map { it.resource }
.groupBy { it.resourceType }
}
return resultMap
}

override suspend fun count(search: Search): Long {
return search.count(database)
}
Expand Down
54 changes: 38 additions & 16 deletions engine/src/main/java/com/google/android/fhir/search/MoreSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ fun Search.getQuery(isCount: Boolean = false): SearchQuery {
return getQuery(isCount, null)
}

internal fun Search.getIncludeQuery(includeIds: List<String>): SearchQuery {
val match =
revIncludeMap
.map {
" ( a.resourceType = '${it.key}' and a.index_name IN (${it.value.joinToString { "\'${it.paramName}\'" }}) ) "
}
.joinToString(separator = "OR")

return SearchQuery(
query =
"""
SELECT a.index_value, b.serializedResource
FROM ReferenceIndexEntity a
JOIN ResourceEntity b
ON a.resourceUuid = b.resourceUuid
AND a.index_value IN( ${includeIds.joinToString()} )
AND ($match)
""".trimIndent(),
args = listOf()
)
}

internal fun Search.getQuery(
isCount: Boolean = false,
nestedContext: NestedContext? = null
Expand Down Expand Up @@ -168,22 +190,22 @@ internal fun Search.getQuery(
$limitStatement)
"""
}
revIncludeMap.isNotEmpty() -> {
"""
select serializedResource from ResourceEntity where resourceUuid in (
with UUIDS as ( select '${type.name}/' || a.resourceId from ResourceEntity a
$sortJoinStatement
WHERE a.resourceType = ?
$filterStatement
$sortOrderStatement
$limitStatement
)
Select resourceUuid
FROM ResourceEntity
WHERE '${type.name}/' || resourceId in UUIDS ${revIncludeMap.toSQLQuery()}
)
""".trimIndent()
}
// revIncludeMap.isNotEmpty() -> {
// """
// select serializedResource from ResourceEntity where resourceUuid in (
// with UUIDS as ( select '${type.name}/' || a.resourceId from ResourceEntity a
// $sortJoinStatement
// WHERE a.resourceType = ?
// $filterStatement
// $sortOrderStatement
// $limitStatement
// )
// Select resourceUuid
// FROM ResourceEntity
// WHERE '${type.name}/' || resourceId in UUIDS ${revIncludeMap.toSQLQuery()}
// )
// """.trimIndent()
// }
else ->
"""
SELECT a.serializedResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ suspend fun FhirEngine.search(resourceType: ResourceType, init: Search.() -> Uni
search.init()
return this.search(search)
}

suspend inline fun <reified R : Resource> FhirEngine.searchWithRevInclude(
init: Search.() -> Unit
): Map<R, Map<ResourceType, List<Resource>>> {
val search = Search(type = R::class.java.newInstance().resourceType)
search.init()
return this.searchWithRevInclude(search)
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ object TestFhirEngineImpl : FhirEngine {
return emptyList()
}

override suspend fun <R : Resource> searchWithRevInclude(
search: Search
): Map<R, Map<ResourceType, List<Resource>>> {
TODO("Not yet implemented")
}

override suspend fun syncUpload(
upload: suspend (List<LocalChange>) -> Flow<Pair<LocalChangeToken, Resource>>
) {
Expand Down