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

Replace flattenParents with directParentName #6314

Merged
merged 32 commits into from Jul 20, 2022

Conversation

ericdecanini
Copy link
Contributor

@ericdecanini ericdecanini commented Jun 15, 2022

Type of change

  • Feature
  • Bugfix
  • Technical
  • Other :

Content

Replaces flattenParents field in RoomSummary with directParentName to increase performance

Motivation and context

Previously to have the space parent displayed in the search screen, we added the flattenParents field to RoomSummary, but using this was triggering a double mapping of the room summary and its parents thus affecting performance.

To alleviate this, we decided on simply saving the parents name directly in RoomSummaryUpdater. This has little if any impact on performance and allows us to achieve the same goal without double mapping.

Screenshots / GIFs

Screenshot_1655297224

  • Search screen space parent subtitles working as previously

Tests

Preferably before doing this, change the build version so a clean install doesn't happen and you can test the migration too

Run the app and see that the search screen displays with spaces showing their space parent (if any) and performance isn't a problem

Tested devices

  • Physical
  • Emulator
  • OS version(s): Android 11

Checklist

override fun doMigrate(realm: DynamicRealm) {
realm.schema.get("RoomSummaryEntity")
?.addField(RoomSummaryEntityFields.DIRECT_PARENT_NAME, String::class.java)
?.transform { it.setString(RoomSummaryEntityFields.DIRECT_PARENT_NAME, "") }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if we migrate it as empty here, it seems like it automatically syncs on launch and updates the value with the corresponding space parent of each room

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's updated after each sync. Is there a need to set a default value.

@ericdecanini ericdecanini marked this pull request as ready for review June 15, 2022 13:04
*/
val flattenParents: List<RoomSummary> = emptyList(),
val directParentName: String? = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be properly updated, if the parent name changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed this one, it properly updates

*/
val flattenParents: List<RoomSummary> = emptyList(),
val directParentName: String? = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericdecanini this should be a list, you can have several direct parents.

Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a list of names, not just one. Or you could format it as a coma separated list, but just one is not correct

…flatten_with_direct_parent

# Conflicts:
#	matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt
#	matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo030.kt
Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think directParentNames is properly cleared before re-computing hierarchy, so it looks like you are continously adding the names to it.
I don't think I have seen an update on the migration (? or i missed a commit)
Also why only display the last() parent, I think web displays them all

@BillCarsonFr
Copy link
Member

Also would be nice to add a new test for that

@ericdecanini
Copy link
Contributor Author

I don't think directParentNames is properly cleared before re-computing hierarchy, so it looks like you are continously adding the names to it. I don't think I have seen an update on the migration (? or i missed a commit) Also why only display the last() parent, I think web displays them all

Pushed a change to make it show all direct parents and clear on sync

See MigrateSessionTo032.kt for the migration
As for unit tests, the real unit testable part of this would be the RoomSummaryUpdater, but it needs to be hugely refactored to be testable. Given the size and importance of this class, I think this should be done separately

Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the migration of the DB.


override fun doMigrate(realm: DynamicRealm) {
realm.schema.get("RoomSummaryEntity")
?.addField(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, String::class.java)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need a List and not a String here, no? So use addRealmListField. I cannot test the migration since I have another issue with the Auth DB. Will be fixed once develop is been merged to your branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes! Did addRealmListField initially but lost it during a dev merge

@@ -235,6 +235,8 @@ internal open class RoomSummaryEntity(
if (value != field) field = value
}

var directParentNames: RealmList<String> = RealmList()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker, but If there is no set fun, this could be moved to the constructor.

@ericdecanini ericdecanini requested a review from bmarty July 5, 2022 08:36
@@ -207,9 +207,9 @@ class RoomSummaryItemFactory @Inject constructor(

private fun getSearchResultSubtitle(roomSummary: RoomSummary): String {
val userId = roomSummary.directUserId
val spaceName = roomSummary.flattenParents.lastOrNull()?.name
val directParent = roomSummary.directParentNames.takeIf { it.isNotEmpty() }?.joinToString()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please ensure parity with web to display parents in results.
and add a test in SpaceHierarchyTest

…flatten_with_direct_parent

# Conflicts:
#	matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo032.kt
#	vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderGroup.kt
#	vector/src/main/java/im/vector/app/features/home/room/list/RoomListSectionBuilderSpace.kt
…flatten_with_direct_parent

# Conflicts:
#	matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt
@ericdecanini ericdecanini requested a review from bmarty July 19, 2022 09:17
Copy link
Contributor

@fedrunov fedrunov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good except migration tests, not sure if we really need them

import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields
import org.matrix.android.sdk.test.fakes.FakeDynamicRealm

internal class MigrateSessionTo033Test {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tests for Realm migrations are excessive. Migration classes never change, so there is no need to retest them, but we put extra work on our CI to run this tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests removed

ericdecanini and others added 5 commits July 19, 2022 13:49
…flatten_with_direct_parent

# Conflicts:
#	matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo033.kt
Copy link
Member

@BillCarsonFr BillCarsonFr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a quick test, so ok now when CI happy

0 -> null
1 -> directParentNames.first()
2 -> stringProvider.getQuantityString(R.plurals.search_space_parents, 1, directParentNames[0], directParentNames[1])
else -> stringProvider.getQuantityString(R.plurals.search_space_parents, size - 1, directParentNames[0], directParentNames.size - 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could use size instead of directParentNames.size,

<plurals name="search_space_parents">
<item quantity="one">%1$s and %2$s</item>
<item quantity="other">%1$s and %2$d others</item>
</plurals>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct way to handle plurals, it can lead to errors with translations.
Also the resource name is a bit strange to me
You must have something like:

    <strings name="search_space_two parents">%1$s and %2$s</string>
    <plurals name="search_space_parents">
        <item quantity="one">%1$s and %2$d other</item>
        <item quantity="other">%1$s and %2$d others</item>
    </plurals>

This is managed the same way for room_displayname_two_members if you want to have a look on it.

@ericdecanini ericdecanini requested a review from bmarty July 20, 2022 13:26
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update.
Please fix the conflict by creating a MigrateSessionTo035.kt.

…flatten_with_direct_parent

# Conflicts:
#	matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo034.kt
override fun doMigrate(realm: DynamicRealm) {
realm.schema.get("RoomSummaryEntity")
?.addRealmListField(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, String::class.java)
?.transform { it.setString(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, "") }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did not test the migration. The app is crashing.
The correct code is

Suggested change
?.transform { it.setString(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, "") }
?.transform { it.setList(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, RealmList("")) }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swear I tested this. Maybe the way I was testing it didn't actually cause this migration to happen

@ericdecanini ericdecanini requested a review from bmarty July 20, 2022 15:51
@sonarcloud
Copy link

sonarcloud bot commented Jul 20, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

2.3% 2.3% Coverage
0.0% 0.0% Duplication

@ericdecanini ericdecanini merged commit a2cf872 into develop Jul 20, 2022
@ericdecanini ericdecanini deleted the task/eric/replace_flatten_with_direct_parent branch July 20, 2022 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants