Skip to content

Commit

Permalink
TH2-1077 change get nearest message to get current batch
Browse files Browse the repository at this point in the history
  • Loading branch information
Paleontolog committed Nov 25, 2020
1 parent 6b767d7 commit 92c8578
Showing 1 changed file with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,30 @@ class SearchMessagesHandler(
timelineDirection: TimeRelation
): StoredMessageId? {
var daysChecking = 2
var isCurrentDay = true
var timestamp = startTimestamp
var messageId: StoredMessageId? = null
while (messageId == null && daysChecking >= 0) {
messageId = cradle.getFirstMessageIdSuspend(
timestamp,
stream,
direction,
timelineDirection
// with TimeRelation.BEFORE we always select current batch
if (isCurrentDay)
TimeRelation.BEFORE
else
timelineDirection
)
daysChecking -= 1
isCurrentDay = false
timestamp = nextDay(timestamp, timelineDirection)
}
return messageId
}

private suspend fun initStreamMessageIdMap(request: MessageSearchRequest):
MutableMap<Pair<String, Direction>, StoredMessageId?> {
var timestamp = chooseStartTimestamp(request)
val timestamp = chooseStartTimestamp(request)

return mutableMapOf<Pair<String, Direction>, StoredMessageId?>().apply {
for (stream in request.stream ?: emptyList()) {
Expand Down Expand Up @@ -235,18 +241,6 @@ class SearchMessagesHandler(
.toList()
}

private suspend fun getNewStartId(
messages: List<StoredMessage>,
timelineDirection: TimeRelation
): StoredMessageId? {
if (messages.isEmpty()) return null
return if (timelineDirection == TimeRelation.AFTER) {
messages.last()
} else {
messages.first()
}.id
}

private suspend fun pullMoreMerged(
streamMessageIndexMap: MutableMap<Pair<String, Direction>, StoredMessageId?>,
timelineDirection: TimeRelation,
Expand All @@ -260,7 +254,7 @@ class SearchMessagesHandler(
perStreamLimit,
timelineDirection
).let {
streamMessageIndexMap[stream] = getNewStartId(it, timelineDirection)
streamMessageIndexMap[stream] = if (it.isNotEmpty()) it.last().id else null
it
}
}
Expand Down

0 comments on commit 92c8578

Please sign in to comment.