Skip to content

Commit

Permalink
tweak cached paginators
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 28, 2021
1 parent c38af23 commit 70a3f59
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 83 deletions.
108 changes: 52 additions & 56 deletions modules/api/src/main/GameApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import lila.common.config._
import lila.common.Json.jodaWrites
import lila.common.paginator.{ Paginator, PaginatorJson }
import lila.db.dsl._
import lila.db.paginator.{ Adapter, CachedAdapter }
import lila.db.paginator.Adapter
import lila.game.BSONHandlers._
import lila.game.Game.{ BSONFields => G }
import lila.game.JsonView._
Expand Down Expand Up @@ -39,39 +39,37 @@ final private[api] class GameApi(
page: Int
): Fu[JsObject] =
Paginator(
adapter = new CachedAdapter(
adapter = new Adapter[Game](
collection = gameRepo.coll,
selector = {
if (~playing) lila.game.Query.nowPlaying(user.id)
else
$doc(
G.playerUids -> user.id,
G.status $gte chess.Status.Mate.id,
G.analysed -> analysed.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
)
} ++ $doc(
G.rated -> rated.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
),
projection = none,
sort = $doc(G.createdAt -> -1),
readPreference = ReadPreference.secondaryPreferred
),
nbResults =
if (~playing) gameCache.nbPlaying(user.id)
adapter = new Adapter[Game](
collection = gameRepo.coll,
selector = {
if (~playing) lila.game.Query.nowPlaying(user.id)
else
fuccess {
rated.fold(user.count.game) {
case true => user.count.rated
case _ => user.count.casual
$doc(
G.playerUids -> user.id,
G.status $gte chess.Status.Mate.id,
G.analysed -> analysed.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
)
} ++ $doc(
G.rated -> rated.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
),
projection = none,
sort = $doc(G.createdAt -> -1),
readPreference = ReadPreference.secondaryPreferred
).withNbResults(
if (~playing) gameCache.nbPlaying(user.id)
else
fuccess {
rated.fold(user.count.game) {
case true => user.count.rated
case _ => user.count.casual
}
}
),
currentPage = page,
maxPerPage = nb
Expand All @@ -98,32 +96,30 @@ final private[api] class GameApi(
page: Int
): Fu[JsObject] =
Paginator(
adapter = new CachedAdapter(
adapter = new Adapter[Game](
collection = gameRepo.coll,
selector = {
if (~playing) lila.game.Query.nowPlayingVs(users._1.id, users._2.id)
else
lila.game.Query.opponents(users._1, users._2) ++ $doc(
G.status $gte chess.Status.Mate.id,
G.analysed -> analysed.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
)
} ++ $doc(
G.rated -> rated.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
),
projection = none,
sort = $doc(G.createdAt -> -1),
readPreference = ReadPreference.secondaryPreferred
adapter = new Adapter[Game](
collection = gameRepo.coll,
selector = {
if (~playing) lila.game.Query.nowPlayingVs(users._1.id, users._2.id)
else
lila.game.Query.opponents(users._1, users._2) ++ $doc(
G.status $gte chess.Status.Mate.id,
G.analysed -> analysed.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
)
} ++ $doc(
G.rated -> rated.map[BSONValue] {
case true => BSONBoolean(true)
case _ => $doc("$exists" -> false)
}
),
nbResults =
if (~playing) gameCache.nbPlaying(users._1.id)
else crosstableApi(users._1.id, users._2.id).dmap(_.nbGames)
projection = none,
sort = $doc(G.createdAt -> -1),
readPreference = ReadPreference.secondaryPreferred
).withNbResults(
if (~playing) gameCache.nbPlaying(users._1.id)
else crosstableApi(users._1.id, users._2.id).dmap(_.nbGames)
),
currentPage = page,
maxPerPage = nb
Expand Down
12 changes: 3 additions & 9 deletions modules/game/src/main/PaginatorBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ final class PaginatorBuilder(gameRepo: GameRepo)(implicit ec: scala.concurrent.E
apply(selector, Query.sortCreated, nb) _

def apply(selector: Bdoc, sort: Bdoc, nb: Option[Int] = None)(page: Int): Fu[Paginator[Game]] =
apply(nb.fold(noCacheAdapter(selector, sort)) { cached =>
cacheAdapter(selector, sort, fuccess(cached))
apply(nb.fold[AdapterLike[Game]](noCacheAdapter(selector, sort)) { cached =>
noCacheAdapter(selector, sort) withNbResults fuccess(cached)
})(page)

private def apply(adapter: AdapterLike[Game])(page: Int): Fu[Paginator[Game]] =
paginator(adapter, page)

private def cacheAdapter(selector: Bdoc, sort: Bdoc, nbResults: Fu[Int]): AdapterLike[Game] =
new CachedAdapter(
adapter = noCacheAdapter(selector, sort),
nbResults = nbResults
)

private def noCacheAdapter(selector: Bdoc, sort: Bdoc): AdapterLike[Game] =
private def noCacheAdapter(selector: Bdoc, sort: Bdoc) =
new Adapter[Game](
collection = gameRepo.coll,
selector = selector,
Expand Down
32 changes: 14 additions & 18 deletions modules/relation/src/main/RelationApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,26 +104,22 @@ final class RelationApi(
coll.countSel($doc("u1" -> userId, "r" -> Block))

def followingPaginatorAdapter(userId: ID) =
new CachedAdapter[Followed](
adapter = new Adapter[Followed](
collection = coll,
selector = $doc("u1" -> userId, "r" -> Follow),
projection = $doc("u2" -> true, "_id" -> false).some,
sort = $empty
),
nbResults = countFollowing(userId)
).map(_.userId)
new Adapter[Followed](
collection = coll,
selector = $doc("u1" -> userId, "r" -> Follow),
projection = $doc("u2" -> true, "_id" -> false).some,
sort = $empty
).withNbResults(countFollowing(userId))
.map(_.userId)

def followersPaginatorAdapter(userId: ID) =
new CachedAdapter[Follower](
adapter = new Adapter[Follower](
collection = coll,
selector = $doc("u2" -> userId, "r" -> Follow),
projection = $doc("u1" -> true, "_id" -> false).some,
sort = $empty
),
nbResults = countFollowers(userId)
).map(_.userId)
new Adapter[Follower](
collection = coll,
selector = $doc("u2" -> userId, "r" -> Follow),
projection = $doc("u1" -> true, "_id" -> false).some,
sort = $empty
).withNbResults(countFollowers(userId))
.map(_.userId)

def blockingPaginatorAdapter(userId: ID) =
new Adapter[Blocked](
Expand Down

0 comments on commit 70a3f59

Please sign in to comment.