-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup and improvements to filter builder
- Loading branch information
1 parent
6dbbd02
commit 30d3fb0
Showing
16 changed files
with
106 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package lightdb.filter | ||
|
||
import lightdb.doc.{Document, DocumentModel} | ||
|
||
class FilterBuilder[Doc <: Document[Doc], Model <: DocumentModel[Doc]](val model: Model, | ||
minShould: Int, | ||
filters: List[FilterClause[Doc]]) extends Filter.Multi[Doc](minShould, filters) { | ||
def minShould(i: Int): FilterBuilder[Doc, Model] = new FilterBuilder(model, i, filters) | ||
|
||
def withFilter(filter: Filter[Doc], condition: Condition, boost: Option[Double] = None): FilterBuilder[Doc, Model] = | ||
new FilterBuilder(model, minShould, filters = filters ::: List(FilterClause(filter, condition, boost))) | ||
|
||
def must(f: Model => Filter[Doc], boost: Option[Double] = None): FilterBuilder[Doc, Model] = withFilter(f(model), Condition.Must, boost) | ||
def mustNot(f: Model => Filter[Doc], boost: Option[Double] = None): FilterBuilder[Doc, Model] = withFilter(f(model), Condition.MustNot, boost) | ||
def filter(f: Model => Filter[Doc], boost: Option[Double] = None): FilterBuilder[Doc, Model] = withFilter(f(model), Condition.Filter, boost) | ||
def should(f: Model => Filter[Doc], boost: Option[Double] = None): FilterBuilder[Doc, Model] = withFilter(f(model), Condition.Should, boost) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package lightdb.filter | ||
|
||
case class FilterClause[Doc](filter: Filter[Doc], condition: Condition, boost: Option[Double]) | ||
import lightdb.doc.Document | ||
|
||
case class FilterClause[Doc <: Document[Doc]](filter: Filter[Doc], condition: Condition, boost: Option[Double]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,31 @@ | ||
package lightdb | ||
|
||
import lightdb.doc.{Document, DocumentModel} | ||
|
||
import scala.language.implicitConversions | ||
|
||
package object filter { | ||
implicit class ListFilterExtras[V, Doc, Filter](fs: FilterSupport[List[V], Doc, Filter]) { | ||
def has(value: V): Filter = fs.is(List(value)) | ||
} | ||
implicit class SetFilterExtras[V, Doc, Filter](fs: FilterSupport[Set[V], Doc, Filter]) { | ||
def has(value: V): Filter = fs.is(Set(value)) | ||
} | ||
implicit class FilterExtras[Doc <: Document[Doc]](val filter: Filter[Doc]) extends AnyVal { | ||
def &&(that: Filter[Doc]): Filter[Doc] = (filter, that) match { | ||
case (b1: Filter.Multi[Doc], b2: Filter.Multi[Doc]) if b1.minShould == b2.minShould => | ||
Filter.Multi(minShould = b1.minShould, filters = b1.filters ::: b2.filters) | ||
case (_, b: Filter.Multi[Doc]) => b.conditional(filter, Condition.Must) | ||
case (b: Filter.Multi[Doc], _) => b.conditional(that, Condition.Must) | ||
case _ => Filter.Multi(minShould = 1).conditional(filter, Condition.Must).conditional(that, Condition.Must) | ||
} | ||
|
||
def ||(that: Filter[Doc]): Filter[Doc] = (filter, that) match { | ||
case (b1: Filter.Multi[Doc], b2: Filter.Multi[Doc]) if b1.minShould == b2.minShould => | ||
Filter.Multi(minShould = b1.minShould, filters = b1.filters ::: b2.filters) | ||
case (_, b: Filter.Multi[Doc]) => b.conditional(filter, Condition.Should) | ||
case (b: Filter.Multi[Doc], _) => b.conditional(that, Condition.Should) | ||
case _ => Filter.Multi(minShould = 1).conditional(filter, Condition.Should).conditional(that, Condition.Should) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.