Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

optimize preparedstatement cache memory usage #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.mauricio.async.db.mysql.codec

/**
* optimize memory usage
* we only need statementId,columnSize and parameterSize
*/
class LitePreparedStatementHolder(val statementId: Array[Byte], val columnSize: Int, val parameterSize: Int)

object LitePreparedStatementHolder {
def apply(preparedStatementHolder: PreparedStatementHolder): LitePreparedStatementHolder = {
val res = new LitePreparedStatementHolder(preparedStatementHolder.statementId, preparedStatementHolder.columns.size, preparedStatementHolder.parameters.size)
res
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class MySQLConnectionHandler(
private final val sendLongDataEncoder = new SendLongDataEncoder()
private final val currentParameters = new ArrayBuffer[ColumnDefinitionMessage]()
private final val currentColumns = new ArrayBuffer[ColumnDefinitionMessage]()
private final val parsedStatements = new HashMap[String,PreparedStatementHolder]()
private final val parsedStatements = new HashMap[String,LitePreparedStatementHolder]()
private final val binaryRowDecoder = new BinaryRowDecoder()

private var currentPreparedStatementHolder : PreparedStatementHolder = null
Expand Down Expand Up @@ -201,7 +201,7 @@ class MySQLConnectionHandler(

this.parsedStatements.get(preparedStatement.statement) match {
case Some( item ) => {
this.executePreparedStatement(item.statementId, item.columns.size, preparedStatement.values, item.parameters)
this.executePreparedStatement(item.statementId, item.columnSize, preparedStatement.values, item.parameterSize)
}
case None => {
decoder.preparedStatementPrepareStarted()
Expand Down Expand Up @@ -237,8 +237,8 @@ class MySQLConnectionHandler(
}
}

private def executePreparedStatement( statementId : Array[Byte], columnsCount : Int, values : Seq[Any], parameters : Seq[ColumnDefinitionMessage] ): Future[ChannelFuture] = {
decoder.preparedStatementExecuteStarted(columnsCount, parameters.size)
private def executePreparedStatement( statementId : Array[Byte], columnsCount : Int, values : Seq[Any], parameterSize : Int ): Future[ChannelFuture] = {
decoder.preparedStatementExecuteStarted(columnsCount, parameterSize)
this.currentColumns.clear()
this.currentParameters.clear()

Expand All @@ -259,10 +259,10 @@ class MySQLConnectionHandler(
}
}
channelFuture flatMap { _ =>
writeAndHandleError(new PreparedStatementExecuteMessage(statementId, values, nonLongIndices.toSet, parameters))
writeAndHandleError(new PreparedStatementExecuteMessage(statementId, values, nonLongIndices.toSet, parameterSize))
}
} else {
writeAndHandleError(new PreparedStatementExecuteMessage(statementId, values, nonLongIndices.toSet, parameters))
writeAndHandleError(new PreparedStatementExecuteMessage(statementId, values, nonLongIndices.toSet, parameterSize))
}
}

Expand Down Expand Up @@ -308,12 +308,12 @@ class MySQLConnectionHandler(
this.currentQuery = new MutableResultSet[ColumnDefinitionMessage](columns)

if ( this.currentPreparedStatementHolder != null ) {
this.parsedStatements.put( this.currentPreparedStatementHolder.statement, this.currentPreparedStatementHolder )
this.parsedStatements.put( this.currentPreparedStatementHolder.statement, LitePreparedStatementHolder(this.currentPreparedStatementHolder) )
this.executePreparedStatement(
this.currentPreparedStatementHolder.statementId,
this.currentPreparedStatementHolder.columns.size,
this.currentPreparedStatement.values,
this.currentPreparedStatementHolder.parameters
this.currentPreparedStatementHolder.parameters.size
)
this.currentPreparedStatementHolder = null
this.currentPreparedStatement = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PreparedStatementExecuteEncoder( rowEncoder : BinaryRowEncoder ) extends M
buffer.writeByte(0x00) // no cursor
buffer.writeInt(1)

if ( m.parameters.isEmpty ) {
if ( m.parameterSize == 0 ) {
buffer
} else {
Unpooled.wrappedBuffer(buffer, encodeValues(m.values, m.valuesToInclude))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ case class PreparedStatementExecuteMessage (
statementId : Array[Byte],
values : Seq[Any],
valuesToInclude : Set[Int],
parameters : Seq[ColumnDefinitionMessage] )
parameterSize : Int )
extends ClientMessage( ClientMessage.PreparedStatementExecute )