PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/commons/src/main/scala/io/prediction/commons/settings/mongodb/MongoOfflineEvals.scala

https://github.com/eddieliu/PredictionIO
Scala | 107 lines | 79 code | 23 blank | 5 comment | 0 complexity | 517f915512371d9ce84e94f10222e526 MD5 | raw file
  1. package io.prediction.commons.settings.mongodb
  2. import io.prediction.commons.settings.{ OfflineEval, OfflineEvals }
  3. import com.mongodb.casbah.Imports._
  4. import com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers
  5. import com.github.nscala_time.time.Imports._
  6. class MongoOfflineEvals(db: MongoDB) extends OfflineEvals {
  7. private val emptyObj = MongoDBObject()
  8. private val offlineEvalColl = db("offlineEvals")
  9. private val seq = new MongoSequences(db)
  10. private def genNextId = seq.genNext("offlineEvalid")
  11. private val getFields = MongoDBObject( // fields to be read
  12. "engineid" -> 1,
  13. "name" -> 1,
  14. "iterations" -> 1,
  15. "tuneid" -> 1,
  16. "createtime" -> 1,
  17. "starttime" -> 1,
  18. "endtime" -> 1
  19. )
  20. RegisterJodaTimeConversionHelpers()
  21. private def dbObjToOfflineEval(dbObj: DBObject) = {
  22. OfflineEval(
  23. id = dbObj.as[Int]("_id"),
  24. engineid = dbObj.as[Int]("engineid"),
  25. name = dbObj.as[String]("name"),
  26. iterations = dbObj.as[Int]("iterations"),
  27. tuneid = dbObj.getAs[Int]("tuneid"),
  28. createtime = dbObj.getAs[DateTime]("createtime"),
  29. starttime = dbObj.getAs[DateTime]("starttime"),
  30. endtime = dbObj.getAs[DateTime]("endtime")
  31. )
  32. }
  33. class MongoOfflineEvalIterator(it: MongoCursor) extends Iterator[OfflineEval] {
  34. def next = dbObjToOfflineEval(it.next)
  35. def hasNext = it.hasNext
  36. }
  37. /** Insert an OfflineEval and return id (id of the offlineEval parameter is not used) */
  38. def insert(offlineEval: OfflineEval): Int = {
  39. val id = genNextId
  40. // required fields
  41. val obj = MongoDBObject(
  42. "_id" -> id,
  43. "engineid" -> offlineEval.engineid,
  44. "name" -> offlineEval.name,
  45. "iterations" -> offlineEval.iterations,
  46. "tuneid" -> offlineEval.tuneid)
  47. // option fields
  48. val createtimeObj = offlineEval.createtime.map(x => MongoDBObject("createtime" -> x)).getOrElse(emptyObj)
  49. val starttimeObj = offlineEval.starttime.map(x => MongoDBObject("starttime" -> x)).getOrElse(emptyObj)
  50. val endtimeObj = offlineEval.endtime.map(x => MongoDBObject("endtime" -> x)).getOrElse(emptyObj)
  51. val optObj = createtimeObj ++ starttimeObj ++ endtimeObj
  52. offlineEvalColl.insert(obj ++ optObj)
  53. id
  54. }
  55. def get(id: Int): Option[OfflineEval] = {
  56. // NOTE: _id field is always returned although it is not specified in getFields.
  57. offlineEvalColl.findOne(MongoDBObject("_id" -> id), getFields) map { dbObjToOfflineEval(_) }
  58. }
  59. def getAll() = new MongoOfflineEvalIterator(offlineEvalColl.find())
  60. def getByEngineid(engineid: Int): Iterator[OfflineEval] = new MongoOfflineEvalIterator(
  61. offlineEvalColl.find(MongoDBObject("engineid" -> engineid), getFields).sort(MongoDBObject("name" -> 1))
  62. )
  63. def getByTuneid(tuneid: Int): Iterator[OfflineEval] = new MongoOfflineEvalIterator(
  64. offlineEvalColl.find(MongoDBObject("tuneid" -> tuneid), getFields).sort(MongoDBObject("name" -> 1))
  65. )
  66. def getByIdAndEngineid(id: Int, engineid: Int): Option[OfflineEval] = offlineEvalColl.findOne(MongoDBObject("_id" -> id, "engineid" -> engineid)) map { dbObjToOfflineEval(_) }
  67. def update(offlineEval: OfflineEval, upsert: Boolean = false) = {
  68. val obj = MongoDBObject(
  69. "_id" -> offlineEval.id,
  70. "engineid" -> offlineEval.engineid,
  71. "name" -> offlineEval.name,
  72. "iterations" -> offlineEval.iterations,
  73. "tuneid" -> offlineEval.tuneid)
  74. // option fields
  75. val createtimeObj = offlineEval.createtime.map(x => MongoDBObject("createtime" -> x)).getOrElse(emptyObj)
  76. val starttimeObj = offlineEval.starttime.map(x => MongoDBObject("starttime" -> x)).getOrElse(emptyObj)
  77. val endtimeObj = offlineEval.endtime.map(x => MongoDBObject("endtime" -> x)).getOrElse(emptyObj)
  78. val optObj = createtimeObj ++ starttimeObj ++ endtimeObj
  79. offlineEvalColl.update(MongoDBObject("_id" -> offlineEval.id), obj ++ optObj, upsert)
  80. }
  81. def delete(id: Int) = {
  82. offlineEvalColl.remove(MongoDBObject("_id" -> id))
  83. }
  84. }