From 3829abe120533849d1fbeb7219bec84bb9c0f6c0 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 28 Apr 2020 16:54:19 +0200 Subject: [PATCH] LuceneBankingPersistence now only opens index directory for writing when persisting account transactions. Should fix exception that a second DocumentsWriter got instantiated on resume on Android --- .../fints4java/android/di/BankingModule.kt | 2 +- .../persistence/LuceneBankingPersistence.kt | 46 ++++++++----------- .../search/LuceneRemitteeSearcherTest.kt | 4 +- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt index 334ff599..4ade2bad 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/di/BankingModule.kt @@ -104,7 +104,7 @@ class BankingModule(internal val mainActivity: AppCompatActivity) { @Provides @Singleton fun provideBankingPersistence(@Named(IndexFolderKey) indexFolder: File, @Named(DatabaseFolderKey) databaseFolder: File, serializer: ISerializer) : IBankingPersistence { - return LuceneBankingPersistence(databaseFolder, indexFolder, serializer) + return LuceneBankingPersistence(indexFolder, databaseFolder, serializer) } @Provides diff --git a/persistence/LuceneBankingPersistence/src/main/kotlin/net/dankito/banking/persistence/LuceneBankingPersistence.kt b/persistence/LuceneBankingPersistence/src/main/kotlin/net/dankito/banking/persistence/LuceneBankingPersistence.kt index f514150a..8475aa32 100644 --- a/persistence/LuceneBankingPersistence/src/main/kotlin/net/dankito/banking/persistence/LuceneBankingPersistence.kt +++ b/persistence/LuceneBankingPersistence/src/main/kotlin/net/dankito/banking/persistence/LuceneBankingPersistence.kt @@ -19,47 +19,41 @@ import net.dankito.utils.lucene.index.DocumentsWriter import net.dankito.utils.lucene.index.FieldBuilder import net.dankito.utils.serialization.ISerializer import net.dankito.utils.serialization.JacksonJsonSerializer -import java.io.Closeable import java.io.File open class LuceneBankingPersistence( + protected val indexFolder: File, databaseFolder: File, - indexFolder: File, serializer: ISerializer = JacksonJsonSerializer() -) : BankingPersistenceJson(File(databaseFolder, "accounts.json"), serializer), IBankingPersistence, Closeable { +) : BankingPersistenceJson(File(databaseFolder, "accounts.json"), serializer), IBankingPersistence { protected val fields = FieldBuilder() - protected val writer = DocumentsWriter(LuceneConfig.getAccountTransactionsIndexFolder(indexFolder)) - - - override fun close() { - writer.close() - } - override fun saveOrUpdateAccountTransactions(bankAccount: BankAccount, transactions: List) { - transactions.forEach { transaction -> - writer.updateDocumentForNonNullFields(IdFieldName, transaction.id, - fields.keywordField(BankAccountIdFieldName, bankAccount.id), - fields.nullableFullTextSearchField(OtherPartyNameFieldName, transaction.otherPartyName, true), - fields.fullTextSearchField(UsageFieldName, transaction.usage, true), - fields.nullableFullTextSearchField(BookingTextFieldName, transaction.bookingText, true), + DocumentsWriter(LuceneConfig.getAccountTransactionsIndexFolder(indexFolder)).use { writer -> + transactions.forEach { transaction -> + writer.updateDocumentForNonNullFields(IdFieldName, transaction.id, + fields.keywordField(BankAccountIdFieldName, bankAccount.id), + fields.nullableFullTextSearchField(OtherPartyNameFieldName, transaction.otherPartyName, true), + fields.fullTextSearchField(UsageFieldName, transaction.usage, true), + fields.nullableFullTextSearchField(BookingTextFieldName, transaction.bookingText, true), - fields.nullableStoredField(OtherPartyBankCodeFieldName, transaction.otherPartyBankCode), - fields.nullableStoredField(OtherPartyAccountIdFieldName, transaction.otherPartyAccountId), - fields.storedField(BookingDateFieldName, transaction.bookingDate), - fields.storedField(AmountFieldName, transaction.amount), - fields.storedField(CurrencyFieldName, transaction.currency), - fields.nullableStoredField(BalanceFieldName, transaction.balance), + fields.nullableStoredField(OtherPartyBankCodeFieldName, transaction.otherPartyBankCode), + fields.nullableStoredField(OtherPartyAccountIdFieldName, transaction.otherPartyAccountId), + fields.storedField(BookingDateFieldName, transaction.bookingDate), + fields.storedField(AmountFieldName, transaction.amount), + fields.storedField(CurrencyFieldName, transaction.currency), + fields.nullableStoredField(BalanceFieldName, transaction.balance), - fields.sortField(BookingDateSortFieldName, transaction.bookingDate) - ) + fields.sortField(BookingDateSortFieldName, transaction.bookingDate) + ) + } + + writer.flushChangesToDisk() } - - writer.flushChangesToDisk() } } \ No newline at end of file diff --git a/persistence/LuceneBankingPersistence/src/test/kotlin/net/dankito/banking/search/LuceneRemitteeSearcherTest.kt b/persistence/LuceneBankingPersistence/src/test/kotlin/net/dankito/banking/search/LuceneRemitteeSearcherTest.kt index 95500c4f..b7ecacca 100644 --- a/persistence/LuceneBankingPersistence/src/test/kotlin/net/dankito/banking/search/LuceneRemitteeSearcherTest.kt +++ b/persistence/LuceneBankingPersistence/src/test/kotlin/net/dankito/banking/search/LuceneRemitteeSearcherTest.kt @@ -45,7 +45,7 @@ class LuceneRemitteeSearcherTest { private val fileUtils = FileUtils() - private val bankingPersistence = LuceneBankingPersistence(databaseFolder, indexFolder) + private val bankingPersistence = LuceneBankingPersistence(indexFolder, databaseFolder) private val underTest = LuceneRemitteeSearcher(indexFolder) @@ -57,8 +57,6 @@ class LuceneRemitteeSearcherTest { @After fun tearDown() { - bankingPersistence.close() - clearDataFolder() }