Browse Source

Some refactoring / Scalafication

Abhinav Sarkar 7 years ago
parent
commit
72ac950374

+ 12
- 8
src/main/scala/net/abhinavsarkar/ircsearch/HttpRequestHandler.scala View File

@@ -26,20 +26,24 @@ trait HttpRequestHandler extends ChannelInboundMessageHandlerAdapter[HttpRequest
26 26
     response
27 27
   }
28 28
 
29
-  protected def sendSuccess(ctx : ChannelHandlerContext, request : HttpRequest, body : String) : HttpResponse = {
30
-    val response = new DefaultHttpResponse(HTTP_1_1, OK)
29
+  private def sendReponse(ctx: ChannelHandlerContext, request: HttpRequest,
30
+      response: HttpResponse, body: String): HttpResponse = {
31 31
     response.setContent(Unpooled.copiedBuffer(body.getBytes))
32 32
     response.setHeader(CONTENT_TYPE, "application/json")
33 33
     writeResponse(ctx, request, response)
34 34
     response
35 35
   }
36 36
 
37
-  protected def sendError(ctx : ChannelHandlerContext, request : HttpRequest, body : String) : HttpResponse = {
38
-    val response = new DefaultHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR)
39
-    response.setContent(Unpooled.copiedBuffer(body.getBytes))
40
-    response.setHeader(CONTENT_TYPE, "text/plain")
41
-    writeResponse(ctx, request, response)
42
-    response
37
+  protected def sendSuccess(ctx : ChannelHandlerContext, request : HttpRequest, body : String) = {
38
+    sendReponse(ctx, request, new DefaultHttpResponse(HTTP_1_1, OK), body)
39
+  }
40
+
41
+  protected def sendServerError(ctx : ChannelHandlerContext, request : HttpRequest, body : String) = {
42
+    sendReponse(ctx, request, new DefaultHttpResponse(HTTP_1_1, INTERNAL_SERVER_ERROR), body)
43
+  }
44
+
45
+  protected def sendClientError(ctx : ChannelHandlerContext, request : HttpRequest, body : String) = {
46
+    sendReponse(ctx, request, new DefaultHttpResponse(HTTP_1_1, BAD_REQUEST), body)
43 47
   }
44 48
 
45 49
   protected def writeResponse(

+ 30
- 26
src/main/scala/net/abhinavsarkar/ircsearch/Server.scala View File

@@ -70,7 +70,7 @@ object Server extends App with Logging {
70 70
     }
71 71
   }
72 72
 
73
-  def stopServer(server : ServerBootstrap) {
73
+  private def stopServer(server : ServerBootstrap) {
74 74
     logger.info("Stopping server")
75 75
     server.shutdown
76 76
     logger.info("Stopped server")
@@ -79,7 +79,7 @@ object Server extends App with Logging {
79 79
 }
80 80
 
81 81
 @Sharable
82
-object UnifiedHandler extends ChannelInboundByteHandlerAdapter {
82
+private object UnifiedHandler extends ChannelInboundByteHandlerAdapter {
83 83
 
84 84
   val httpRequestRouter = new HttpRequestRouter {
85 85
     val Echo = "^/echo$".r
@@ -132,7 +132,7 @@ object UnifiedHandler extends ChannelInboundByteHandlerAdapter {
132 132
 
133 133
 }
134 134
 
135
-class TcpIndexHandler extends ChannelInboundMessageHandlerAdapter[String] {
135
+private class TcpIndexHandler extends ChannelInboundMessageHandlerAdapter[String] {
136 136
   var server: String = null
137 137
   var channel : String = null
138 138
   var botName : String = null
@@ -148,14 +148,14 @@ class TcpIndexHandler extends ChannelInboundMessageHandlerAdapter[String] {
148 148
       botName = values(2)
149 149
       inited = true
150 150
     } else {
151
-      Indexer.index(new IndexRequest(server, channel, botName,
151
+      Indexer.index(IndexRequest(server, channel, botName,
152 152
           List(ChatLine(values(0), values(1).toLong, values(2)))))
153 153
     }
154 154
   }
155 155
 }
156 156
 
157 157
 @Sharable
158
-object EchoHandler extends HttpRequestHandler {
158
+private object EchoHandler extends HttpRequestHandler {
159 159
   override def messageReceived(ctx: ChannelHandlerContext, request: HttpRequest) {
160 160
     val content = request.getContent().toString(Charset.forName("UTF-8"))
161 161
     logRequest(ctx, request, sendSuccess(ctx, request, content))
@@ -163,7 +163,7 @@ object EchoHandler extends HttpRequestHandler {
163 163
 }
164 164
 
165 165
 @Sharable
166
-class IndexHandler extends HttpRequestHandler {
166
+private class IndexHandler extends HttpRequestHandler {
167 167
   implicit val formats = DefaultFormats
168 168
   override def messageReceived(ctx: ChannelHandlerContext, request: HttpRequest) {
169 169
     future {
@@ -176,7 +176,7 @@ class IndexHandler extends HttpRequestHandler {
176 176
 }
177 177
 
178 178
 @Sharable
179
-object SearchHandler extends HttpRequestHandler {
179
+private object SearchHandler extends HttpRequestHandler {
180 180
   implicit val formats = DefaultFormats
181 181
   override def messageReceived(ctx: ChannelHandlerContext, request: HttpRequest) {
182 182
     val f = future {
@@ -186,15 +186,12 @@ object SearchHandler extends HttpRequestHandler {
186 186
         Serialization.read[SearchRequest](content)
187 187
       } else if (HttpMethod.GET.equals(method)) {
188 188
         val params = new QueryStringDecoder(request.getUri).getParameters.toMap
189
-        val server = params("server")(0)
190
-        val channel = params("channel")(0)
191
-        val botName = params("botName")(0)
192
-        val query = params("query")(0)
193
-        val page = params.get("page").collect({ case l => l.get(0) })
194
-        val pageSize = params.get("pageSize").collect({ case l => l.get(0) })
195
-        val details = params.get("details").collect({ case l => l.get(0) })
196
-
197
-        var sr = new SearchRequest(server, channel, botName, query)
189
+        val List(server, channel, botName, query) =
190
+          List("server", "channel", "botName", "query").map(params(_).get(0))
191
+        val List(page, pageSize, details) =
192
+          List("page", "pageSize", "details").map(params.get(_).map({ case l => l.get(0) }))
193
+
194
+        var sr = SearchRequest(server, channel, botName, query)
198 195
         if (page.isDefined)
199 196
           sr = sr.copy(page = page.get.toInt)
200 197
         if (pageSize.isDefined)
@@ -210,16 +207,23 @@ object SearchHandler extends HttpRequestHandler {
210 207
     }
211 208
     f onSuccess {
212 209
       case (searchRequest, searchResult) =>
213
-        logRequest(ctx, request,
214
-          sendSuccess(ctx, request,
215
-            if (searchRequest.details)
216
-              Serialization.write(searchResult)
217
-            else
218
-              Serialization.write(searchResult.toSimpleSearchResult)))
210
+        logRequest(ctx, request, sendSuccess(ctx, request,
211
+          Serialization.write(
212
+            if (searchRequest.details) searchResult else searchResult.toSimpleSearchResult)))
213
+    }
214
+    f onFailure {
215
+      case e => {
216
+        logger.error("Error", e)
217
+        val body = Serialization.write(SearchError(e.getMessage))
218
+        e match {
219
+          case e : NoSuchElementException => {
220
+            logRequest(ctx, request, sendClientError(ctx, request, body))
221
+          }
222
+          case _ => {
223
+            logRequest(ctx, request, sendServerError(ctx, request, body))
224
+          }
225
+        }
226
+      }
219 227
     }
220
-    f onFailure { case e : Exception => {
221
-      logger.error("Error", e)
222
-      logRequest(ctx, request, sendError(ctx, request, e.getMessage))
223
-    }}
224 228
   }
225 229
 }

+ 5
- 8
src/main/scala/net/abhinavsarkar/ircsearch/lucene/Indexer.scala View File

@@ -41,9 +41,7 @@ object Indexer extends Logging {
41 41
   object IndexRecord {
42 42
     def fromIndexRequest(indexRequest : IndexRequest) = {
43 43
       val IndexRequest(server, channel, botName, chatLines) = indexRequest
44
-      for {
45
-        chatLine <- chatLines
46
-      } yield new IndexRecord(server, channel, botName, chatLine)
44
+      chatLines.map(IndexRecord(server, channel, botName, _))
47 45
     }
48 46
   }
49 47
 
@@ -64,14 +62,12 @@ object Indexer extends Logging {
64 62
   private val indexers = mutable.Map[String, IndexWriter]()
65 63
 
66 64
   private def close {
67
-    for (indexer <- indexers.values)
68
-      indexer.close
65
+    indexers.values.foreach(_.close)
69 66
     logger.info("Closed Indexer")
70 67
   }
71 68
 
72 69
   private def flush {
73
-    for (indexer <- indexers.values)
74
-      indexer.commit
70
+    indexers.values.foreach(_.commit)
75 71
     logger.info("Flushed Indexer")
76 72
   }
77 73
 
@@ -145,7 +141,7 @@ object Indexer extends Logging {
145 141
 
146 142
   def start {
147 143
     logger.info("Starting indexer")
148
-    indexingFuture = schedule(0, IndexingDurationSecs.max(ContextDurationSecs), TimeUnit.SECONDS) {
144
+    indexingFuture = schedule(0, IndexingDurationSecs, TimeUnit.SECONDS) {
149 145
       if (!indexQueue.isEmpty) {
150 146
         val indexRecs = new ArrayList[IndexRecord]
151 147
         indexQueue drainTo indexRecs
@@ -172,6 +168,7 @@ object Indexer extends Logging {
172 168
         }
173 169
       }
174 170
     }
171
+
175 172
     flushFuture = schedule(0, FlushDurationSecs, TimeUnit.SECONDS) {
176 173
       doInLock(flush)
177 174
     }

+ 16
- 22
src/main/scala/net/abhinavsarkar/ircsearch/lucene/Searcher.scala View File

@@ -28,8 +28,7 @@ object Searcher extends Logging {
28 28
   private val searcherMgrs = mutable.Map[String, SearcherManager]()
29 29
 
30 30
   def close {
31
-    for (searcherMgr <- searcherMgrs.values)
32
-      searcherMgr.close
31
+    searcherMgrs.values.foreach(_.close)
33 32
     logger.info("Closed Searcher")
34 33
   }
35 34
 
@@ -41,7 +40,6 @@ object Searcher extends Logging {
41 40
 
42 41
         val dir = FSDirectory.open(indexDir)
43 42
         searcherMgrs += (dirPath -> new SearcherManager(dir, new SearcherFactory))
44
-
45 43
       }
46 44
     }
47 45
 
@@ -71,23 +69,18 @@ object Searcher extends Logging {
71 69
                 filterQuery.add(clause)
72 70
                 filters += new QueryWrapperFilter(filterQuery)
73 71
               }
74
-              case "before" => {
75
-                  try {
76
-                    val ts = sdf.parse(termQuery.getTerm.text).getTime
77
-                    filters += NumericRangeFilter.newLongRange(
78
-                        ChatLine.TS, 0, ts, true, true)
79
-                  } catch {
80
-                    case e : ParseException => {}
81
-                  }
72
+              case "before" => try {
73
+                val ts = sdf.parse(termQuery.getTerm.text).getTime
74
+                filters += NumericRangeFilter.newLongRange(ChatLine.TS, 0, ts, true, true)
75
+              } catch {
76
+                case e : ParseException => {}
82 77
               }
83
-              case "after" => {
84
-                try {
85
-                  val ts = sdf.parse(termQuery.getTerm.text).getTime
86
-                  filters += NumericRangeFilter.newLongRange(
87
-                      ChatLine.TS, ts, java.lang.Long.MAX_VALUE, true, true)
88
-                } catch {
89
-                  case e : ParseException => {}
90
-                }
78
+              case "after" => try {
79
+                val ts = sdf.parse(termQuery.getTerm.text).getTime
80
+                filters += NumericRangeFilter.newLongRange(
81
+                    ChatLine.TS, ts, java.lang.Long.MAX_VALUE, true, true)
82
+              } catch {
83
+                case e : ParseException => {}
91 84
               }
92 85
               case _ => newQuery.add(clause)
93 86
             }
@@ -108,7 +101,8 @@ object Searcher extends Logging {
108 101
     logger.debug("Searching : [{} {} {}] {}",
109 102
       searchRequest.server, searchRequest.channel, searchRequest.botName, searchRequest.query)
110 103
 
111
-    val indexDir = Indexer.getIndexDir(searchRequest.server, searchRequest.channel, searchRequest.botName)
104
+    val indexDir =
105
+      Indexer.getIndexDir(searchRequest.server, searchRequest.channel, searchRequest.botName)
112 106
     val analyzer = Indexer.mkAnalyzer
113 107
     try {
114 108
       val queryParser = mkQueryParser(analyzer)
@@ -147,10 +141,10 @@ object Searcher extends Logging {
147 141
         val LineRe = "(\\d+) (.*?): (.*)".r
148 142
         val List(ctxBefore, ctxAfter) = List(contextBefore, contextAfter).map {
149 143
           _.split('\n').filterNot(_.isEmpty).map {
150
-            case LineRe(timestamp, user, message) => new ChatLine(user, timestamp.toLong, message)
144
+            case LineRe(timestamp, user, message) => ChatLine(user, timestamp.toLong, message)
151 145
           }}
152 146
 
153
-        val chatLine = new ChatLine(user, timestamp.toLong, message, ctxBefore.toList, ctxAfter.toList)
147
+        val chatLine = ChatLine(user, timestamp.toLong, message, ctxBefore.toList, ctxAfter.toList)
154 148
         (chatLine, score)
155 149
       }
156 150
       (topDocs.totalHits, docs.toList)

+ 2
- 0
src/main/scala/net/abhinavsarkar/ircsearch/model.scala View File

@@ -43,3 +43,5 @@ object SearchResult {
43 43
 case class SimpleSearchResult(
44 44
     server : String, channel : String, botName : String, query: String,
45 45
     page : Int, pageSize : Int, totalResults : Int, lines : List[List[List[String]]])
46
+
47
+case class SearchError(error : String)

Loading…
Cancel
Save