PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/questions/src/main/scala/com/cyrusinnovation/inquisition/tags/mongodb/MongoTagRepository.scala

https://github.com/cyrusinnovation/inquisition
Scala | 93 lines | 75 code | 18 blank | 0 comment | 0 complexity | 68981dbd4af3aca93440842499156364 MD5 | raw file
  1. package com.cyrusinnovation.inquisition.tags.mongodb
  2. import com.cyrusinnovation.inquisition.tags.TagRepository
  3. import org.springframework.beans.factory.annotation.Autowired
  4. import com.novus.salat._
  5. import com.novus.salat.global._
  6. import org.springframework.stereotype.Repository
  7. import com.mongodb.casbah.Imports._
  8. import com.cyrusinnovation.inquisition.questions.Question
  9. import collection.immutable.{ListMap, TreeMap}
  10. @Repository
  11. class MongoTagRepository @Autowired()(db: MongoDB) extends TagRepository {
  12. val questions = db("questions")
  13. val tags = db("tags")
  14. tags.ensureIndex(MongoDBObject("_id" -> 1))
  15. questions.ensureIndex(MongoDBObject("tags" -> 1))
  16. def db2question(dbObj: DBObject): Question = {
  17. val question = grater[Question].asObject(dbObj)
  18. question.copy(id = Some(dbObj("_id").toString))
  19. }
  20. def findUniqueTagNamesOrderedByTagName(): List[String] = {
  21. questions.distinct("tags").map(x => x.toString).sortBy(x => x).toList
  22. }
  23. def findQuestionsByTag(tag: String): List[Question] = {
  24. val results = questions.find(MongoDBObject("tags" -> tag))
  25. results.map(db2question).toList
  26. }
  27. def findQuestionsByTags(tags: List[String]): List[Question] = {
  28. questions.find("tags" $in tags).map(db2question).toList
  29. }
  30. def findMostPopularTags(numberToRetreive: Int): Map[String, Double] = {
  31. val mapFunction = """function() {
  32. if (!this.tags) {
  33. return;
  34. }
  35. for (index in this.tags) {
  36. emit(this.tags[index], 1);
  37. }
  38. }"""
  39. val reduceFunction = """function(previous, current) {
  40. var count = 0;
  41. for (index in current) {
  42. count += current[index];
  43. }
  44. return count;
  45. }"""
  46. val commandBuilder = MongoDBObject.newBuilder
  47. commandBuilder += "mapreduce" -> "questions"
  48. commandBuilder += "map" -> mapFunction
  49. commandBuilder += "reduce" -> reduceFunction
  50. commandBuilder += "out" -> "tags"
  51. db.command(commandBuilder.result())
  52. val tagList = tags.find().sort(MongoDBObject("value" -> -1)).limit(numberToRetreive)
  53. val map = tagList map { t => t.getAs[String]("_id").get -> t.getAs[Double]("value").get }
  54. ListMap(map.toList:_*)
  55. }
  56. def findTagsByPrefix(tagPrefix: String, limit: Int = 10): List[String] = {
  57. val mongoBlowsBuilder = MongoDBObject.newBuilder
  58. val regexString = "^" + tagPrefix + ".*"
  59. mongoBlowsBuilder += "_id" -> regexString.r
  60. tags.find(mongoBlowsBuilder.result()).limit(limit).map(x => x.getAs[String]("_id").get).toList
  61. }
  62. def deleteTagFromQuestion(questionId: String, tagText: String) {
  63. val updateQuery = $pull(MongoDBObject("tags" -> tagText))
  64. val query = MongoDBObject("_id" -> new ObjectId(questionId))
  65. questions.update(query, updateQuery)
  66. }
  67. def addTagToQuestion(questionId: String, tagText: String) {
  68. val updateQuery = $addToSet("tags" -> tagText)
  69. val query = MongoDBObject("_id" -> new ObjectId(questionId))
  70. questions.update(query, updateQuery)
  71. }
  72. }