Skip to content

Commit

Permalink
Better support for Option in fields
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Apr 11, 2024
1 parent 50a982e commit 67691e0
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/lightdb/index/IndexedField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import lightdb.{Collection, Document}
trait IndexedField[F, D <: Document[D]] {
def fieldName: String
def collection: Collection[D]
def get: D => F
def get: D => Option[F]
}
26 changes: 19 additions & 7 deletions lucene/src/main/scala/lightdb/lucene/LuceneIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,24 @@ case class LuceneIndexer[D <: Document[D]](indexSupport: IndexSupport[D],
}

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)
private def o[F](f: D => F): D => Option[F] = doc => Some(f(doc))
def tokenized(f: D => String): TokenizedField[D] = TokenizedField(fieldName, indexSupport, o(f))
def string(f: D => String, store: Boolean = false): StringField[D] = StringField(fieldName, indexSupport, o(f), store)
def id[T](f: D => Id[T], store: Boolean = false): StringField[D] = StringField(fieldName, indexSupport, o(doc => f(doc).value), store)
def int(f: D => Int): IntField[D] = IntField(fieldName, indexSupport, o(f))
def long(f: D => Long): LongField[D] = LongField(fieldName, indexSupport, o(f))
def float(f: D => Float): FloatField[D] = FloatField(fieldName, indexSupport, o(f))
def double(f: D => Double): DoubleField[D] = DoubleField(fieldName, indexSupport, o(f))
def bigDecimal(f: D => BigDecimal): BigDecimalField[D] = BigDecimalField(fieldName, indexSupport, o(f))
object option {
def tokenized(f: D => Option[String]): TokenizedField[D] = TokenizedField(fieldName, indexSupport, f)
def string(f: D => Option[String], store: Boolean = false): StringField[D] = StringField(fieldName, indexSupport, f, store)
def id[T](f: D => Option[Id[T]], store: Boolean = false): StringField[D] = StringField(fieldName, indexSupport, doc => f(doc).map(_.value), store)
def int(f: D => Option[Int]): IntField[D] = IntField(fieldName, indexSupport, f)
def long(f: D => Option[Long]): LongField[D] = LongField(fieldName, indexSupport, f)
def float(f: D => Option[Float]): FloatField[D] = FloatField(fieldName, indexSupport, f)
def double(f: D => Option[Double]): DoubleField[D] = DoubleField(fieldName, indexSupport, f)
def bigDecimal(f: D => Option[BigDecimal]): BigDecimalField[D] = BigDecimalField(fieldName, indexSupport, f)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.apache.lucene.{document => ld}

case class BigDecimalField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => BigDecimal) extends LuceneIndexedField[BigDecimal, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = List(
new ld.StringField(fieldName, get(doc).toString(), Field.Store.NO)
)
get: D => Option[BigDecimal]) extends LuceneIndexedField[BigDecimal, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = get(doc).toList.map { value =>
new ld.StringField(fieldName, value.toString(), Field.Store.NO)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.STRING
}
8 changes: 4 additions & 4 deletions lucene/src/main/scala/lightdb/lucene/index/DoubleField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import org.apache.lucene.{document => ld}

case class DoubleField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => Double) extends LuceneIndexedField[Double, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = List(
new ld.DoubleField(fieldName, get(doc), Field.Store.NO)
)
get: D => Option[Double]) extends LuceneIndexedField[Double, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = get(doc).toList.map { value =>
new ld.DoubleField(fieldName, value, Field.Store.NO)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.DOUBLE
}
8 changes: 4 additions & 4 deletions lucene/src/main/scala/lightdb/lucene/index/FloatField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import org.apache.lucene.{document => ld}

case class FloatField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => Float) extends LuceneIndexedField[Float, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = List(
new ld.FloatField(fieldName, get(doc), Field.Store.NO)
)
get: D => Option[Float]) extends LuceneIndexedField[Float, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = get(doc).toList.map { value =>
new ld.FloatField(fieldName, value, Field.Store.NO)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.FLOAT
}
8 changes: 4 additions & 4 deletions lucene/src/main/scala/lightdb/lucene/index/IntField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import org.apache.lucene.{document => ld}

case class IntField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => Int) extends LuceneIndexedField[Int, D] {
get: D => Option[Int]) extends LuceneIndexedField[Int, D] {
def ===(value: Int): Filter[D] = is(value)

def is(value: Int): Filter[D] = LuceneFilter(() => ld.IntField.newExactQuery(fieldName, value))

def between(lower: Int, upper: Int): Filter[D] = LuceneFilter(() => ld.IntField.newRangeQuery(fieldName, lower, upper))

override protected[lightdb] def createFields(doc: D): List[Field] = List(
new ld.IntField(fieldName, get(doc), Field.Store.NO)
)
override protected[lightdb] def createFields(doc: D): List[Field] = get(doc).toList.map { value =>
new ld.IntField(fieldName, value, Field.Store.NO)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.INT
}
8 changes: 4 additions & 4 deletions lucene/src/main/scala/lightdb/lucene/index/LongField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import org.apache.lucene.{document => ld}

case class LongField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => Long) extends LuceneIndexedField[Long, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = List(
new ld.LongField(fieldName, get(doc), Field.Store.NO)
)
get: D => Option[Long]) extends LuceneIndexedField[Long, D] {
override protected[lightdb] def createFields(doc: D): List[Field] = get(doc).toList.map { value =>
new ld.LongField(fieldName, value, Field.Store.NO)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.LONG
}
8 changes: 4 additions & 4 deletions lucene/src/main/scala/lightdb/lucene/index/StringField.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.apache.lucene.{document => ld}

case class StringField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => String,
get: D => Option[String],
store: Boolean) extends LuceneIndexedField[String, D] {
def ===(value: String): Filter[D] = is(value)

Expand All @@ -24,9 +24,9 @@ case class StringField[D <: Document[D]](fieldName: String,
LuceneFilter(() => b.build())
}

override protected[lightdb] def createFields(doc: D): List[ld.Field] = List(
new ld.StringField(fieldName, get(doc), if (store) ld.Field.Store.YES else ld.Field.Store.NO)
)
override protected[lightdb] def createFields(doc: D): List[ld.Field] = get(doc).toList.map { value =>
new ld.StringField(fieldName, value, if (store) ld.Field.Store.YES else ld.Field.Store.NO)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.STRING
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import org.apache.lucene.{document => ld}

case class TokenizedField[D <: Document[D]](fieldName: String,
collection: Collection[D],
get: D => String) extends LuceneIndexedField[String, D] {
override protected[lightdb] def createFields(doc: D): List[ld.Field] = List(
new ld.Field(fieldName, get(doc), ld.TextField.TYPE_NOT_STORED)
)
get: D => Option[String]) extends LuceneIndexedField[String, D] {
override protected[lightdb] def createFields(doc: D): List[ld.Field] = get(doc).toList.map { value =>
new ld.Field(fieldName, value, ld.TextField.TYPE_NOT_STORED)
}

override protected[lightdb] def sortType: SortField.Type = SortField.Type.STRING
}

0 comments on commit 67691e0

Please sign in to comment.