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

https://github.com/activetrader/PredictionIO · Scala · 76 lines · 55 code · 18 blank · 3 comment · 0 complexity · 6d91c7fbb3b2a4d0aaafc81b675cd9fd MD5 · raw file

  1. package io.prediction.commons.settings.mongodb
  2. import io.prediction.commons.MongoUtils
  3. import io.prediction.commons.settings.{Engine, Engines}
  4. import com.mongodb.casbah.Imports._
  5. /** MongoDB implementation of Engines. */
  6. class MongoEngines(db: MongoDB) extends Engines {
  7. private val engineColl = db("engines")
  8. private val seq = new MongoSequences(db)
  9. private val getFields = MongoDBObject("appid" -> 1, "name" -> 1, "infoid" -> 1, "itypes" -> 1, "settings" -> 1)
  10. private def dbObjToEngine(dbObj: DBObject) = {
  11. Engine(
  12. id = dbObj.as[Int]("_id"),
  13. appid = dbObj.as[Int]("appid"),
  14. name = dbObj.as[String]("name"),
  15. infoid = dbObj.as[String]("infoid"),
  16. itypes = dbObj.getAs[MongoDBList]("itypes") map { MongoUtils.mongoDbListToListOfString(_) },
  17. settings = MongoUtils.dbObjToMap(dbObj.as[DBObject]("settings"))
  18. )
  19. }
  20. class MongoEngineIterator(it: MongoCursor) extends Iterator[Engine] {
  21. def next = dbObjToEngine(it.next)
  22. def hasNext = it.hasNext
  23. }
  24. def insert(engine: Engine) = {
  25. val id = seq.genNext("engineid")
  26. // required fields
  27. val obj = MongoDBObject(
  28. "_id" -> id,
  29. "appid" -> engine.appid,
  30. "name" -> engine.name,
  31. "infoid" -> engine.infoid,
  32. "settings" -> engine.settings
  33. )
  34. // optional fields
  35. val optObj = engine.itypes.map (x => MongoDBObject("itypes" -> x)).getOrElse(MongoUtils.emptyObj)
  36. engineColl.insert( obj ++ optObj )
  37. id
  38. }
  39. def get(id: Int) = engineColl.findOne(MongoDBObject("_id" -> id), getFields) map { dbObjToEngine(_) }
  40. def getAll() = new MongoEngineIterator(engineColl.find())
  41. def getByAppid(appid: Int) = new MongoEngineIterator(engineColl.find(MongoDBObject("appid" -> appid)).sort(MongoDBObject("name" -> 1)))
  42. def getByAppidAndName(appid: Int, name: String) = engineColl.findOne(MongoDBObject("appid" -> appid, "name" -> name)) map { dbObjToEngine(_) }
  43. def update(engine: Engine, upsert: Boolean = false) = {
  44. val idObj = MongoDBObject("_id" -> engine.id)
  45. val nameObj = MongoDBObject("name" -> engine.name)
  46. val appidObj = MongoDBObject("appid" -> engine.appid)
  47. val infoidObj = MongoDBObject("infoid" -> engine.infoid)
  48. val itypesObj = engine.itypes.map (x => MongoDBObject("itypes" -> x)).getOrElse(MongoUtils.emptyObj)
  49. val settingsObj = MongoDBObject("settings" -> engine.settings)
  50. engineColl.update(
  51. idObj,
  52. idObj ++ appidObj ++ nameObj ++ infoidObj ++ itypesObj ++ settingsObj,
  53. upsert
  54. )
  55. }
  56. def deleteByIdAndAppid(id: Int, appid: Int) = engineColl.remove(MongoDBObject("_id" -> id, "appid" -> appid))
  57. def existsByAppidAndName(appid: Int, name: String) = engineColl.findOne(MongoDBObject("name" -> name, "appid" -> appid)) map { _ => true } getOrElse false
  58. }