-
Notifications
You must be signed in to change notification settings - Fork 243
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
Ordering of patches with cycles during upload #2524
base: master
Are you sure you want to change the base?
Conversation
Is this ready for review ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we use Strongly Connected Components (SCC) ?
- Identify the SCCs
- Consolidate all nodes in each SCC into a supernode.
- Then apply the same topological sorting
So for following graph:-
1 -> 2, 3
2 -> 4, 5
4 -> 1
5 -> 6
7 -> 8, 9
8 -> 7
SCC = [{1,2,4}, {7, 8}]
We can have the topological sort as - [{3}, {6}, {5}, {1, 2, 4}, {9}, {7, 8}]
Current solution is also right but the above solution has more improvements:-
- Time complexity is one-fourth (from first look).
- In current approach the weakly-connected-components will go in one bundle if there is a cycle. Whereas in the above approach only the SCC will go in one bundle -> Reduces the size of the bundle a lot.
It's the Tarjan's Algorithm: https://www.geeksforgeeks.org/tarjan-algorithm-find-strongly-connected-components/ |
engine/src/main/java/com/google/android/fhir/sync/upload/patch/PatchGenerator.kt
Outdated
Show resolved
Hide resolved
@@ -67,3 +67,20 @@ internal data class PatchMapping( | |||
val localChanges: List<LocalChange>, | |||
val generatedPatch: Patch, | |||
) | |||
|
|||
/** Structure to help describe the cyclic nature of ordered [PatchMapping]. */ | |||
internal sealed interface OrderedMapping { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think there is no order in this structure itself - so maybe patch mapping group is a better name - or something similar?
engine/src/main/java/com/google/android/fhir/sync/upload/patch/PatchOrdering.kt
Outdated
Show resolved
Hide resolved
engine/src/main/java/com/google/android/fhir/sync/upload/patch/PatchOrdering.kt
Outdated
Show resolved
Hide resolved
engine/src/main/java/com/google/android/fhir/sync/upload/patch/PatchOrdering.kt
Outdated
Show resolved
Hide resolved
engine/src/main/java/com/google/android/fhir/sync/upload/request/TransactionBundleGenerator.kt
Outdated
Show resolved
Hide resolved
engine/src/main/java/com/google/android/fhir/sync/upload/request/UrlRequestGenerator.kt
Outdated
Show resolved
Hide resolved
|
||
import kotlin.math.min | ||
|
||
internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment pls
|
||
import kotlin.math.min | ||
|
||
internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) { | |
internal class StronglyConnectedPatches(private val directedGraph: Graph, private val nodeCount: Int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you're having these as data members of the class - calling sscOrdered
twice would not have any side effects? Just making sure... because you probably will need to make sure that nothing in your algorithm is modifying the graph and the node count.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
say: the node count is the number of nodes in the directedGraph
, inconsistency between the graph and the node count might generate incorrect results.
/** | ||
* Converts a graph that has ssc to a graph with super nodes as per the provided [nodeToSuperNode] | ||
* mapping. | ||
* | ||
* **Input** | ||
* * oldGraph `[A : B, C], [B : A, C], [C : A, B, F], [D : E], [E : D], [F : G]` | ||
* * nodeToSuperNode `[A:1, B:1, C:1, D:2, E:2, F:3, G:4]` | ||
* | ||
* **Output** `[1 : 3], [2], [3: 4]` | ||
*/ | ||
private fun transformGraphWithSSCtoGraphWithSuperNodes( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what this function actually does is: generating edges in the graph G' of super nodes from the edges in the original graph G.
so if there's an edge a->b in G, then there is an edge from S(a)->S(b) in G' where S(x) is the super node of x and S(a)!=S(b).
maybe you can add this to the description as at the moment it's not as detailed.
return createTopologicalOrderedList(adjacencyList).mapNotNull { resourceIdToPatchMapping[it] } | ||
|
||
return StronglyConnectedPatches(adjacencyList, resourceIdToPatchMapping.size) | ||
.sscOrdered { createTopologicalOrderedList(it) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you can do
.sscOrdered(::createTopologicalOrderedList)
|
||
import kotlin.math.min | ||
|
||
internal class StronglyConnectedPatches(private val diGraph: Graph, private val nodesCount: Int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this needs some tests. especially simple cases e.g. graph of size 1, graph of size 0. graph of size 2 with or without a single link.
@@ -67,3 +67,20 @@ internal data class PatchMapping( | |||
val localChanges: List<LocalChange>, | |||
val generatedPatch: Patch, | |||
) | |||
|
|||
/** Structure to help describe the cyclic nature of ordered [PatchMapping]. */ | |||
internal sealed interface PatchMappingGroup { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if we can define a single structure which is a collection of patch mappings that should be uploaded together. in (possibly most) some cases, they should just be individual patch mappings, but in the case of cycles, they would be bundled together as a result of your SCC calculation.
private typealias Node = String | ||
typealias Node = String | ||
|
||
typealias Graph = Map<Node, List<Node>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think about this alternative definition:
data class Node(private id: String, private out: List<Node>)
typealias Graph = List<Node>
and potentially this may simplify your code a bit. because you can define things like
data class SuperNode(val out: List<SuperNode>, val nodes: List<Nodes>)
data class Node(val id: String, val out: List<Node>, val superNode: SuperNode)
and another thing is that - if we use this, we might be able to do away with copying the strings.
val intToNode = mutableMapOf<Int, String>() | ||
val nodeToInt = mutableMapOf<String, Int>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comments here.
val sccs = IntArray(graph.size) | ||
val stack = ArrayDeque<Int>() | ||
|
||
fun dfs(at: Int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add comment here
IMPORTANT: All PRs must be linked to an issue (except for extremely trivial and straightforward changes).
Fixes #2500
Description
Alternative(s) considered
Have you considered any alternatives? And if so, why have you chosen the approach in this PR?
Type
Choose one: Bug fix
Screenshots (if applicable)
Checklist
./gradlew spotlessApply
and./gradlew spotlessCheck
to check my code follows the style guide of this project../gradlew check
and./gradlew connectedCheck
to test my changes locally.