Skip to content

Commit

Permalink
Refactored the search engine processing pipeline to be more efficient.
Browse files Browse the repository at this point in the history
Partial functions FTW!
  • Loading branch information
jponge committed Feb 23, 2012
1 parent 0cb3aef commit 3dc2d0a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
12 changes: 4 additions & 8 deletions finagle-test.iws
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<file leaf-file-name="MyApp.scala" pinned="false" current="true" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/main/scala/MyApp.scala">
<provider selected="true" editor-type-id="text-editor">
<state line="119" column="23" selection-start="3617" selection-end="3617" vertical-scroll-proportion="0.73214287">
<state line="60" column="16" selection-start="1820" selection-end="1820" vertical-scroll-proportion="0.56643355">
<folding />
</state>
</provider>
Expand Down Expand Up @@ -140,10 +140,6 @@
<option name="myItemId" value="finagle-test" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ProjectViewProjectNode" />
</PATH_ELEMENT>
<PATH_ELEMENT>
<option name="myItemId" value="External Libraries" />
<option name="myItemType" value="com.intellij.ide.projectView.impl.nodes.ExternalLibrariesNode" />
</PATH_ELEMENT>
</PATH>
<PATH>
<PATH_ELEMENT>
Expand Down Expand Up @@ -372,7 +368,7 @@
</component>
<component name="ToolWindowManager">
<frame x="0" y="22" width="1440" height="874" extended-state="0" />
<editor active="true" />
<editor active="false" />
<layout>
<window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
<window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
Expand All @@ -385,7 +381,7 @@
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="true" content_ui="tabs" />
<window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.24928162" sideWeight="0.6701987" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="true" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.24928162" sideWeight="0.6701987" order="0" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
Expand Down Expand Up @@ -476,7 +472,7 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/main/scala/MyApp.scala">
<provider selected="true" editor-type-id="text-editor">
<state line="119" column="23" selection-start="3617" selection-end="3617" vertical-scroll-proportion="0.73214287">
<state line="60" column="16" selection-start="1820" selection-end="1820" vertical-scroll-proportion="0.56643355">
<folding />
</state>
</provider>
Expand Down
68 changes: 38 additions & 30 deletions src/main/scala/MyApp.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package my.app

import com.twitter.finagle.{Service, SimpleFilter}
import my.app.MyApp.WikipediaSearchFilter
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.handler.codec.http.HttpResponseStatus._
import org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1
Expand Down Expand Up @@ -56,69 +55,78 @@ object MyApp {
client(request) ensure { client.release() }
}
}

case class SearchEngineQuery(
query: String,
keywords: Seq[String]
)

trait SearchEngineFilter extends SimpleFilter[HttpRequest, HttpResponse] {
trait SearchEngineProcessor extends PartialFunction[String, SearchEngineQuery] {

def searchEngineTest: Regex
def queryExtractor: Regex
def keywordSplitter: Regex

def apply(request: HttpRequest, service: Service[HttpRequest, HttpResponse]) = {
def isDefinedAt(uri: String) = searchEngineTest.findFirstIn(uri).isDefined

val uri = request.getUri()
if (searchEngineTest.findFirstIn(uri).isDefined) {
val query = queryExtractor.findFirstMatchIn(uri).get.group(1)
val keywords = keywordSplitter.split(query)
println(keywords.reduce { _ + ", " + _ })
}
def apply(uri: String): SearchEngineQuery = {
val query = queryExtractor.findFirstMatchIn(uri).get.group(1)
val keywords = keywordSplitter.split(query)

service(request)
SearchEngineQuery(query, keywords)
}
}

class GoogleSearchFilter extends SearchEngineFilter {

class GoogleSearch extends SearchEngineProcessor {
val searchEngineTest = "www.google.*q=.*".r
val queryExtractor = "q=([^&]*)".r
val keywordSplitter = "(%20)|(\\+)".r
}

class BingSearchFilter extends SearchEngineFilter {

class BingSearch extends SearchEngineProcessor {
val searchEngineTest = "www.bing.com.*q=.*".r
val queryExtractor = "q=([^&]*)".r
val keywordSplitter = "\\+".r
}

class YahooSearchFilter extends SearchEngineFilter {

class YahooSearch extends SearchEngineProcessor {
val searchEngineTest = "search.yahoo.com.*p=.*".r
val queryExtractor = "p=([^&]*)".r
val keywordSplitter = "(%20)|(\\+)".r
}

class WikipediaSearchFilter extends SearchEngineFilter {

class WikipediaSearch extends SearchEngineProcessor {
val searchEngineTest = "wikipedia.org.*search=.*".r
val queryExtractor = "search=([^&]*)".r
val keywordSplitter = "\\+".r
}

class SearchEngineFilter(val processor: String => Option[SearchEngineQuery]) extends SimpleFilter[HttpRequest, HttpResponse] {

def apply(request: HttpRequest, service: Service[HttpRequest, HttpResponse]) = {

val query = processor(request.getUri())
if (query.isDefined) {
println(query.get)
}

service(request)
}
}

def main(args: Array[String]) {
val handleExceptions = new HandleExceptions
val proxyClient = new ProxyHttpClient
val googleFilter = new GoogleSearchFilter
val bingFilter = new BingSearchFilter
val yahooFilter = new YahooSearchFilter
val wikipediaFilter = new WikipediaSearchFilter

val myService =
handleExceptions andThen
googleFilter andThen
bingFilter andThen
yahooFilter andThen
wikipediaFilter andThen
proxyClient

val google = new GoogleSearch
val bing = new BingSearch
val yahoo = new YahooSearch
val wikipedia = new WikipediaSearch

val searchEngineProcessor = (google orElse bing orElse yahoo orElse wikipedia).lift
val searchEngineFilter = new SearchEngineFilter(searchEngineProcessor)

val myService = handleExceptions andThen searchEngineFilter andThen proxyClient

val server: Server = ServerBuilder()
.codec(Http())
Expand Down

0 comments on commit 3dc2d0a

Please sign in to comment.