Skip to content

Commit 328c3e8

Browse files
committed
Add += operator as shortcut
1 parent 7703992 commit 328c3e8

File tree

7 files changed

+55
-50
lines changed

7 files changed

+55
-50
lines changed

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/BatchOperationTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class BatchOperationTest {
2323
val maxSize = 100
2424
every { provider.applyBatch(match { it.size > maxSize }) } throws TransactionTooLargeException()
2525

26-
val op = BatchOperation(provider, null)
26+
val batch = BatchOperation(provider, null)
2727
repeat(4*maxSize) {
28-
op.enqueue(BatchOperation.CpoBuilder.newInsert("test://".toUri()))
28+
batch += BatchOperation.CpoBuilder.newInsert("test://".toUri())
2929
}
30-
op.commit()
30+
batch.commit()
3131

3232
// one too large batch (with 400 operations) +
3333
// then two still too large batches with 200 operations each +
@@ -41,9 +41,9 @@ class BatchOperationTest {
4141

4242
every { provider.applyBatch(any()) } throws TransactionTooLargeException()
4343

44-
val op = BatchOperation(provider, null)
45-
op.enqueue(BatchOperation.CpoBuilder.newInsert("test://".toUri()))
46-
op.commit()
44+
val batch = BatchOperation(provider, null)
45+
batch += BatchOperation.CpoBuilder.newInsert("test://".toUri())
46+
batch.commit()
4747
}
4848

4949
}

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/CalendarBatchOperationTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ class CalendarBatchOperationTest {
5252

5353
@Test
5454
fun testCalendarProvider_OperationsPerYieldPoint_501() {
55-
val builder = CalendarBatchOperation(provider)
55+
val batch = CalendarBatchOperation(provider)
5656

5757
// 501 operations should succeed with CalendarBatchOperation
5858
repeat(501) { idx ->
59-
builder.enqueue(
60-
BatchOperation.CpoBuilder.newInsert(Events.CONTENT_URI.asSyncAdapter(testAccount))
61-
.withValue(Events.TITLE, "Event $idx"))
59+
batch += BatchOperation.CpoBuilder.newInsert(Events.CONTENT_URI.asSyncAdapter(testAccount))
60+
.withValue(Events.TITLE, "Event $idx")
6261
}
63-
builder.commit()
62+
batch.commit()
6463
}
6564

6665
}

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/ContactsBatchOperationTest.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,28 @@ class ContactsBatchOperationTest {
5050

5151
@Test(expected = LocalStorageException::class)
5252
fun testContactsProvider_OperationsPerYieldPoint_500_WithoutMax() {
53-
val builder = BatchOperation(provider, maxOperationsPerYieldPoint = null)
53+
val batch = BatchOperation(provider, maxOperationsPerYieldPoint = null)
5454

5555
// 500 operations should fail with BatchOperation(maxOperationsPerYieldPoint = null) (max. 499)
5656
repeat(500) { idx ->
57-
builder.enqueue(
58-
BatchOperation.CpoBuilder.newInsert(ContactsContract.RawContacts.CONTENT_URI)
59-
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, testAccount.type)
60-
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, testAccount.name)
61-
)
57+
batch += BatchOperation.CpoBuilder.newInsert(ContactsContract.RawContacts.CONTENT_URI)
58+
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, testAccount.type)
59+
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, testAccount.name)
6260
}
63-
builder.commit()
61+
batch.commit()
6462
}
6563

6664
@Test
6765
fun testContactsProvider_OperationsPerYieldPoint_501() {
68-
val builder = ContactsBatchOperation(provider)
66+
val batch = ContactsBatchOperation(provider)
6967

7068
// 501 operations should succeed with ContactsBatchOperation
7169
repeat(501) { idx ->
72-
builder.enqueue(
73-
BatchOperation.CpoBuilder.newInsert(ContactsContract.RawContacts.CONTENT_URI)
74-
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, testAccount.type)
75-
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, testAccount.name)
76-
)
70+
batch += BatchOperation.CpoBuilder.newInsert(ContactsContract.RawContacts.CONTENT_URI)
71+
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, testAccount.type)
72+
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, testAccount.name)
7773
}
78-
builder.commit()
74+
batch.commit()
7975
}
8076

