Improve code quality

This commit is contained in:
Christian Basler 2018-05-29 21:00:25 +02:00
parent b44a2f8809
commit cbebc38579
2 changed files with 17 additions and 11 deletions

View File

@ -641,24 +641,26 @@ private class Encoder(val options: Options, output: ByteArray) : Coder(output) {
* Lookup table for turning Base64 alphabet positions (6 bits)
* into output bytes.
*/
private val ENCODE = charArrayOf(
private val ENCODE = charsAsBytes(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
).map { it.toByte() }.toByteArray()
)
/**
* Lookup table for turning Base64 alphabet positions (6 bits)
* into output bytes.
*/
private val ENCODE_WEBSAFE = charArrayOf(
private val ENCODE_WEBSAFE = charsAsBytes(
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
).map { it.toByte() }.toByteArray()
)
private fun charsAsBytes(vararg elements: Char): ByteArray = elements.map { it.toByte() }.toByteArray()
}
}

View File

@ -56,13 +56,13 @@ class JdbcMessageRepository(private val config: JdbcConfig) : AbstractMessageRep
override fun find(where: String, offset: Int, limit: Int): List<Plaintext> {
val result = LinkedList<Plaintext>()
val limit = if (limit == 0) "" else "LIMIT $limit OFFSET $offset"
val limitClause = if (limit == 0) "" else "LIMIT $limit OFFSET $offset"
try {
config.getConnection().use { connection ->
connection.createStatement().use { stmt ->
stmt.executeQuery(
"""SELECT id, iv, type, sender, recipient, data, ack_data, sent, received, initial_hash, status, ttl, retries, next_try, conversation
FROM Message WHERE $where $limit""").use { rs ->
FROM Message WHERE $where $limitClause""").use { rs ->
while (rs.next()) {
val message = getMessage(connection, rs)
message.initialHash = rs.getBytes("initial_hash")
@ -84,8 +84,8 @@ class JdbcMessageRepository(private val config: JdbcConfig) : AbstractMessageRep
).build {
id = rs.getLong("id")
inventoryVector = InventoryVector.fromHash(rs.getBytes("iv"))
from = rs.getString("sender")?.let { ctx.addressRepository.getAddress(it) ?: BitmessageAddress(it) }
to = rs.getString("recipient")?.let { ctx.addressRepository.getAddress(it) ?: BitmessageAddress(it) }
from = rs.getAddress("sender")
to = rs.getAddress("recipient")
ackData = rs.getBytes("ack_data")
sent = rs.getObject("sent") as Long?
received = rs.getObject("received") as Long?
@ -99,6 +99,10 @@ class JdbcMessageRepository(private val config: JdbcConfig) : AbstractMessageRep
}
}
private fun ResultSet.getAddress(columnLabel: String): BitmessageAddress? = getString(columnLabel)?.let { address ->
ctx.addressRepository.getAddress(address) ?: BitmessageAddress(address)
}
private fun findLabels(connection: Connection, where: String): List<Label> {
val result = ArrayList<Label>()
try {
@ -258,18 +262,18 @@ class JdbcMessageRepository(private val config: JdbcConfig) : AbstractMessageRep
}
override fun findConversations(label: Label?, offset: Int, limit: Int): List<UUID> {
val where = if (label == null) {
val whereClause = if (label == null) {
"id NOT IN (SELECT message_id FROM Message_Label)"
} else {
"id IN (SELECT message_id FROM Message_Label WHERE label_id=${label.id})"
}
val limit = if (limit == 0) "" else "LIMIT $limit OFFSET $offset"
val limitClause = if (limit == 0) "" else "LIMIT $limit OFFSET $offset"
val result = LinkedList<UUID>()
try {
config.getConnection().use { connection ->
connection.createStatement().use { stmt ->
stmt.executeQuery(
"SELECT DISTINCT conversation FROM Message WHERE $where $limit").use { rs ->
"SELECT DISTINCT conversation FROM Message WHERE $whereClause $limitClause").use { rs ->
while (rs.next()) {
result.add(rs.getObject(1) as UUID)
}