Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryPrinter for getting a string representation of a Query #106

Merged
merged 7 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ jobs:
- name: Submit Dependencies
uses: scalacenter/sbt-dependency-submission@v2
with:
modules-ignore: rootjs_2.12 rootjs_2.13 rootjs_3 docs_2.12 docs_2.13 docs_3 rootjvm_2.12 rootjvm_2.13 rootjvm_3 rootnative_2.12 rootnative_2.13 rootnative_3
modules-ignore: lucille-benchmarks_2.12 lucille-benchmarks_2.13 lucille-benchmarks_3 rootjs_2.12 rootjs_2.13 rootjs_3 docs_2.12 docs_2.13 docs_3 rootjvm_2.12 rootjvm_2.13 rootjvm_3 rootnative_2.12 rootnative_2.13 rootnative_3
configs-ignore: test scala-tool scala-doc-tool test-internal

site:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pink.cozydev.lucille
package benchmarks

import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._

import pink.cozydev.lucille.Query
import pink.cozydev.lucille.QueryPrinter
import cats.data.NonEmptyList

/** To run the benchmark from within sbt:
*
* jmh:run -i 10 -wi 10 -f 2 -t 1 pink.cozydev.lucille.benchmarks.QueryPrinterBenchmark
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not run the benchmarks, just fyi

*
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but
* more is better.
*/
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
class QueryPrinterBenchmark {
import Query._

var orQueries10: Query = _
var orQueries1000: Query = _
var queries: Vector[Query] = Vector.empty

@Setup
def setup(): Unit = {
orQueries10 = Or(NonEmptyList(Term("o"), (1 to 10).map(i => Term(i.toString)).toList))
orQueries1000 = Or(NonEmptyList(Term("o"), (1 to 1000).map(i => Term(i.toString)).toList))
queries = Vector(
Term("term"),
Phrase("phrase query"),
Prefix("prefi"),
Proximity("proximity query", 2),
Fuzzy("fuzzy", None),
Fuzzy("fuzzy", Some(2)),
TermRegex("/.ump(s|ing)/"),
TermRange(None, None, false, false),
TermRange(Some("apple"), None, true, false),
TermRange(None, Some("banana"), false, true),
TermRange(Some("apple"), Some("banana"), true, true),
)
}

@Benchmark
def orQueries10Print(): String =
QueryPrinter.print(orQueries10)

@Benchmark
def orQueries1000Print(): String =
QueryPrinter.print(orQueries1000)

@Benchmark
def termQueriesPrint(): Vector[String] =
queries.map(QueryPrinter.print)

}
8 changes: 8 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
),
)

lazy val benchmarks = project
.in(file("benchmarks"))
.dependsOn(core.jvm)
.settings(
name := "lucille-benchmarks"
)
.enablePlugins(NoPublishPlugin, JmhPlugin)

import laika.ast.Path.Root
import laika.helium.config.{IconLink, HeliumIcon, TextLink, ThemeNavigationSection}
lazy val docs = project
Expand Down
95 changes: 95 additions & 0 deletions core/src/main/scala/pink/cozydev/lucille/QueryPrinter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2022 CozyDev
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated aside: do we need to update this?

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pink.cozydev.lucille

import pink.cozydev.lucille.Query._
import cats.data.NonEmptyList

object QueryPrinter {

def print(query: Query): String = {
val sb = new StringBuilder()

def printQ(query: Query): Unit =
query match {
case q: MultiQuery => printEachNel(q.qs, " ")
case q: TermQuery => strTermQuery(q)
case q: Or => printEachNel(q.qs, " OR ")
case q: And => printEachNel(q.qs, " AND ")
case q: Not =>
sb.append("NOT ")
printQ(q.q)
case q: Group =>
sb.append('(')
printEachNel(q.qs, " ")
sb.append(')')
case q: UnaryPlus =>
sb.append('+')
printQ(q.q)
case q: UnaryMinus =>
sb.append('-')
printQ(q.q)
case q: MinimumMatch =>
sb.append('(')
printEachNel(q.qs, " ")
sb.append(s")@${q.num}")
case q: Field =>
sb.append(q.field)
sb.append(':')
printQ(q.q)
}

def strTermQuery(q: TermQuery): Unit =
q match {
case q: Term => sb.append(q.str)
case q: Phrase =>
sb.append('"')
sb.append(q.str)
sb.append('"')
case q: Prefix =>
sb.append(q.str)
sb.append('*')
case q: Proximity =>
sb.append('"')
sb.append(q.str)
sb.append("\"~")
sb.append(q.num.toString())
case q: Fuzzy =>
sb.append(q.str)
sb.append('~')
q.num.foreach(i => sb.append(i.toString()))
case q: TermRegex => sb.append(q.str)
case q: TermRange =>
if (q.lowerInc) sb.append('{') else sb.append('[')
sb.append(q.lower.getOrElse("*"))
sb.append(" TO ")
sb.append(q.upper.getOrElse("*"))
if (q.upperInc) sb.append('}') else sb.append(']')
}

def printEachNel(nel: NonEmptyList[Query], sep: String): Unit = {
printQ(nel.head)
nel.tail.foreach { q =>
sb.append(sep)
printQ(q)
}
}

printQ(query)
sb.result()
}
}
148 changes: 148 additions & 0 deletions core/src/test/scala/pink/cozydev/lucille/QueryPrinterSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright 2022 CozyDev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pink.cozydev.lucille

