PageRenderTime 103ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/main/scala/helix/db/mongo.scala

http://github.com/timperrett/helix
Scala | 145 lines | 105 code | 24 blank | 16 comment | 4 complexity | d45168d52e22420c7b19251d9dbf1e66 MD5 | raw file
  1. package helix.db
  2. import helix.domain._
  3. import helix.lib.Repositories
  4. trait MongoRepositories extends Repositories {
  5. // import net.liftweb.common.Full
  6. // import net.liftweb.util.Props
  7. import helix.util.Config._
  8. import com.novus.salat._
  9. import com.novus.salat.global._
  10. import com.novus.salat.dao._
  11. import com.mongodb.casbah.Imports._
  12. import com.mongodb.casbah.MongoURI
  13. class MongoRepository extends HelixRepository {
  14. def listProjectsAlphabetically(limit: Int, offset: Int): List[Project] =
  15. ProjectDAO.find(MongoDBObject("setupComplete" -> true))
  16. .limit(limit)
  17. .skip(offset)
  18. .sort(orderBy = MongoDBObject("name" -> 1)).toList
  19. /** lists for projects **/
  20. def listFiveNewestProjects: List[Project] =
  21. ProjectDAO.find(MongoDBObject("setupComplete" -> true)
  22. ).limit(5).sort(orderBy = MongoDBObject("_id" -> -1)).toList
  23. def listFiveMostActiveProjects: List[Project] =
  24. ProjectDAO.find(MongoDBObject("setupComplete" -> true)
  25. ).limit(5).sort(orderBy = MongoDBObject("activityScore" -> -1)).toList
  26. /** global lists **/
  27. def listScalaVersions = ScalaVersionDAO.find(MongoDBObject()
  28. ).sort(orderBy = MongoDBObject(
  29. "major" -> -1, "minor" -> -1, "micro" -> -1, "mode" -> 1)).toList
  30. // def listAllTags: List[Tag]
  31. /** finders **/
  32. def findProjectByGroupAndArtifact(group: String, artifact: String): Option[Project] =
  33. ProjectDAO.findOne(MongoDBObject("groupId" -> group, "artifactId" -> artifact))
  34. def findAllProjectCount = ProjectDAO.count()
  35. def findAverageForkCount: Double =
  36. ProjectDAO.group(
  37. MongoDBObject(), // condition
  38. MongoDBObject(), // key
  39. MongoDBObject("count" -> 0, "forks" -> 0), //initial
  40. "function(doc, out){ out.count++; out.forks+=doc.forkCount;}",
  41. "function(out){ out.average = out.forks / out.count; }").lastOption.map(
  42. _.get("average").asInstanceOf[Double]).getOrElse(0D)
  43. def findAverageWatcherCount: Double =
  44. ProjectDAO.group(
  45. MongoDBObject(), // condition
  46. MongoDBObject(), // key
  47. MongoDBObject("count" -> 0, "watchers" -> 0), //initial
  48. "function(doc, out){ out.count++; out.watchers+=doc.watcherCount;}",
  49. "function(out){ out.average = out.watchers / out.count; }").lastOption.map(
  50. _.get("average").asInstanceOf[Double]).getOrElse(0D)
  51. def findAverageContributorCount: Double =
  52. ProjectDAO.group(
  53. MongoDBObject(), // condition
  54. MongoDBObject(), // key
  55. MongoDBObject("count" -> 0, "contributors" -> 0), //initial
  56. "function(doc, out){ out.count++; out.contributors+=doc.contributorCount;}",
  57. "function(out){ out.average = out.contributors / out.count; }").lastOption.map(
  58. _.get("average").asInstanceOf[Double]).getOrElse(0D)
  59. def findStaleProjects(boundry: Long): List[Project] =
  60. ProjectDAO.find("updatedAt" $lt boundry).toList
  61. def findContributorByLogin(login: String): Option[Contributor] =
  62. ContributorDAO.findOne(MongoDBObject("login" -> login))
  63. /** creators **/
  64. def createProject(project: Project): Option[Project] =
  65. for(r <- ProjectDAO.insert(project)) yield project
  66. def createScalaVersion(version: ScalaVersion) =
  67. !ScalaVersionDAO.insert(version).isEmpty
  68. def createContributor(contributor: Contributor): Option[Contributor] =
  69. for(c <- ContributorDAO.insert(contributor)) yield contributor
  70. /** updaters **/
  71. // this is less than ideal, but there appears to be some
  72. // nightmarish overloading issue and implicits within salat
  73. def updateProject[T](id: T, project: Project): Unit =
  74. ProjectDAO.update(
  75. MongoDBObject("_id" -> id), project, false, false,
  76. new WriteConcern)
  77. def updateContributor[T](id: T, contrib: Contributor): Unit =
  78. ContributorDAO.update(
  79. MongoDBObject("_id" -> id), contrib, false, false,
  80. new WriteConcern)
  81. /** internals **/
  82. private lazy val mongo: MongoDB = {
  83. val db = MongoConnection(
  84. Conf.get[String]("mongo.host").getOrElse("localhost"),
  85. Conf.get[Int]("mongo.port").getOrElse(27017))(
  86. Conf.get[String]("mongo.db").getOrElse("helix")
  87. )
  88. // if the env specifies username and password, try to use
  89. // them, otherwise, just try to connect without auth.
  90. (Conf.get[String]("mongo.username"), Conf.get[String]("mongo.password")) match {
  91. case (Some(username), Some(password)) =>
  92. if(db.authenticate(username, password)) db
  93. else throw new IllegalArgumentException("Inavlid username and/or password")
  94. case _ => db
  95. }
  96. }
  97. /** DAOs **/
  98. object ProjectDAO extends SalatDAO[Project, ObjectId](collection = mongo("projects")){
  99. import scala.collection.mutable.ArrayBuffer
  100. import scala.collection.JavaConverters._
  101. // this is a hack to work around a bug within Casbah:
  102. // https://twitter.com/#!/rit/status/116531065513967617
  103. def group[A <% DBObject, B <% DBObject, C <% DBObject](key: A, cond: B, initial: C, reduce: String, finalize: String): List[DBObject] = {
  104. val cmd = MongoDBObject(
  105. "ns" -> collection.getName,
  106. "key" -> key,
  107. "cond" -> cond,
  108. "$reduce" -> reduce,
  109. "initial" -> initial,
  110. "finalize" -> finalize)
  111. val result = collection.getDB.command(MongoDBObject("group" -> cmd))
  112. result.get("retval").asInstanceOf[DBObject].toMap.asScala
  113. .map(_._2.asInstanceOf[DBObject]).asInstanceOf[ArrayBuffer[DBObject]].toList
  114. }
  115. }
  116. object ScalaVersionDAO extends SalatDAO[ScalaVersion, ObjectId](
  117. collection = mongo("scala_versions"))
  118. object ContributorDAO extends SalatDAO[Contributor, ObjectId](
  119. collection = mongo("contributors"))
  120. }
  121. }