Search algorithm improved

Language settings in default tag fixed
This commit is contained in:
tom5079
2019-07-03 19:40:19 +09:00
parent 2046d87031
commit 3992a07340
6 changed files with 152 additions and 85 deletions

View File

@@ -1,13 +1,12 @@
package xyz.quaver.hitomi
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import java.util.*
import java.util.concurrent.Executors
val searchDispatcher = Executors.newFixedThreadPool(4).asCoroutineDispatcher()
fun doSearch(query: String) : List<Int> {
val time = System.currentTimeMillis()
val terms = query
.trim()
.replace(Regex("""^\?"""), "")
@@ -43,24 +42,24 @@ fun doSearch(query: String) : List<Int> {
//positive results
positiveTerms.map {
launch(searchDispatcher) {
val newResults = getGalleryIDsForQuery(it)
filterPositive(newResults.sorted())
async(Dispatchers.IO) {
Pair(getGalleryIDsForQuery(it), true)
}
}+negativeTerms.map {
async(Dispatchers.IO) {
Pair(getGalleryIDsForQuery(it), false)
}
}.forEach {
it.join()
}
val (result, isPositive) = it.await()
//negative results
negativeTerms.map {
launch(searchDispatcher) {
filterNegative(getGalleryIDsForQuery(it).sorted())
when {
isPositive -> filterPositive(result.sorted())
else -> filterNegative(result.sorted())
}
}.forEach {
it.join()
}
}
return results
println("PUPIL/SEARCH TIME ${System.currentTimeMillis() - time}ms")
return results
}