-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b570b88
Add initial, very simple printer
valencik e364ccd
Add initial benchmarks
valencik 7a3543b
Rework benchmarks, no parsing
valencik 15f28d9
Add StringBuild approach
valencik dd91a73
Remove old printer
valencik d50e98c
Simplify / reduce benchmarks
valencik 397901f
Ditch the toString benchmarks
valencik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
59 changes: 59 additions & 0 deletions
59
benchmarks/src/main/scala/pink/cozydev/lucille/QueryPrinterBenchmark.scala
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,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 | ||
* | ||
* 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) | ||
|
||
} |
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
95 changes: 95 additions & 0 deletions
95
core/src/main/scala/pink/cozydev/lucille/QueryPrinter.scala
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,95 @@ | ||
/* | ||
* Copyright 2022 CozyDev | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
148
core/src/test/scala/pink/cozydev/lucille/QueryPrinterSuite.scala
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,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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