From 278d5b05e6475097710cb96c44749e75ec4de1a7 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sun, 26 Nov 2017 20:30:05 +0100 Subject: [PATCH] Split LabelRepository off the MessageRepository --- .../ch/dissem/bitmessage/BitmessageContext.kt | 11 ++ .../ch/dissem/bitmessage/InternalContext.kt | 11 +- .../ch/dissem/bitmessage/entity/Plaintext.kt | 133 ++++++++------ .../ports/AbstractLabelRepository.kt | 33 ++++ .../ports/AbstractMessageRepository.kt | 22 +-- .../dissem/bitmessage/ports/DefaultLabeler.kt | 14 +- .../bitmessage/ports/LabelRepository.kt | 27 +++ .../bitmessage/ports/MessageRepository.kt | 6 - .../ch/dissem/bitmessage/utils/Collections.kt | 9 + .../bitmessage/BitmessageContextTest.kt | 1 + .../ch/dissem/bitmessage/utils/TestUtils.kt | 2 + .../dissem/bitmessage/demo/Application.java | 2 +- .../java/ch/dissem/bitmessage/SystemTest.java | 6 +- .../networking/NetworkHandlerTest.kt | 2 + .../repository/JdbcLabelRepository.kt | 123 +++++++++++++ .../repository/JdbcMessageRepository.kt | 166 +++++------------- .../repository/JdbcLabelRepositoryTest.kt | 49 ++++++ .../repository/JdbcMessageRepositoryTest.kt | 24 +-- .../dissem/bitmessage/wif/WifExporterTest.kt | 1 + .../dissem/bitmessage/wif/WifImporterTest.kt | 1 + 20 files changed, 407 insertions(+), 236 deletions(-) create mode 100644 core/src/main/kotlin/ch/dissem/bitmessage/ports/AbstractLabelRepository.kt create mode 100644 core/src/main/kotlin/ch/dissem/bitmessage/ports/LabelRepository.kt create mode 100644 repositories/src/main/kotlin/ch/dissem/bitmessage/repository/JdbcLabelRepository.kt create mode 100644 repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcLabelRepositoryTest.kt diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt index 94a29ee..a97cfdc 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/BitmessageContext.kt @@ -73,6 +73,9 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) val addresses: AddressRepository @JvmName("addresses") get + val labels: LabelRepository + @JvmName("labels") get + val messages: MessageRepository @JvmName("messages") get @@ -301,6 +304,7 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) var nodeRegistry by Delegates.notNull() var networkHandler by Delegates.notNull() var addressRepo by Delegates.notNull() + var labelRepo by Delegates.notNull() var messageRepo by Delegates.notNull() var proofOfWorkRepo by Delegates.notNull() var proofOfWorkEngine: ProofOfWorkEngine? = null @@ -330,6 +334,11 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) return this } + fun labelRepo(labelRepo: LabelRepository): Builder { + this.labelRepo = labelRepo + return this + } + fun messageRepo(messageRepo: MessageRepository): Builder { this.messageRepo = messageRepo return this @@ -386,6 +395,7 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) builder.nodeRegistry, builder.networkHandler, builder.addressRepo, + builder.labelRepo, builder.messageRepo, builder.proofOfWorkRepo, builder.proofOfWorkEngine ?: MultiThreadedPOWEngine(), @@ -400,6 +410,7 @@ class BitmessageContext private constructor(builder: BitmessageContext.Builder) builder.preferences ) this.addresses = builder.addressRepo + this.labels = builder.labelRepo this.messages = builder.messageRepo (builder.listener as? Listener.WithContext)?.setContext(this) internals.proofOfWorkService.doMissingProofOfWork(builder.preferences.doMissingProofOfWorkDelayInSeconds * 1000L) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt index 6415b8d..c7b303d 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/InternalContext.kt @@ -40,11 +40,12 @@ import java.util.concurrent.Executors */ class InternalContext( val cryptography: Cryptography, - val inventory: ch.dissem.bitmessage.ports.Inventory, + val inventory: Inventory, val nodeRegistry: NodeRegistry, val networkHandler: NetworkHandler, val addressRepository: AddressRepository, - val messageRepository: ch.dissem.bitmessage.ports.MessageRepository, + val labelRepository: LabelRepository, + val messageRepository: MessageRepository, val proofOfWorkRepository: ProofOfWorkRepository, val proofOfWorkEngine: ProofOfWorkEngine, val customCommandHandler: CustomCommandHandler, @@ -216,7 +217,9 @@ class InternalContext( companion object { private val LOG = LoggerFactory.getLogger(InternalContext::class.java) - @JvmField val NETWORK_NONCE_TRIALS_PER_BYTE: Long = 1000 - @JvmField val NETWORK_EXTRA_BYTES: Long = 1000 + @JvmField + val NETWORK_NONCE_TRIALS_PER_BYTE: Long = 1000 + @JvmField + val NETWORK_EXTRA_BYTES: Long = 1000 } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt index 236d581..d382436 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/Plaintext.kt @@ -35,6 +35,7 @@ import java.nio.ByteBuffer import java.util.* import java.util.Collections import kotlin.collections.HashSet +import kotlin.collections.LinkedHashSet private fun message(encoding: Plaintext.Encoding, subject: String, body: String): ByteArray = when (encoding) { SIMPLE -> "Subject:$subject\nBody:$body".toByteArray() @@ -254,7 +255,7 @@ class Plaintext private constructor( received = builder.received, initialHash = null, ttl = builder.ttl, - labels = builder.labels, + labels = LinkedHashSet(builder.labels), status = builder.status ?: Status.RECEIVED ) { id = builder.id @@ -278,28 +279,30 @@ class Plaintext private constructor( get() { val s = Scanner(ByteArrayInputStream(message), "UTF-8") val firstLine = s.nextLine() - if (encodingCode == EXTENDED.code) { - if (Message.TYPE == extendedData?.type) { - return (extendedData!!.content as? Message)?.subject + return when (encodingCode) { + EXTENDED.code -> if (Message.TYPE == extendedData?.type) { + (extendedData!!.content as? Message)?.subject } else { - return null + null + } + SIMPLE.code -> firstLine.substring("Subject:".length).trim { it <= ' ' } + else -> { + if (firstLine.length > 50) { + firstLine.substring(0, 50).trim { it <= ' ' } + "..." + } else { + firstLine + } } - } else if (encodingCode == SIMPLE.code) { - return firstLine.substring("Subject:".length).trim { it <= ' ' } - } else if (firstLine.length > 50) { - return firstLine.substring(0, 50).trim { it <= ' ' } + "..." - } else { - return firstLine } } val text: String? get() { if (encodingCode == EXTENDED.code) { - if (Message.TYPE == extendedData?.type) { - return (extendedData?.content as Message?)?.body + return if (Message.TYPE == extendedData?.type) { + (extendedData?.content as Message?)?.body } else { - return null + null } } else { val text = String(message) @@ -322,20 +325,20 @@ class Plaintext private constructor( val parents: List get() { val extendedData = extendedData ?: return emptyList() - if (Message.TYPE == extendedData.type) { - return (extendedData.content as Message).parents + return if (Message.TYPE == extendedData.type) { + (extendedData.content as Message).parents } else { - return emptyList() + emptyList() } } val files: List get() { val extendedData = extendedData ?: return emptyList() - if (Message.TYPE == extendedData.type) { - return (extendedData.content as Message).files + return if (Message.TYPE == extendedData.type) { + (extendedData.content as Message).files } else { - return emptyList() + emptyList() } } @@ -378,8 +381,8 @@ class Plaintext private constructor( override fun toString(): String { val subject = subject - if (subject?.isNotEmpty() ?: false) { - return subject!! + if (subject?.isNotEmpty() == true) { + return subject } else { return Strings.hex( initialHash ?: return super.toString() @@ -529,32 +532,43 @@ class Plaintext private constructor( } class Builder(internal val type: Type) { - internal var id: Any? = null - internal var inventoryVector: InventoryVector? = null - internal var from: BitmessageAddress? = null - internal var to: BitmessageAddress? = null - private var addressVersion: Long = 0 - private var stream: Long = 0 - private var behaviorBitfield: Int = 0 - private var publicSigningKey: ByteArray? = null - private var publicEncryptionKey: ByteArray? = null - private var nonceTrialsPerByte: Long = 0 - private var extraBytes: Long = 0 - private var destinationRipe: ByteArray? = null - private var preventAck: Boolean = false - internal var encoding: Long = 0 - internal var message = ByteArray(0) - internal var ackData: ByteArray? = null - internal var ackMessage: ByteArray? = null - internal var signature: ByteArray? = null - internal var sent: Long? = null - internal var received: Long? = null - internal var status: Status? = null - internal val labels = LinkedHashSet