Fix circular dependency

This commit is contained in:
Hubert Van De Walle 2020-08-19 19:02:15 +02:00
parent 68109f8666
commit 08c804ccb5
10 changed files with 42 additions and 28 deletions

View File

@ -14,6 +14,11 @@
<artifactId>persistance</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>be.simplenotes</groupId>
<artifactId>search</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>be.simplenotes</groupId>
<artifactId>domain</artifactId>

View File

@ -9,7 +9,7 @@ import be.simplenotes.domain.usecases.NoteService
import be.simplenotes.domain.usecases.markdown.InvalidMeta
import be.simplenotes.domain.usecases.markdown.MissingMeta
import be.simplenotes.domain.usecases.markdown.ValidationError
import be.simplenotes.search.SearchTerms
import be.simplenotes.domain.usecases.search.SearchTerms
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response

View File

@ -1,6 +1,6 @@
package be.simplenotes.app.utils
import be.simplenotes.search.SearchTerms
import be.simplenotes.domain.usecases.search.SearchTerms
private val titleRe = """title:['"](?<title>.*?)['"]""".toRegex()
private val outerTitleRe = """(?<title>title:['"].*?['"])""".toRegex()

View File

@ -1,6 +1,6 @@
package be.simplenotes.app.utils
import be.simplenotes.search.SearchTerms
import be.simplenotes.domain.usecases.search.SearchTerms
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

View File

@ -14,11 +14,6 @@
<artifactId>shared</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>be.simplenotes</groupId>
<artifactId>search</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>be.simplenotes</groupId>
<artifactId>shared</artifactId>

View File

@ -10,8 +10,8 @@ import be.simplenotes.domain.usecases.markdown.MarkdownConverter
import be.simplenotes.domain.usecases.markdown.MarkdownParsingError
import be.simplenotes.domain.usecases.repositories.NoteRepository
import be.simplenotes.domain.usecases.repositories.UserRepository
import be.simplenotes.search.NoteSearcher
import be.simplenotes.search.SearchTerms
import be.simplenotes.domain.usecases.search.NoteSearcher
import be.simplenotes.domain.usecases.search.SearchTerms
import java.util.*
class NoteService(

View File

@ -0,0 +1,17 @@
package be.simplenotes.domain.usecases.search
import be.simplenotes.domain.model.PersistedNote
import be.simplenotes.domain.model.PersistedNoteMetadata
import java.util.*
data class SearchTerms(val title: String?, val tag: String?, val content: String?)
interface NoteSearcher {
fun indexNote(userId: Int, note: PersistedNote)
fun indexNotes(userId: Int, notes: List<PersistedNote>)
fun deleteIndex(userId: Int, uuid: UUID)
fun updateIndex(userId: Int, note: PersistedNote)
fun search(userId: Int, terms: SearchTerms): List<PersistedNoteMetadata>
fun dropIndex(userId: Int)
fun dropAll()
}

View File

@ -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())
}

View File

@ -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")) }
}

View File

@ -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,