Skip to content

Commit

Permalink
more DB code rewrite WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 1, 2016
1 parent 67ef640 commit d11f0ac
Show file tree
Hide file tree
Showing 76 changed files with 303 additions and 404 deletions.
2 changes: 1 addition & 1 deletion app/controllers/Account.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import play.api.mvc._, Results._
import lila.api.Context
import lila.app._
import lila.common.LilaCookie
import lila.db.api.$find
import lila.db.dsl.$find
import lila.security.Permission
import lila.user.tube.userTube
import lila.user.{ User => UserModel, UserRepo }
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/User.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import lila.api.{ Context, BodyContext }
import lila.app._
import lila.app.mashup.GameFilterMenu
import lila.common.LilaCookie
import lila.db.api.$find
import lila.db.dsl.$find
import lila.evaluation.{ PlayerAggregateAssessment }
import lila.game.{ GameRepo, Pov }
import lila.rating.PerfType
Expand Down
2 changes: 1 addition & 1 deletion app/mashup/GameFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lila.app
package mashup

import lila.common.paginator.Paginator
import lila.db.api.SortOrder
import lila.db.dsl.SortOrder
import lila.game.{ Game, Query, GameRepo }
import lila.user.User

Expand Down
2 changes: 1 addition & 1 deletion app/mashup/TeamInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mashup

import scala.concurrent.duration._

import lila.db.api._
import lila.db.dsl._
import lila.forum.MiniForumPost
import lila.game.{ GameRepo, Game }
import lila.team.tube._
Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/GameApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import chess.format.pgn.Pgn
import lila.analyse.{ AnalysisRepo, Analysis }
import lila.common.paginator.{ Paginator, PaginatorJson }
import lila.common.PimpedJson._
import lila.db.api._
import lila.db.dsl._
import lila.db.Implicits._
import lila.db.paginator.{ BSONAdapter, CachedAdapter }
import lila.game.BSONHandlers._
Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/PgnDump.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lila.api

import chess.format.pgn.{ Pgn, Parser }
import lila.db.api.$query
import lila.db.dsl.$query
import lila.db.Implicits._
import lila.game.Game
import lila.game.{ GameRepo, Query }
Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/RatingFest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import play.api.libs.iteratee._
import play.api.libs.json.Json
import reactivemongo.bson._

import lila.db.api._
import lila.db.dsl._
import lila.db.Implicits._
import lila.game.BSONHandlers.gameBSONHandler
import lila.game.Game.{ BSONFields => G }
Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/UserApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lila.api
import play.api.libs.json._

import lila.common.PimpedJson._
import lila.db.api._
import lila.db.dsl._
import lila.db.Implicits._
import lila.game.GameRepo
import lila.hub.actorApi.{ router => R }
Expand Down
2 changes: 1 addition & 1 deletion modules/bookmark/src/main/BookmarkApi.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lila.bookmark

import lila.db.api._
import lila.db.dsl._
import lila.game.tube.gameTube
import lila.game.{ Game, GameRepo }
import lila.user.User
Expand Down
2 changes: 1 addition & 1 deletion modules/bookmark/src/main/BookmarkRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.joda.time.DateTime
import play.api.libs.json._
import reactivemongo.bson._

import lila.db.api._
import lila.db.dsl._
import lila.db.Implicits._
import tube.bookmarkTube

Expand Down
2 changes: 1 addition & 1 deletion modules/bookmark/src/main/PaginatorBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import play.modules.reactivemongo.json.ImplicitBSONHandlers._

import lila.common.paginator._
import lila.common.PimpedJson._
import lila.db.api._
import lila.db.dsl._
import lila.db.Implicits._
import lila.db.paginator._
import lila.game.{ Game, GameRepo }
Expand Down
2 changes: 1 addition & 1 deletion modules/coordinate/src/main/CoordinateApi.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lila.coordinate

import lila.db.Types.Coll
import lila.db.dsl.Coll
import reactivemongo.bson._

