/commons/src/main/scala/io/prediction/commons/appdata/mongodb/MongoUsers.scala

https://github.com/activetrader/PredictionIO · Scala · 71 lines · 53 code · 13 blank · 5 comment · 0 complexity · f66fd4af663706ac22fb1dc3e56c8799 MD5 · raw file

  1. package io.prediction.commons.appdata.mongodb
  2. import io.prediction.commons.MongoUtils.{emptyObj, mongoDbListToListOfString, idWithAppid}
  3. import io.prediction.commons.MongoUtils.{attributesToMongoDBObject, getAttributesFromDBObject}
  4. import io.prediction.commons.appdata.{User, Users}
  5. import com.mongodb.casbah.Imports._
  6. import com.mongodb.casbah.commons.conversions.scala._
  7. import com.github.nscala_time.time.Imports._
  8. /** MongoDB implementation of Users. */
  9. class MongoUsers(db: MongoDB) extends Users {
  10. private val emptyObj = MongoDBObject()
  11. private val userColl = db("users")
  12. RegisterJodaTimeConversionHelpers()
  13. def insert(user: User) = {
  14. val id = MongoDBObject("_id" -> idWithAppid(user.appid, user.id))
  15. val appid = MongoDBObject("appid" -> user.appid)
  16. val ct = MongoDBObject("ct" -> user.ct)
  17. val lnglat = user.latlng map { l => MongoDBObject("lnglat" -> MongoDBList(l._2, l._1)) } getOrElse emptyObj
  18. val inactive = user.inactive map { i => MongoDBObject("inactive" -> i) } getOrElse emptyObj
  19. //val attributes = user.attributes map { a => MongoDBObject("attributes" -> a) } getOrElse emptyObj
  20. // add "ca_" prefix for custom attributes
  21. val attributes = user.attributes map { a => attributesToMongoDBObject(a) } getOrElse emptyObj
  22. userColl.save(id ++ appid ++ ct ++ lnglat ++ inactive ++ attributes)
  23. }
  24. def get(appid: Int, id: String) = userColl.findOne(MongoDBObject("_id" -> idWithAppid(appid, id))) map { dbObjToUser(_) }
  25. def getByAppid(appid: Int) = new MongoUsersIterator(userColl.find(MongoDBObject("appid" -> appid)))
  26. def update(user: User) = {
  27. val id = MongoDBObject("_id" -> idWithAppid(user.appid, user.id))
  28. val appid = MongoDBObject("appid" -> user.appid)
  29. val ct = MongoDBObject("ct" -> user.ct)
  30. val lnglat = user.latlng map { l => MongoDBObject("lnglat" -> MongoDBList(l._2, l._1)) } getOrElse emptyObj
  31. val inactive = user.inactive map { i => MongoDBObject("inactive" -> i) } getOrElse emptyObj
  32. //val attributes = user.attributes map { a => MongoDBObject("attributes" -> a) } getOrElse emptyObj
  33. val attributes = user.attributes map { a => attributesToMongoDBObject(a) } getOrElse emptyObj
  34. userColl.update(id, id ++ appid ++ ct ++ lnglat ++ inactive ++ attributes)
  35. }
  36. def delete(appid: Int, id: String) = userColl.remove(MongoDBObject("_id" -> idWithAppid(appid, id)))
  37. def delete(user: User) = delete(user.appid, user.id)
  38. def deleteByAppid(appid: Int): Unit = {
  39. userColl.remove(MongoDBObject("appid" -> appid))
  40. }
  41. def countByAppid(appid: Int): Long = userColl.count(MongoDBObject("appid" -> appid))
  42. private def dbObjToUser(dbObj: DBObject) = {
  43. val appid = dbObj.as[Int]("appid")
  44. User(
  45. id = dbObj.as[String]("_id").drop(appid.toString.length + 1),
  46. appid = appid,
  47. ct = dbObj.as[DateTime]("ct"),
  48. latlng = dbObj.getAs[MongoDBList]("lnglat") map { lnglat => (lnglat(1).asInstanceOf[Double], lnglat(0).asInstanceOf[Double]) },
  49. inactive = dbObj.getAs[Boolean]("inactive"),
  50. //attributes = dbObj.getAs[DBObject]("attributes") map { dbObjToMap(_) }
  51. attributes = Option(getAttributesFromDBObject(dbObj)).filter(!_.isEmpty)
  52. )
  53. }
  54. class MongoUsersIterator(it: MongoCursor) extends Iterator[User] {
  55. def next = dbObjToUser(it.next)
  56. def hasNext = it.hasNext
  57. }
  58. }