8177
}

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/JtxBatchOperationTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class JtxBatchOperationTest {
4747

4848
@Test
4949
fun testJtxBoard_OperationsPerYieldPoint_501() {
50-
val builder = JtxBatchOperation(provider)
50+
val batch = JtxBatchOperation(provider)
5151
val uri = JtxCollection.create(testAccount, provider, ContentValues().apply {
5252
put(JtxContract.JtxCollection.ACCOUNT_NAME, testAccount.name)
5353
put(JtxContract.JtxCollection.ACCOUNT_TYPE, testAccount.type)
@@ -58,13 +58,11 @@ class JtxBatchOperationTest {
5858
try {
5959
// 501 operations should succeed with JtxBatchOperation
6060
repeat(501) { idx ->
61-
builder.enqueue(
62-
BatchOperation.CpoBuilder.newInsert(JtxContract.JtxICalObject.CONTENT_URI.asSyncAdapter(testAccount))
63-
.withValue(JtxContract.JtxICalObject.ICALOBJECT_COLLECTIONID, collectionId)
64-
.withValue(JtxContract.JtxICalObject.SUMMARY, "Entry $idx")
65-
)
61+
batch += BatchOperation.CpoBuilder.newInsert(JtxContract.JtxICalObject.CONTENT_URI.asSyncAdapter(testAccount))
62+
.withValue(JtxContract.JtxICalObject.ICALOBJECT_COLLECTIONID, collectionId)
63+
.withValue(JtxContract.JtxICalObject.SUMMARY, "Entry $idx")
6664
}
67-
builder.commit()
65+
batch.commit()
6866
} finally {
6967
val collection = JtxCollection<JtxICalObject>(testAccount, provider, mockk(), collectionId)
7068
collection.delete()

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/TasksBatchOperationTest.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,33 @@ class TasksBatchOperationTest(
2121

2222
@Test(expected = LocalStorageException::class)
2323
fun testTasksProvider_OperationsPerYieldPoint_500_WithoutMax() {
24-
val builder = BatchOperation(provider.client, maxOperationsPerYieldPoint = null)
24+
val batch = BatchOperation(provider.client, maxOperationsPerYieldPoint = null)
2525
val taskList = TestTaskList.Companion.create(testAccount, provider)
2626
try {
2727
// 500 operations should fail with BatchOperation(maxOperationsPerYieldPoint = null) (max. 499)
2828
repeat(500) { idx ->
29-
builder.enqueue(
30-
BatchOperation.CpoBuilder.newInsert(provider.tasksUri())
31-
.withValue(TaskContract.Tasks.LIST_ID, taskList.id)
32-
.withValue(TaskContract.Tasks.TITLE, "Task $idx")
33-
)
29+
batch += BatchOperation.CpoBuilder.newInsert(provider.tasksUri())
30+
.withValue(TaskContract.Tasks.LIST_ID, taskList.id)
31+
.withValue(TaskContract.Tasks.TITLE, "Task $idx")
3432
}
35-
builder.commit()
33+
batch.commit()
3634
} finally {
3735
taskList.delete()
3836
}
3937
}
4038

4139
@Test
4240
fun testTasksProvider_OperationsPerYieldPoint_501() {
43-
val builder = TasksBatchOperation(provider.client)
41+
val batch = TasksBatchOperation(provider.client)
4442
val taskList = TestTaskList.Companion.create(testAccount, provider)
4543
try {
4644
// 501 operations should succeed with ContactsBatchOperation
4745
repeat(501) { idx ->
48-
builder.enqueue(
49-
BatchOperation.CpoBuilder.newInsert(provider.tasksUri())
50-
.withValue(TaskContract.Tasks.LIST_ID, taskList.id)
51-
.withValue(TaskContract.Tasks.TITLE, "Task $idx")
52-
)
46+
batch += BatchOperation.CpoBuilder.newInsert(provider.tasksUri())
47+
.withValue(TaskContract.Tasks.LIST_ID, taskList.id)
48+
.withValue(TaskContract.Tasks.TITLE, "Task $idx")
5349
}
54-
builder.commit()
50+
batch.commit()
5551
} finally {
5652
taskList.delete()
5753
}

lib/src/main/kotlin/at/bitfire/synctools/storage/BatchOperation.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,30 @@ open class BatchOperation internal constructor(
4343

4444
fun nextBackrefIdx() = queue.size
4545

46+
/**
47+
* Enqueues an operation to the current batch.
48+
*
49+
* @param operation operation to add
50+
* @return this batch (for chaining)
51+
*/
4652
fun enqueue(operation: CpoBuilder): BatchOperation {
4753
queue.add(operation)
4854
return this
4955
}
5056

51-
fun enqueueAll(operations: Iterable<CpoBuilder>) {
57+
/**
58+
* Shortcut for [enqueue].
59+
*/
60+
operator fun plusAssign(operation: CpoBuilder) {
61+
enqueue(operation)
62+
}
63+
64+
/**
65+
* Shortcut for [enqueue] of multiple operations.
66+
*/
67+
operator fun plusAssign(operations: Iterable<CpoBuilder>) {
5268
for (operation in operations)
53-
enqueue(operation)
69+
this += operation
5470
}
5571

5672
/**

lib/src/main/kotlin/at/bitfire/vcard4android/contactrow/ContactProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import android.content.ContentProviderClient
1010
import android.content.ContentValues
1111
import android.net.Uri
1212
import android.provider.ContactsContract.RawContacts
13+
import at.bitfire.synctools.storage.BatchOperation
1314
import at.bitfire.vcard4android.AndroidContact
1415
import at.bitfire.vcard4android.Contact
15-
import at.bitfire.synctools.storage.BatchOperation
1616
import java.util.logging.Level
1717
import java.util.logging.Logger
1818

@@ -97,7 +97,7 @@ class ContactProcessor(
9797
fun insertDataRows(dataRowUri: Uri, rawContactId: Long?, contact: Contact, batch: BatchOperation, readOnly: Boolean) {
9898
for (factory in dataRowBuilderFactories) {
9999
val builder = factory.newInstance(dataRowUri, rawContactId, contact, readOnly)
100-
batch.enqueueAll(builder.build())
100+
batch += builder.build()
101101
}
102102
}
103103

0 commit comments

Comments
 (0)