[WIP] Topic APIs

This commit is contained in:
Ali
2022-09-28 12:15:06 +02:00
parent d7d3b1b9cb
commit d01a7853fa
60 changed files with 4264 additions and 1861 deletions

View File

@@ -5,12 +5,19 @@ private func extractKey(_ key: ValueBoxKey) -> MessageIndex {
}
class MessageHistoryThreadsTable: Table {
struct ItemId: Hashable {
var peerId: PeerId
var threadId: Int64
}
static func tableSpec(_ id: Int32) -> ValueBoxTable {
return ValueBoxTable(id: id, keyType: .binary, compactValuesOnCreation: true)
}
private let sharedKey = ValueBoxKey(length: 8 + 8 + 4 + 4 + 4)
private(set) var updatedIds = Set<ItemId>()
override init(valueBox: ValueBox, table: ValueBoxTable, useCaches: Bool) {
super.init(valueBox: valueBox, table: table, useCaches: useCaches)
}
@@ -38,10 +45,12 @@ class MessageHistoryThreadsTable: Table {
func add(threadId: Int64, index: MessageIndex) {
self.valueBox.set(self.table, key: self.key(threadId: threadId, index: index, key: self.sharedKey), value: MemoryBuffer())
self.updatedIds.insert(ItemId(peerId: index.id.peerId, threadId: threadId))
}
func remove(threadId: Int64, index: MessageIndex) {
self.valueBox.remove(self.table, key: self.key(threadId: threadId, index: index, key: self.sharedKey), secure: false)
self.updatedIds.insert(ItemId(peerId: index.id.peerId, threadId: threadId))
}
func earlierIndices(threadId: Int64, peerId: PeerId, namespace: MessageId.Namespace, index: MessageIndex?, includeFrom: Bool, count: Int) -> [MessageIndex] {
@@ -82,6 +91,27 @@ class MessageHistoryThreadsTable: Table {
return indices
}
func getTop(peerId: PeerId, threadId: Int64, namespaces: Set<MessageId.Namespace>) -> MessageIndex? {
var maxIndex: MessageIndex?
for namespace in namespaces {
var namespaceIndex: MessageIndex?
self.valueBox.range(self.table, start: self.upperBound(threadId: threadId, peerId: peerId, namespace: namespace), end: self.lowerBound(threadId: threadId, peerId: peerId, namespace: namespace), keys: { key in
namespaceIndex = extractKey(key)
return false
}, limit: 1)
if let namespaceIndex = namespaceIndex {
if let maxIndexValue = maxIndex {
if namespaceIndex > maxIndexValue {
maxIndex = namespaceIndex
}
} else {
maxIndex = namespaceIndex
}
}
}
return maxIndex
}
func getMessageCountInRange(threadId: Int64, peerId: PeerId, namespace: MessageId.Namespace, lowerBound: MessageIndex?, upperBound: MessageIndex?) -> Int {
if let lowerBound = lowerBound {
precondition(lowerBound.id.namespace == namespace)
@@ -109,4 +139,10 @@ class MessageHistoryThreadsTable: Table {
}
return Int(self.valueBox.count(self.table, start: lowerBoundKey, end: upperBoundKey))
}
override func beforeCommit() {
super.beforeCommit()
self.updatedIds.removeAll()
}
}