From 70a3f59b55e294f1da72742a5d08ae6f49e674c3 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 28 Sep 2021 12:13:56 +0200 Subject: [PATCH] tweak cached paginators --- modules/api/src/main/GameApi.scala | 108 +++++++++---------- modules/game/src/main/PaginatorBuilder.scala | 12 +-- modules/relation/src/main/RelationApi.scala | 32 +++--- 3 files changed, 69 insertions(+), 83 deletions(-) diff --git a/modules/api/src/main/GameApi.scala b/modules/api/src/main/GameApi.scala index 2c2cbb8118..3226de6426 100644 --- a/modules/api/src/main/GameApi.scala +++ b/modules/api/src/main/GameApi.scala @@ -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._ @@ -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 @@ -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 diff --git a/modules/game/src/main/PaginatorBuilder.scala b/modules/game/src/main/PaginatorBuilder.scala index 141f48e2f1..1251dce8af 100644 --- a/modules/game/src/main/PaginatorBuilder.scala +++ b/modules/game/src/main/PaginatorBuilder.scala @@ -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, diff --git a/modules/relation/src/main/RelationApi.scala b/modules/relation/src/main/RelationApi.scala index ce2760194e..23861aa41b 100644 --- a/modules/relation/src/main/RelationApi.scala +++ b/modules/relation/src/main/RelationApi.scala @@ -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](