refactor build and db module
This commit is contained in:
parent
ef9975e445
commit
5418e90e13
|
@ -1,21 +1,21 @@
|
|||
package lila.db
|
||||
|
||||
import com.mongodb.casbah.MongoCollection
|
||||
import com.mongodb.casbah.Imports._
|
||||
import scalaz.effects._
|
||||
import reactivemongo.api._
|
||||
import reactivemongo.bson._
|
||||
|
||||
abstract class CappedRepo[A](collection: MongoCollection, max: Int) {
|
||||
import play.modules.reactivemongo.Implicits._
|
||||
|
||||
val naturalOrder = DBObject("$natural" -> -1)
|
||||
import play.api.libs.concurrent.Execution.Implicits._
|
||||
|
||||
val recent: IO[List[A]] = io {
|
||||
collection.find(DBObject())
|
||||
.sort(naturalOrder)
|
||||
.limit(max)
|
||||
.toList.map(decode).flatten
|
||||
abstract class CappedRepo[Doc](coll: ReactiveColl, json: JsonTube[Doc], max: Int) extends DbApi {
|
||||
|
||||
val naturalOrder = sort desc "$natural"
|
||||
|
||||
val recent: Fu[List[Doc]] = (
|
||||
richerQueryBuilder(coll.genericQueryBuilder query select.all).sort(naturalOrder) limit max
|
||||
).cursor[Option[Doc]].toList map (_.flatten)
|
||||
|
||||
private implicit val bsonDocumentReader = new BSONDocumentReader[Option[Doc]] {
|
||||
def read(bson: BSONDocument): Option[Doc] = json.fromMongo(JsObjectReader read bson).asOpt
|
||||
}
|
||||
|
||||
def decode(obj: DBObject): Option[A]
|
||||
|
||||
def encode(obj: A): DBObject
|
||||
}
|
||||
|
|
|
@ -55,4 +55,7 @@ trait sort {
|
|||
|
||||
def asc = SortOrder.Ascending
|
||||
def desc = SortOrder.Descending
|
||||
|
||||
def asc(field: String) = field -> SortOrder.Ascending
|
||||
def desc(field: String) = field -> SortOrder.Descending
|
||||
}
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
package lila.db
|
||||
|
||||
import com.mongodb.casbah.MongoConnection
|
||||
import com.mongodb.{ Mongo, MongoOptions, ServerAddress ⇒ MongoServer }
|
||||
|
||||
final class MongoDbEnv(settings: Settings) {
|
||||
|
||||
import settings._
|
||||
|
||||
def apply(coll: String) = connection(coll)
|
||||
|
||||
// lazy val cache = new Cache(connection(CoreCollectionCache))
|
||||
|
||||
lazy val connection = MongoConnection(server, options)(MongoDbName)
|
||||
|
||||
private lazy val server = new MongoServer(MongoHost, MongoPort)
|
||||
|
||||
// http://stackoverflow.com/questions/6520439/how-to-configure-mongodb-java-driver-mongooptions-for-production-use
|
||||
private val options = new MongoOptions() ~ { o ⇒
|
||||
o.connectionsPerHost = MongoConnectionsPerHost
|
||||
o.autoConnectRetry = MongoAutoConnectRetry
|
||||
o.connectTimeout = MongoConnectTimeout
|
||||
o.threadsAllowedToBlockForConnectionMultiplier = MongoBlockingThreads
|
||||
}
|
||||
}
|
|
@ -5,4 +5,6 @@ import reactivemongo.api._
|
|||
final class Mongo2Env(val db: DB, settings: Settings) {
|
||||
|
||||
import settings._
|
||||
|
||||
def apply(name: String): ReactiveColl = db(name)
|
||||
}
|
|
@ -22,8 +22,6 @@ abstract class Repo[Doc <: WithStringId](coll: ReactiveColl, json: JsonTube[Doc]
|
|||
def byIds(ids: Seq[ID]) = apply(select byIds ids)
|
||||
}
|
||||
|
||||
implicit def jsObjectToQueryBuilder(js: JsObject): QueryBuilder = query(js)
|
||||
|
||||
object count {
|
||||
|
||||
def apply(q: JsObject): Fu[Int] = db command Count(name, JsObjectWriter.write(q).some)
|
||||
|
|
|
@ -78,36 +78,42 @@ object ApplicationBuild extends Build with Resolvers with Dependencies {
|
|||
)) dependsOn (user, wiki) aggregate (scalachess, common, db, user, wiki)
|
||||
|
||||
lazy val common = project("common").settings(
|
||||
libraryDependencies := Seq(
|
||||
scalaz, scalalib, jodaTime, jodaConvert, playProvided, reactivemongo)
|
||||
libraryDependencies ++= Seq(playProvided, reactivemongo)
|
||||
)
|
||||
|
||||
lazy val memo = project("memo", Seq(common)).settings(
|
||||
libraryDependencies := Seq(scalaz, scalalib, guava, findbugs)
|
||||
libraryDependencies ++= Seq(guava, findbugs)
|
||||
)
|
||||
|
||||
lazy val db = project("db", Seq(common)).settings(
|
||||
libraryDependencies := Seq(
|
||||
scalaz, scalalib, playProvided, salat, reactivemongo, playReactivemongo
|
||||
libraryDependencies ++= Seq(
|
||||
playProvided, reactivemongo, playReactivemongo
|
||||
)
|
||||
).settings(srcMain: _*)
|
||||
|
||||
lazy val user = project("user", Seq(common, memo, db, scalachess)).settings(
|
||||
libraryDependencies := Seq(
|
||||
scalaz, scalalib, hasher, jodaTime, jodaConvert, playProvided,
|
||||
sprayCaching, playTestProvided, reactivemongo, playReactivemongo)
|
||||
libraryDependencies ++= Seq(
|
||||
hasher, playProvided, sprayCaching, playTestProvided, reactivemongo, playReactivemongo)
|
||||
).settings(srcMain: _*)
|
||||
|
||||
lazy val wiki = project("wiki", Seq(common, db)).settings(
|
||||
libraryDependencies := Seq(
|
||||
scalaz, scalalib, jodaTime, jodaConvert, playProvided,
|
||||
playTestProvided, reactivemongo, playReactivemongo)
|
||||
libraryDependencies ++= Seq(
|
||||
playProvided, reactivemongo, playReactivemongo)
|
||||
).settings(srcMain: _*)
|
||||
|
||||
lazy val scalachess = project("scalachess").settings(
|
||||
libraryDependencies := Seq(scalaz, scalalib, hasher, jodaTime, jodaConvert)
|
||||
libraryDependencies ++= Seq(hasher)
|
||||
)
|
||||
|
||||
private def defaultDeps = Seq(scalaz, scalalib, jodaTime, jodaConvert)
|
||||
|
||||
private def project(name: String, deps: Seq[sbt.ClasspathDep[sbt.ProjectReference]] = Seq.empty) =
|
||||
Project(name, file(name), dependencies = deps, settings = buildSettings)
|
||||
Project(
|
||||
name,
|
||||
file(name),
|
||||
dependencies = deps,
|
||||
settings = Seq(
|
||||
libraryDependencies := defaultDeps
|
||||
) ++ buildSettings
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue