Browse Source

Moved out configuration to external file

Abhinav Sarkar 7 years ago
parent
commit
3d03db775d

+ 20
- 0
irc-search.conf View File

@@ -0,0 +1,20 @@
1
+server {
2
+  port = 9090
3
+}
4
+
5
+indexing {
6
+  context {
7
+    size = 2
8
+    durationSecs = 20
9
+  }
10
+  
11
+  runIntervalSecs = 10
12
+  flushIntervalSecs = 60
13
+  rateLimitPerSec = 1000
14
+}
15
+
16
+searching {
17
+  maxHits = 1000
18
+  messageFieldBoost = 2
19
+}
20
+

+ 11
- 0
pom.xml View File

@@ -84,6 +84,17 @@
84 84
 			<artifactId>guava</artifactId>
85 85
 			<version>14.0.1</version>
86 86
 		</dependency>
87
+		<dependency>
88
+			<groupId>org.streum</groupId>
89
+			<artifactId>configrity-core_${scala.majorversion}</artifactId>
90
+			<version>1.0.0</version>
91
+			<exclusions>
92
+				<exclusion>
93
+					<groupId>org.scalatest</groupId>
94
+					<artifactId>scalatest_${scala.majorversion}</artifactId>
95
+				</exclusion>
96
+			</exclusions>
97
+		</dependency>
87 98
 	</dependencies>
88 99
 
89 100
 	<build>

+ 34
- 36
src/main/scala/net/abhinavsarkar/ircsearch/Server.scala View File

@@ -9,6 +9,8 @@ import scala.concurrent.future
9 9
 
10 10
 import com.typesafe.scalalogging.slf4j.Logging
11 11
 
12
+import org.streum.configrity.Configuration
13
+
12 14
 import au.com.bytecode.opencsv.CSVParser
13 15
 
14 16
 import io.netty.bootstrap.ServerBootstrap
@@ -31,42 +33,39 @@ import net.liftweb.json.Serialization
31 33
 
32 34
 object Server extends App with Logging {
33 35
 
34
-  if (args.isEmpty) {
35
-    println("Please specify port to run the server on")
36
-    System.exit(1)
37
-  } else {
38
-    val port = args(0).toInt
39
-    logger.info("Starting server at port {}", port: Integer)
40
-
41
-    val server = (new ServerBootstrap)
42
-      .group(new NioEventLoopGroup(1), new NioEventLoopGroup(1))
43
-      .channel(classOf[NioServerSocketChannel])
44
-      .childHandler(new ChannelInitializer[SocketChannel] {
45
-        def initChannel(ch: SocketChannel) {
46
-          val p = ch.pipeline
47
-            .addLast("unihandler", UnifiedHandler)
48
-        }})
49
-      .localAddress(new InetSocketAddress(port))
50
-
51
-    val cleanup = { () =>
52
-      stopServer(server)
53
-      Indexer.stop
54
-      Searcher.close
55
-    }
36
+  val config = Configuration.loadResource("/irc-search.conf")
37
+  val port = config[Int]("server.port")
38
+
39
+  logger.info("Starting server at port {}", port: Integer)
40
+
41
+  val server = (new ServerBootstrap)
42
+    .group(new NioEventLoopGroup(1), new NioEventLoopGroup(1))
43
+    .channel(classOf[NioServerSocketChannel])
44
+    .childHandler(new ChannelInitializer[SocketChannel] {
45
+      def initChannel(ch: SocketChannel) {
46
+        val p = ch.pipeline
47
+          .addLast("unihandler", UnifiedHandler)
48
+      }})
49
+    .localAddress(new InetSocketAddress(port))
50
+
51
+  val cleanup = { () =>
52
+    stopServer(server)
53
+    Indexer.stop
54
+    Searcher.close
55
+  }
56 56
 
57
-    Runtime.getRuntime.addShutdownHook(
58
-      new Thread("ShutdownHook") {
59
-        override def run = cleanup()
60
-      })
61
-
62
-    try {
63
-      Indexer.start
64
-      server.bind().sync.channel.closeFuture.sync
65
-    } catch {
66
-      case e : Exception => {
67
-        logger.error("Exception while running server. Stopping server", e)
68
-        cleanup()
69
-      }
57
+  Runtime.getRuntime.addShutdownHook(
58
+    new Thread("ShutdownHook") {
59
+      override def run = cleanup()
60
+    })
61
+
62
+  try {
63
+    Indexer.start
64
+    server.bind().sync.channel.closeFuture.sync
65
+  } catch {
66
+    case e : Exception => {
67
+      logger.error("Exception while running server. Stopping server", e)
68
+      cleanup()
70 69
     }
71 70
   }
72 71
 
@@ -77,7 +76,6 @@ object Server extends App with Logging {
77 76
   }
78 77
 
79 78
 }
80
-
81 79
 @Sharable
82 80
 private object UnifiedHandler extends ChannelInboundByteHandlerAdapter {
83 81
 

+ 16
- 13
src/main/scala/net/abhinavsarkar/ircsearch/lucene/Indexer.scala View File

@@ -20,6 +20,7 @@ import org.apache.lucene.document.{ Field, LongField, StringField, TextField }
20 20
 import org.apache.lucene.index.{ IndexWriter, IndexWriterConfig }
21 21
 import org.apache.lucene.store.FSDirectory
22 22
 import org.apache.lucene.util.Version
23
+import org.streum.configrity.Configuration
23 24
 
24 25
 import com.google.common.util.concurrent.RateLimiter
25 26
 import com.typesafe.scalalogging.slf4j.Logging
@@ -45,12 +46,14 @@ object Indexer extends Logging {
45 46
     }
46 47
   }
47 48
 
48
-  val LUCENE_VERSION = Version.LUCENE_43
49
-  val ContextSize = 2
50
-  val ContextDurationSecs = 20
51
-  val IndexingDurationSecs = 10
52
-  val FlushDurationSecs = 60
53
-  val RateLimitPerSec = 1000
49
+  private val config = Configuration.loadResource("/irc-search.conf").detach("indexing")
50
+
51
+  val LuceneVersion = Version.LUCENE_43
52
+  private val ContextSize = config[Int]("context.size")
53
+  private val ContextDurationSecs = config[Int]("context.durationSecs")
54
+  private val RunIntervalSecs = config[Int]("runIntervalSecs")
55
+  private val FlushIntervalSecs = config[Int]("flushIntervalSecs")
56
+  private val RateLimitPerSec = config[Int]("rateLimitPerSec")
54 57
 
55 58
   private val indexQueue = new PriorityBlockingQueue[IndexRecord]
56 59
   private val scheduler = Executors.newScheduledThreadPool(2)
@@ -72,12 +75,12 @@ object Indexer extends Logging {
72 75
   }
73 76
 
74 77
   def mkAnalyzer : Analyzer = {
75
-    val defAnalyzer = new StandardAnalyzer(LUCENE_VERSION)
78
+    val defAnalyzer = new StandardAnalyzer(LuceneVersion)
76 79
     val fieldAnalyzers = Map(
77 80
         ChatLine.USER -> new KeywordAnalyzer,
78
-        ChatLine.MSG -> new EnglishAnalyzer(LUCENE_VERSION),
79
-        ChatLine.CTXB -> new EnglishAnalyzer(LUCENE_VERSION),
80
-        ChatLine.CTXA -> new EnglishAnalyzer(LUCENE_VERSION))
81
+        ChatLine.MSG -> new EnglishAnalyzer(LuceneVersion),
82
+        ChatLine.CTXB -> new EnglishAnalyzer(LuceneVersion),
83
+        ChatLine.CTXA -> new EnglishAnalyzer(LuceneVersion))
81 84
 
82 85
     new PerFieldAnalyzerWrapper(defAnalyzer, fieldAnalyzers)
83 86
   }
@@ -90,7 +93,7 @@ object Indexer extends Logging {
90 93
           assert(indexDir.isDirectory)
91 94
         }
92 95
         val indexer = new IndexWriter(FSDirectory.open(indexDir),
93
-            new IndexWriterConfig(LUCENE_VERSION, mkAnalyzer))
96
+            new IndexWriterConfig(LuceneVersion, mkAnalyzer))
94 97
         indexers += (dirPath -> indexer)
