Code style improvements (thanks to codebeat.co)

This commit is contained in:
Christian Basler 2017-11-27 22:26:30 +01:00
parent 760e423b9b
commit f6ebd62c8d
3 changed files with 24 additions and 43 deletions

View File

@ -24,6 +24,7 @@ import ch.dissem.apps.abit.util.Labels
import ch.dissem.bitmessage.entity.valueobject.Label import ch.dissem.bitmessage.entity.valueobject.Label
import ch.dissem.bitmessage.ports.AbstractLabelRepository import ch.dissem.bitmessage.ports.AbstractLabelRepository
import ch.dissem.bitmessage.ports.MessageRepository import ch.dissem.bitmessage.ports.MessageRepository
import org.jetbrains.anko.db.transaction
import java.util.* import java.util.*
/** /**
@ -40,7 +41,7 @@ class AndroidLabelRepository(private val sql: SqlHelper, private val context: Co
sql.readableDatabase.query( sql.readableDatabase.query(
TABLE_NAME, projection, TABLE_NAME, projection,
where, null, null, null, where, null, null, null,
COLUMN_ORDER COLUMN_ORDER
).use { c -> ).use { c ->
while (c.moveToNext()) { while (c.moveToNext()) {
@ -60,9 +61,7 @@ class AndroidLabelRepository(private val sql: SqlHelper, private val context: Co
values.put(COLUMN_ORDER, label.ord) values.put(COLUMN_ORDER, label.ord)
db.update(TABLE_NAME, values, "id=?", arrayOf(label.id.toString())) db.update(TABLE_NAME, values, "id=?", arrayOf(label.id.toString()))
} else { } else {
try { db.transaction {
db.beginTransaction()
val exists = DatabaseUtils.queryNumEntries(db, TABLE_NAME, "label=?", arrayOf(label.toString())) > 0 val exists = DatabaseUtils.queryNumEntries(db, TABLE_NAME, "label=?", arrayOf(label.toString())) > 0
if (exists) { if (exists) {
@ -79,9 +78,6 @@ class AndroidLabelRepository(private val sql: SqlHelper, private val context: Co
values.put(COLUMN_ORDER, label.ord) values.put(COLUMN_ORDER, label.ord)
db.insertOrThrow(TABLE_NAME, null, values) db.insertOrThrow(TABLE_NAME, null, values)
} }
db.setTransactionSuccessful()
} finally {
db.endTransaction()
} }
} }
} }

View File

@ -33,6 +33,7 @@ import ch.dissem.bitmessage.ports.AlreadyStoredException
import ch.dissem.bitmessage.ports.MessageRepository import ch.dissem.bitmessage.ports.MessageRepository
import ch.dissem.bitmessage.utils.Encode import ch.dissem.bitmessage.utils.Encode
import ch.dissem.bitmessage.utils.Strings.hex import ch.dissem.bitmessage.utils.Strings.hex
import org.jetbrains.anko.db.transaction
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.util.* import java.util.*
@ -182,9 +183,7 @@ class AndroidMessageRepository(private val sql: SqlHelper) : AbstractMessageRepo
saveContactIfNecessary(message.from) saveContactIfNecessary(message.from)
saveContactIfNecessary(message.to) saveContactIfNecessary(message.to)
val db = sql.writableDatabase val db = sql.writableDatabase
try { db.transaction {
db.beginTransaction()
// save message // save message
if (message.id == null) { if (message.id == null) {
insert(db, message) insert(db, message)
@ -204,11 +203,6 @@ class AndroidMessageRepository(private val sql: SqlHelper) : AbstractMessageRepo
values.put(JT_COLUMN_MESSAGE, message.id as Long?) values.put(JT_COLUMN_MESSAGE, message.id as Long?)
db.insertOrThrow(JOIN_TABLE_NAME, null, values) db.insertOrThrow(JOIN_TABLE_NAME, null, values)
} }
db.setTransactionSuccessful()
} catch (e: SQLiteConstraintException) {
throw AlreadyStoredException(cause = e)
} finally {
db.endTransaction()
} }
} }

View File

@ -16,6 +16,7 @@ import ch.dissem.bitmessage.utils.UnixTime.DAY
import ch.dissem.bitmessage.utils.UnixTime.MINUTE import ch.dissem.bitmessage.utils.UnixTime.MINUTE
import ch.dissem.bitmessage.utils.UnixTime.now import ch.dissem.bitmessage.utils.UnixTime.now
import ch.dissem.bitmessage.utils.max import ch.dissem.bitmessage.utils.max
import org.jetbrains.anko.db.transaction
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import java.util.* import java.util.*
import kotlin.concurrent.getOrSet import kotlin.concurrent.getOrSet
@ -64,30 +65,25 @@ class AndroidNodeRegistry(private val sql: SqlHelper) : NodeRegistry {
} }
override fun getKnownAddresses(limit: Int, vararg streams: Long): List<NetworkAddress> { override fun getKnownAddresses(limit: Int, vararg streams: Long): List<NetworkAddress> {
val projection = arrayOf(COLUMN_STREAM, COLUMN_ADDRESS, COLUMN_PORT, COLUMN_SERVICES, COLUMN_TIME)
val result = LinkedList<NetworkAddress>() val result = LinkedList<NetworkAddress>()
try {
sql.readableDatabase.query( sql.readableDatabase.query(
TABLE_NAME, projection, TABLE_NAME,
"stream IN (?)", arrayOf(COLUMN_STREAM, COLUMN_ADDRESS, COLUMN_PORT, COLUMN_SERVICES, COLUMN_TIME),
arrayOf(SqlStrings.join(*streams)), null, null, "stream IN (?)",
"time DESC", arrayOf(SqlStrings.join(*streams)), null, null,
limit.toString() "time DESC",
).use { c -> limit.toString()
while (c.moveToNext()) { ).use { c ->
result.add(NetworkAddress( while (c.moveToNext()) {
time = c.getLong(c.getColumnIndex(COLUMN_TIME)), result.add(NetworkAddress(
stream = c.getLong(c.getColumnIndex(COLUMN_STREAM)), time = c.getLong(c.getColumnIndex(COLUMN_TIME)),
services = c.getLong(c.getColumnIndex(COLUMN_SERVICES)), stream = c.getLong(c.getColumnIndex(COLUMN_STREAM)),
IPv6 = c.getBlob(c.getColumnIndex(COLUMN_ADDRESS)), services = c.getLong(c.getColumnIndex(COLUMN_SERVICES)),
port = c.getInt(c.getColumnIndex(COLUMN_PORT)) IPv6 = c.getBlob(c.getColumnIndex(COLUMN_ADDRESS)),
)) port = c.getInt(c.getColumnIndex(COLUMN_PORT))
} ))
} }
} catch (e: Exception) {
LOG.error(e.message, e)
throw ApplicationException(e)
} }
if (result.isEmpty()) { if (result.isEmpty()) {
@ -101,9 +97,7 @@ class AndroidNodeRegistry(private val sql: SqlHelper) : NodeRegistry {
} }
override fun offerAddresses(nodes: List<NetworkAddress>) { override fun offerAddresses(nodes: List<NetworkAddress>) {
val db = sql.writableDatabase sql.writableDatabase.transaction {
db.beginTransaction()
try {
cleanUp() cleanUp()
nodes nodes
.filter { .filter {
@ -120,9 +114,6 @@ class AndroidNodeRegistry(private val sql: SqlHelper) : NodeRegistry {
} }
} }
} }
db.setTransactionSuccessful()
} finally {
db.endTransaction()
} }
} }