Log search times
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / SearchPage.kt
index 321146a..577ea90 100644 (file)
@@ -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
@@ -30,9 +31,10 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web
 
        private val cache: Cache<Iterable<Phrase>, Pagination<Post>> = CacheBuilder.newBuilder().ticker(ticker).expireAfterAccess(5, MINUTES).build()
 
-       override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
+       override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
+               val startTime = System.currentTimeMillis()
                val phrases = try {
-                       request.parameters["query"].emptyToNull?.parse()
+                       freenetRequest.parameters["query"].emptyToNull?.parse()
                } catch (te: TextException) {
                        redirect("index.html")
                }
@@ -42,9 +44,9 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web
                        0 -> redirect("index.html")
                        1 -> phrases.first().phrase.also { word ->
                                when {
-                                       word.removePrefix("sone://").let(webInterface.core::getSone).isPresent -> redirect("viewSone.html?sone=${word.removePrefix("sone://")}")
-                                       word.removePrefix("post://").let(webInterface.core::getPost).isPresent -> redirect("viewPost.html?post=${word.removePrefix("post://")}")
-                                       word.removePrefix("reply://").let(webInterface.core::getPostReply).isPresent -> redirect("viewPost.html?post=${word.removePrefix("reply://").let(webInterface.core::getPostReply).get().postId}")
+                                       word.removePrefix("sone://").let(webInterface.core::getSone) != null -> redirect("viewSone.html?sone=${word.removePrefix("sone://")}")
+                                       word.removePrefix("post://").let(webInterface.core::getPost) != null -> redirect("viewPost.html?post=${word.removePrefix("post://")}")
+                                       word.removePrefix("reply://").let(webInterface.core::getPostReply) != null -> redirect("viewPost.html?post=${word.removePrefix("reply://").let(webInterface.core::getPostReply)?.postId}")
                                        word.removePrefix("album://").let(webInterface.core::getAlbum) != null -> redirect("imageBrowser.html?album=${word.removePrefix("album://")}")
                                        word.removePrefix("image://").let { webInterface.core.getImage(it, false) } != null -> redirect("imageBrowser.html?image=${word.removePrefix("image://")}")
                                }
@@ -53,14 +55,15 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web
 
                val sonePagination = webInterface.core.sones
                                .scoreAndPaginate(phrases) { it.allText() }
-                               .apply { page = request.parameters["sonePage"].emptyToNull?.toIntOrNull() ?: 0 }
+                               .apply { page = freenetRequest.parameters["sonePage"].emptyToNull?.toIntOrNull() ?: 0 }
                val postPagination = cache.get(phrases) {
                        webInterface.core.sones
                                        .flatMap(Sone::getPosts)
                                        .filter { Post.FUTURE_POSTS_FILTER.apply(it) }
                                        .scoreAndPaginate(phrases) { it.allText() }
-               }.apply { page = request.parameters["postPage"].emptyToNull?.toIntOrNull() ?: 0 }
+               }.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<Int> {
-               var nextIndex = indexOf(needle)
-               val positions = mutableListOf<Int>()
-               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)