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

This commit is contained in:
dankito 2020-04-28 16:54:19 +02:00
parent 3f2a1fec95
commit 3829abe120
3 changed files with 22 additions and 30 deletions

View File

@ -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

View File

@ -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<AccountTransaction>) {
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()
}
}

View File

@ -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()
}