Fix circular dependency
This commit is contained in:
+10
-15
@@ -2,6 +2,8 @@ package be.simplenotes.search
|
||||
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import be.simplenotes.domain.usecases.search.NoteSearcher
|
||||
import be.simplenotes.domain.usecases.search.SearchTerms
|
||||
import be.simplenotes.search.utils.rmdir
|
||||
import org.apache.lucene.analysis.standard.StandardAnalyzer
|
||||
import org.apache.lucene.index.*
|
||||
@@ -10,17 +12,10 @@ import org.apache.lucene.store.Directory
|
||||
import org.apache.lucene.store.FSDirectory
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.nio.file.FileVisitResult
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.SimpleFileVisitor
|
||||
import java.nio.file.attribute.BasicFileAttributes
|
||||
import java.util.*
|
||||
|
||||
data class SearchTerms(val title: String?, val tag: String?, val content: String?)
|
||||
|
||||
class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
|
||||
class NoteSearcherImpl(basePath: Path = Path.of("/tmp", "lucene")) : NoteSearcher {
|
||||
private val baseFile = basePath.toFile()
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
@@ -38,7 +33,7 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
|
||||
}
|
||||
// endregion
|
||||
|
||||
fun indexNote(userId: Int, note: PersistedNote) {
|
||||
override fun indexNote(userId: Int, note: PersistedNote) {
|
||||
logger.debug("Indexing note ${note.uuid} for user $userId")
|
||||
|
||||
val dir = getDirectory(userId)
|
||||
@@ -53,7 +48,7 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
|
||||
}
|
||||
}
|
||||
|
||||
fun indexNotes(userId: Int, notes: List<PersistedNote>) {
|
||||
override fun indexNotes(userId: Int, notes: List<PersistedNote>) {
|
||||
logger.debug("Indexing notes for user $userId")
|
||||
|
||||
val dir = getDirectory(userId)
|
||||
@@ -68,7 +63,7 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteIndex(userId: Int, uuid: UUID) {
|
||||
override fun deleteIndex(userId: Int, uuid: UUID) {
|
||||
logger.debug("Deleting index $uuid for user $userId")
|
||||
|
||||
val dir = getDirectory(userId)
|
||||
@@ -82,13 +77,13 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
|
||||
}
|
||||
}
|
||||
|
||||
fun updateIndex(userId: Int, note: PersistedNote) {
|
||||
override fun updateIndex(userId: Int, note: PersistedNote) {
|
||||
logger.debug("Updating note ${note.uuid} for user $userId")
|
||||
deleteIndex(userId, note.uuid)
|
||||
indexNote(userId, note)
|
||||
}
|
||||
|
||||
fun search(userId: Int, terms: SearchTerms): List<PersistedNoteMetadata> {
|
||||
override fun search(userId: Int, terms: SearchTerms): List<PersistedNoteMetadata> {
|
||||
val searcher = getIndexSearcher(userId)
|
||||
|
||||
val builder = BooleanQuery.Builder()
|
||||
@@ -115,8 +110,8 @@ class NoteSearcher(basePath: Path = Path.of("/tmp", "lucene")) {
|
||||
return topDocs.toResults(searcher)
|
||||
}
|
||||
|
||||
fun dropIndex(userId: Int) = rmdir(File(baseFile, userId.toString()).toPath())
|
||||
override fun dropIndex(userId: Int) = rmdir(File(baseFile, userId.toString()).toPath())
|
||||
|
||||
fun dropAll() = rmdir(baseFile.toPath())
|
||||
override fun dropAll() = rmdir(baseFile.toPath())
|
||||
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package be.simplenotes.search
|
||||
|
||||
import be.simplenotes.domain.usecases.search.NoteSearcher
|
||||
import org.koin.dsl.module
|
||||
import java.nio.file.Path
|
||||
|
||||
val searchModule = module {
|
||||
single { NoteSearcher(Path.of(".lucene")) }
|
||||
single<NoteSearcher> { NoteSearcherImpl(Path.of(".lucene")) }
|
||||
}
|
||||
|
||||
+3
-2
@@ -3,6 +3,7 @@ package be.simplenotes.search
|
||||
import be.simplenotes.domain.model.NoteMetadata
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import be.simplenotes.domain.usecases.search.SearchTerms
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.intellij.lang.annotations.Language
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
@@ -13,10 +14,10 @@ import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
@ResourceLock("lucene")
|
||||
internal class NoteSearcherTest {
|
||||
internal class NoteSearcherImplTest {
|
||||
|
||||
// region setup
|
||||
private val searcher = NoteSearcher()
|
||||
private val searcher = NoteSearcherImpl()
|
||||
|
||||
private fun index(
|
||||
title: String,
|
||||
Reference in New Issue
Block a user