-
Notifications
You must be signed in to change notification settings - Fork 19
Concurrent Connections #145
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
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
622a89b
add unit tests. set jdbc to WAL
stevensJourney 24afc55
wip read and write locks
stevensJourney 651482e
wip android notifications
stevensJourney 8f5ccf0
wip android jdbc
stevensJourney 81822e9
wip share jdbc
stevensJourney c57f443
cleanup
stevensJourney 30d420d
wip remove interop
stevensJourney 2701d87
update core
stevensJourney f88a4bc
wip
stevensJourney 747fbd9
Improve drivers for ios
stevensJourney f069a62
remove unused core interop
stevensJourney a28e11c
Merge remote-tracking branch 'origin/main' into concurrent
stevensJourney 611b806
cleanup transactions
stevensJourney f8bf9c9
Gradle task for Android JNI. Catch Swift transaction errors
stevensJourney 4242e9a
lint
stevensJourney d81122a
proguard rules
stevensJourney dacf29f
Don't link sqlite from dependencies
simolus3 73d0866
Don't include cinterop at all
simolus3 eea8fc4
Get static driver test working
simolus3 5dd5f28
Link core framework again
simolus3 dea89c0
Back to upstream dependencies
simolus3 4aee79d
Some build docs
simolus3 458c84d
Merge pull request #147 from powersync-ja/native-static-link-sqlite
simolus3 522a909
cleanup requery
stevensJourney afd82e7
cleanup readme
stevensJourney 50ed399
Merge remote-tracking branch 'origin/main' into concurrent
stevensJourney 15729e2
update connection pool to use channel implementation
stevensJourney f9a2218
cleanup
stevensJourney 394507b
Merge remote-tracking branch 'origin/main' into concurrent
stevensJourney 824e559
update global locks
stevensJourney 0a9bae6
add tests for connection pool close
stevensJourney 6b83087
update readme
stevensJourney 7853359
cleanup
stevensJourney 814b52d
test
stevensJourney bc78c36
Fix NPE when compiling sqlite3 for iOS
simolus3 c4f88f7
Add unit tests for db directory
stevensJourney 91de8c0
cleanup
stevensJourney dfa419d
update changelog
stevensJourney c27f39f
Update core/README.md
stevensJourney 58f5890
Update core/src/commonMain/kotlin/com/powersync/db/internal/PowerSync…
stevensJourney fe1cb3f
Add changelog entry for removing requery dependnecy. Cleanup gradle s…
stevensJourney 7e9e537
Add readLock check to closed database test. Assert no nested transact…
stevensJourney 0548d62
Ensure read connections are readonly
stevensJourney 17913ba
fix test
stevensJourney 47a2700
test: print exceptions in setup
stevensJourney d584cd2
test
stevensJourney 318df0a
add logs for tests
stevensJourney 5f83328
change read only setup for ios
stevensJourney 11ca77f
remove test
stevensJourney 6603ad8
Hack to lookup bundle earlier
simolus3 11bd61e
remove test code
stevensJourney fdc340a
remove unecessary log config in build script
stevensJourney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,4 +93,133 @@ class AndroidDatabaseTest { | |
query.cancel() | ||
} | ||
} | ||
|
||
@Test | ||
fun testConcurrentReads() = | ||
runTest { | ||
database.execute( | ||
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", | ||
listOf( | ||
"steven", | ||
"[email protected]", | ||
), | ||
) | ||
|
||
val pausedTransaction = CompletableDeferred<Unit>() | ||
val transactionItemCreated = CompletableDeferred<Unit>() | ||
// Start a long running writeTransaction | ||
val transactionJob = | ||
async { | ||
database.writeTransaction { tx -> | ||
// Create another user | ||
// External readers should not see this user while the transaction is open | ||
tx.execute( | ||
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", | ||
listOf( | ||
"steven", | ||
"[email protected]", | ||
), | ||
) | ||
|
||
transactionItemCreated.complete(Unit) | ||
|
||
// Block this transaction until we free it | ||
runBlocking { | ||
pausedTransaction.await() | ||
} | ||
} | ||
} | ||
|
||
// Make sure to wait for the item to have been created in the transaction | ||
transactionItemCreated.await() | ||
// Try and read while the write transaction is busy | ||
val result = database.getAll("SELECT * FROM users") { UserRow.from(it) } | ||
// The transaction is not commited yet, we should only read 1 user | ||
assertEquals(result.size, 1) | ||
|
||
// Let the transaction complete | ||
pausedTransaction.complete(Unit) | ||
transactionJob.await() | ||
|
||
val afterTx = database.getAll("SELECT * FROM users") { UserRow.from(it) } | ||
assertEquals(afterTx.size, 2) | ||
} | ||
|
||
@Test | ||
fun transactionReads() = | ||
runTest { | ||
database.execute( | ||
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", | ||
listOf( | ||
"steven", | ||
"[email protected]", | ||
), | ||
) | ||
|
||
database.writeTransaction { tx -> | ||
val userCount = | ||
tx.getAll("SELECT COUNT(*) as count FROM users") { cursor -> cursor.getLong(0)!! } | ||
assertEquals(userCount[0], 1) | ||
|
||
tx.execute( | ||
"INSERT INTO users (id, name, email) VALUES (uuid(), ?, ?)", | ||
listOf( | ||
"steven", | ||
"[email protected]", | ||
), | ||
) | ||
|
||
// Getters inside the transaction should be able to see the latest update | ||
val userCount2 = | ||
tx.getAll("SELECT COUNT(*) as count FROM users") { cursor -> cursor.getLong(0)!! } | ||
assertEquals(userCount2[0], 2) | ||
} | ||
} | ||
|
||
@Test | ||
fun openDBWithDirectory() = | ||
runTest { | ||
val tempDir = | ||
InstrumentationRegistry | ||
.getInstrumentation() | ||
.targetContext.cacheDir.canonicalPath | ||
val dbFilename = "testdb" | ||
|
||
val db = | ||
PowerSyncDatabase( | ||
factory = DatabaseDriverFactory(InstrumentationRegistry.getInstrumentation().targetContext), | ||
schema = Schema(UserRow.table), | ||
dbDirectory = tempDir, | ||
dbFilename = dbFilename, | ||
) | ||
|
||
val path = db.get("SELECT file FROM pragma_database_list;") { it.getString(0)!! } | ||
|
||
assertEquals(path.contains(tempDir), true) | ||
|
||
db.close() | ||
} | ||
|
||
@Test | ||
fun readConnectionsReadOnly() = | ||
runTest { | ||
val exception = | ||
assertThrows(PowerSyncException::class.java) { | ||
// This version of assertThrows does not support suspending functions | ||
runBlocking { | ||
database.getOptional( | ||
""" | ||
INSERT INTO | ||
users (id, name, email) | ||
VALUES | ||
(uuid(), ?, ?) | ||
RETURNING * | ||
""".trimIndent(), | ||
parameters = listOf("steven", "[email protected]"), | ||
) {} | ||
} | ||
} | ||
// The exception messages differ slightly between drivers | ||
assertEquals(exception.message!!.contains("write a readonly database"), true) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
binaries/ | ||
# Required for the JDBC SQLite driver, but should not be commiteed | ||
src/androidMain/jni | ||
|
||
testdb-* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
# PowerSync core module | ||
|
||
The PowerSync core module provides the core functionality for the PowerSync Kotlin Multiplatform SDK. | ||
The PowerSync core module provides the core functionality for the PowerSync Kotlin Multiplatform | ||
SDK. | ||
|
||
## Structure | ||
|
||
This is a Kotlin Multiplatform project targeting Android, iOS platforms, with the following structure: | ||
This is a Kotlin Multiplatform project targeting Android, iOS platforms, with the following | ||
structure: | ||
|
||
- `commonMain` - Shared code for all targets, which includes the `PowerSyncBackendConnector` interface and `PowerSyncBuilder` for building a `PowerSync` instance. It also defines | ||
- `commonMain` - Shared code for all targets, which includes the `PowerSyncBackendConnector` | ||
interface and `PowerSyncBuilder` for building a `PowerSync` instance. It also defines | ||
the `DatabaseDriverFactory` class to be implemented in each platform. | ||
- `androidMain` - Android specific code, which includes a implementation of `DatabaseDriverFactory` class that creates an instance of `app.cash.sqldelight.driver.android.AndroidSqliteDriver` using | ||
a `io.requery.android.database.sqlite.RequerySQLiteOpenHelperFactory`. It also includes native SQLite bindings for Android. | ||
- `iosMain` - iOS specific code, which includes a implementation of `DatabaseDriverFactory` class that creates an instance of `app.cash.sqldelight.driver.native.NativeSqliteDriver` and also sets up native SQLite bindings for iOS. | ||
- `commonJava` - Common Java code including a Java SQLite driver using | ||
the [Xerial JDBC Driver](https://github.com/xerial/sqlite-jdbc). This is used by both the Android | ||
and JVM drivers. | ||
- `androidMain` - Android specific code, which includes an implementation of | ||
`DatabaseDriverFactory`. | ||
- `jvmMain` - JVM specific code which includes an implementation of `DatabaseDriverFactory`. | ||
- `iosMain` - iOS specific code, which includes am implementation of `DatabaseDriverFactory` class | ||
that creates an instance of `app.cash.sqldelight.driver.native.NativeSqliteDriver` and also sets | ||
up native SQLite bindings for iOS. | ||
|
||
## Note on SQLDelight | ||
|
||
The PowerSync core module, internally makes use of [SQLDelight](https://cashapp.github.io/sqldelight) for it database API and typesafe database query generation. | ||
The PowerSync core module, internally makes use | ||
of [SQLDelight](https://sqldelight.github.io/sqldelight/latest/) for it database API and typesafe database | ||
query generation. | ||
|
||
The PowerSync core module does not currently support integrating with SQLDelight from client applications. | ||
The PowerSync core module does not currently support integrating with SQLDelight from client | ||
applications. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.