Skip to content

Commit

Permalink
Merge pull request #57 from qiaoyuang/main
Browse files Browse the repository at this point in the history
Add the new target linuxArm64 and optimize the performance of concurr…
  • Loading branch information
qiaoyuang authored Nov 6, 2023
2 parents 649bcc1 + af03cf8 commit ee2b759
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 28 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

### sqllin-dsl

* Add the new native target support: `linuxArm64`
* Add the new API `Database#suspendedScope`, it could be used to ensure concurrency safety([#55](https://github.com/ctripcorp/SQLlin/pull/55))
* Begin with this version, _sqllin-dsl_ depends on _kotlinx.coroutines_ version `1.7.3`
* ***Breaking change***: Remove the public class `DBEntity`, we have deprecated it in version `1.1.1`
* **Breaking change**: Remove the public class `DBEntity`, we have deprecated it in version `1.1.1`

### sqllin-driver

* Add the new native target support: `linuxArm64`

### sqllin-processor

Expand Down Expand Up @@ -46,7 +51,7 @@ a runtime exception. Thanks for [@nbransby](https://github.com/nbransby)
### sqllin-driver

* Add the new JVM target
* ***Breaking change***: Remove the public property: `DatabaseConnection#closed`
* **Breaking change**: Remove the public property: `DatabaseConnection#closed`
* The Android (<= 9) target supports to set the `journalMode` and `synchronousMode` now

## v1.1.1 / 2023-08-12
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SQLlin supports these platforms:
- macOS (x64, arm64)
- watchOS (x64, arm32, arm64, simulatorArm64, deviceArm64)
- tvOS (x64, arm64, simulatorArm64)
- Linux (x64)
- Linux (x64, arm64)
- Windows (mingwX64)

The architecture design of SQLlin is shown in the figure:
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SQLlin 支持如下平台:
- macOS (x64, arm64)
- watchOS (x64, arm32, arm64, simulatorArm64, deviceArm64)
- tvOS (x64, arm64, simulatorArm64)
- Linux (x64)
- Linux (x64, arm64)
- Windows (mingwX64)

SQLlin 的架构设计如下图所示:
Expand Down
2 changes: 2 additions & 0 deletions sqllin-driver/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ kotlin {
tvosSimulatorArm64(),

linuxX64(),
linuxArm64(),

mingwX64(),
).forEach {
Expand Down Expand Up @@ -92,6 +93,7 @@ kotlin {
}

tasks.findByName("publishLinuxX64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
tasks.findByName("publishLinuxArm64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
tasks.findByName("publishMingwX64PublicationToMavenRepository")?.enabled = HostManager.hostIsMingw
}

Expand Down
2 changes: 2 additions & 0 deletions sqllin-dsl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ kotlin {
tvosSimulatorArm64(),

linuxX64(),
linuxArm64(),

mingwX64(),
).forEach {
Expand Down Expand Up @@ -94,6 +95,7 @@ kotlin {
}

tasks.findByName("publishLinuxX64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
tasks.findByName("publishLinuxArm64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
tasks.findByName("publishMingwX64PublicationToMavenRepository")?.enabled = HostManager.hostIsMingw
}

Expand Down
36 changes: 23 additions & 13 deletions sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,26 @@ public class Database(
*/
public operator fun <T> invoke(block: Database.() -> T): T {
val result = block()
executeAllStatement()
executeAllStatements(prepareForExecution())
return result
}

private val statementsMutex by lazy { Mutex() }
private val assembledMutex by lazy { Mutex() }
private val executiveMutex by lazy { Mutex() }

public suspend infix fun <T> suspendedScope(block: suspend Database.() -> T): T =
statementsMutex.withLock {
public suspend infix fun <T> suspendedScope(block: suspend Database.() -> T): T {
val (result, executiveLinkedList) = assembledMutex.withLock {
val result = block()
executeAllStatement()
result
val executiveLinkedList = executiveMutex.withLock {
prepareForExecution()
}
result to executiveLinkedList
}
executiveMutex.withLock {
executeAllStatements(executiveLinkedList)
}
return result
}

/**
* Transaction.
Expand All @@ -96,7 +104,7 @@ public class Database(
if (isInTransaction)
return false
transactionStatementsGroup = TransactionStatementsGroup(databaseConnection)
executeEngine.addStatement(transactionStatementsGroup!!)
executiveEngine.addStatement(transactionStatementsGroup!!)
return true
}

Expand All @@ -117,13 +125,13 @@ public class Database(
* SQL execute.
*/

private val executeEngine = DatabaseExecuteEngine()
private val executiveEngine = DatabaseExecuteEngine()

private fun addStatement(statement: SingleStatement) {
if (isInTransaction)
transactionStatementsGroup!!.addStatement(statement)
else
executeEngine.addStatement(statement)
executiveEngine.addStatement(statement)
}

private fun <T> addSelectStatement(statement: SelectStatement<T>) {
Expand All @@ -133,7 +141,9 @@ public class Database(
addStatement(statement)
}

private fun executeAllStatement() = executeEngine.executeAllStatement()
private fun prepareForExecution() = executiveEngine.prepareForExecution()
private fun executeAllStatements(executiveLinkedList: StatementLinkedList<ExecutableStatement>) =
executiveEngine executeAllStatement executiveLinkedList

/**
* Insert.
Expand All @@ -156,8 +166,8 @@ public class Database(
val statement = Update.update(this, databaseConnection, it, clause)
it addStatement statement
statement
} ?: Update.update(this, databaseConnection, executeEngine, clause).also {
executeEngine addStatement it
} ?: Update.update(this, databaseConnection, executiveEngine, clause).also {
executiveEngine addStatement it
}

/**
Expand Down Expand Up @@ -266,7 +276,7 @@ public class Database(

private val unionSelectStatementGroupStack by lazy { Stack<UnionSelectStatementGroup<*>>() }

private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executeEngine
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executiveEngine

public inline fun <T> Table<T>.UNION(block: Table<T>.(Table<T>) -> Unit): FinalSelectStatement<T> {
beginUnion<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,41 @@

package com.ctrip.sqllin.dsl.sql.statement

import kotlin.concurrent.Volatile

/**
* Collect and execute all SQL statement in 'database {}' block
* @author yaqiao
*/

internal class DatabaseExecuteEngine : StatementContainer {

private var statementLinkedList: StatementLinkedList<ExecutableStatement>? = null
@Volatile
private var statementsLinkedList: StatementLinkedList<ExecutableStatement>? = null

override infix fun changeLastStatement(statement: SingleStatement) {
if (statementLinkedList?.lastStatement is UpdateStatementWithoutWhereClause<*>
|| statementLinkedList?.lastStatement is SelectStatement<*>)
statementLinkedList!!.resetLastStatement(statement)
if (statementsLinkedList?.lastStatement is UpdateStatementWithoutWhereClause<*>
|| statementsLinkedList?.lastStatement is SelectStatement<*>)
statementsLinkedList!!.resetLastStatement(statement)
else
throw IllegalStateException("Current statement can't append clause.")
}

infix fun addStatement(statement: ExecutableStatement) {
if (statementLinkedList != null)
statementLinkedList!!.addStatement(statement)
if (statementsLinkedList != null)
statementsLinkedList!!.addStatement(statement)
else
statementLinkedList = StatementLinkedList(statement)
statementsLinkedList = StatementLinkedList(statement)
}

fun prepareForExecution(): StatementLinkedList<ExecutableStatement> {
val executiveLinkedList = statementsLinkedList
statementsLinkedList = null
return executiveLinkedList ?: throw IllegalStateException("The statementsLinkedList can't be null")
}

fun executeAllStatement() = statementLinkedList?.run {
forEach {
infix fun executeAllStatement(executiveLinkedList: StatementLinkedList<ExecutableStatement>) {
executiveLinkedList.forEach {
when (it) {
is SingleStatement -> {
println("SQL String: ${it.sqlStr}")
Expand All @@ -50,6 +59,5 @@ internal class DatabaseExecuteEngine : StatementContainer {
is TransactionStatementsGroup -> it.execute()
}
}
statementLinkedList = null
} ?: Unit
}
}

0 comments on commit ee2b759

Please sign in to comment.