From e1a8b812f73a246665b99cdc2730e25052aa90cf Mon Sep 17 00:00:00 2001 From: Matt Hicks Date: Sun, 24 Mar 2024 13:17:13 -0500 Subject: [PATCH] Cleaning up --- .../index/lucene/LuceneIndexerSupport.scala | 37 ++++--------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/lucene/src/main/scala/lightdb/index/lucene/LuceneIndexerSupport.scala b/lucene/src/main/scala/lightdb/index/lucene/LuceneIndexerSupport.scala index c4ad8fe7..9629635d 100644 --- a/lucene/src/main/scala/lightdb/index/lucene/LuceneIndexerSupport.scala +++ b/lucene/src/main/scala/lightdb/index/lucene/LuceneIndexerSupport.scala @@ -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) { @@ -61,7 +55,7 @@ case class LuceneIndexer[D <: Document[D]](collection: Collection[D], } } if (document.iterator().hasNext) { - writer.add(document) + indexWriter.addDocument(document) } value } @@ -69,8 +63,8 @@ case class LuceneIndexer[D <: Document[D]](collection: Collection[D], 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 { @@ -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() } @@ -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)