Skip to content

Commit ee2b759

Browse files
authored
Merge pull request #57 from qiaoyuang/main
Add the new target linuxArm64 and optimize the performance of concurr…
2 parents 649bcc1 + af03cf8 commit ee2b759

File tree

7 files changed

+55
-28
lines changed

7 files changed

+55
-28
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010

1111
### sqllin-dsl
1212

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

1722
### sqllin-processor
1823

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

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

5257
## v1.1.1 / 2023-08-12

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ SQLlin supports these platforms:
3737
- macOS (x64, arm64)
3838
- watchOS (x64, arm32, arm64, simulatorArm64, deviceArm64)
3939
- tvOS (x64, arm64, simulatorArm64)
40-
- Linux (x64)
40+
- Linux (x64, arm64)
4141
- Windows (mingwX64)
4242

4343
The architecture design of SQLlin is shown in the figure:

README_CN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ SQLlin 支持如下平台:
3434
- macOS (x64, arm64)
3535
- watchOS (x64, arm32, arm64, simulatorArm64, deviceArm64)
3636
- tvOS (x64, arm64, simulatorArm64)
37-
- Linux (x64)
37+
- Linux (x64, arm64)
3838
- Windows (mingwX64)
3939

4040
SQLlin 的架构设计如下图所示:

