/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
- package io.prediction.commons.settings.mongodb
- import com.mongodb.casbah.Imports.MongoDB
- import com.mongodb.casbah.query.Imports._
- /** Provides incremental sequence number generation. */
- class MongoSequences(db: MongoDB) {
- private val seqColl = db("seq")
- /** Get the next sequence number from the given sequence name. */
- def genNext(name: String): Int = {
- val qFind = MongoDBObject("_id" -> name)
- val qField = MongoDBObject("next" -> 1)
- val qSort = MongoDBObject()
- val qRemove = false
- val qModify = $inc("next" -> 1)
- val qReturnNew = true
- val qUpsert = true
- seqColl.findAndModify(qFind, qField, qSort, qRemove, qModify, qReturnNew, qUpsert).get.getAsOrElse[Number]("next", 0).intValue
- }
- }