From cbebc3857999c1c194abaa6ec6217710c4e19ef5 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 29 May 2018 21:00:25 +0200 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=20Improve=20code=20quality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ch/dissem/bitmessage/utils/Base64.kt | 10 ++++++---- .../repository/JdbcMessageRepository.kt | 18 +++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Base64.kt b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Base64.kt index f37415e..e1a3f3d 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/utils/Base64.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/utils/Base64.kt @@ -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() } } diff --git a/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepository.kt b/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepository.kt index 744c085..2c6c432 100644 --- a/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepository.kt +++ b/repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepository.kt @@ -56,13 +56,13 @@ class JdbcMessageRepository(private val config: JdbcConfig) : AbstractMessageRep override fun find(where: String, offset: Int, limit: Int): List { 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) }