Skip to content

Do not split transactions when uploading data #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.0.0-BETA31

* Added helpers for Attachment syncing.
* Fix `getNextCrudTransaction()` only returning a single item.

## 1.0.0-BETA30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,25 @@ class DatabaseTest {
val count = database.get("SELECT COUNT(*) from people") { it.getLong(0)!! }
count shouldBe 1
}

@Test
fun testCrudTransaction() =
databaseTest {
database.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("a", "a@example.org"))

database.writeTransaction {
it.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("b", "b@example.org"))
it.execute("INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", listOf("c", "c@example.org"))
}

var transaction = database.getNextCrudTransaction()
transaction!!.crud shouldHaveSize 1
transaction.complete(null)

transaction = database.getNextCrudTransaction()
transaction!!.crud shouldHaveSize 2
transaction.complete(null)

database.getNextCrudTransaction() shouldBe null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ internal interface BucketStorage {

fun nextCrudItem(transaction: PowerSyncTransaction): CrudEntry?

fun getCrudItemsByTransactionId(
transactionId: Int,
transaction: PowerSyncTransaction,
): List<CrudEntry>

suspend fun hasCrud(): Boolean

fun hasCrud(transaction: PowerSyncTransaction): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,24 @@ internal class BucketStorageImpl(
return id ?: throw IllegalStateException("Client ID not found")
}

override suspend fun nextCrudItem(): CrudEntry? = db.getOptional(sql = nextCrudQuery, mapper = nextCrudMapper)
override suspend fun nextCrudItem(): CrudEntry? = db.getOptional(sql = nextCrudQuery, mapper = crudEntryMapper)

override fun nextCrudItem(transaction: PowerSyncTransaction): CrudEntry? =
transaction.getOptional(sql = nextCrudQuery, mapper = nextCrudMapper)
transaction.getOptional(sql = nextCrudQuery, mapper = crudEntryMapper)

override fun getCrudItemsByTransactionId(
transactionId: Int,
transaction: PowerSyncTransaction,
): List<CrudEntry> =
transaction.getAll(
sql = transactionCrudQuery,
parameters = listOf(transactionId),
mapper = crudEntryMapper,
)

private val nextCrudQuery = "SELECT id, tx_id, data FROM ${InternalTable.CRUD} ORDER BY id ASC LIMIT 1"
private val nextCrudMapper: (SqlCursor) -> CrudEntry = { cursor ->
private val transactionCrudQuery = "SELECT id, tx_id, data FROM ${InternalTable.CRUD} WHERE tx_id = ? ORDER BY id ASC"
private val crudEntryMapper: (SqlCursor) -> CrudEntry = { cursor ->
CrudEntry.fromRow(
CrudRow(
id = cursor.getString(0)!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,10 @@ internal class PowerSyncDatabaseImpl(
if (txId == null) {
listOf(entry)
} else {
transaction.getAll("SELECT id, tx_id, data FROM ps_crud ORDER BY id ASC LIMIT 1") {
CrudEntry.fromRow(
CrudRow(
id = it.getString("id"),
data = it.getString("data"),
txId = it.getLongOptional("tx_id")?.toInt(),
),
)
}
bucketStorage.getCrudItemsByTransactionId(
transactionId = txId,
transaction = transaction,
)
}

return@readTransaction CrudTransaction(
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ development=true
RELEASE_SIGNING_ENABLED=true
# Library config
GROUP=com.powersync
LIBRARY_VERSION=1.0.0-BETA30
LIBRARY_VERSION=1.0.0-BETA31
GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git
# POM
POM_URL=https://github.com/powersync-ja/powersync-kotlin/
Expand Down