-
Notifications
You must be signed in to change notification settings - Fork 742
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
Improve usage of realm #5297
Improve usage of realm #5297
Changes from 3 commits
fcca75e
d27acfa
80d19fa
4cc8016
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ package org.matrix.android.sdk.internal.database.mapper | |
import org.matrix.android.sdk.api.session.room.model.ReadReceipt | ||
import org.matrix.android.sdk.internal.database.RealmSessionProvider | ||
import org.matrix.android.sdk.internal.database.model.ReadReceiptsSummaryEntity | ||
import org.matrix.android.sdk.internal.database.model.UserEntity | ||
import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntity | ||
import org.matrix.android.sdk.internal.database.query.where | ||
import javax.inject.Inject | ||
|
||
|
@@ -29,14 +29,12 @@ internal class ReadReceiptsSummaryMapper @Inject constructor(private val realmSe | |
if (readReceiptsSummaryEntity == null) { | ||
return emptyList() | ||
} | ||
return realmSessionProvider.withRealm { realm -> | ||
val readReceipts = readReceiptsSummaryEntity.readReceipts | ||
readReceipts | ||
.mapNotNull { | ||
val user = UserEntity.where(realm, it.userId).findFirst() | ||
?: return@mapNotNull null | ||
ReadReceipt(user.asDomain(), it.originServerTs.toLong()) | ||
} | ||
} | ||
val readReceipts = readReceiptsSummaryEntity.readReceipts | ||
return readReceipts | ||
.mapNotNull { | ||
val roomMember = RoomMemberSummaryEntity.where(readReceiptsSummaryEntity.realm, roomId = it.roomId, userId = it.userId).findFirst() | ||
?: return@mapNotNull null | ||
ReadReceipt(roomMember.asDomain(), it.originServerTs.toLong()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are there any side effects from switching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User avatar can be set per room, so maybe there will be a visible effect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No this should just fix some cases where user has changed avatar/name only in the room |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,8 +90,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, | |
private val timelineEventsChangeListener = | ||
OrderedRealmCollectionChangeListener { results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet -> | ||
Timber.v("on timeline events chunk update") | ||
val frozenResults = results.freeze() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤞 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main fix ^ =P |
||
handleDatabaseChangeSet(frozenResults, changeSet) | ||
handleDatabaseChangeSet(results, changeSet) | ||
} | ||
|
||
private var timelineEventEntities: RealmResults<TimelineEventEntity> = chunkEntity.sortedTimelineEvents(timelineSettings.rootThreadEventId) | ||
|
@@ -287,7 +286,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, | |
* @return the number of events loaded. If we are in a thread timeline it also returns | ||
* whether or not we reached the end/root message | ||
*/ | ||
private suspend fun loadFromStorage(count: Int, direction: Timeline.Direction): LoadedFromStorage { | ||
private fun loadFromStorage(count: Int, direction: Timeline.Direction): LoadedFromStorage { | ||
val displayIndex = getNextDisplayIndex(direction) ?: return LoadedFromStorage() | ||
val baseQuery = timelineEventEntities.where() | ||
|
||
|
@@ -428,10 +427,10 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, | |
* This method is responsible for managing insertions and updates of events on this chunk. | ||
* | ||
*/ | ||
private fun handleDatabaseChangeSet(frozenResults: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet) { | ||
private fun handleDatabaseChangeSet(results: RealmResults<TimelineEventEntity>, changeSet: OrderedCollectionChangeSet) { | ||
val insertions = changeSet.insertionRanges | ||
for (range in insertions) { | ||
val newItems = frozenResults | ||
val newItems = results | ||
.subList(range.startIndex, range.startIndex + range.length) | ||
.map { it.buildAndDecryptIfNeeded() } | ||
builtEventsIndexes.entries.filter { it.value >= range.startIndex }.forEach { it.setValue(it.value + range.length) } | ||
|
@@ -447,7 +446,7 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, | |
val modifications = changeSet.changeRanges | ||
for (range in modifications) { | ||
for (modificationIndex in (range.startIndex until range.startIndex + range.length)) { | ||
val updatedEntity = frozenResults[modificationIndex] ?: continue | ||
val updatedEntity = results[modificationIndex] ?: continue | ||
try { | ||
builtEvents[modificationIndex] = updatedEntity.buildAndDecryptIfNeeded() | ||
} catch (failure: Throwable) { | ||
|
@@ -458,20 +457,20 @@ internal class TimelineChunk(private val chunkEntity: ChunkEntity, | |
if (insertions.isNotEmpty() || modifications.isNotEmpty()) { | ||
onBuiltEvents(true) | ||
} | ||
|
||
} | ||
|
||
private fun getNextDisplayIndex(direction: Timeline.Direction): Int? { | ||
val frozenTimelineEvents = timelineEventEntities.freeze() | ||
if (frozenTimelineEvents.isEmpty()) { | ||
if (timelineEventEntities.isEmpty()) { | ||
return null | ||
} | ||
return if (builtEvents.isEmpty()) { | ||
if (initialEventId != null) { | ||
frozenTimelineEvents.where().equalTo(TimelineEventEntityFields.EVENT_ID, initialEventId).findFirst()?.displayIndex | ||
timelineEventEntities.where().equalTo(TimelineEventEntityFields.EVENT_ID, initialEventId).findFirst()?.displayIndex | ||
} else if (direction == Timeline.Direction.BACKWARDS) { | ||
frozenTimelineEvents.first(null)?.displayIndex | ||
timelineEventEntities.first(null)?.displayIndex | ||
} else { | ||
frozenTimelineEvents.last(null)?.displayIndex | ||
timelineEventEntities.last(null)?.displayIndex | ||
} | ||
} else if (direction == Timeline.Direction.FORWARDS) { | ||
builtEvents.first().displayIndex + 1 | ||
|
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.
is it worth holding onto the
Realm.WRITE_EXECUTOR.asCoroutineDispatcher()
instance?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 don't think so, at least realm is not doing that neither