Implemented delegating searches to InMemoryBankFinder during indexing as indexing takes quite a long time on Android

This commit is contained in:
dankito 2020-04-24 13:40:58 +02:00
parent 7a1d6b165a
commit 75ffcabd0d
2 changed files with 28 additions and 4 deletions

View File

@ -3,7 +3,11 @@ package net.dankito.fints.banks
import net.dankito.fints.model.BankInfo import net.dankito.fints.model.BankInfo
open class InMemoryBankFinder : BankFinderBase(), IBankFinder { open class InMemoryBankFinder() : BankFinderBase(), IBankFinder {
internal constructor(bankList: List<BankInfo>) : this() {
this.bankListField = bankList
}
protected var bankListField: List<BankInfo>? = null protected var bankListField: List<BankInfo>? = null

View File

@ -54,7 +54,14 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder {
protected val searcher = Searcher(indexDir) protected val searcher = Searcher(indexDir)
protected var bankFinderWhileUpdatingIndex: IBankFinder? = null
override fun findBankByBankCode(query: String): List<BankInfo> { override fun findBankByBankCode(query: String): List<BankInfo> {
bankFinderWhileUpdatingIndex?.let {
return it.findBankByBankCode(query)
}
if (query.isBlank()) { if (query.isBlank()) {
return getBankList() return getBankList()
} }
@ -65,6 +72,10 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder {
} }
override fun findBankByNameBankCodeOrCity(query: String?): List<BankInfo> { override fun findBankByNameBankCodeOrCity(query: String?): List<BankInfo> {
bankFinderWhileUpdatingIndex?.let {
return it.findBankByNameBankCodeOrCity(query)
}
if (query.isNullOrBlank()) { if (query.isNullOrBlank()) {
return getBankList() return getBankList()
} }
@ -81,10 +92,14 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder {
} }
override fun getBankList(): List<BankInfo> { override fun getBankList(): List<BankInfo> {
bankFinderWhileUpdatingIndex?.let {
return it.getBankList()
}
return getBanksFromQuery(queries.allDocumentsThatHaveField(BankInfoNameFieldName)) return getBanksFromQuery(queries.allDocumentsThatHaveField(BankInfoNameFieldName))
} }
protected fun getBanksFromQuery(query: Query): List<BankInfo> { protected open fun getBanksFromQuery(query: Query): List<BankInfo> {
val results = searcher.search(query, 100_000) // there are more than 16.000 banks in bank list -> 10.000 is too few val results = searcher.search(query, 100_000) // there are more than 16.000 banks in bank list -> 10.000 is too few
return results.hits.map { result -> return results.hits.map { result ->
@ -128,12 +143,15 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder {
} }
protected open fun updateIndex(bankListFileHash: String) { protected open fun updateIndex(bankListFileHash: String) {
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)
fileUtils.deleteFolderRecursively(indexDir) fileUtils.deleteFolderRecursively(indexDir)
indexDir.mkdirs() indexDir.mkdirs()
DocumentsWriter(indexDir).use { writer -> DocumentsWriter(indexDir).use { writer ->
val banks = loadBankListFile()
writer.saveDocuments(banks.map { writer.saveDocuments(banks.map {
createDocumentForBank(it, writer) createDocumentForBank(it, writer)
} ) } )
@ -144,6 +162,8 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder {
writer.optimizeIndex() writer.optimizeIndex()
} }
bankFinderWhileUpdatingIndex = null
} }
protected open fun createDocumentForBank(bank: BankInfo, writer: DocumentsWriter): Document { protected open fun createDocumentForBank(bank: BankInfo, writer: DocumentsWriter): Document {