Skip to content

Commit

Permalink
Update to ReactiveMongo 0.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cchantep committed Oct 23, 2016
1 parent f48b3c8 commit 4ad2c23
Show file tree
Hide file tree
Showing 33 changed files with 200 additions and 147 deletions.
7 changes: 5 additions & 2 deletions modules/api/src/main/PgnDump.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ final class PgnDump(
}
}

def exportUserGames(userId: String): Enumerator[String] =
GameRepo.sortedCursor(Query user userId, Query.sortCreated).enumerate() &> toPgn
def exportUserGames(userId: String): Enumerator[String] = {
import reactivemongo.play.iteratees.cursorProducer
GameRepo.sortedCursor(Query user userId, Query.sortCreated).
enumerator() &> toPgn
}

def exportGamesFromIds(ids: List[String]): Enumerator[String] =
Enumerator.enumerate(ids grouped 50) &>
Expand Down
23 changes: 12 additions & 11 deletions modules/blog/src/main/Notifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ private[blog] final class Notifier(
}

private def doSend(post: Document): Funit = {
import reactivemongo.play.iteratees.cursorProducer

val content = NewBlogPost(
id = NewBlogPost.Id(post.id),
slug = NewBlogPost.Slug(post.slug),
title = NewBlogPost.Title(~post.getText("blog.title")))
UserRepo.recentlySeenNotKidIdsCursor(DateTime.now minusWeeks 1)
.enumerate(500 * 1000, stopOnError = true) &>
Enumeratee.map {
_.getAs[String]("_id") err "User without an id"
} |>>>
Iteratee.foldM[String, Int](0) {
case (count, userId) => notifyApi.addNotificationWithoutSkipOrEvent(
Notification(Notification.Notifies(userId), content)
) inject (count + 1)
} addEffect { count =>
logger.info(s"Sent $count notifications")
} void
.enumerator(500 * 1000) &> Enumeratee.map {
_.getAs[String]("_id") err "User without an id"
} |>>>
Iteratee.foldM[String, Int](0) {
case (count, userId) => notifyApi.addNotificationWithoutSkipOrEvent(
Notification(Notification.Notifies(userId), content)
) inject (count + 1)
} addEffect { count =>
logger.info(s"Sent $count notifications")
} void
}
}
8 changes: 4 additions & 4 deletions modules/db/src/main/BSON.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object BSON extends Handlers {
val b = collection.immutable.Map.newBuilder[String, V]
for (tuple <- bson.elements)
// assume that all values in the document are Bdocs
b += (tuple._1 -> vr.read(tuple._2.asInstanceOf[Bdoc]))
b += (tuple.name -> vr.read(tuple.value.asInstanceOf[Bdoc]))
b.result
}
}
Expand Down Expand Up @@ -82,7 +82,7 @@ object BSON extends Handlers {
val valueReader = vr.asInstanceOf[BSONReader[BSONValue, V]]
// mutable optimized implementation
val b = collection.immutable.Map.newBuilder[String, V]
for (tuple <- bson.elements) b += (tuple._1 -> valueReader.read(tuple._2))
for (tuple <- bson.elements) b += (tuple.name -> valueReader.read(tuple.value))
b.result
}
}
Expand All @@ -108,7 +108,7 @@ object BSON extends Handlers {
val map = {
// mutable optimized implementation
val b = collection.immutable.Map.newBuilder[String, BSONValue]
for (tuple <- doc.stream if tuple.isSuccess) b += (tuple.get._1 -> tuple.get._2)
for (tuple <- doc.stream if tuple.isSuccess) b += (tuple.get.name -> tuple.get.value)
b.result
}

Expand Down Expand Up @@ -194,7 +194,7 @@ object BSON extends Handlers {
}
def debugArr(doc: Barr): String = doc.values.toList.map(debug).mkString("[", ", ", "]")
def debugDoc(doc: Bdoc): String = (doc.elements.toList map {
case (k, v) => s"$k: ${debug(v)}"
case BSONElement(k, v) => s"$k: ${debug(v)}"
}).mkString("{", ", ", "}")

def hashDoc(doc: Bdoc): String = debugDoc(doc).replace(" ", "")
Expand Down
2 changes: 1 addition & 1 deletion modules/db/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class Env(
lifecycle: play.api.inject.ApplicationLifecycle) {

lazy val (connection, dbName) = {
val driver = new MongoDriver(Some(config))
val driver = MongoDriver(config)

registerDriverShutdownHook(driver)

Expand Down
20 changes: 12 additions & 8 deletions modules/db/src/main/dsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import reactivemongo.api._
import reactivemongo.api.collections.GenericQueryBuilder
import reactivemongo.bson._

trait dsl {
trait dsl extends LowPriorityDsl {

type Coll = reactivemongo.api.collections.bson.BSONCollection
type Bdoc = BSONDocument
Expand Down Expand Up @@ -116,7 +116,8 @@ trait dsl {
}

def $rename(item: (String, String), items: (String, String)*)(implicit writer: BSONWriter[String, _ <: BSONValue]): BSONDocument = {
$doc("$rename" -> $doc((Seq(item) ++ items).map(Producer.nameValue2Producer[String]): _*))
$doc("$rename" -> $doc((item +: items).
map { case (k, v) => BSONElement(k, BSONString(v)) }))
}

def $setOnInsert(item: Producer[BSONElement], items: Producer[BSONElement]*): BSONDocument = {
Expand All @@ -128,7 +129,7 @@ trait dsl {
}

def $unset(field: String, fields: String*): BSONDocument = {
$doc("$unset" -> $doc((Seq(field) ++ fields).map(_ -> BSONString(""))))
$doc("$unset" -> $doc((Seq(field) ++ fields).map(k => BSONElement(k, BSONString("")))))
}

def $min(item: Producer[BSONElement]): BSONDocument = {
Expand Down Expand Up @@ -184,7 +185,7 @@ trait dsl {
}

def $currentDate(items: (String, CurrentDateValueProducer[_])*): BSONDocument = {
$doc("$currentDate" -> $doc(items.map(item => item._1 -> item._2.produce)))
$doc("$currentDate" -> $doc(items.map(item => BSONElement(item._1, item._2.produce))))
}
// End of Top Level Field Update Operators
//**********************************************************************************************//
Expand Down Expand Up @@ -371,13 +372,16 @@ trait dsl {
with LogicalOperators
with ArrayOperators

implicit def toBSONElement[V <: BSONValue](expression: Expression[V])(implicit writer: BSONWriter[V, _ <: BSONValue]): Producer[BSONElement] = {
expression.field -> expression.value
}

implicit def toBSONDocument[V <: BSONValue](expression: Expression[V])(implicit writer: BSONWriter[V, _ <: BSONValue]): BSONDocument =
$doc(expression.field -> expression.value)

}

sealed trait LowPriorityDsl { self: dsl =>
// Priority lower than toBSONDocument
implicit def toBSONElement[V <: BSONValue](expression: Expression[V])(implicit writer: BSONWriter[V, _ <: BSONValue]): Producer[BSONElement] = {
BSONElement(expression.field, expression.value)
}
}

object dsl extends dsl with CollExt with QueryBuilderExt with CursorExt with Handlers
5 changes: 3 additions & 2 deletions modules/evaluation/src/main/Assessible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ case class Assessible(analysed: Analysed) {
case PlayerFlags(T, T, T, T, T, T, T) => Cheating // all T, obvious cheat
case PlayerFlags(T, _, T, _, _, T, _) => Cheating // high accuracy, high blurs, no fast moves
case PlayerFlags(T, _, _, T, _, _, _) => Cheating // high accuracy, moderate blurs


case PlayerFlags(_, T, _, T, T, _, _) => LikelyCheating // always has advantage, moderate blurs, highly consistent move times
case PlayerFlags(_, _, _, T, T, _, _) => LikelyCheating // high accuracy, moderate blurs => 93% chance cheating
case PlayerFlags(T, _, _, _, _, _, T) => LikelyCheating // Holds are bad, hmk?
case PlayerFlags(_, T, _, _, _, _, T) => LikelyCheating // Holds are bad, hmk?
case PlayerFlags(_, T, _, T, T, _, _) => LikelyCheating // always has advantage, moderate blurs, highly consistent move times

case PlayerFlags(_, T, T, _, _, _, _) => LikelyCheating // always has advantage, high blurs

case PlayerFlags(_, T, _, _, T, T, _) => Unclear // always has advantage, consistent move times
Expand Down
5 changes: 4 additions & 1 deletion modules/explorer/src/main/ExplorerIndexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ private final class ExplorerIndexer(
Query.turnsMoreThan(8) ++
Query.noProvisional ++
Query.bothRatingsGreaterThan(1501)

import reactivemongo.api._
import reactivemongo.play.iteratees.cursorProducer

gameColl.find(query)
.sort(Query.sortChronological)
.cursor[Game](ReadPreference.secondary)
.enumerate(maxGames, stopOnError = true) &>
.enumerator(maxGames) &>
Enumeratee.mapM[Game].apply[Option[GamePGN]] { game =>
makeFastPgn(game) map {
_ map { game -> _ }
Expand Down
5 changes: 3 additions & 2 deletions modules/forum/src/main/PostRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lila.forum
import lila.db.dsl._
import lila.user.User.BSONFields
import org.joda.time.DateTime
import reactivemongo.api.ReadPreference
import reactivemongo.api.{ CursorProducer, ReadPreference }

object PostRepo extends PostRepo(false) {

Expand Down Expand Up @@ -77,6 +77,7 @@ sealed abstract class PostRepo(troll: Boolean) {

def cursor(
selector: Bdoc,
readPreference: ReadPreference = ReadPreference.secondaryPreferred) =
readPreference: ReadPreference = ReadPreference.secondaryPreferred)(
implicit cp: CursorProducer[Post]) =
coll.find(selector).cursor[Post](readPreference)
}
4 changes: 3 additions & 1 deletion modules/forumSearch/src/main/ForumSearchApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class ForumSearchApi(
Fields.troll -> view.post.troll,
Fields.date -> view.post.createdAt.getDate)

import reactivemongo.play.iteratees.cursorProducer

def reset = client match {
case c: ESClientHttp => c.putMapping >> {
import play.api.libs.iteratee._
Expand All @@ -44,7 +46,7 @@ final class ForumSearchApi(
PostRepo.cursor(
selector = $empty,
readPreference = ReadPreference.secondaryPreferred)
.enumerate(maxEntries, stopOnError = true) &>
.enumerator(maxEntries) &>
Enumeratee.grouped(Iteratee takeUpTo batchSize) |>>>
Iteratee.foldM[Seq[Post], Int](0) {
case (nb, posts) => for {
Expand Down
4 changes: 2 additions & 2 deletions modules/game/src/main/GameDiff.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import lila.db.ByteArray

private[game] object GameDiff {

type Set = (String, BSONValue)
type Unset = (String, BSONBoolean)
type Set = BSONElement // [String, BSONValue]
type Unset = BSONElement //[String, BSONBoolean]

def apply(a: Game, b: Game): (List[Set], List[Unset]) = {

Expand Down
16 changes: 9 additions & 7 deletions modules/game/src/main/GameRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import scala.util.Random
import chess.format.{ Forsyth, FEN }
import chess.{ Color, Status }
import org.joda.time.DateTime
import reactivemongo.api.ReadPreference
import reactivemongo.api.{ CursorProducer, ReadPreference }
import reactivemongo.bson.BSONBinary

import lila.db.BSON.BSONJodaDateTimeHandler
Expand Down Expand Up @@ -86,13 +86,15 @@ object GameRepo {

def cursor(
selector: Bdoc,
readPreference: ReadPreference = ReadPreference.secondaryPreferred) =
readPreference: ReadPreference = ReadPreference.secondaryPreferred)(
implicit cp: CursorProducer[Game]) =
coll.find(selector).cursor[Game](readPreference)

def sortedCursor(
selector: Bdoc,
sort: Bdoc,
readPreference: ReadPreference = ReadPreference.secondaryPreferred) =
readPreference: ReadPreference = ReadPreference.secondaryPreferred)(
implicit cp: CursorProducer[Game]) =
coll.find(selector).sort(sort).cursor[Game](readPreference)

def unrate(gameId: String) =
Expand All @@ -112,7 +114,7 @@ object GameRepo {
case (Nil, Nil) => funit
case (sets, unsets) => coll.update(
$id(progress.origin.id),
nonEmptyMod("$set", $doc(sets)) ++ nonEmptyMod("$unset", $doc(unsets))
nonEmptyMod("$set", $doc(sets)) ++ nonEmptyMod("$unset", $doc(unsets: _*))
).void
}

Expand Down Expand Up @@ -362,7 +364,7 @@ object GameRepo {
Project($doc(
F.playerUids -> true,
F.id -> false)),
Unwind(F.playerUids),
UnwindField(F.playerUids),
Match($doc(F.playerUids -> $doc("$ne" -> userId))),
GroupField(F.playerUids)("gs" -> SumValue(1)),
Sort(Descending("gs")),
Expand Down Expand Up @@ -431,15 +433,15 @@ object GameRepo {
Match,
Sort,
SumValue,
Unwind
UnwindField
}

coll.aggregate(Match($doc(
F.createdAt $gt since,
F.status $gte chess.Status.Mate.id,
s"${F.playerUids}.0" $exists true
)), List(
Unwind(F.playerUids),
UnwindField(F.playerUids),
Match($doc(
F.playerUids -> $doc("$ne" -> "")
)),
Expand Down
4 changes: 3 additions & 1 deletion modules/gameSearch/src/main/GameSearchApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ final class GameSearchApi(client: ESClient) extends SearchReadApi[Game, Query] {
import lila.db.BSON.BSONJodaDateTimeHandler
import reactivemongo.api._
import reactivemongo.bson._
import reactivemongo.play.iteratees.cursorProducer

var nbSkipped = 0
val batchSize = 1000
val maxGames = Int.MaxValue
Expand All @@ -109,7 +111,7 @@ final class GameSearchApi(client: ESClient) extends SearchReadApi[Game, Query] {
selector = $doc("ca" $gt since),
sort = $doc("ca" -> 1),
readPreference = ReadPreference.secondaryPreferred)
.enumerate(maxGames, stopOnError = true) &>
.enumerator(maxGames) &>
Enumeratee.grouped(Iteratee takeUpTo batchSize) |>>>
Enumeratee.mapM[Seq[Game]].apply[(Seq[Game], Set[String])] { games =>
GameRepo filterAnalysed games.map(_.id) map games.->
Expand Down
2 changes: 1 addition & 1 deletion modules/history/src/main/History.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object History {

private[history] implicit val RatingsMapReader = new BSONDocumentReader[RatingsMap] {
def read(doc: BSONDocument): RatingsMap = doc.stream.flatMap {
case scala.util.Success((k, BSONInteger(v))) => parseIntOption(k) map (_ -> v)
case scala.util.Success(BSONElement(k, BSONInteger(v))) => parseIntOption(k) map (_ -> v)
case _ => none[(Int, Int)]
}.toList sortBy (_._1)
}
Expand Down
2 changes: 1 addition & 1 deletion modules/history/src/main/HistoryApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class HistoryApi(coll: Coll) {
coll.update(
$id(user.id),
$doc("$set" -> $doc(changes.map {
case (perf, rating) => s"$perf.$days" -> $int(rating)
case (perf, rating) => BSONElement(s"$perf.$days", $int(rating))
})),
upsert = true
).void
Expand Down
2 changes: 1 addition & 1 deletion modules/hub/src/main/MultiThrottler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class MultiThrottler(
self ! MultiThrottler.Done(id)
}
}
executions = executions + (id -> fut)
executions = executions + (id -> {}/*fut*/)

case _: MultiThrottler.Work => // already executing similar work

Expand Down
Loading

0 comments on commit 4ad2c23

Please sign in to comment.