From 29afad0a15317fa735b3700b2c69724219c0a3f7 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 24 Apr 2020 13:44:25 +0200 Subject: [PATCH] Extracted writeBanksToIndex() and catching exceptions during indexing --- .../dankito/fints/banks/LuceneBankFinder.kt | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt index a0aa18aa..6d8af14d 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt @@ -11,6 +11,7 @@ import net.dankito.utils.lucene.search.QueryBuilder import net.dankito.utils.lucene.search.Searcher import org.apache.lucene.document.Document import org.apache.lucene.search.Query +import org.slf4j.LoggerFactory import java.io.File @@ -33,6 +34,9 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder { const val BankInfoPinTanVersionFieldName = "pin_tan_version" const val BankInfoOldBankCodeFieldName = "old_bank_code" + + private val log = LoggerFactory.getLogger(LuceneBankFinder::class.java) + } @@ -143,27 +147,34 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder { } protected open fun updateIndex(bankListFileHash: String) { - val banks = loadBankListFile() + try { + val banks = loadBankListFile() - // while indexing - which takes a long time on Android - use InMemoryBankFinder so that user sees at least some search results even though it's slower - bankFinderWhileUpdatingIndex = InMemoryBankFinder(banks) + // while indexing - which takes a long time on Android - use InMemoryBankFinder so that user sees at least some search results even though it's slower + bankFinderWhileUpdatingIndex = InMemoryBankFinder(banks) - fileUtils.deleteFolderRecursively(indexDir) - indexDir.mkdirs() + fileUtils.deleteFolderRecursively(indexDir) // delete current index + indexDir.mkdirs() + writeBanksToIndex(banks, bankListFileHash) + + bankFinderWhileUpdatingIndex = null // now use LuceneBankFinder again for searching + } catch (e: Exception) { + log.error("Could not update index", e) + } + } + + protected open fun writeBanksToIndex(banks: List, bankListFileHash: String) { DocumentsWriter(indexDir).use { writer -> writer.saveDocuments(banks.map { createDocumentForBank(it, writer) - } ) + }) writer.updateDocument(IndexedBankListFileHashIdFieldName, IndexedBankListFileHashIdFieldValue, - fields.storedField(IndexedBankListFileHashFieldName, bankListFileHash) - ) + fields.storedField(IndexedBankListFileHashFieldName, bankListFileHash)) writer.optimizeIndex() } - - bankFinderWhileUpdatingIndex = null } protected open fun createDocumentForBank(bank: BankInfo, writer: DocumentsWriter): Document {