mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
refactor and cleanup [skip ci]
This commit is contained in:
51
submodules/Postbox/Sources/GlobalMessageIdsTable.swift
Normal file
51
submodules/Postbox/Sources/GlobalMessageIdsTable.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
import Foundation
|
||||
|
||||
final class GlobalMessageIdsTable: Table {
|
||||
static func tableSpec(_ id: Int32) -> ValueBoxTable {
|
||||
return ValueBoxTable(id: id, keyType: .int64, compactValuesOnCreation: false)
|
||||
}
|
||||
|
||||
private let seedConfiguration: SeedConfiguration
|
||||
|
||||
private let sharedKey = ValueBoxKey(length: 8)
|
||||
private let sharedBuffer = WriteBuffer()
|
||||
|
||||
init(valueBox: ValueBox, table: ValueBoxTable, seedConfiguration: SeedConfiguration) {
|
||||
self.seedConfiguration = seedConfiguration
|
||||
|
||||
super.init(valueBox: valueBox, table: table)
|
||||
}
|
||||
|
||||
private func key(_ id: Int32) -> ValueBoxKey {
|
||||
self.sharedKey.setInt64(0, value: Int64(id))
|
||||
return self.sharedKey
|
||||
}
|
||||
|
||||
func set(_ globalId: Int32, id: MessageId) {
|
||||
assert(id.namespace == 0)
|
||||
assert(id.peerId.namespace == 0 || id.peerId.namespace == 1)
|
||||
assert(self.seedConfiguration.globalMessageIdsPeerIdNamespaces.contains(GlobalMessageIdsNamespace(peerIdNamespace: id.peerId.namespace, messageIdNamespace: id.namespace)))
|
||||
|
||||
self.sharedBuffer.reset()
|
||||
var idPeerId: Int64 = id.peerId.toInt64()
|
||||
var idNamespace: Int32 = id.namespace
|
||||
self.sharedBuffer.write(&idPeerId, offset: 0, length: 8)
|
||||
self.sharedBuffer.write(&idNamespace, offset: 0, length: 4)
|
||||
self.valueBox.set(self.table, key: self.key(globalId), value: self.sharedBuffer)
|
||||
}
|
||||
|
||||
func get(_ globalId: Int32) -> MessageId? {
|
||||
if let value = self.valueBox.get(self.table, key: self.key(globalId)) {
|
||||
var idPeerId: Int64 = 0
|
||||
var idNamespace: Int32 = 0
|
||||
value.read(&idPeerId, offset: 0, length: 8)
|
||||
value.read(&idNamespace, offset: 0, length: 4)
|
||||
return MessageId(peerId: PeerId(idPeerId), namespace: idNamespace, id: globalId)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func remove(_ globalId: Int32) {
|
||||
self.valueBox.remove(self.table, key: self.key(globalId), secure: false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user