95 98
       }
96 99
     }
@@ -141,7 +144,7 @@ object Indexer extends Logging {
141 144
 
142 145
   def start {
143 146
     logger.info("Starting indexer")
144
-    indexingFuture = schedule(0, IndexingDurationSecs, TimeUnit.SECONDS) {
147
+    indexingFuture = schedule(0, RunIntervalSecs, TimeUnit.SECONDS) {
145 148
       if (!indexQueue.isEmpty) {
146 149
         val indexRecs = new ArrayList[IndexRecord]
147 150
         indexQueue drainTo indexRecs
@@ -169,7 +172,7 @@ object Indexer extends Logging {
169 172
       }
170 173
     }
171 174
 
172
-    flushFuture = schedule(0, FlushDurationSecs, TimeUnit.SECONDS) {
175
+    flushFuture = schedule(0, FlushIntervalSecs, TimeUnit.SECONDS) {
173 176
       doInLock(flush)
174 177
     }
175 178
   }

+ 6
- 3
src/main/scala/net/abhinavsarkar/ircsearch/lucene/Searcher.scala View File

@@ -15,6 +15,7 @@ import org.apache.lucene.search.{ BooleanClause, BooleanQuery, Filter, FilteredQ
15 15
                                   NumericRangeFilter, Query, QueryWrapperFilter, SearcherFactory,
16 16
                                   SearcherManager, Sort, SortField, TermQuery }
17 17
 import org.apache.lucene.store.FSDirectory
18
+import org.streum.configrity.Configuration
18 19
 
19 20
 import com.typesafe.scalalogging.slf4j.Logging
20 21
 
@@ -22,8 +23,10 @@ import net.abhinavsarkar.ircsearch.model._
22 23
 
23 24
 object Searcher extends Logging {
24 25
 
25
-  val MaxHits = 1000
26
-  val MessageFieldBoost = java.lang.Float.valueOf(2.0f)
26
+  private val config = Configuration.loadResource("/irc-search.conf").detach("searching")
27
+
28
+  private val MaxHits = config[Int]("maxHits")
29
+  private val MessageFieldBoost = java.lang.Float.valueOf(config[Float]("messageFieldBoost"))
27 30
 
28 31
   private val searcherMgrs = mutable.Map[String, SearcherManager]()
29 32
 
@@ -47,7 +50,7 @@ object Searcher extends Logging {
47 50
   }
48 51
 
49 52
   private def mkQueryParser(analyzer : Analyzer) =
50
-    new MultiFieldQueryParser(Indexer.LUCENE_VERSION,
53
+    new MultiFieldQueryParser(Indexer.LuceneVersion,
51 54
         List(ChatLine.MSG, ChatLine.CTXB, ChatLine.CTXA).toArray, analyzer,
52 55
         Map(ChatLine.MSG -> MessageFieldBoost))
53 56
 

Loading…
Cancel
Save