import pink.cozydev.lucille.Query._
import cats.data.NonEmptyList

class QueryPrinterSimpleQueriesSuite extends munit.FunSuite {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎


test("prints MultiQuery query") {
val q = MultiQuery(NonEmptyList.of(Term("hello"), Term("hi")))
val str = QueryPrinter.print(q)
assertEquals(str, "hello hi")
}

test("prints OR query") {
val q = Or(NonEmptyList.of(Term("hello"), Term("hi")))
val str = QueryPrinter.print(q)
assertEquals(str, "hello OR hi")
}

test("prints AND query") {
val q = And(NonEmptyList.of(Term("hello"), Term("hi")))
val str = QueryPrinter.print(q)
assertEquals(str, "hello AND hi")
}

test("prints Not query") {
val q = Not(Group(NonEmptyList.of(Term("hello"), Term("hi"))))
val str = QueryPrinter.print(q)
assertEquals(str, "NOT (hello hi)")
}

test("prints Group query") {
val q = Group(NonEmptyList.of(Term("hello"), Term("hi")))
val str = QueryPrinter.print(q)
assertEquals(str, "(hello hi)")
}

test("prints UnaryMinus query") {
val q = UnaryMinus(Term("hello"))
val str = QueryPrinter.print(q)
assertEquals(str, "-hello")
}

test("prints UnaryPlus query") {
val q = UnaryPlus(Term("hello"))
val str = QueryPrinter.print(q)
assertEquals(str, "+hello")
}

test("prints MinimumMatch query") {
val q = MinimumMatch(NonEmptyList.of(Term("hello"), Term("hi")), 2)
val str = QueryPrinter.print(q)
assertEquals(str, "(hello hi)@2")
}

test("prints Field query") {
val q = Field("msg", MinimumMatch(NonEmptyList.of(Term("hello"), Term("hi")), 2))
val str = QueryPrinter.print(q)
assertEquals(str, "msg:(hello hi)@2")
}

}

class QueryPrinterSimpleQueryTermSuite extends munit.FunSuite {

test("prints single term") {
val q = Term("hello")
val str = QueryPrinter.print(q)
assertEquals(str, "hello")
}

test("prints phrase") {
val q = Phrase("hello friend")
val str = QueryPrinter.print(q)
assertEquals(str, "\"hello friend\"")
}

test("prints prefix term") {
val q = Prefix("hel")
val str = QueryPrinter.print(q)
assertEquals(str, "hel*")
}

test("prints proximity term") {
val q = Proximity("cats jumped", 2)
val str = QueryPrinter.print(q)
assertEquals(str, "\"cats jumped\"~2")
}

test("prints fuzzy (no num) term") {
val q = Fuzzy("hello", None)
val str = QueryPrinter.print(q)
assertEquals(str, "hello~")
}

test("prints fuzzy (num) term") {
val q = Fuzzy("hello", Some(2))
val str = QueryPrinter.print(q)
assertEquals(str, "hello~2")
}

test("prints regex term") {
val q = TermRegex("/.ump(s|ing)/")
val str = QueryPrinter.print(q)
assertEquals(str, "/.ump(s|ing)/")
}

test("prints term range [* TO *]") {
val q = TermRange(None, None, false, false)
val str = QueryPrinter.print(q)
assertEquals(str, "[* TO *]")
}

test("prints term range [Apple TO Banana]") {
val q = TermRange(Some("Apple"), Some("Banana"), false, false)
val str = QueryPrinter.print(q)
assertEquals(str, "[Apple TO Banana]")
}

test("prints term range {Apple TO Banana]") {
val q = TermRange(Some("Apple"), Some("Banana"), true, false)
val str = QueryPrinter.print(q)
assertEquals(str, "{Apple TO Banana]")
}

test("prints term range [Apple TO Banana}") {
val q = TermRange(Some("Apple"), Some("Banana"), false, true)
val str = QueryPrinter.print(q)
assertEquals(str, "[Apple TO Banana}")
}

}
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.14.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.16")

addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2")

addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")