Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 105058f

Browse files
committed
BatchOperation: set "yield allowed" after every 500 operations
1 parent 1accbfd commit 105058f

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

lib/src/main/kotlin/at/bitfire/ical4android/BatchOperation.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ import java.util.logging.Logger
1919
class BatchOperation(
2020
private val providerClient: ContentProviderClient
2121
) {
22-
22+
23+
companion object {
24+
25+
/** Maximum number of operations per yield point. Taken from SQLiteContentProvider, which most content providers
26+
* are based on. */
27+
const val MAX_OPERATIONS_PER_YIELD_POINT = 500
28+
29+
}
30+
2331
private val logger = Logger.getLogger(javaClass.name)
2432

2533
private val queue = LinkedList<CpoBuilder>()
@@ -133,6 +141,7 @@ class BatchOperation(
133141
* 2. If a back reference points to a row outside of start/end,
134142
* replace it by the actual result, which has already been calculated. */
135143

144+
var currentIdx = 0
136145
for (cpoBuilder in queue.subList(start, end)) {
137146
for ((backrefKey, backref) in cpoBuilder.valueBackrefs) {
138147
val originalIdx = backref.originalIndex
@@ -148,6 +157,9 @@ class BatchOperation(
148157
backref.setIndex(originalIdx - start)
149158
}
150159

160+
if (currentIdx++.mod(MAX_OPERATIONS_PER_YIELD_POINT) == 0)
161+
cpoBuilder.withYieldAllowed()
162+
151163
cpo += cpoBuilder.build()
152164
}
153165
return cpo
@@ -158,7 +170,7 @@ class BatchOperation(
158170
/** index of the referenced row in the original, nonsplitted transaction */
159171
val originalIndex: Int
160172
) {
161-
/** overriden index, i.e. index within the splitted transaction */
173+
/** overridden index, i.e. index within the splitted transaction */
162174
private var index: Int? = null
163175

164176
/**
@@ -182,8 +194,8 @@ class BatchOperation(
182194
* value back references.
183195
*/
184196
class CpoBuilder private constructor(
185-
val uri: Uri,
186-
val type: Type
197+
val uri: Uri,
198+
val type: Type
187199
) {
188200

189201
enum class Type { INSERT, UPDATE, DELETE }
@@ -203,6 +215,8 @@ class BatchOperation(
203215
val values = mutableMapOf<String, Any?>()
204216
val valueBackrefs = mutableMapOf<String, BackReference>()
205217

218+
private var yieldAllowed = false
219+
206220

207221
fun withSelection(select: String, args: Array<String>): CpoBuilder {
208222
selection = select
@@ -226,6 +240,10 @@ class BatchOperation(
226240
return this
227241
}
228242

243+
fun withYieldAllowed() {
244+
yieldAllowed = true
245+
}
246+
229247

230248
fun build(): ContentProviderOperation {
231249
val builder = when (type) {
@@ -242,6 +260,9 @@ class BatchOperation(
242260
for ((key, backref) in valueBackrefs)
243261
builder.withValueBackReference(key, backref.getIndex())
244262

263+
if (yieldAllowed)
264+
builder.withYieldAllowed(true)
265+
245266
return builder.build()
246267
}
247268

0 commit comments

Comments
 (0)