X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FSearchPage.kt;h=577ea9021d73cdbfd74f253842ad2bf1cf709e1f;hb=a9c9094ae93da0d112df6bb1a1d601cde4abe4ff;hp=02d63c5742250c7227ebacde3e6cf7b6da075fe6;hpb=fd32e307db87c617a4c455f592b6e649ec50ae8a;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt index 02d63c5..577ea90 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -3,6 +3,7 @@ package net.pterodactylus.sone.web.pages import com.google.common.base.Ticker import com.google.common.cache.Cache import com.google.common.cache.CacheBuilder +import freenet.support.Logger import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Sone @@ -31,6 +32,7 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web private val cache: Cache, Pagination> = CacheBuilder.newBuilder().ticker(ticker).expireAfterAccess(5, MINUTES).build() override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + val startTime = System.currentTimeMillis() val phrases = try { freenetRequest.parameters["query"].emptyToNull?.parse() } catch (te: TextException) { @@ -61,6 +63,7 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web .scoreAndPaginate(phrases) { it.allText() } }.apply { page = freenetRequest.parameters["postPage"].emptyToNull?.toIntOrNull() ?: 0 } + Logger.normal(SearchPage::class.java, "Finished search for “${freenetRequest.parameters["query"]}” in ${System.currentTimeMillis() - startTime}ms.") templateContext["sonePagination"] = sonePagination templateContext["soneHits"] = sonePagination.items templateContext["postPagination"] = postPagination @@ -106,15 +109,12 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2) } - private fun String.findAll(needle: String): List { - var nextIndex = indexOf(needle) - val positions = mutableListOf() - while (nextIndex != -1) { - positions += nextIndex - nextIndex = indexOf(needle, nextIndex + 1) - } - return positions - } + private fun String.findAll(needle: String) = + generateSequence(indexOf(needle).takeIf { it > -1 }) { lastPosition -> + lastPosition + .let { indexOf(needle, it + 1) } + .takeIf { it > -1 } + }.toList() private fun String.parse() = StringEscaper.parseLine(this)