sqllin-driver/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ kotlin {
5252
tvosSimulatorArm64(),
5353

5454
linuxX64(),
55+
linuxArm64(),
5556

5657
mingwX64(),
5758
).forEach {
@@ -92,6 +93,7 @@ kotlin {
9293
}
9394

9495
tasks.findByName("publishLinuxX64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
96+
tasks.findByName("publishLinuxArm64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
9597
tasks.findByName("publishMingwX64PublicationToMavenRepository")?.enabled = HostManager.hostIsMingw
9698
}
9799

sqllin-dsl/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ kotlin {
5454
tvosSimulatorArm64(),
5555

5656
linuxX64(),
57+
linuxArm64(),
5758

5859
mingwX64(),
5960
).forEach {
@@ -94,6 +95,7 @@ kotlin {
9495
}
9596

9697
tasks.findByName("publishLinuxX64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
98+
tasks.findByName("publishLinuxArm64PublicationToMavenRepository")?.enabled = HostManager.hostIsLinux
9799
tasks.findByName("publishMingwX64PublicationToMavenRepository")?.enabled = HostManager.hostIsMingw
98100
}
99101

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/Database.kt

+23-13
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,26 @@ public class Database(
6969
*/
7070
public operator fun <T> invoke(block: Database.() -> T): T {
7171
val result = block()
72-
executeAllStatement()
72+
executeAllStatements(prepareForExecution())
7373
return result
7474
}
7575

76-
private val statementsMutex by lazy { Mutex() }
76+
private val assembledMutex by lazy { Mutex() }
77+
private val executiveMutex by lazy { Mutex() }
7778

78-
public suspend infix fun <T> suspendedScope(block: suspend Database.() -> T): T =
79-
statementsMutex.withLock {
79+
public suspend infix fun <T> suspendedScope(block: suspend Database.() -> T): T {
80+
val (result, executiveLinkedList) = assembledMutex.withLock {
8081
val result = block()
81-
executeAllStatement()
82-
result
82+
val executiveLinkedList = executiveMutex.withLock {
83+
prepareForExecution()
84+
}
85+
result to executiveLinkedList
8386
}
87+
executiveMutex.withLock {
88+
executeAllStatements(executiveLinkedList)
89+
}
90+
return result
91+
}
8492

8593
/**
8694
* Transaction.
@@ -96,7 +104,7 @@ public class Database(
96104
if (isInTransaction)
97105
return false
98106
transactionStatementsGroup = TransactionStatementsGroup(databaseConnection)
99-
executeEngine.addStatement(transactionStatementsGroup!!)
107+
executiveEngine.addStatement(transactionStatementsGroup!!)
100108
return true
101109
}
102110

@@ -117,13 +125,13 @@ public class Database(
117125
* SQL execute.
118126
*/
119127

120-
private val executeEngine = DatabaseExecuteEngine()
128+
private val executiveEngine = DatabaseExecuteEngine()
121129

122130
private fun addStatement(statement: SingleStatement) {
123131
if (isInTransaction)
124132
transactionStatementsGroup!!.addStatement(statement)
125133
else
126-
executeEngine.addStatement(statement)
134+
executiveEngine.addStatement(statement)
127135
}
128136

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

136-
private fun executeAllStatement() = executeEngine.executeAllStatement()
144+
private fun prepareForExecution() = executiveEngine.prepareForExecution()
145+
private fun executeAllStatements(executiveLinkedList: StatementLinkedList<ExecutableStatement>) =
146+
executiveEngine executeAllStatement executiveLinkedList
137147

138148
/**
139149
* Insert.
@@ -156,8 +166,8 @@ public class Database(
156166
val statement = Update.update(this, databaseConnection, it, clause)
157167
it addStatement statement
158168
statement
159-
} ?: Update.update(this, databaseConnection, executeEngine, clause).also {
160-
executeEngine addStatement it
169+
} ?: Update.update(this, databaseConnection, executiveEngine, clause).also {
170+
executiveEngine addStatement it
161171
}
162172

163173
/**
@@ -266,7 +276,7 @@ public class Database(
266276

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

269-
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executeEngine
279+
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executiveEngine
270280

271281
public inline fun <T> Table<T>.UNION(block: Table<T>.(Table<T>) -> Unit): FinalSelectStatement<T> {
272282
beginUnion<T>()

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/sql/statement/DatabaseExecuteEngine.kt

+19-11
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,41 @@
1616

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

19+
import kotlin.concurrent.Volatile
20+
1921
/**
2022
* Collect and execute all SQL statement in 'database {}' block
2123
* @author yaqiao
2224
*/
2325

2426
internal class DatabaseExecuteEngine : StatementContainer {
2527

26-
private var statementLinkedList: StatementLinkedList<ExecutableStatement>? = null
28+
@Volatile
29+
private var statementsLinkedList: StatementLinkedList<ExecutableStatement>? = null
2730

2831
override infix fun changeLastStatement(statement: SingleStatement) {
29-
if (statementLinkedList?.lastStatement is UpdateStatementWithoutWhereClause<*>
30-
|| statementLinkedList?.lastStatement is SelectStatement<*>)
31-
statementLinkedList!!.resetLastStatement(statement)
32+
if (statementsLinkedList?.lastStatement is UpdateStatementWithoutWhereClause<*>
33+
|| statementsLinkedList?.lastStatement is SelectStatement<*>)
34+
statementsLinkedList!!.resetLastStatement(statement)
3235
else
3336
throw IllegalStateException("Current statement can't append clause.")
3437
}
3538

3639
infix fun addStatement(statement: ExecutableStatement) {
37-
if (statementLinkedList != null)
38-
statementLinkedList!!.addStatement(statement)
40+
if (statementsLinkedList != null)
41+
statementsLinkedList!!.addStatement(statement)
3942
else
40-
statementLinkedList = StatementLinkedList(statement)
43+
statementsLinkedList = StatementLinkedList(statement)
44+
}
45+
46+
fun prepareForExecution(): StatementLinkedList<ExecutableStatement> {
47+
val executiveLinkedList = statementsLinkedList
48+
statementsLinkedList = null
49+
return executiveLinkedList ?: throw IllegalStateException("The statementsLinkedList can't be null")
4150
}
4251

43-
fun executeAllStatement() = statementLinkedList?.run {
44-
forEach {
52+
infix fun executeAllStatement(executiveLinkedList: StatementLinkedList<ExecutableStatement>) {
53+
executiveLinkedList.forEach {
4554
when (it) {
4655
is SingleStatement -> {
4756
println("SQL String: ${it.sqlStr}")
@@ -50,6 +59,5 @@ internal class DatabaseExecuteEngine : StatementContainer {
5059
is TransactionStatementsGroup -> it.execute()
5160
}
5261
}
53-
statementLinkedList = null
54-
} ?: Unit
62+
}
5563
}

0 commit comments

Comments
 (0)