Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Apr 10, 2024
1 parent 785d21c commit 574e5b6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 53 deletions.
16 changes: 0 additions & 16 deletions core/src/main/scala/lightdb/index/IndexManager.scala

This file was deleted.

6 changes: 2 additions & 4 deletions core/src/main/scala/lightdb/index/IndexSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import lightdb.{Collection, Document}
trait IndexSupport[D <: Document[D]] extends Collection[D] {
lazy val query: Query[D] = Query(this)

protected def indexer: Indexer[D]
override def commit(): IO[Unit] = super.commit().flatMap(_ => index.commit())

override def commit(): IO[Unit] = super.commit().flatMap(_ => indexer.commit())

def index: IndexManager[D]
def index: Indexer[D]

def doSearch(query: Query[D],
context: SearchContext[D],
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/scala/lightdb/index/Indexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import lightdb.query.SearchContext
import lightdb.{Collection, Document, Id, query}

trait Indexer[D <: Document[D]] {
def indexSupport: IndexSupport[D]
protected var _fields = List.empty[IndexedField[_, D]]

private[lightdb] def delete(id: Id[D]): IO[Unit]
def fields: List[IndexedField[_, D]] = _fields

def indexSupport: IndexSupport[D]

def commit(): IO[Unit]

def count(): IO[Int]

def withSearchContext[Return](f: SearchContext[D] => IO[Return]): IO[Return]
}

protected[lightdb] def register[F](field: IndexedField[F, D]): Unit = synchronized {
_fields = field :: _fields
}

private[lightdb] def delete(id: Id[D]): IO[Unit]
}
22 changes: 0 additions & 22 deletions lucene/src/main/scala/lightdb/lucene/LuceneIndexManager.scala

This file was deleted.

15 changes: 14 additions & 1 deletion lucene/src/main/scala/lightdb/lucene/LuceneIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lightdb.lucene

import cats.effect.IO
import lightdb.index.{IndexSupport, Indexer}
import lightdb.lucene.index.{BigDecimalField, DoubleField, FloatField, IntField, LongField, StringField, TokenizedField}
import lightdb.query.SearchContext
import lightdb.{Document, Id}
import org.apache.lucene.analysis.Analyzer
Expand Down Expand Up @@ -64,9 +65,21 @@ case class LuceneIndexer[D <: Document[D]](indexSupport: IndexSupport[D],
searcherManager.maybeRefreshBlocking()
}

def apply(name: String): IndexedFieldBuilder = IndexedFieldBuilder(name)

override def commit(): IO[Unit] = IO(commitBlocking())

override def count(): IO[Int] = withSearchContext { context =>
IO(context.indexSupport.asInstanceOf[LuceneSupport[D]].indexSearcher(context).count(new MatchAllDocsQuery))
}
}

case class IndexedFieldBuilder(fieldName: String) {
def tokenized(f: D => String): TokenizedField[D] = TokenizedField(fieldName, indexSupport, f)
def string(f: D => String, store: Boolean = false): StringField[D] = StringField(fieldName, indexSupport, f, store)
def int(f: D => Int): IntField[D] = IntField(fieldName, indexSupport, f)
def long(f: D => Long): LongField[D] = LongField(fieldName, indexSupport, f)
def float(f: D => Float): FloatField[D] = FloatField(fieldName, indexSupport, f)
def double(f: D => Double): DoubleField[D] = DoubleField(fieldName, indexSupport, f)
def bigDecimal(f: D => BigDecimal): BigDecimalField[D] = BigDecimalField(fieldName, indexSupport, f)
}
}
13 changes: 6 additions & 7 deletions lucene/src/main/scala/lightdb/lucene/LuceneSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lightdb.lucene

import cats.effect.IO
import lightdb._
import lightdb.index.{IndexManager, IndexSupport, IndexedField, Indexer}
import lightdb.index.{IndexSupport, IndexedField, Indexer}
import lightdb.lucene.index._
import lightdb.query.{Filter, IndexContext, PagedResults, Query, SearchContext, Sort}
import org.apache.lucene.search.{IndexSearcher, MatchAllDocsQuery, ScoreDoc, SearcherFactory, SearcherManager, SortField, TopFieldDocs, Query => LuceneQuery, Sort => LuceneSort}
Expand All @@ -18,14 +18,13 @@ import java.nio.file.{Files, Path}
import java.util.concurrent.ConcurrentHashMap

trait LuceneSupport[D <: Document[D]] extends IndexSupport[D] {
override protected[lucene] lazy val indexer: LuceneIndexer[D] = LuceneIndexer[D](this)
override lazy val index: LuceneIndexManager[D] = LuceneIndexManager(this)
override lazy val index: LuceneIndexer[D] = LuceneIndexer(this)

val _id: StringField[D] = index("_id").string(_._id.value, store = true)

protected[lucene] def indexSearcher(context: SearchContext[D]): IndexSearcher = indexer.contextMapping.get(context)
protected[lucene] def indexSearcher(context: SearchContext[D]): IndexSearcher = index.contextMapping.get(context)

def withSearchContext[Return](f: SearchContext[D] => IO[Return]): IO[Return] = indexer.withSearchContext(f)
def withSearchContext[Return](f: SearchContext[D] => IO[Return]): IO[Return] = index.withSearchContext(f)

private def sort2SortField(sort: Sort): SortField = sort match {
case Sort.BestMatch => SortField.FIELD_SCORE
Expand Down Expand Up @@ -71,11 +70,11 @@ trait LuceneSupport[D <: Document[D]] extends IndexSupport[D] {
fields <- IO(index.fields.flatMap { field =>
field.asInstanceOf[LuceneIndexedField[_, D]].createFields(doc)
})
_ = indexer.addDoc(doc._id, fields)
_ = index.addDoc(doc._id, fields)
_ <- super.postSet(doc)
} yield ()

override protected def postDelete(doc: D): IO[Unit] = indexer.delete(doc._id).flatMap { _ =>
override protected def postDelete(doc: D): IO[Unit] = index.delete(doc._id).flatMap { _ =>
super.postDelete(doc)
}
}

0 comments on commit 574e5b6

Please sign in to comment.