History range deletion

This commit is contained in:
Ali
2021-11-23 16:31:35 +04:00
parent 385e3b95db
commit c2112fcfab
16 changed files with 468 additions and 346 deletions

View File

@@ -2904,7 +2904,7 @@ final class MessageHistoryTable: Table {
return (result, mediaRefs, count == 0 ? nil : lastIndex)
}
func fetch(peerId: PeerId, namespace: MessageId.Namespace, tag: MessageTags?, threadId: Int64?, from fromIndex: MessageIndex, includeFrom: Bool, to toIndex: MessageIndex, limit: Int) -> [IntermediateMessage] {
func fetch(peerId: PeerId, namespace: MessageId.Namespace, tag: MessageTags?, threadId: Int64?, from fromIndex: MessageIndex, includeFrom: Bool, to toIndex: MessageIndex, ignoreMessagesInTimestampRange: ClosedRange<Int32>?, limit: Int) -> [IntermediateMessage] {
precondition(fromIndex.id.peerId == toIndex.id.peerId)
precondition(fromIndex.id.namespace == toIndex.id.namespace)
var result: [IntermediateMessage] = []
@@ -2926,6 +2926,11 @@ final class MessageHistoryTable: Table {
localIncludeFrom = false
for index in sliceIndices {
if let ignoreMessagesInTimestampRange = ignoreMessagesInTimestampRange {
if ignoreMessagesInTimestampRange.contains(index.timestamp) {
continue
}
}
if let tag = tag {
if self.tagsTable.entryExists(tag: tag, index: index) {
indices.append(index)
@@ -2961,6 +2966,72 @@ final class MessageHistoryTable: Table {
} else {
indices = self.tagsTable.earlierIndices(tag: tag, peerId: peerId, namespace: namespace, index: fromIndex, includeFrom: includeFrom, count: limit)
}
for index in indices {
if let ignoreMessagesInTimestampRange = ignoreMessagesInTimestampRange {
if ignoreMessagesInTimestampRange.contains(index.timestamp) {
continue
}
}
if fromIndex < toIndex {
if index < fromIndex || index > toIndex {
continue
}
} else {
if index < toIndex || index > fromIndex {
continue
}
}
if let message = self.getMessage(index) {
result.append(message)
} else {
assertionFailure()
}
}
} else if ignoreMessagesInTimestampRange != nil {
var indices: [MessageIndex] = []
var startIndex = fromIndex
var localIncludeFrom = includeFrom
while true {
let startKey: ValueBoxKey
if localIncludeFrom && startIndex != MessageIndex.upperBound(peerId: peerId, namespace: namespace) {
if startIndex < toIndex {
startKey = self.key(startIndex).predecessor
} else {
startKey = self.key(startIndex).successor
}
} else {
startKey = self.key(startIndex)
}
var sliceIndices: [MessageIndex] = []
self.valueBox.range(self.table, start: startKey, end: self.key(toIndex), values: { key, value in
sliceIndices.append(extractKey(key))
return true
}, limit: limit)
if sliceIndices.isEmpty {
break
}
startIndex = sliceIndices[sliceIndices.count - 1]
localIncludeFrom = false
for index in sliceIndices {
if let ignoreMessagesInTimestampRange = ignoreMessagesInTimestampRange {
if ignoreMessagesInTimestampRange.contains(index.timestamp) {
continue
}
}
indices.append(index)
if indices.count >= limit {
break
}
}
if indices.count >= limit {
break
}
}
assert(Set(indices).count == indices.count)
for index in indices {
if fromIndex < toIndex {
if index < fromIndex || index > toIndex {