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

https://github.com/SNaaS/PredictionIO · Scala · 21 lines · 16 code · 3 blank · 2 comment · 0 complexity · 1a82b4445d3437be49d1b2c0cadc3619 MD5 · raw file

  1. package io.prediction.commons.settings.mongodb
  2. import com.mongodb.casbah.Imports.MongoDB
  3. import com.mongodb.casbah.query.Imports._
  4. /** Provides incremental sequence number generation. */
  5. class MongoSequences(db: MongoDB) {
  6. private val seqColl = db("seq")
  7. /** Get the next sequence number from the given sequence name. */
  8. def genNext(name: String): Int = {
  9. val qFind = MongoDBObject("_id" -> name)
  10. val qField = MongoDBObject("next" -> 1)
  11. val qSort = MongoDBObject()
  12. val qRemove = false
  13. val qModify = $inc("next" -> 1)
  14. val qReturnNew = true
  15. val qUpsert = true
  16. seqColl.findAndModify(qFind, qField, qSort, qRemove, qModify, qReturnNew, qUpsert).get.getAsOrElse[Number]("next", 0).intValue
  17. }
  18. }