Skip to content

Commit 088738e

Browse files
committed
Use ArrayDeque to replace the LinkedList (self-implemented)
1 parent b4ac6e3 commit 088738e

File tree

9 files changed

+46
-210
lines changed

9 files changed

+46
-210
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
* Update `kotlinx.coroutines`'s version to `1.8.0`
1414
* Update `kotlinx.serialization`'s version to `1.6.3`
15-
* Modified the SQL statements' splicing method, that fixed the [issue#77](https://github.com/ctripcorp/SQLlin/issues/77) that users can't read/write special symbols as the values in SQL statements.
15+
* Modify the SQL statements' splicing method, that fixed the [issue#77](https://github.com/ctripcorp/SQLlin/issues/77) that users can't read/write special symbols as the values in SQL statements.
16+
* Performance optimization, use `ArrayDeque` to replace the LinkedList for SQL statements management (self-implemented)
1617

1718
### sqllin-driver
1819

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public class DatabaseScope internal constructor(
8686
}
8787

8888
private fun <T> addSelectStatement(statement: SelectStatement<T>) {
89-
if (unionSelectStatementGroupStack.isNotEmpty)
90-
(unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).addSelectStatement(statement)
89+
if (unionSelectStatementGroupStack.isNotEmpty())
90+
(unionSelectStatementGroupStack.last() as UnionSelectStatementGroup<T>).addSelectStatement(statement)
9191
else
9292
addStatement(statement)
9393
}
@@ -223,9 +223,9 @@ public class DatabaseScope internal constructor(
223223
* The 'UNION' clause of Select.
224224
*/
225225

226-
private val unionSelectStatementGroupStack by lazy { Stack<UnionSelectStatementGroup<*>>() }
226+
private val unionSelectStatementGroupStack by lazy { ArrayDeque<UnionSelectStatementGroup<*>>() }
227227

228-
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.top ?: transactionStatementsGroup ?: executiveEngine
228+
private fun getSelectStatementGroup(): StatementContainer = unionSelectStatementGroupStack.lastOrNull() ?: transactionStatementsGroup ?: executiveEngine
229229

230230
public inline fun <T> Table<T>.UNION(block: Table<T>.(Table<T>) -> Unit): FinalSelectStatement<T> {
231231
beginUnion<T>()
@@ -252,16 +252,16 @@ public class DatabaseScope internal constructor(
252252
}
253253

254254
public fun <T> beginUnion() {
255-
unionSelectStatementGroupStack.push(UnionSelectStatementGroup<T>())
255+
unionSelectStatementGroupStack.add(UnionSelectStatementGroup<T>())
256256
}
257257

258258
public fun <T> createUnionSelectStatement(isUnionAll: Boolean): FinalSelectStatement<T> {
259-
check(unionSelectStatementGroupStack.isNotEmpty) { "Please invoke the 'beginUnion' before you invoke this function!!!" }
260-
return (unionSelectStatementGroupStack.top as UnionSelectStatementGroup<T>).unionStatements(isUnionAll)
259+
check(unionSelectStatementGroupStack.isNotEmpty()) { "Please invoke the 'beginUnion' before you invoke this function!!!" }
260+
return (unionSelectStatementGroupStack.last() as UnionSelectStatementGroup<T>).unionStatements(isUnionAll)
261261
}
262262

263263
public fun <T> endUnion(selectStatement: SelectStatement<T>?) {
264-
unionSelectStatementGroupStack.pop()
264+
unionSelectStatementGroupStack.removeLast()
265265
selectStatement?.let { addSelectStatement(it) }
266266
}
267267

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,23 @@ internal class DatabaseExecuteEngine(
2525
private val enableSimpleSQLLog: Boolean,
2626
) : StatementContainer {
2727

28-
private lateinit var statementsLinkedList: StatementLinkedList<ExecutableStatement>
28+
private val statementList = ArrayDeque<ExecutableStatement>()
2929

3030
override infix fun changeLastStatement(statement: SingleStatement) {
31-
if (statementsLinkedList.lastStatement is UpdateStatementWithoutWhereClause<*>
32-
|| statementsLinkedList.lastStatement is SelectStatement<*>)
33-
statementsLinkedList.resetLastStatement(statement)
34-
else
31+
if (statementList.lastOrNull() is UpdateStatementWithoutWhereClause<*>
32+
|| statementList.lastOrNull() is SelectStatement<*>) {
33+
statementList.removeLast()
34+
statementList.add(statement)
35+
} else
3536
throw IllegalStateException("Current statement can't append clause.")
3637
}
3738

3839
infix fun addStatement(statement: ExecutableStatement) {
39-
if (::statementsLinkedList.isInitialized)
40-
statementsLinkedList.addStatement(statement)
41-
else
42-
statementsLinkedList = StatementLinkedList(statement)
40+
statementList.add(statement)
4341
}
4442

4543
fun executeAllStatement() {
46-
statementsLinkedList.forEach {
44+
statementList.forEach {
4745
when (it) {
4846
is SingleStatement -> {
4947
if (enableSimpleSQLLog)

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,10 @@ internal class TransactionStatementsGroup(
2929
private val enableSimpleSQLLog: Boolean,
3030
) : ExecutableStatement, StatementContainer {
3131

32-
private lateinit var statementList: StatementLinkedList<SingleStatement>
32+
private val statementList = ArrayDeque<SingleStatement>()
3333

3434
infix fun addStatement(statement: SingleStatement) {
35-
if (this::statementList.isInitialized)
36-
statementList.addStatement(statement)
37-
else
38-
statementList = StatementLinkedList(statement)
35+
statementList.add(statement)
3936
}
4037

4138
override fun execute() = databaseConnection.withTransaction {
@@ -47,10 +44,11 @@ internal class TransactionStatementsGroup(
4744
}
4845

4946
override infix fun changeLastStatement(statement: SingleStatement) {
50-
if (statementList.lastStatement is UpdateStatementWithoutWhereClause<*>
51-
|| statementList.lastStatement is SelectStatement<*>)
52-
statementList resetLastStatement statement
53-
else
47+
if (statementList.lastOrNull() is UpdateStatementWithoutWhereClause<*>
48+
|| statementList.lastOrNull() is SelectStatement<*>) {
49+
statementList.removeLast()
50+
statementList.add(statement)
51+
} else
5452
throw IllegalStateException("Current statement can't append clause.")
5553
}
5654
}

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

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,31 @@ package com.ctrip.sqllin.dsl.sql.statement
2323

2424
internal class UnionSelectStatementGroup<T> : StatementContainer {
2525

26-
private var statementLinkedList: StatementLinkedList<SelectStatement<T>>? = null
26+
private val statementList = ArrayDeque<SelectStatement<T>>()
2727

2828
infix fun addSelectStatement(selectStatement: SelectStatement<T>) {
29-
if (statementLinkedList != null)
30-
statementLinkedList!!.addStatement(selectStatement)
31-
else
32-
statementLinkedList = StatementLinkedList(selectStatement)
29+
statementList.add(selectStatement)
3330
}
3431

3532
internal fun unionStatements(isUnionAll: Boolean): FinalSelectStatement<T> {
36-
require(statementLinkedList?.hasNext() == true) { "Please write at least two 'select' statements on 'UNION' scope" }
37-
var firstStatement: SelectStatement<T>? = null
33+
require(statementList.isNotEmpty()) { "Please write at least two 'select' statements on 'UNION' scope" }
3834
var parameters: MutableList<String>? = null
3935
val unionSqlStr = buildString {
40-
statementLinkedList!!.run {
41-
val unionKeyWord = if (isUnionAll) " UNION ALL " else " UNION "
42-
do {
43-
val next = next()
44-
append(next.sqlStr)
45-
val hasNext = hasNext()
46-
if (firstStatement == null) {
47-
firstStatement = next
48-
if (!hasNext)
49-
throw IllegalStateException("Please write at least two 'select' statements on 'UNION' scope")
50-
}
51-
if (parameters == null) {
52-
parameters = next.parameters
53-
} else next.parameters?.let {
54-
parameters!!.addAll(it)
55-
}
56-
if (hasNext)
57-
append(unionKeyWord)
58-
} while (hasNext)
36+
check(statementList.size > 1) { "Please write at least two 'select' statements on 'UNION' scope" }
37+
val unionKeyWord = if (isUnionAll) " UNION ALL " else " UNION "
38+
statementList.forEachIndexed { index, statement ->
39+
append(statement.sqlStr)
40+
if (parameters == null)
41+
parameters = statement.parameters
42+
else statement.parameters?.let {
43+
parameters!!.addAll(it)
44+
}
45+
if (index != statementList.lastIndex)
46+
append(unionKeyWord)
5947
}
6048
}
6149

62-
return firstStatement!!.run {
50+
return statementList.first().run {
6351
FinalSelectStatement(
6452
sqlStr = unionSqlStr,
6553
deserializer = deserializer,
@@ -72,9 +60,10 @@ internal class UnionSelectStatementGroup<T> : StatementContainer {
7260

7361
@Suppress("UNCHECKED_CAST")
7462
override fun changeLastStatement(statement: SingleStatement) {
75-
if (statementLinkedList?.lastStatement is SelectStatement<*>)
76-
statementLinkedList!!.resetLastStatement(statement as SelectStatement<T>)
77-
else
63+
if (statementList.lastOrNull() is SelectStatement<*>) {
64+
statementList.removeLast()
65+
statementList.add(statement as SelectStatement<T>)
66+
} else
7867
throw IllegalStateException("Current statement can't append clause")
7968
}
8069
}

sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/TestPrimitiveTypeForKSP.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ data class TestPrimitiveTypeForKSP(
3737
val testULong: ULong,
3838
val testUShort: UShort,
3939
val testUByte: UByte,
40-
val testBoolean: Boolean,
41-
val testChar: Char,
40+
val testBoolean: Boolean?,
41+
val testChar: Char?,
4242
val testString: String,
4343
)

0 commit comments

Comments
 (0)