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

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

https://github.com/eddieliu/PredictionIO
Scala | 74 lines | 58 code | 13 blank | 3 comment | 0 complexity | 4b73b621a2660cea19ff2f9d02676070 MD5 | raw file
  1. package io.prediction.commons.settings.mongodb
  2. import io.prediction.commons.MongoUtils
  3. import io.prediction.commons.settings.{ AlgoInfo, AlgoInfos, Param }
  4. import com.mongodb.casbah.Imports._
  5. /** MongoDB implementation of AlgoInfos. */
  6. class MongoAlgoInfos(db: MongoDB) extends AlgoInfos {
  7. private val coll = db("algoInfos")
  8. private def dbObjToAlgoInfo(dbObj: DBObject) = {
  9. AlgoInfo(
  10. id = dbObj.as[String]("_id"),
  11. name = dbObj.as[String]("name"),
  12. description = dbObj.getAs[String]("description"),
  13. batchcommands = dbObj.getAs[MongoDBList]("batchcommands") map { MongoUtils.mongoDbListToListOfString(_) },
  14. offlineevalcommands = dbObj.getAs[MongoDBList]("offlineevalcommands") map { MongoUtils.mongoDbListToListOfString(_) },
  15. params = (dbObj.as[DBObject]("params") map { p => (p._1, MongoParam.dbObjToParam(p._1, p._2.asInstanceOf[DBObject])) }).toMap,
  16. paramsections = dbObj.as[Seq[DBObject]]("paramsections") map { MongoParam.dbObjToParamSection(_) },
  17. paramorder = MongoUtils.mongoDbListToListOfString(dbObj.as[MongoDBList]("paramorder")),
  18. engineinfoid = dbObj.as[String]("engineinfoid"),
  19. techreq = MongoUtils.mongoDbListToListOfString(dbObj.as[MongoDBList]("techreq")),
  20. datareq = MongoUtils.mongoDbListToListOfString(dbObj.as[MongoDBList]("datareq")),
  21. capabilities = dbObj.getAs[MongoDBList]("capabilities") map { MongoUtils.mongoDbListToListOfString(_) } getOrElse Seq[String]())
  22. }
  23. def insert(algoInfo: AlgoInfo) = {
  24. // required fields
  25. val obj = MongoDBObject(
  26. "_id" -> algoInfo.id,
  27. "name" -> algoInfo.name,
  28. "params" -> (algoInfo.params mapValues { MongoParam.paramToDBObj(_) }),
  29. "paramsections" -> (algoInfo.paramsections map { MongoParam.paramSectionToDBObj(_) }),
  30. "paramorder" -> algoInfo.paramorder,
  31. "engineinfoid" -> algoInfo.engineinfoid,
  32. "techreq" -> algoInfo.techreq,
  33. "datareq" -> algoInfo.datareq,
  34. "capabilities" -> algoInfo.capabilities)
  35. // optional fields
  36. val descriptionObj = algoInfo.description.map { d => MongoDBObject("description" -> d) } getOrElse MongoUtils.emptyObj
  37. val batchcommandsObj = algoInfo.batchcommands.map { c => MongoDBObject("batchcommands" -> c) } getOrElse MongoUtils.emptyObj
  38. val offlineevalcommandsObj = algoInfo.offlineevalcommands.map { c => MongoDBObject("offlineevalcommands" -> c) } getOrElse MongoUtils.emptyObj
  39. coll.insert(obj ++ descriptionObj ++ batchcommandsObj ++ offlineevalcommandsObj)
  40. }
  41. def get(id: String) = coll.findOne(MongoDBObject("_id" -> id)) map { dbObjToAlgoInfo(_) }
  42. def getAll() = coll.find().toSeq map { dbObjToAlgoInfo(_) }
  43. def getByEngineInfoId(engineinfoid: String) = coll.find(MongoDBObject("engineinfoid" -> engineinfoid)).sort(MongoDBObject("_id" -> 1)).toSeq map { dbObjToAlgoInfo(_) }
  44. def update(algoInfo: AlgoInfo, upsert: Boolean = false) = {
  45. val idObj = MongoDBObject("_id" -> algoInfo.id)
  46. val requiredObj = MongoDBObject(
  47. "name" -> algoInfo.name,
  48. "params" -> (algoInfo.params mapValues { MongoParam.paramToDBObj(_) }),
  49. "paramsections" -> (algoInfo.paramsections map { MongoParam.paramSectionToDBObj(_) }),
  50. "paramorder" -> algoInfo.paramorder,
  51. "engineinfoid" -> algoInfo.engineinfoid,
  52. "techreq" -> algoInfo.techreq,
  53. "datareq" -> algoInfo.datareq,
  54. "capabilities" -> algoInfo.capabilities)
  55. val descriptionObj = algoInfo.description.map { d => MongoDBObject("description" -> d) } getOrElse MongoUtils.emptyObj
  56. val batchcommandsObj = algoInfo.batchcommands.map { c => MongoDBObject("batchcommands" -> c) } getOrElse MongoUtils.emptyObj
  57. val offlineevalcommandsObj = algoInfo.offlineevalcommands.map { c => MongoDBObject("offlineevalcommands" -> c) } getOrElse MongoUtils.emptyObj
  58. coll.update(idObj, idObj ++ requiredObj ++ descriptionObj ++ batchcommandsObj ++ offlineevalcommandsObj, upsert)
  59. }
  60. def delete(id: String) = coll.remove(MongoDBObject("_id" -> id))
  61. }