mirror of https://github.com/oxen-io/session-ios
WIP: migration / autoincrement logic
TODO: -[ ] contact offer -[ ] verify all paths that utilized timestampForSorting, e.g. make sure SN appear before the message they affect, etc. -[x] Monotonic ID -[x] New extension which sorts by id -[x] Migration -[ ] batch migration?pull/1/head
parent
ae668ceca9
commit
a60d8eb161
@ -0,0 +1,40 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class OWS110SortIdMigration: OWSDatabaseMigration {
|
||||
// increment a similar constant for each migration.
|
||||
@objc
|
||||
class func migrationId() -> String {
|
||||
return "110"
|
||||
}
|
||||
|
||||
override public func runUp(completion: @escaping OWSDatabaseMigrationCompletion) {
|
||||
Logger.debug("")
|
||||
|
||||
// TODO batch this?
|
||||
self.dbReadWriteConnection().readWrite { transaction in
|
||||
guard let legacySorting: YapDatabaseAutoViewTransaction = transaction.extension(TSMessageDatabaseViewExtensionName_Legacy) as? YapDatabaseAutoViewTransaction else {
|
||||
owsFailDebug("legacySorting was unexpectedly nil")
|
||||
return
|
||||
}
|
||||
|
||||
legacySorting.enumerateGroups { group, _ in
|
||||
legacySorting.enumerateKeysAndObjects(inGroup: group) { (_, _, object, _, _) in
|
||||
guard let interaction = object as? TSInteraction else {
|
||||
owsFailDebug("unexpected object: \(type(of: object))")
|
||||
return
|
||||
}
|
||||
|
||||
interaction.saveNextSortId(transaction: transaction)
|
||||
Logger.debug("thread: \(interaction.uniqueThreadId), timestampForSorting:\(interaction.timestampForSorting()), sortId: \(interaction.sortId)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
completion()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@objc
|
||||
public class SSKIncrementingIdFinder: NSObject {
|
||||
|
||||
private static let collectionName = "IncrementingIdCollection"
|
||||
|
||||
@objc
|
||||
public class func nextId(key: String, transaction: YapDatabaseReadWriteTransaction) -> UInt64 {
|
||||
let previousId: UInt64 = transaction.object(forKey: key, inCollection: collectionName) as? UInt64 ?? 0
|
||||
let nextId: UInt64 = previousId + 1
|
||||
|
||||
transaction.setObject(nextId, forKey: key, inCollection: collectionName)
|
||||
Logger.debug("key: \(key) nextId: \(nextId)")
|
||||
return nextId
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue