Skip to content

Commit

Permalink
Rewrite scala3 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jun 3, 2024
1 parent f6e7478 commit c19ba12
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 123 deletions.
3 changes: 2 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ maxColumn = 110
spaces.inImportCurlyBraces = true
rewrite.rules = [SortModifiers]
rewrite.redundantBraces.stringInterpolation = true

rewrite.scala3.removeOptionalBraces = yes
rewrite.scala3.convertToNewSyntax = yes
fileOverride {
"glob:**/build.sbt" {
runner.dialect = scala213
Expand Down
2 changes: 1 addition & 1 deletion modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ object SearchServiceImpl:
case q: Query.Team => lila.search.Index("team")

import smithy4s.json.Json.given
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.core.*

given [A: Schema]: Indexable[A] = (a: A) => writeToString(a)
given Indexable[ForumSource | GameSource | StudySource | TeamSource] =
Expand Down
2 changes: 1 addition & 1 deletion modules/client/src/main/scala/PlaySearchClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lila.search
package client

import akka.util.ByteString
import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.core.*
import lila.search.spec.*
import play.api.libs.ws.BodyWritable
import play.api.libs.ws.InMemoryBody
Expand Down
4 changes: 2 additions & 2 deletions modules/client/src/main/scala/SearchClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.annotation.targetName

trait SearchClient extends SearchService[Future] { client =>
trait SearchClient extends SearchService[Future]:
client =>
@targetName("storeBulkTeamWithPair")
def storeBulkTeam(sources: List[(String, TeamSource)]): Future[Unit] =
client.storeBulkTeam(sources.map(TeamSourceWithId.apply.tupled))
Expand Down Expand Up @@ -35,7 +36,6 @@ trait SearchClient extends SearchService[Future] { client =>

def storeTeam(id: String, source: TeamSource): Future[Unit] =
client.store(id, Source.team(source))
}

object SearchClient:

Expand Down
20 changes: 7 additions & 13 deletions modules/core/src/main/scala/ESClient.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package lila.search

import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture => _, _ }
import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture as _, * }
import com.sksamuel.elastic4s.fields.ElasticField
import com.sksamuel.elastic4s.{ ElasticClient, ElasticDsl, Index => ESIndex, Response }
import com.sksamuel.elastic4s.{ ElasticClient, ElasticDsl, Index as ESIndex, Response }
import com.sksamuel.elastic4s.{ Executor, Functor, Indexable }
import cats.syntax.all.*
import cats.MonadThrow

case class Index(name: String) extends AnyVal {
case class Index(name: String) extends AnyVal:
def toES: ESIndex = ESIndex(name)
}

trait ESClient[F[_]] {
trait ESClient[F[_]]:

def search[A](index: Index, query: A, from: From, size: Size)(implicit q: Queryable[A]): F[SearchResponse]
def count[A](index: Index, query: A)(implicit q: Queryable[A]): F[CountResponse]
Expand All @@ -23,11 +22,9 @@ trait ESClient[F[_]] {
def refreshIndex(index: Index): F[Unit]
def status: F[String]

}
object ESClient:

object ESClient {

def apply[F[_]: MonadThrow: Functor: Executor](client: ElasticClient) = new ESClient[F] {
def apply[F[_]: MonadThrow: Functor: Executor](client: ElasticClient) = new ESClient[F]:

def status: F[String] =
client
Expand Down Expand Up @@ -56,7 +53,7 @@ object ESClient {
client.execute(indexInto(index.name).source(obj).id(id.value)).void

def storeBulk[A](index: Index, objs: Seq[(String, A)])(implicit indexable: Indexable[A]): F[Unit] =
if (objs.isEmpty) ().pure[F]
if objs.isEmpty then ().pure[F]
else
client.execute {
ElasticDsl.bulk {
Expand Down Expand Up @@ -99,6 +96,3 @@ object ESClient {

private def dropIndex(index: Index) =
client.execute { deleteIndex(index.name) }
}

}
12 changes: 4 additions & 8 deletions modules/core/src/main/scala/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@ import lila.search.game.Sorting

sealed abstract class Query

object Query {
object Query:
implicit val query: Queryable[Query] =
new Queryable[Query] {
new Queryable[Query]:
def searchDef(query: Query)(from: From, size: Size) =
query match {
query match
case q: Game => game.GameQuery.query.searchDef(q)(from, size)
case q: Study => study.StudyQuery.query.searchDef(q)(from, size)
case q: Forum => forum.ForumQuery.query.searchDef(q)(from, size)
case q: Team => team.TeamQuery.query.searchDef(q)(from, size)
}

def countDef(query: Query) =
query match {
query match
case q: Game => game.GameQuery.query.countDef(q)
case q: Study => study.StudyQuery.query.countDef(q)
case q: Forum => forum.ForumQuery.query.countDef(q)
case q: Team => team.TeamQuery.query.countDef(q)
}
}
}

case class Team(text: String) extends Query

Expand Down
12 changes: 4 additions & 8 deletions modules/core/src/main/scala/Queryable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ package lila.search

import com.sksamuel.elastic4s.requests.searches.SearchRequest

trait Queryable[A] {
trait Queryable[A]:

def searchDef(query: A)(from: From, size: Size): Index => SearchRequest

def countDef(query: A): Index => SearchRequest
}

case class ParsedQuery(terms: List[String], filters: Map[String, String]) {
case class ParsedQuery(terms: List[String], filters: Map[String, String]):

def apply(fk: String): Option[String] = filters.get(fk)
}

object QueryParser {
object QueryParser:

private val spaceRegex = "[ +]+".r

def apply(q: String, filterKeys: Seq[String]): ParsedQuery = {
def apply(q: String, filterKeys: Seq[String]): ParsedQuery =

val terms = spaceRegex.split(q.trim.toLowerCase).toList

Expand All @@ -32,5 +30,3 @@ object QueryParser {
}
.getOrElse(parsed.copy(terms = parsed.terms :+ term))
}
}
}
11 changes: 4 additions & 7 deletions modules/core/src/main/scala/Range.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lila.search

import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.ElasticDsl.*

final class Range[A] private (val a: Option[A], val b: Option[A]) {
final class Range[A] private (val a: Option[A], val b: Option[A]):

def queries(name: String) =
a.fold(b.toList.map { bb => rangeQuery(name).lte(bb.toString) }) { aa =>
Expand All @@ -14,20 +14,17 @@ final class Range[A] private (val a: Option[A], val b: Option[A]) {
def map[B](f: A => B) = new Range(a.map(f), b.map(f))

def nonEmpty = a.nonEmpty || b.nonEmpty
}

object Range {
object Range:

def apply[A](a: Option[A], b: Option[A])(implicit o: Ordering[A]): Range[A] =
(a, b) match {
(a, b) match
case (Some(aa), Some(bb)) =>
o.lt(aa, bb)
.fold(
new Range(a, b),
new Range(b, a)
)
case (x, y) => new Range(x, y)
}

def none[A]: Range[A] = new Range(None, None)
}
9 changes: 3 additions & 6 deletions modules/core/src/main/scala/Which.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package lila.search

object Which {
object Which:

def mapping(index: Index) =
index match {
index match
case Index("game") => Some(game.Mapping.fields)
case Index("forum") => Some(forum.Mapping.fields)
case Index("team") => Some(team.Mapping.fields)
case Index("study") => Some(study.Mapping.fields)
case _ => None
}

def refreshInterval(index: Index) =
index match {
index match
case Index("study") => "10s"
case _ => "300s"
}
}
18 changes: 7 additions & 11 deletions modules/core/src/main/scala/forum.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package lila.search
package forum

import com.sksamuel.elastic4s.ElasticDsl._
import com.sksamuel.elastic4s.ElasticDsl.*
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder

object Fields {
object Fields:
val body = "bo"
val topic = "to"
val topicId = "ti"
val author = "au"
val troll = "tr"
val date = "da"
}

object Mapping {
import Fields._
object Mapping:
import Fields.*
def fields =
Seq(
textField(body).copy(boost = Some(2), analyzer = Some("english")),
Expand All @@ -24,10 +23,9 @@ object Mapping {
booleanField(troll).copy(docValues = Some(false)),
dateField(date)
)
}

object ForumQuery {
implicit val query: lila.search.Queryable[Forum] = new lila.search.Queryable[Forum] {
object ForumQuery:
implicit val query: lila.search.Queryable[Forum] = new lila.search.Queryable[Forum]:

def searchDef(query: Forum)(from: From, size: Size) =
index =>
Expand All @@ -45,13 +43,11 @@ object ForumQuery {

private def makeQuery(query: Forum) = boolQuery().must(
parsed(query.text).terms.map { term =>
multiMatchQuery(term).fields(searchableFields *)
multiMatchQuery(term).fields(searchableFields*)
} ::: List(
parsed(query.text)("user").map { termQuery(Fields.author, _) },
(!query.troll).option(termQuery(Fields.troll, false))
).flatten
)
}

private val searchableFields = List(Fields.body, Fields.topic, Fields.author)
}
38 changes: 14 additions & 24 deletions modules/core/src/main/scala/game.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package lila.search
package game

import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture => _, _ }
import scala.concurrent.duration._
import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture as _, * }
import scala.concurrent.duration.*

object Fields {
object Fields:
val status = "s"
val turns = "t"
val rated = "r"
Expand All @@ -23,10 +23,9 @@ object Fields {
val whiteUser = "wu"
val blackUser = "bu"
val source = "so"
}

object Mapping {
import Fields._
object Mapping:
import Fields.*
def fields =
Seq( // only keep docValues for sortable fields
keywordField(status).copy(docValues = Some(false)),
Expand All @@ -48,10 +47,9 @@ object Mapping {
keywordField(blackUser).copy(docValues = Some(false)),
keywordField(source).copy(docValues = Some(false))
)
}

object GameQuery {
implicit val query: lila.search.Queryable[Game] = new lila.search.Queryable[Game] {
object GameQuery:
implicit val query: lila.search.Queryable[Game] = new lila.search.Queryable[Game]:

val timeout = 5.seconds

Expand All @@ -65,9 +63,9 @@ object GameQuery {

def countDef(query: Game) = index => (search(index.name).query(makeQuery(query)) size 0).timeout(timeout)

private def makeQuery(query: Game) = {
private def makeQuery(query: Game) =

import query._
import query.*
def usernames = List(user1, user2).flatten

def hasAiQueries =
Expand All @@ -94,44 +92,36 @@ object GameQuery {
date.map(Date.formatter.print).queries(Fields.date),
hasAiQueries,
(hasAi.getOrElse(true)).fold(aiLevel.queries(Fields.ai), Nil),
if (perf.nonEmpty) List(termsQuery(Fields.perf, perf)) else Nil,
if perf.nonEmpty then List(termsQuery(Fields.perf, perf)) else Nil,
toQueries(source, Fields.source),
toQueries(rated, Fields.rated),
toQueries(status, Fields.status),
toQueries(analysed, Fields.analysed),
toQueries(whiteUser, Fields.whiteUser),
toQueries(blackUser, Fields.blackUser)
).flatten match {
).flatten match
case Nil => matchAllQuery()
case queries => boolQuery().must(queries)
}

}
}
}

case class Sorting(f: String, order: String) {
case class Sorting(f: String, order: String):
import com.sksamuel.elastic4s.requests.searches.sort.SortOrder
def definition =
fieldSort {
(Sorting.fieldKeys contains f).fold(f, Sorting.default.f)
}.order((order.toLowerCase == "asc").fold(SortOrder.ASC, SortOrder.DESC))
}

object Sorting {
object Sorting:

val default = Sorting(Fields.date, "desc")

val fieldKeys = List(Fields.date, Fields.turns, Fields.averageRating)
}

case class Clocking(
initMin: Option[Int] = None,
initMax: Option[Int] = None,
incMin: Option[Int] = None,
incMax: Option[Int] = None
) {
):

def init = Range(initMin, initMax)
def inc = Range(incMin, incMax)
}
Loading

0 comments on commit c19ba12

Please sign in to comment.