Skip to content
Browse files

Made lucene.Database an Actor so it can be used asynchronously.

Thanks to this change it's was possible to change
LuceneOptimizedDatabase so it asks for Lucene indexing of latest
change asynchronously.

Change-Id: Icc98bd668aedb643ac30714cdc514c83f7a3e104
Signed-off-by: Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com>
  • Loading branch information...
1 parent 75e8deb commit e011a81ed54bcf947a1964e1e4c9c17375af40d5 @gkossakowski committed Jun 3, 2010
View
17 src/main/scala/com/google/gimd/lucene/Database.scala
@@ -16,19 +16,21 @@ package com.google.gimd.lucene
import org.apache.lucene.store.Directory
import org.apache.lucene.index.{IndexCommit, IndexReader}
import org.eclipse.jgit.revwalk.{RevCommit, RevWalk}
+import org.eclipse.jgit.lib.{ObjectIdSubclassMap, ObjectId, AnyObjectId}
import com.google.gimd.lucene.Database.{UD_COMMIT, LuceneState}
import org.apache.lucene.analysis.SimpleAnalyzer
import com.google.gimd.file.FileType
import com.google.gimd.jgit.JGitBranch
+import actors.Actor
/**
* Database that manages Lucene's Index for a given branch and list of file types.
*
* The management is automatic which means it's Database responsibility to keep Index up-to-date
* and to doing in efficient way.
*/
-final class Database(val branch: JGitBranch, val fileTypes: List[FileType[_]]) {
+final class Database(val branch: JGitBranch, val fileTypes: List[FileType[_]]) extends Actor {
val luceneDirectory: Directory = {
import java.io.File
@@ -38,6 +40,17 @@ final class Database(val branch: JGitBranch, val fileTypes: List[FileType[_]]) {
private var state: ObjectIdSubclassMap[LuceneState] = null
+ start()
+
+ def act = {
+ state == null
+ loop {
+ react {
+ case targetCommit: AnyObjectId => reply(read(targetCommit))
+ }
+ }
+ }
+
/**
* Obtain an index reader to search through a specific Git commit.
* <p>
@@ -52,7 +65,7 @@ final class Database(val branch: JGitBranch, val fileTypes: List[FileType[_]]) {
* @return the index reader for the requested commit.
* @throws IOException
*/
- def read(targetCommit: AnyObjectId): IndexReader = {
+ protected def read(targetCommit: AnyObjectId): IndexReader = {
if (state == null) {
state = scan(luceneDirectory)
}
View
9 src/main/scala/com/google/gimd/lucene/LuceneOptimizedDatabase.scala
@@ -15,7 +15,8 @@ package com.google.gimd.lucene
import java.io.IOException
import com.google.gimd.jgit.{JGitDatabaseException, JGitSnapshot, JGitDatabase}
-import org.spearce.jgit.revwalk.RevWalk
+import com.google.gimd.Snapshot
+import com.google.gimd.modification.DatabaseModification
trait LuceneOptimizedDatabase extends JGitDatabase {
@@ -31,4 +32,10 @@ trait LuceneOptimizedDatabase extends JGitDatabase {
}
}
+ override def modifyAndReturn[T](modification: Snapshot => (DatabaseModification, T)): T = {
+ val (result, commitId) = super.modifyAndReturnWithCommit(modification)
+ (luceneDb ! commitId)
+ result
+ }
+
}
View
3 src/main/scala/com/google/gimd/lucene/LuceneOptimizedSnapshot.scala
@@ -19,6 +19,7 @@ import com.google.gimd.jgit.{FileTypeTreeFilter, JGitSnapshot}
import com.google.gimd.file.{File, FileType}
import org.apache.lucene.search.{Scorer, Collector, IndexSearcher, Query => LQuery}
import collection.mutable.ListBuffer
+import org.apache.lucene.index.IndexReader
/**
* LuceneOptimizedSnapshot is a trait that implements query optimization by using Lucene's index.
@@ -36,7 +37,7 @@ trait LuceneOptimizedSnapshot extends JGitSnapshot {
val luceneQuery = QueryBuilder(q)
def searchLucene(luceneQuery: LQuery): Iterator[File[W]] = {
- val indexReader = luceneDb.read(commit)
+ val indexReader = (luceneDb !? commit).asInstanceOf[IndexReader]
val searcher = new IndexSearcher(indexReader)
val collector = new PathCollector
searcher.search(luceneQuery, collector)

0 comments on commit e011a81

Please sign in to comment.
Something went wrong with that request. Please try again.