Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Mar 24, 2024
1 parent d7eb5b9 commit e1a8b81
Showing 1 changed file with 8 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ case class LuceneIndexer[D <: Document[D]](collection: Collection[D],
.map(p => FSDirectory.open(p))
.getOrElse(new ByteBuffersDirectory())

private var _writer: LuceneWriter = _
private lazy val config = new IndexWriterConfig(analyzer)
private lazy val indexWriter = new IndexWriter(directory, config)
private var _reader: LuceneReader = _

private def writer: LuceneWriter = synchronized {
if (disposed) throw new RuntimeException("LuceneIndexer is already disposed")
if (_writer == null) {
_writer = new LuceneWriter
}
_writer
}
private def reader: LuceneReader = synchronized {
if (disposed) throw new RuntimeException("LuceneIndexer is already disposed")
if (_reader == null) {
Expand All @@ -61,16 +55,16 @@ case class LuceneIndexer[D <: Document[D]](collection: Collection[D],
}
}
if (document.iterator().hasNext) {
writer.add(document)
indexWriter.addDocument(document)
}
value
}

override def delete(id: Id[D]): IO[Unit] = IO.unit

override def commit(): IO[Unit] = IO {
writer.flush()
writer.commit()
indexWriter.flush()
indexWriter.commit()
}

override def count(): IO[Long] = IO {
Expand All @@ -92,11 +86,9 @@ case class LuceneIndexer[D <: Document[D]](collection: Collection[D],
} yield ()

def close(): IO[Unit] = IO {
if (_writer != null) {
_writer.flush()
_writer.commit()
_writer.close()
}
indexWriter.flush()
indexWriter.commit()
indexWriter.close()
if (_reader != null) {
_reader.close()
}
Expand All @@ -106,19 +98,6 @@ case class LuceneIndexer[D <: Document[D]](collection: Collection[D],
disposed = true
}

class LuceneWriter {
private lazy val config = new IndexWriterConfig(analyzer)
private lazy val indexWriter = new IndexWriter(directory, config)

def add(document: LuceneDocument): Unit = indexWriter.addDocument(document)
def flush(): Unit = indexWriter.flush()
def commit(): Unit = indexWriter.commit()
def close(): Unit = {
indexWriter.close()
_writer = null
}
}

class LuceneReader {
private lazy val indexReader = DirectoryReader.open(directory)
private lazy val indexSearcher = new IndexSearcher(indexReader)
Expand Down

0 comments on commit e1a8b81

Please sign in to comment.