final class CoordinateApi(scoreColl: Coll) {
Expand Down
3 changes: 3 additions & 0 deletions modules/explorer/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.typesafe.config.Config

final class Env(
config: Config,
gameColl: lila.db.dsl.Coll,
system: ActorSystem) {

private val Endpoint = config getString "endpoint"
private val MassImportEndpoint = config getString "mass_import.endpoint"
private val IndexFlow = config getBoolean "index_flow"

private lazy val indexer = new ExplorerIndexer(
gameColl = gameColl,
endpoint = Endpoint,
massImportEndpoint = MassImportEndpoint)

Expand Down Expand Up @@ -41,5 +43,6 @@ object Env {

lazy val current = "explorer" boot new Env(
config = lila.common.PlayApp loadConfig "explorer",
gameColl = lila.game.Env.current.gameColl,
system = lila.common.PlayApp.system)
}
10 changes: 4 additions & 6 deletions modules/explorer/src/main/ExplorerIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import play.api.libs.iteratee._
import play.api.libs.ws.WS
import play.api.Play.current

import lila.db.api._
import lila.db.Implicits._
import lila.db.dsl._
import lila.game.BSONHandlers.gameBSONHandler
import lila.game.tube.gameTube
import lila.game.{ Game, GameRepo, Query, PgnDump, Player }
import lila.user.UserRepo

private final class ExplorerIndexer(
gameColl: Coll,
endpoint: String,
massImportEndpoint: String) {

Expand All @@ -40,16 +39,15 @@ private final class ExplorerIndexer(
def apply(sinceStr: String): Funit =
parseDate(sinceStr).fold(fufail[Unit](s"Invalid date $sinceStr")) { since =>
logger.info(s"Start indexing since $since")
val query = $query(
val query =
Query.createdSince(since) ++
Query.rated ++
Query.finished ++
Query.turnsMoreThan(8) ++
Query.noProvisional ++
Query.bothRatingsGreaterThan(1501)
)
import reactivemongo.api._
pimpQB(query)
gameColl.find($empty)
.sort(Query.sortChronological)
.cursor[Game](ReadPreference.secondaryPreferred)
.enumerate(maxGames, stopOnError = true) &>
Expand Down
9 changes: 9 additions & 0 deletions modules/forum/src/main/BSONHandlers.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lila.forum

private object BSONHandlers {

import lila.db.BSON.BSONJodaDateTimeHandler
implicit val CategBSONHandler = reactivemongo.bson.Macros.handler[Categ]
implicit val TopicBSONHandler = reactivemongo.bson.Macros.handler[Topic]
implicit val PostBSONHandler = reactivemongo.bson.Macros.handler[Post]
}
20 changes: 3 additions & 17 deletions modules/forum/src/main/Categ.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package lila.forum

case class Categ(
id: String, // slug
_id: String, // slug
name: String,
desc: String,
pos: Int,
Expand All @@ -13,6 +13,8 @@ case class Categ(
nbPostsTroll: Int,
lastPostIdTroll: String) {

def id = _id

def nbTopics(troll: Boolean): Int = troll.fold(nbTopicsTroll, nbTopics)
def nbPosts(troll: Boolean): Int = troll.fold(nbPostsTroll, nbPosts)
def lastPostId(troll: Boolean): String = troll.fold(lastPostIdTroll, lastPostId)
Expand All @@ -31,19 +33,3 @@ case class Categ(

def slug = id
}

object Categ {

import lila.db.JsTube
import JsTube.Helpers._
import play.api.libs.json._

private implicit def topicTube = Topic.tube

private def defaults = Json.obj("team" -> none[String])

private[forum] lazy val tube = JsTube(
reader = (__.json update merge(defaults)) andThen Json.reads[Categ],
writer = Json.writes[Categ]
)
}
22 changes: 11 additions & 11 deletions modules/forum/src/main/CategApi.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package lila.forum

import lila.common.paginator._
import lila.db.api._
import lila.db.Implicits._
import lila.db.dsl._
import lila.db.paginator._
import lila.user.{ User, UserContext }
import tube._

private[forum] final class CategApi(env: Env) {

import BSONHandlers._

def list(teams: Set[String], troll: Boolean): Fu[List[CategView]] = for {
categs CategRepo withTeams teams
views (categs map { categ =>
Expand All @@ -27,7 +27,7 @@ private[forum] final class CategApi(env: Env) {
def makeTeam(slug: String, name: String): Funit =
CategRepo.nextPosition flatMap { position =>
val categ = Categ(
id = teamSlug(slug),
_id = teamSlug(slug),
name = name,
desc = "Forum of the team " + name,
pos = position,
Expand Down Expand Up @@ -55,10 +55,10 @@ private[forum] final class CategApi(env: Env) {
hidden = topic.hidden,
lang = "en".some,
categId = categ.id)
$insert(categ) >>
$insert(post) >>
$insert(topic withPost post) >>
$update(categ withTopic post)
env.categColl.insert(categ).void >>
env.postColl.insert(post).void >>
env.topicColl.insert(topic withPost post).void >>
env.categColl.update($id(categ.id), categ withTopic post).void
}

def show(slug: String, page: Int, troll: Boolean): Fu[Option[(Categ, Paginator[TopicView])]] =
Expand All @@ -75,17 +75,17 @@ private[forum] final class CategApi(env: Env) {
topicIdsTroll = topicsTroll map (_.id)
nbPostsTroll PostRepoTroll countByTopics topicIdsTroll
lastPostTroll PostRepoTroll lastByTopics topicIdsTroll
_ $update(categ.copy(
_ env.categColl.update($id(categ.id), categ.copy(
nbTopics = topics.size,
nbPosts = nbPosts,
lastPostId = lastPost ?? (_.id),
nbTopicsTroll = topicsTroll.size,
nbPostsTroll = nbPostsTroll,
lastPostIdTroll = lastPostTroll ?? (_.id)
))
)).void
} yield ()

def denormalize: Funit = $find.all[Categ] flatMap { categs =>
def denormalize: Funit = env.categColl.list[Categ]($empty) flatMap { categs =>
categs.map(denormalize).sequenceFu
} void
}
30 changes: 14 additions & 16 deletions modules/forum/src/main/CategRepo.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
package lila.forum

import play.api.libs.json.Json

import lila.db.api._
import lila.db.Implicits._
import tube.categTube
import lila.db.dsl._

object CategRepo {

def bySlug(slug: String) = $find byId slug
import BSONHandlers.CategBSONHandler

// dirty
private val coll = Env.current.categColl

def bySlug(slug: String) = coll.byId[Categ](slug)

def withTeams(teams: Set[String]): Fu[List[Categ]] =
$find($query($or(Seq(
Json.obj("team" -> $exists(false)),
Json.obj("team" -> $in(teams))
))) sort $sort.asc("pos"))
coll.find($or(
"team" $exists false,
"team" $in teams
)).sort($sort asc "pos").cursor[Categ].collect[List]()

def nextPosition: Fu[Int] = $primitive.one(
$select.all,
"pos",
_ sort $sort.desc("pos")
)(_.asOpt[Int]) map (~_ + 1)
def nextPosition: Fu[Int] =
coll.primitiveOne[Int]($empty, $sort desc "pos", "pos") map (~_ + 1)

def nbPosts(id: String): Fu[Int] =
$primitive.one($select(id), "nbPosts")(_.asOpt[Int]) map (~_)
coll.primitiveOne[Int]($id(id), "nbPosts") map (~_)
}
1 change: 0 additions & 1 deletion modules/forum/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ final class Env(
lazy val recent = new Recent(postApi, RecentTtl, RecentNb, PublicCategIds)

def cli = new lila.common.Cli {
import tube._
def process = {
case "forum" :: "denormalize" :: Nil =>
topicApi.denormalize >> categApi.denormalize inject "Forum denormalized"
Expand Down
15 changes: 4 additions & 11 deletions modules/forum/src/main/Post.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ornicar.scalalib.Random
import lila.user.User

case class Post(
id: String,
_id: String,
topicId: String,
categId: String,
author: Option[String],
Expand All @@ -19,6 +19,8 @@ case class Post(
lang: Option[String],
createdAt: DateTime) {

def id = _id

def showAuthor = (author map (_.trim) filter ("" !=)) | User.anonymous

def showUserIdOrAuthor = userId | showAuthor
Expand All @@ -43,7 +45,7 @@ object Post {
lang: Option[String],
troll: Boolean,
hidden: Boolean): Post = Post(
id = Random nextStringUppercase idSize,
_id = Random nextStringUppercase idSize,
topicId = topicId,
author = author,
userId = userId,
Expand All @@ -55,13 +57,4 @@ object Post {
hidden = hidden,
createdAt = DateTime.now,
categId = categId)

import lila.db.JsTube
import JsTube.Helpers._
import play.api.libs.json._

private[forum] lazy val tube = JsTube(
(__.json update readDate('createdAt)) andThen Json.reads[Post],
Json.writes[Post] andThen (__.json update writeDate('createdAt))
)
}
Loading

0 comments on commit d11f0ac

Please sign in to comment.