Use inout for stop variables

This commit is contained in:
Max Radermacher 2026-03-16 13:49:10 -05:00 committed by GitHub
parent 966bf179d5
commit 6b9d30fd87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 41 additions and 97 deletions

View File

@ -2153,54 +2153,26 @@ public extension %(class_name)s {
// Records are not visited in any particular order.
class func anyEnumerate(
transaction: DBReadTransaction,
block: (%s, UnsafeMutablePointer<ObjCBool>) -> Void
) {
anyEnumerate(transaction: transaction, batched: false, block: block)
}
// Traverses all records.
// Records are not visited in any particular order.
class func anyEnumerate(
transaction: DBReadTransaction,
batched: Bool = false,
block: (%s, UnsafeMutablePointer<ObjCBool>) -> Void
) {
let batchSize = batched ? Batching.kDefaultBatchSize : 0
anyEnumerate(transaction: transaction, batchSize: batchSize, block: block)
}
// Traverses all records.
// Records are not visited in any particular order.
//
// If batchSize > 0, the enumeration is performed in autoreleased batches.
class func anyEnumerate(
transaction: DBReadTransaction,
batchSize: UInt,
block: (%s, UnsafeMutablePointer<ObjCBool>) -> Void
block: (%s) -> Void,
) {
let cursor = %s.grdbFetchCursor(transaction: transaction)
Batching.loop(batchSize: batchSize,
loopBlock: { stop in
do {
guard let value = try cursor.next() else {
stop.pointee = true
return
}
block(value, stop)
} catch let error {
owsFailDebug("Couldn't fetch model: \\(error)")
}
})
do {
while let value = try cursor.next() {
block(value)
}
} catch let error {
owsFailDebug("Couldn't fetch model: \\(error)")
}
}
""" % (
(str(clazz.name),) * 4
(str(clazz.name),) * 2
)
swift_body += """
// Does not order the results.
class func anyFetchAll(transaction: DBReadTransaction) -> [%s] {
var result = [%s]()
anyEnumerate(transaction: transaction) { (model, _) in
anyEnumerate(transaction: transaction) { model in
result.append(model)
}
return result

View File

@ -145,7 +145,7 @@ enum OWSOrphanDataCleaner {
OWSReaction.anyEnumerate(transaction: transaction, batchingPreference: .batched()) { reaction, stop in
if Task.isCancelled {
stop.pointee = true
stop = true
return
}
if !allInteractionIds.contains(reaction.uniqueMessageId) {
@ -156,7 +156,7 @@ enum OWSOrphanDataCleaner {
TSMention.anyEnumerate(transaction: transaction, batchingPreference: .batched()) { mention, stop in
if Task.isCancelled {
stop.pointee = true
stop = true
return
}
if !allInteractionIds.contains(mention.uniqueMessageId) {

View File

@ -92,7 +92,7 @@ public class GroupV2UpdatesImpl: GroupV2Updates {
groupSecretParams: groupSecretParams,
lastRefreshDate: nil,
)
stop.pointee = true
stop = true
return
}

View File

@ -4993,50 +4993,22 @@ public extension TSInteraction {
// Records are not visited in any particular order.
class func anyEnumerate(
transaction: DBReadTransaction,
block: (TSInteraction, UnsafeMutablePointer<ObjCBool>) -> Void
) {
anyEnumerate(transaction: transaction, batched: false, block: block)
}
// Traverses all records.
// Records are not visited in any particular order.
class func anyEnumerate(
transaction: DBReadTransaction,
batched: Bool = false,
block: (TSInteraction, UnsafeMutablePointer<ObjCBool>) -> Void
) {
let batchSize = batched ? Batching.kDefaultBatchSize : 0
anyEnumerate(transaction: transaction, batchSize: batchSize, block: block)
}
// Traverses all records.
// Records are not visited in any particular order.
//
// If batchSize > 0, the enumeration is performed in autoreleased batches.
class func anyEnumerate(
transaction: DBReadTransaction,
batchSize: UInt,
block: (TSInteraction, UnsafeMutablePointer<ObjCBool>) -> Void
block: (TSInteraction) -> Void,
) {
let cursor = TSInteraction.grdbFetchCursor(transaction: transaction)
Batching.loop(batchSize: batchSize,
loopBlock: { stop in
do {
guard let value = try cursor.next() else {
stop.pointee = true
return
}
block(value, stop)
} catch let error {
owsFailDebug("Couldn't fetch model: \(error)")
}
})
do {
while let value = try cursor.next() {
block(value)
}
} catch let error {
owsFailDebug("Couldn't fetch model: \(error)")
}
}
// Does not order the results.
class func anyFetchAll(transaction: DBReadTransaction) -> [TSInteraction] {
var result = [TSInteraction]()
anyEnumerate(transaction: transaction) { (model, _) in
anyEnumerate(transaction: transaction) { model in
result.append(model)
}
return result

View File

@ -711,7 +711,7 @@ public enum DatabaseRecovery {
}
databaseStorage.write { tx in
TSInteraction.anyEnumerate(transaction: tx) { interaction, _ in
TSInteraction.anyEnumerate(transaction: tx) { interaction in
guard let message = interaction as? TSMessage else {
return
}

View File

@ -5081,7 +5081,7 @@ public class GRDBSchemaMigrator {
}
migrator.registerMigration(.dataMigration_groupIdMapping) { transaction in
TSThread.anyEnumerate(transaction: transaction) { (thread: TSThread, _: UnsafeMutablePointer<ObjCBool>) in
TSThread.anyEnumerate(transaction: transaction) { thread, _ in
guard let groupThread = thread as? TSGroupThread else {
return
}
@ -5118,7 +5118,7 @@ public class GRDBSchemaMigrator {
groupThread.update(with: newGroupModel, transaction: transaction)
} catch {
thrownError = error
stop.pointee = true
stop = true
}
}
return thrownError.map { .failure($0) } ?? .success(())
@ -5211,7 +5211,7 @@ public class GRDBSchemaMigrator {
migrator.registerMigration(.dataMigration_moveToThreadAssociatedData) { transaction in
var thrownError: Error?
TSThread.anyEnumerate(transaction: transaction) { (thread, stop: UnsafeMutablePointer<ObjCBool>) in
TSThread.anyEnumerate(transaction: transaction) { thread, stop in
do {
try ThreadAssociatedData(
threadUniqueId: thread.uniqueId,
@ -5223,7 +5223,7 @@ public class GRDBSchemaMigrator {
).insert(transaction.database)
} catch {
thrownError = error
stop.pointee = true
stop = true
}
}
return thrownError.map { .failure($0) } ?? .success(())

View File

@ -216,7 +216,7 @@ public extension SDSCodableModel {
static func anyEnumerate(
transaction: DBReadTransaction,
batchingPreference: BatchingPreference = .unbatched,
block: (Self, UnsafeMutablePointer<ObjCBool>) -> Void,
block: (Self, inout Bool) -> Void,
) {
SDSCodableModelDatabaseInterfaceImpl().enumerateModels(
modelType: Self.self,
@ -232,7 +232,7 @@ public extension SDSCodableModel {
transaction: DBReadTransaction,
sql: String,
arguments: StatementArguments,
block: (Self, UnsafeMutablePointer<ObjCBool>) -> Void,
block: (Self, inout Bool) -> Void,
) {
SDSCodableModelDatabaseInterfaceImpl().enumerateModels(
modelType: Self.self,

View File

@ -16,7 +16,7 @@ extension SDSCodableModelDatabaseInterfaceImpl {
modelType: Model.Type,
transaction: DBReadTransaction,
batchingPreference: BatchingPreference,
block: (Model, UnsafeMutablePointer<ObjCBool>) -> Void,
block: (Model, inout Bool) -> Void,
) {
let batchSize = batchSize(batchingPreference: batchingPreference)
enumerateModels(
@ -36,7 +36,7 @@ extension SDSCodableModelDatabaseInterfaceImpl {
sql: String,
arguments: StatementArguments,
batchingPreference: BatchingPreference,
block: (Model, UnsafeMutablePointer<ObjCBool>) -> Void,
block: (Model, inout Bool) -> Void,
) {
let batchSize = batchSize(batchingPreference: batchingPreference)
enumerateModels(
@ -68,7 +68,7 @@ extension SDSCodableModelDatabaseInterfaceImpl {
sql: String? = nil,
arguments: StatementArguments? = nil,
batchSize: UInt,
block: (Model, UnsafeMutablePointer<ObjCBool>) -> Void,
block: (Model, inout Bool) -> Void,
) {
failIfThrows {
var recordCursor: RecordCursor<Model>
@ -84,11 +84,11 @@ extension SDSCodableModelDatabaseInterfaceImpl {
try Batching.loop(batchSize: batchSize) { stop in
guard let value = try recordCursor.next() else {
stop.pointee = true
stop = true
return
}
value.anyDidEnumerateOne(transaction: transaction)
block(value, stop)
block(value, &stop)
}
}
}

View File

@ -74,7 +74,7 @@ protocol SDSCodableModelDatabaseInterface {
modelType: Model.Type,
transaction: DBReadTransaction,
batchingPreference: BatchingPreference,
block: @escaping (Model, UnsafeMutablePointer<ObjCBool>) -> Void,
block: @escaping (Model, inout Bool) -> Void,
)
/// Traverse all records, in no particular order.
@ -84,7 +84,7 @@ protocol SDSCodableModelDatabaseInterface {
sql: String,
arguments: StatementArguments,
batchingPreference: BatchingPreference,
block: @escaping (Model, UnsafeMutablePointer<ObjCBool>) -> Void,
block: @escaping (Model, inout Bool) -> Void,
)
}

View File

@ -15,22 +15,22 @@ public enum Batching {
// autoreleasepool is used.
public static func loop(
batchSize: UInt,
loopBlock: (UnsafeMutablePointer<ObjCBool>) throws -> Void,
loopBlock: (_ stop: inout Bool) throws -> Void,
) rethrows {
var stop: ObjCBool = false
var stop = false
guard batchSize > 0 else {
// No batching.
while !stop.boolValue {
while !stop {
try loopBlock(&stop)
}
return
}
// With batching.
while !stop.boolValue {
while !stop {
try autoreleasepool {
for _ in 0..<batchSize {
guard !stop.boolValue else {
guard !stop else {
return
}
try loopBlock(&stop)