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

Updated PerResourcePatchGenerator to return ordered PatchMapping to avoid referential integrity issues. #2442

Merged
merged 14 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Review comments: Changes to cache PerResourcePatchGenerator instance
  • Loading branch information
aditya-07 committed Mar 5, 2024
commit cdc2f208a0a62845788d83b2ab5f3976fdd7b426
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal object PatchGeneratorFactory {
): PatchGenerator =
when (mode) {
is PatchGeneratorMode.PerChange -> PerChangePatchGenerator
is PatchGeneratorMode.PerResource -> PerResourcePatchGenerator(database)
is PatchGeneratorMode.PerResource -> PerResourcePatchGenerator.with(database)
}
}

Expand Down
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import com.google.android.fhir.sync.upload.patch.PatchOrdering.orderByReferences
* maintain an audit trail, but instead, multiple changes made to the same FHIR resource on the
* client can be recorded as a single change on the server.
*/
internal class PerResourcePatchGenerator(val database: Database) : PatchGenerator {
internal class PerResourcePatchGenerator private constructor(val database: Database) :
PatchGenerator {

override suspend fun generate(localChanges: List<LocalChange>): List<PatchMapping> {
return generateSquashedChangesMapping(localChanges).orderByReferences(database)
Expand Down Expand Up @@ -144,4 +145,20 @@ internal class PerResourcePatchGenerator(val database: Database) : PatchGenerato
mergedOperations.values.flatten().forEach(mergedNode::add)
return objectMapper.writeValueAsString(mergedNode)
}

companion object {

private lateinit var _instance: PerResourcePatchGenerator
aditya-07 marked this conversation as resolved.
Show resolved Hide resolved

@Synchronized
fun with(database: Database): PerResourcePatchGenerator {
if (!::_instance.isInitialized) {
_instance = PerResourcePatchGenerator(database)
} else if (_instance.database != database) {
_instance = PerResourcePatchGenerator(database)
}

return _instance
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PerResourcePatchGeneratorTest {
fun setUp() {
MockitoAnnotations.openMocks(this)
runTest { whenever(database.getLocalChangeResourceReferences(any())).thenReturn(emptyList()) }
patchGenerator = PerResourcePatchGenerator(database)
patchGenerator = PerResourcePatchGenerator.with(database)
}

@Test
Expand Down
Loading