diff --git a/all/src/test/scala/spec/AbstractAsyncSpec.scala b/all/src/test/scala/spec/AbstractAsyncSpec.scala index cbb9b5d1..a616ce60 100644 --- a/all/src/test/scala/spec/AbstractAsyncSpec.scala +++ b/all/src/test/scala/spec/AbstractAsyncSpec.scala @@ -237,8 +237,8 @@ abstract class AbstractAsyncSpec extends AsyncWordSpec with AsyncIOSpec with Mat object Person extends DocumentModel[Person] with JsonConversion[Person] { implicit val rw: RW[Person] = RW.gen - val name: F[String] = field("name", _.name) - val age: F[Int] = field.index("age", _.age) + val name: F[String] = field("name", (p: Person) => p.name) + val age: F[Int] = field.index("age", (p: Person) => p.age) } object InitialSetupUpgrade extends AsyncDatabaseUpgrade { diff --git a/async/src/main/scala/lightdb/async/AsyncCollection.scala b/async/src/main/scala/lightdb/async/AsyncCollection.scala index b2c9f4fa..f8554197 100644 --- a/async/src/main/scala/lightdb/async/AsyncCollection.scala +++ b/async/src/main/scala/lightdb/async/AsyncCollection.scala @@ -2,7 +2,7 @@ package lightdb.async import cats.effect.IO import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.collection.Collection import lightdb.doc.{Document, DocumentModel} import lightdb.transaction.Transaction diff --git a/async/src/main/scala/lightdb/async/AsyncQuery.scala b/async/src/main/scala/lightdb/async/AsyncQuery.scala index 4dd1b542..fbc86406 100644 --- a/async/src/main/scala/lightdb/async/AsyncQuery.scala +++ b/async/src/main/scala/lightdb/async/AsyncQuery.scala @@ -4,10 +4,11 @@ import cats.effect.IO import fabric.Json import lightdb.aggregate.AggregateFunction import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.collection.Collection import lightdb.distance.Distance import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field import lightdb.filter._ import lightdb.materialized.MaterializedIndex import lightdb.spatial.{DistanceAndDoc, Geo} @@ -190,7 +191,7 @@ case class AsyncQuery[Doc <: Document[Doc], Model <: DocumentModel[Doc]](collect .search .docs .iterator).map { iterator => - val grouped = GroupedIterator[Doc, F](iterator, doc => field.get(doc)) + val grouped = GroupedIterator[Doc, F](iterator, doc => field.get(doc, field)) fs2.Stream.fromBlockingIterator[IO](grouped, 512) } fs2.Stream.force(io) diff --git a/async/src/main/scala/lightdb/async/AsyncTransactionConvenience.scala b/async/src/main/scala/lightdb/async/AsyncTransactionConvenience.scala index 70a42a67..34544286 100644 --- a/async/src/main/scala/lightdb/async/AsyncTransactionConvenience.scala +++ b/async/src/main/scala/lightdb/async/AsyncTransactionConvenience.scala @@ -3,7 +3,7 @@ package lightdb.async import cats.effect.IO import lightdb.doc.{Document, DocumentModel} import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ case class AsyncTransactionConvenience[Doc <: Document[Doc], Model <: DocumentModel[Doc]](collection: AsyncCollection[Doc, Model]) { def insert(doc: Doc): IO[Doc] = collection.transaction { implicit transaction => diff --git a/benchmark/src/main/scala/benchmark/bench/impl/LightDBBench.scala b/benchmark/src/main/scala/benchmark/bench/impl/LightDBBench.scala index 82de17a4..71806638 100644 --- a/benchmark/src/main/scala/benchmark/bench/impl/LightDBBench.scala +++ b/benchmark/src/main/scala/benchmark/bench/impl/LightDBBench.scala @@ -6,8 +6,9 @@ import lightdb.doc.{Document, DocumentModel, JsonConversion} import lightdb.sql.SQLConversion import lightdb.store.StoreManager import lightdb.upgrade.DatabaseUpgrade -import lightdb.{Field, Id, LightDB} +import lightdb.{Id, LightDB} import fabric.rw._ +import lightdb.field.Field import java.nio.file.Path import java.sql.ResultSet diff --git a/core/src/main/scala/lightdb/KeyValue.scala b/core/src/main/scala/lightdb/KeyValue.scala index f24ff355..31b02a7d 100644 --- a/core/src/main/scala/lightdb/KeyValue.scala +++ b/core/src/main/scala/lightdb/KeyValue.scala @@ -3,11 +3,12 @@ package lightdb import fabric.Json import fabric.rw._ import lightdb.doc.{Document, DocumentModel, JsonConversion} +import lightdb.field.Field case class KeyValue(_id: Id[KeyValue], json: Json) extends Document[KeyValue] object KeyValue extends DocumentModel[KeyValue] with JsonConversion[KeyValue] { override implicit val rw: RW[KeyValue] = RW.gen - val json: Field[KeyValue, Json] = field("json", _.json) + val json: Field[KeyValue, Json] = field("json", (doc: KeyValue) => doc.json) } \ No newline at end of file diff --git a/core/src/main/scala/lightdb/Query.scala b/core/src/main/scala/lightdb/Query.scala index 4dea7346..c214c644 100644 --- a/core/src/main/scala/lightdb/Query.scala +++ b/core/src/main/scala/lightdb/Query.scala @@ -1,13 +1,14 @@ package lightdb import fabric.Json -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.aggregate.{AggregateFunction, AggregateQuery} import lightdb.collection.Collection import lightdb.distance.Distance import lightdb.doc.{Document, DocumentModel} import lightdb.error.NonIndexedFieldException import lightdb.facet.FacetQuery +import lightdb.field.Field import lightdb.filter._ import lightdb.materialized.MaterializedIndex import lightdb.spatial.{DistanceAndDoc, Geo} @@ -152,7 +153,7 @@ case class Query[Doc <: Document[Doc], Model <: DocumentModel[Doc]](collection: .search .docs .iterator - GroupedIterator[Doc, F](iterator, doc => field.get(doc)) + GroupedIterator[Doc, F](iterator, doc => field.get(doc, field)) } } diff --git a/core/src/main/scala/lightdb/SearchResults.scala b/core/src/main/scala/lightdb/SearchResults.scala index 650b6ad2..9c9b1f2c 100644 --- a/core/src/main/scala/lightdb/SearchResults.scala +++ b/core/src/main/scala/lightdb/SearchResults.scala @@ -1,6 +1,6 @@ package lightdb -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel} import lightdb.facet.FacetResult import lightdb.transaction.Transaction diff --git a/core/src/main/scala/lightdb/Sort.scala b/core/src/main/scala/lightdb/Sort.scala index 9662c324..764d5f84 100644 --- a/core/src/main/scala/lightdb/Sort.scala +++ b/core/src/main/scala/lightdb/Sort.scala @@ -1,6 +1,7 @@ package lightdb import lightdb.doc.Document +import lightdb.field.Field import lightdb.spatial.Geo trait Sort diff --git a/core/src/main/scala/lightdb/aggregate/AggregateFilter.scala b/core/src/main/scala/lightdb/aggregate/AggregateFilter.scala index 3bf932f7..297e4767 100644 --- a/core/src/main/scala/lightdb/aggregate/AggregateFilter.scala +++ b/core/src/main/scala/lightdb/aggregate/AggregateFilter.scala @@ -1,8 +1,8 @@ package lightdb.aggregate import fabric.{Json, Str} -import lightdb.Field import lightdb.doc.Document +import lightdb.field.Field import lightdb.spatial.Geo sealed trait AggregateFilter[Doc <: Document[Doc]] { diff --git a/core/src/main/scala/lightdb/aggregate/AggregateFunction.scala b/core/src/main/scala/lightdb/aggregate/AggregateFunction.scala index b656433f..698ad9cd 100644 --- a/core/src/main/scala/lightdb/aggregate/AggregateFunction.scala +++ b/core/src/main/scala/lightdb/aggregate/AggregateFunction.scala @@ -2,8 +2,8 @@ package lightdb.aggregate import fabric.rw._ import lightdb.distance.Distance -import lightdb.Field import lightdb.doc.Document +import lightdb.field.Field import lightdb.filter.FilterSupport import lightdb.materialized.Materializable import lightdb.spatial.Geo diff --git a/core/src/main/scala/lightdb/aggregate/AggregateSupport.scala b/core/src/main/scala/lightdb/aggregate/AggregateSupport.scala index 9b954cac..860c9745 100644 --- a/core/src/main/scala/lightdb/aggregate/AggregateSupport.scala +++ b/core/src/main/scala/lightdb/aggregate/AggregateSupport.scala @@ -1,8 +1,8 @@ package lightdb.aggregate import fabric.rw._ -import lightdb.Field import lightdb.doc.Document +import lightdb.field.Field trait AggregateSupport[Doc <: Document[Doc], V] { this: Field[Doc, V] => diff --git a/core/src/main/scala/lightdb/collection/Collection.scala b/core/src/main/scala/lightdb/collection/Collection.scala index 4407b062..abe4d4d1 100644 --- a/core/src/main/scala/lightdb/collection/Collection.scala +++ b/core/src/main/scala/lightdb/collection/Collection.scala @@ -10,7 +10,7 @@ import lightdb.transaction.Transaction import lightdb.trigger.CollectionTriggers import lightdb.util.Initializable import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import java.util.concurrent.ConcurrentHashMap import scala.jdk.CollectionConverters.IteratorHasAsScala diff --git a/core/src/main/scala/lightdb/doc/DocumentModel.scala b/core/src/main/scala/lightdb/doc/DocumentModel.scala index 5a9ef368..47aa017d 100644 --- a/core/src/main/scala/lightdb/doc/DocumentModel.scala +++ b/core/src/main/scala/lightdb/doc/DocumentModel.scala @@ -5,7 +5,8 @@ import lightdb.collection.Collection import lightdb.facet.FacetValue import lightdb.filter.FilterBuilder import lightdb._ -import lightdb.Field._ +import lightdb.field.{Field, FieldGetter} +import lightdb.field.Field._ import scala.language.implicitConversions @@ -14,7 +15,7 @@ trait DocumentModel[Doc <: Document[Doc]] { private var _fields = List.empty[Field[Doc, _]] - val _id: UniqueIndex[Doc, Id[Doc]] = field.unique("_id", _._id) + val _id: UniqueIndex[Doc, Id[Doc]] = field.unique("_id", (doc: Doc) => doc._id) def id(value: String = Unique()): Id[Doc] = Id(value) @@ -43,18 +44,18 @@ trait DocumentModel[Doc <: Document[Doc]] { field } - def apply[V: RW](name: String, get: Doc => V): Field[Doc, V] = { + def apply[V: RW](name: String, get: FieldGetter[Doc, V]): Field[Doc, V] = { add[V, Field[Doc, V]](Field(name, get)) } - def index[V: RW](name: String, get: Doc => V): Indexed[Doc, V] = + def index[V: RW](name: String, get: FieldGetter[Doc, V]): Indexed[Doc, V] = add[V, Indexed[Doc, V]](Field.indexed(name, get)) - def unique[V: RW](name: String, get: Doc => V): UniqueIndex[Doc, V] = + def unique[V: RW](name: String, get: FieldGetter[Doc, V]): UniqueIndex[Doc, V] = add[V, UniqueIndex[Doc, V]](Field.unique(name, get)) - def tokenized(name: String, get: Doc => String): Tokenized[Doc] = - add[String, Tokenized[Doc]](Field.tokenized(name, doc => get(doc))) + def tokenized(name: String, get: FieldGetter[Doc, String]): Tokenized[Doc] = + add[String, Tokenized[Doc]](Field.tokenized(name, get)) def facet(name: String, get: Doc => List[FacetValue], diff --git a/core/src/main/scala/lightdb/doc/RecordDocumentModel.scala b/core/src/main/scala/lightdb/doc/RecordDocumentModel.scala index aff4697c..c744bafd 100644 --- a/core/src/main/scala/lightdb/doc/RecordDocumentModel.scala +++ b/core/src/main/scala/lightdb/doc/RecordDocumentModel.scala @@ -1,21 +1,21 @@ package lightdb.doc -import lightdb.Field import fabric.rw._ +import lightdb.field.Field trait RecordDocumentModel[Doc <: RecordDocument[Doc]] extends DocumentModel[Doc] { protected def indexCreated: Boolean = false protected def indexModified: Boolean = false val created: Field[Doc, Long] = if (indexCreated) { - field.index("created", _.created) + field.index("created", (doc: Doc) => doc.created) } else { - field("created", _.created) + field("created", (doc: Doc) => doc.created) } val modified: Field[Doc, Long] = if (indexModified) { - field.index("modified", _.modified) + field.index("modified", (doc: Doc) => doc.modified) } else { - field("modified", _.modified) + field("modified", (doc: Doc) => doc.modified) } } diff --git a/core/src/main/scala/lightdb/error/NonIndexedFieldException.scala b/core/src/main/scala/lightdb/error/NonIndexedFieldException.scala index 8a1fc2cf..7b373fd1 100644 --- a/core/src/main/scala/lightdb/error/NonIndexedFieldException.scala +++ b/core/src/main/scala/lightdb/error/NonIndexedFieldException.scala @@ -1,5 +1,6 @@ package lightdb.error -import lightdb.{Field, Query} +import lightdb.Query +import lightdb.field.Field case class NonIndexedFieldException(query: Query[_, _], fields: List[Field[_, _]]) extends RuntimeException(s"Attempting to execute a query with non-indexed fields in an indexed store mode. Not indexed fields: ${fields.map(_.name).mkString(", ")}") diff --git a/core/src/main/scala/lightdb/facet/FacetQuery.scala b/core/src/main/scala/lightdb/facet/FacetQuery.scala index 9bd7f957..6ed09442 100644 --- a/core/src/main/scala/lightdb/facet/FacetQuery.scala +++ b/core/src/main/scala/lightdb/facet/FacetQuery.scala @@ -1,6 +1,6 @@ package lightdb.facet -import lightdb.Field.FacetField +import lightdb.field.Field.FacetField import lightdb.doc.Document case class FacetQuery[Doc <: Document[Doc]](field: FacetField[Doc], diff --git a/core/src/main/scala/lightdb/facet/FacetValue.scala b/core/src/main/scala/lightdb/facet/FacetValue.scala index d6696e86..c39e89e9 100644 --- a/core/src/main/scala/lightdb/facet/FacetValue.scala +++ b/core/src/main/scala/lightdb/facet/FacetValue.scala @@ -1,6 +1,6 @@ package lightdb.facet -import fabric.rw.RW +import fabric.rw._ case class FacetValue(path: List[String]) diff --git a/core/src/main/scala/lightdb/Field.scala b/core/src/main/scala/lightdb/field/Field.scala similarity index 84% rename from core/src/main/scala/lightdb/Field.scala rename to core/src/main/scala/lightdb/field/Field.scala index ff2bb56a..375d7dd2 100644 --- a/core/src/main/scala/lightdb/Field.scala +++ b/core/src/main/scala/lightdb/field/Field.scala @@ -1,9 +1,9 @@ -package lightdb +package lightdb.field -import fabric.{Json, Null, arr, bool, num, str} import fabric.define.DefType import fabric.io.JsonParser import fabric.rw._ +import fabric.{Json, Null, arr, bool, num, str} import lightdb.aggregate.AggregateSupport import lightdb.distance.Distance import lightdb.doc.Document @@ -14,7 +14,7 @@ import lightdb.materialized.Materializable import lightdb.spatial.Geo sealed class Field[Doc <: Document[Doc], V](val name: String, - val get: Doc => V, + val get: FieldGetter[Doc, V], val getRW: () => RW[V], val indexed: Boolean = false) extends FilterSupport[V, Doc, Filter[Doc]] with AggregateSupport[Doc, V] with Materializable[Doc, V] { implicit def rw: RW[V] = getRW() @@ -37,7 +37,7 @@ sealed class Field[Doc <: Document[Doc], V](val name: String, lazy val isSpatial: Boolean = className.exists(_.startsWith("lightdb.spatial.Geo")) - def getJson(doc: Doc): Json = get(doc).json + def getJson(doc: Doc): Json = get(doc, this).json override def is(value: V): Filter[Doc] = Filter.Equals(name, value) @@ -76,9 +76,9 @@ sealed class Field[Doc <: Document[Doc], V](val name: String, parsed(words, allowLeadingWildcard = matchEndsWith) } - def opt: Field[Doc, Option[V]] = new Field[Doc, Option[V]](name, doc => Option(get(doc)), () => implicitly[RW[Option[V]]], indexed) + def opt: Field[Doc, Option[V]] = new Field[Doc, Option[V]](name, (doc: Doc) => Option(get(doc, this)), () => implicitly[RW[Option[V]]], indexed) - def list: Field[Doc, List[V]] = new Field[Doc, List[V]](name, doc => List(get(doc)), () => implicitly[RW[List[V]]], indexed) + def list: Field[Doc, List[V]] = new Field[Doc, List[V]](name, (doc: Doc) => List(get(doc, this)), () => implicitly[RW[List[V]]], indexed) override def distance(from: Geo.Point, radius: Distance): Filter[Doc] = Filter.Distance(name, from, radius) @@ -91,13 +91,13 @@ object Field { var MaxIn: Option[Int] = Some(1_000) - def apply[Doc <: Document[Doc], V](name: String, get: Doc => V)(implicit getRW: => RW[V]): Field[Doc, V] = new Field[Doc, V]( + def apply[Doc <: Document[Doc], V](name: String, get: FieldGetter[Doc, V])(implicit getRW: => RW[V]): Field[Doc, V] = new Field[Doc, V]( name = name, get = get, getRW = () => getRW ) - def indexed[Doc <: Document[Doc], V](name: String, get: Doc => V)(implicit getRW: => RW[V]): Indexed[Doc, V] = new Field[Doc, V]( + def indexed[Doc <: Document[Doc], V](name: String, get: FieldGetter[Doc, V])(implicit getRW: => RW[V]): Indexed[Doc, V] = new Field[Doc, V]( name = name, get = get, getRW = () => getRW, @@ -106,7 +106,7 @@ object Field { override def toString: String = s"Indexed(name = ${this.name})" } - def tokenized[Doc <: Document[Doc]](name: String, get: Doc => String): Tokenized[Doc] = new Field[Doc, String]( + def tokenized[Doc <: Document[Doc]](name: String, get: FieldGetter[Doc, String]): Tokenized[Doc] = new Field[Doc, String]( name = name, get = get, getRW = () => stringRW, @@ -115,7 +115,7 @@ object Field { override def toString: String = s"Tokenized(name = ${this.name})" } - def unique[Doc <: Document[Doc], V](name: String, get: Doc => V)(implicit getRW: => RW[V]): UniqueIndex[Doc, V] = new Field[Doc, V]( + def unique[Doc <: Document[Doc], V](name: String, get: FieldGetter[Doc, V])(implicit getRW: => RW[V]): UniqueIndex[Doc, V] = new Field[Doc, V]( name = name, get = get, getRW = () => getRW, @@ -125,7 +125,7 @@ object Field { } def facet[Doc <: Document[Doc]](name: String, - get: Doc => List[FacetValue], + get: FieldGetter[Doc, List[FacetValue]], hierarchical: Boolean, multiValued: Boolean, requireDimCount: Boolean): FacetField[Doc] = new FacetField[Doc]( @@ -164,7 +164,7 @@ object Field { trait Tokenized[Doc <: Document[Doc]] extends Indexed[Doc, String] class FacetField[Doc <: Document[Doc]](name: String, - get: Doc => List[FacetValue], + get: FieldGetter[Doc, List[FacetValue]], val hierarchical: Boolean, val multiValued: Boolean, val requireDimCount: Boolean) extends Field[Doc, List[FacetValue]](name, get, getRW = () => implicitly[RW[List[FacetValue]]], indexed = true) with Indexed[Doc, List[FacetValue]] { diff --git a/core/src/main/scala/lightdb/field/FieldGetter.scala b/core/src/main/scala/lightdb/field/FieldGetter.scala new file mode 100644 index 00000000..6f30a8e4 --- /dev/null +++ b/core/src/main/scala/lightdb/field/FieldGetter.scala @@ -0,0 +1,15 @@ +package lightdb.field + +import lightdb.doc.Document + +import scala.language.implicitConversions + +trait FieldGetter[Doc <: Document[Doc], V] { + def apply(doc: Doc, field: Field[Doc, V]): V +} + +object FieldGetter { + implicit def function2Getter[Doc <: Document[Doc], V](f: Doc => V): FieldGetter[Doc, V] = new FieldGetter[Doc, V] { + override def apply(doc: Doc, field: Field[Doc, V]): V = f(doc) + } +} \ No newline at end of file diff --git a/core/src/main/scala/lightdb/filter/Filter.scala b/core/src/main/scala/lightdb/filter/Filter.scala index 678983ee..b3806dce 100644 --- a/core/src/main/scala/lightdb/filter/Filter.scala +++ b/core/src/main/scala/lightdb/filter/Filter.scala @@ -1,8 +1,8 @@ package lightdb.filter import fabric.{Json, Str} -import lightdb.Field import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field import lightdb.spatial.Geo sealed trait Filter[Doc <: Document[Doc]] { diff --git a/core/src/main/scala/lightdb/materialized/MaterializedIndex.scala b/core/src/main/scala/lightdb/materialized/MaterializedIndex.scala index dd8c8bbd..64cbb676 100644 --- a/core/src/main/scala/lightdb/materialized/MaterializedIndex.scala +++ b/core/src/main/scala/lightdb/materialized/MaterializedIndex.scala @@ -1,8 +1,8 @@ package lightdb.materialized import fabric.Json -import lightdb.Field import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field case class MaterializedIndex[Doc <: Document[Doc], Model <: DocumentModel[Doc]](json: Json, model: Model) extends Materialized[Doc, Model] { def get[V](f: Model => Field[Doc, V]): Option[V] = { diff --git a/core/src/main/scala/lightdb/store/Conversion.scala b/core/src/main/scala/lightdb/store/Conversion.scala index 4203d8a0..ae012289 100644 --- a/core/src/main/scala/lightdb/store/Conversion.scala +++ b/core/src/main/scala/lightdb/store/Conversion.scala @@ -1,7 +1,7 @@ package lightdb.store -import lightdb.Field import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field import lightdb.materialized.MaterializedIndex import lightdb.spatial.{DistanceAndDoc, Geo} diff --git a/core/src/main/scala/lightdb/store/InMemoryIndexes.scala b/core/src/main/scala/lightdb/store/InMemoryIndexes.scala index f0bf133b..e3298f14 100644 --- a/core/src/main/scala/lightdb/store/InMemoryIndexes.scala +++ b/core/src/main/scala/lightdb/store/InMemoryIndexes.scala @@ -1,7 +1,7 @@ package lightdb.store import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.collection.Collection import lightdb.doc.{Document, DocumentModel} import lightdb.transaction.Transaction diff --git a/core/src/main/scala/lightdb/store/MapStore.scala b/core/src/main/scala/lightdb/store/MapStore.scala index 98c7c4a8..f1f14a0b 100644 --- a/core/src/main/scala/lightdb/store/MapStore.scala +++ b/core/src/main/scala/lightdb/store/MapStore.scala @@ -3,7 +3,7 @@ package lightdb.store import lightdb.aggregate.AggregateQuery import lightdb.collection.Collection import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel} import lightdb.materialized.MaterializedAggregate import lightdb.transaction.Transaction diff --git a/core/src/main/scala/lightdb/store/Store.scala b/core/src/main/scala/lightdb/store/Store.scala index 6eb9143f..7394da2c 100644 --- a/core/src/main/scala/lightdb/store/Store.scala +++ b/core/src/main/scala/lightdb/store/Store.scala @@ -8,7 +8,8 @@ import lightdb.doc.{Document, DocumentModel} import lightdb.materialized.MaterializedAggregate import lightdb.transaction.Transaction import lightdb._ -import lightdb.Field._ +import lightdb.field.Field +import lightdb.field.Field._ import java.io.File diff --git a/core/src/main/scala/lightdb/store/split/SplitStore.scala b/core/src/main/scala/lightdb/store/split/SplitStore.scala index 1424e86f..aa157b6c 100644 --- a/core/src/main/scala/lightdb/store/split/SplitStore.scala +++ b/core/src/main/scala/lightdb/store/split/SplitStore.scala @@ -7,7 +7,7 @@ import lightdb.materialized.MaterializedAggregate import lightdb.store.{Conversion, Store, StoreMode} import lightdb.transaction.{Transaction, TransactionKey} import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import scala.language.implicitConversions diff --git a/core/src/main/scala/lightdb/trigger/BasicCollectionTrigger.scala b/core/src/main/scala/lightdb/trigger/BasicCollectionTrigger.scala index 8289cf76..416370f0 100644 --- a/core/src/main/scala/lightdb/trigger/BasicCollectionTrigger.scala +++ b/core/src/main/scala/lightdb/trigger/BasicCollectionTrigger.scala @@ -1,8 +1,8 @@ package lightdb.trigger -import lightdb.Field import lightdb.collection.Collection import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field import lightdb.transaction.Transaction trait BasicCollectionTrigger[Doc <: Document[Doc], Model <: DocumentModel[Doc]] extends CollectionTrigger[Doc] { diff --git a/core/src/main/scala/lightdb/trigger/CollectionTrigger.scala b/core/src/main/scala/lightdb/trigger/CollectionTrigger.scala index e4119c5f..719c35f4 100644 --- a/core/src/main/scala/lightdb/trigger/CollectionTrigger.scala +++ b/core/src/main/scala/lightdb/trigger/CollectionTrigger.scala @@ -1,7 +1,7 @@ package lightdb.trigger -import lightdb.Field import lightdb.doc.Document +import lightdb.field.Field import lightdb.transaction.Transaction trait CollectionTrigger[Doc <: Document[Doc]] { diff --git a/core/src/main/scala/lightdb/trigger/CollectionTriggers.scala b/core/src/main/scala/lightdb/trigger/CollectionTriggers.scala index fcaf72b6..173242cb 100644 --- a/core/src/main/scala/lightdb/trigger/CollectionTriggers.scala +++ b/core/src/main/scala/lightdb/trigger/CollectionTriggers.scala @@ -1,6 +1,6 @@ package lightdb.trigger -import lightdb.Field.UniqueIndex +import lightdb.field.Field.UniqueIndex import lightdb.doc.Document import lightdb.transaction.Transaction diff --git a/core/src/main/scala/lightdb/util/Aggregator.scala b/core/src/main/scala/lightdb/util/Aggregator.scala index ef7c17b8..459c4c3a 100644 --- a/core/src/main/scala/lightdb/util/Aggregator.scala +++ b/core/src/main/scala/lightdb/util/Aggregator.scala @@ -2,11 +2,11 @@ package lightdb.util import fabric.rw._ import fabric.{Json, Null, Num, NumDec, NumInt, Obj, Str, num} -import lightdb.Field import lightdb.SortDirection.Ascending import lightdb.aggregate.{AggregateQuery, AggregateType} import lightdb.collection.Collection import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field import lightdb.materialized.MaterializedAggregate import lightdb.transaction.Transaction diff --git a/core/src/main/scala/lightdb/util/InMemoryIndex.scala b/core/src/main/scala/lightdb/util/InMemoryIndex.scala index f674d0df..84469df9 100644 --- a/core/src/main/scala/lightdb/util/InMemoryIndex.scala +++ b/core/src/main/scala/lightdb/util/InMemoryIndex.scala @@ -1,7 +1,7 @@ package lightdb.util import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.Document import java.util.Comparator @@ -12,7 +12,7 @@ class InMemoryIndex[Doc <: Document[Doc], V](field: Indexed[Doc, V], comparator: private val currentValue = new ConcurrentHashMap[Id[Doc], V] private val sorted = new AtomicList[V](comparator) - def set(doc: Doc): Unit = set(doc._id, field.get(doc)) + def set(doc: Doc): Unit = set(doc._id, field.get(doc, field)) def set(id: Id[Doc], value: V): Unit = { Option(currentValue.get(id)).foreach(previous => remove(id, previous)) diff --git a/core/src/test/scala/spec/AbstractBasicSpec.scala b/core/src/test/scala/spec/AbstractBasicSpec.scala index 3ae28523..2809c1c2 100644 --- a/core/src/test/scala/spec/AbstractBasicSpec.scala +++ b/core/src/test/scala/spec/AbstractBasicSpec.scala @@ -5,11 +5,12 @@ import lightdb.backup.{DatabaseBackup, DatabaseRestore} import lightdb.collection.Collection import lightdb.doc.{Document, DocumentModel, JsonConversion, MaterializedModel} import lightdb.feature.DBFeatureKey +import lightdb.field.Field import lightdb.filter._ import lightdb.store.StoreManager import lightdb.transaction.Transaction import lightdb.upgrade.DatabaseUpgrade -import lightdb.{Field, Id, LightDB, Sort, StoredValue} +import lightdb.{Id, LightDB, Sort, StoredValue} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec import perfolation.double2Implicits @@ -416,11 +417,11 @@ abstract class AbstractBasicSpec extends AnyWordSpec with Matchers { spec => object Person extends DocumentModel[Person] with JsonConversion[Person] { override implicit val rw: RW[Person] = RW.gen - val name: I[String] = field.index("name", _.name) - val age: I[Int] = field.index("age", _.age) - val city: I[Option[City]] = field.index("city", _.city) - val nicknames: I[Set[String]] = field.index("nicknames", _.nicknames) - val search: T = field.tokenized("search", doc => s"${doc.name} ${doc.age}") + val name: I[String] = field.index("name", (p: Person) => p.name) + val age: I[Int] = field.index("age", (p: Person) => p.age) + val city: I[Option[City]] = field.index("city", (p: Person) => p.city) + val nicknames: I[Set[String]] = field.index("nicknames", (p: Person) => p.nicknames) + val search: T = field.tokenized("search", (doc: Person) => s"${doc.name} ${doc.age}") } case class City(name: String) diff --git a/core/src/test/scala/spec/AbstractFacetSpec.scala b/core/src/test/scala/spec/AbstractFacetSpec.scala index c2a9f018..a4991191 100644 --- a/core/src/test/scala/spec/AbstractFacetSpec.scala +++ b/core/src/test/scala/spec/AbstractFacetSpec.scala @@ -1,10 +1,11 @@ package spec -import fabric.rw.RW +import fabric.rw._ import lightdb.collection.Collection import lightdb.{Id, LightDB} import lightdb.doc.{Document, DocumentModel, JsonConversion} import lightdb.facet.FacetValue +import lightdb.filter._ import lightdb.store.StoreManager import lightdb.upgrade.DatabaseUpgrade import org.scalatest.matchers.should.Matchers @@ -279,10 +280,10 @@ abstract class AbstractFacetSpec extends AnyWordSpec with Matchers { spec => object Entry extends DocumentModel[Entry] with JsonConversion[Entry] { override implicit val rw: RW[Entry] = RW.gen - val name: I[String] = field.index("name", _.name) - val authors: F[List[String]] = field("authors", _.authors) - val keywords: I[List[String]] = field.index("keywords", _.keywords) - val publishDate: F[PublishDate] = field("publishDate", _.publishDate) + val name: I[String] = field.index("name", (e: Entry) => e.name) + val authors: F[List[String]] = field("authors", (e: Entry) => e.authors) + val keywords: I[List[String]] = field.index("keywords", (e: Entry) => e.keywords) + val publishDate: F[PublishDate] = field("publishDate", (e: Entry) => e.publishDate) val authorsFacet: FF = field.facet("authorsFacet", _.authors.map(a => FacetValue(a)), multiValued = true) val keywordsFacet: FF = field.facet("keywordsFacet", _.keywords.map(k => FacetValue(k)), multiValued = true) diff --git a/core/src/test/scala/spec/AbstractSpatialSpec.scala b/core/src/test/scala/spec/AbstractSpatialSpec.scala index 8c6441bb..5f8567ab 100644 --- a/core/src/test/scala/spec/AbstractSpatialSpec.scala +++ b/core/src/test/scala/spec/AbstractSpatialSpec.scala @@ -4,10 +4,11 @@ import fabric.rw._ import lightdb.collection.Collection import lightdb.distance._ import lightdb.doc.{Document, DocumentModel, JsonConversion} +import lightdb.field.Field import lightdb.spatial.Geo import lightdb.store.StoreManager import lightdb.upgrade.DatabaseUpgrade -import lightdb.{Field, Id, LightDB} +import lightdb.{Id, LightDB} import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpec @@ -139,9 +140,9 @@ abstract class AbstractSpatialSpec extends AnyWordSpec with Matchers { spec => object Person extends DocumentModel[Person] with JsonConversion[Person] { override implicit val rw: RW[Person] = RW.gen - val name: F[String] = field("name", _.name) - val age: F[Int] = field("age", _.age) - val point: I[Geo.Point] = field.index("point", _.point) - val geo: I[List[Geo]] = field.index("geo", _.geo) + val name: F[String] = field("name", (p: Person) => p.name) + val age: F[Int] = field("age", (p: Person) => p.age) + val point: I[Geo.Point] = field.index("point", (p: Person) => p.point) + val geo: I[List[Geo]] = field.index("geo", (p: Person) => p.geo) } } diff --git a/core/src/test/scala/spec/AbstractSpecialCasesSpec.scala b/core/src/test/scala/spec/AbstractSpecialCasesSpec.scala index b7b764bb..8e455a04 100644 --- a/core/src/test/scala/spec/AbstractSpecialCasesSpec.scala +++ b/core/src/test/scala/spec/AbstractSpecialCasesSpec.scala @@ -12,8 +12,7 @@ import org.scalatest.wordspec.AnyWordSpec import java.nio.file.Path -trait AbstractSpecialCasesSpec extends AnyWordSpec with Matchers { - spec => +trait AbstractSpecialCasesSpec extends AnyWordSpec with Matchers { spec => private lazy val specName: String = getClass.getSimpleName specName should { @@ -76,9 +75,9 @@ trait AbstractSpecialCasesSpec extends AnyWordSpec with Matchers { object SpecialOne extends RecordDocumentModel[SpecialOne] with JsonConversion[SpecialOne] { override implicit val rw: RW[SpecialOne] = RW.gen - val name: F[String] = field("name", _.name) - val wrappedString: F[WrappedString] = field("wrappedString", _.wrappedString) - val person: F[Person] = field("person", _.person) + val name: F[String] = field("name", (d: SpecialOne) => d.name) + val wrappedString: F[WrappedString] = field("wrappedString", (d: SpecialOne) => d.wrappedString) + val person: F[Person] = field("person", (d: SpecialOne) => d.person) } case class WrappedString(v: String) { diff --git a/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala b/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala index c4231903..2625ab51 100644 --- a/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala +++ b/halodb/src/main/scala/lightdb/halodb/HaloDBStore.scala @@ -6,7 +6,7 @@ import fabric.rw.{Asable, Convertible} import lightdb.aggregate.AggregateQuery import lightdb.collection.Collection import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel} import lightdb.materialized.MaterializedAggregate import lightdb.store.{Conversion, Store, StoreManager, StoreMode} diff --git a/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala b/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala index 612968c1..8b0f4eef 100644 --- a/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala +++ b/lucene/src/main/scala/lightdb/lucene/LuceneStore.scala @@ -8,9 +8,10 @@ import lightdb.SortDirection.Ascending import lightdb.aggregate.{AggregateQuery, AggregateType} import lightdb.collection.Collection import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel, JsonConversion} import lightdb.facet.{FacetResult, FacetResultValue} +import lightdb.field.Field import lightdb.filter.{Condition, Filter} import lightdb.lucene.index.Index import lightdb.materialized.{MaterializedAggregate, MaterializedIndex} @@ -115,7 +116,7 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory: var fields = List.empty[LuceneField] def add(field: LuceneField): Unit = fields = field :: fields field match { - case ff: FacetField[Doc] => ff.get(doc).flatMap { value => + case ff: FacetField[Doc] => ff.get(doc, ff).flatMap { value => if (value.path.nonEmpty || ff.hierarchical) { val path = if (ff.hierarchical) value.path ::: List("$ROOT$") else value.path Some(new LuceneFacetField(field.name, path: _*)) @@ -124,7 +125,7 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory: } } case t: Tokenized[Doc] => - List(new LuceneField(field.name, t.get(doc), if (fs == LuceneField.Store.YES) TextField.TYPE_STORED else TextField.TYPE_NOT_STORED)) + List(new LuceneField(field.name, t.get(doc, t), if (fs == LuceneField.Store.YES) TextField.TYPE_STORED else TextField.TYPE_NOT_STORED)) case _ => def addJson(json: Json, d: DefType): Unit = { if (field.isSpatial) { @@ -319,7 +320,7 @@ class LuceneStore[Doc <: Document[Doc], Model <: DocumentModel[Doc]](directory: case Conversion.Distance(field, from, sort, radius) => idsAndScores.iterator.map { case (id, score) => val doc = collection(id)(transaction) - val distance = field.get(doc).map(d => Spatial.distance(from, d)) + val distance = field.get(doc, field).map(d => Spatial.distance(from, d)) DistanceAndDoc(doc, distance) -> score } } diff --git a/mapdb/src/main/scala/lightdb/mapdb/MapDBStore.scala b/mapdb/src/main/scala/lightdb/mapdb/MapDBStore.scala index ea3f5a12..578fba94 100644 --- a/mapdb/src/main/scala/lightdb/mapdb/MapDBStore.scala +++ b/mapdb/src/main/scala/lightdb/mapdb/MapDBStore.scala @@ -3,7 +3,7 @@ package lightdb.mapdb import lightdb.aggregate.AggregateQuery import lightdb.collection.Collection import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel} import lightdb.materialized.MaterializedAggregate import lightdb.store.{Conversion, Store, StoreManager, StoreMode} diff --git a/redis/src/main/scala/lightdb/redis/RedisStore.scala b/redis/src/main/scala/lightdb/redis/RedisStore.scala index d39ce77f..6d690424 100644 --- a/redis/src/main/scala/lightdb/redis/RedisStore.scala +++ b/redis/src/main/scala/lightdb/redis/RedisStore.scala @@ -3,7 +3,7 @@ package lightdb.redis import lightdb.aggregate.AggregateQuery import lightdb.collection.Collection import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel} import lightdb.materialized.MaterializedAggregate import lightdb.store.{Conversion, Store, StoreMode} diff --git a/rocksdb/src/main/scala/lightdb/rocksdb/RocksDBStore.scala b/rocksdb/src/main/scala/lightdb/rocksdb/RocksDBStore.scala index 437397d5..864c6c6a 100644 --- a/rocksdb/src/main/scala/lightdb/rocksdb/RocksDBStore.scala +++ b/rocksdb/src/main/scala/lightdb/rocksdb/RocksDBStore.scala @@ -5,7 +5,7 @@ import fabric.rw.{Asable, Convertible} import lightdb.aggregate.AggregateQuery import lightdb.collection.Collection import lightdb._ -import lightdb.Field._ +import lightdb.field.Field._ import lightdb.doc.{Document, DocumentModel} import lightdb.materialized.MaterializedAggregate import lightdb.store.{Conversion, Store, StoreMode} diff --git a/sql/src/main/scala/lightdb/sql/SQLArg.scala b/sql/src/main/scala/lightdb/sql/SQLArg.scala index 6ceb33b6..bb8e9b9e 100644 --- a/sql/src/main/scala/lightdb/sql/SQLArg.scala +++ b/sql/src/main/scala/lightdb/sql/SQLArg.scala @@ -7,7 +7,8 @@ import fabric.rw._ import lightdb.doc.Document import lightdb.spatial.Geo import lightdb._ -import lightdb.Field._ +import lightdb.field.Field +import lightdb.field.Field._ import java.sql.{JDBCType, PreparedStatement, SQLType, Types} @@ -56,7 +57,7 @@ object SQLArg { } object FieldArg { - def apply[Doc <: Document[Doc], F](doc: Doc, field: Field[Doc, F]): FieldArg[Doc, F] = apply(field, field.get(doc)) + def apply[Doc <: Document[Doc], F](doc: Doc, field: Field[Doc, F]): FieldArg[Doc, F] = apply(field, field.get(doc, field)) } case class StringArg(s: String) extends SQLArg { diff --git a/sql/src/main/scala/lightdb/sql/SQLStore.scala b/sql/src/main/scala/lightdb/sql/SQLStore.scala index 57f7dfc3..dbb4c028 100644 --- a/sql/src/main/scala/lightdb/sql/SQLStore.scala +++ b/sql/src/main/scala/lightdb/sql/SQLStore.scala @@ -16,7 +16,8 @@ import lightdb.store.{Conversion, Store, StoreMode} import lightdb.transaction.{Transaction, TransactionKey} import lightdb.util.ActionIterator import lightdb._ -import lightdb.Field._ +import lightdb.field.Field +import lightdb.field.Field._ import java.sql.{Connection, PreparedStatement, ResultSet} import scala.language.implicitConversions diff --git a/sqlite/src/main/scala/lightdb/sql/SQLiteStore.scala b/sqlite/src/main/scala/lightdb/sql/SQLiteStore.scala index 8166272a..f75eb9da 100644 --- a/sqlite/src/main/scala/lightdb/sql/SQLiteStore.scala +++ b/sqlite/src/main/scala/lightdb/sql/SQLiteStore.scala @@ -7,8 +7,9 @@ import fabric.rw._ import lightdb.collection.Collection import lightdb.distance.Distance import lightdb.sql.connect.{ConnectionManager, DBCPConnectionManager, SQLConfig, SingleConnectionManager} -import lightdb.{Field, LightDB, SortDirection} +import lightdb.{LightDB, SortDirection} import lightdb.doc.{Document, DocumentModel} +import lightdb.field.Field import lightdb.filter.Filter import lightdb.spatial.{Geo, Spatial} import lightdb.store.{Conversion, Store, StoreManager, StoreMode}