Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #37 from hmrc/upgrade_reactivemongo
Browse files Browse the repository at this point in the history
Upgrade reactivemongo to 0.11.3
  • Loading branch information
Claudio Scandura committed Jul 21, 2015
2 parents d162886 + 67bcd52 commit 5b6c92d
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: scala
scala:
- 2.11.6
- 2.11.7
jdk:
- oraclejdk7
- oraclejdk8
Expand Down
8 changes: 4 additions & 4 deletions project/HmrcBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ object HmrcBuild extends Build {
.settings(
autoSourceHeader := false,
targetJvm := "jvm-1.7",
scalaVersion := "2.11.6",
scalaVersion := "2.11.7",
libraryDependencies ++= appDependencies,
resolvers += Resolver.typesafeRepo("releases"),
crossScalaVersions := Seq("2.11.6")
crossScalaVersions := Seq("2.11.7")
)
}

object Dependencies {

object Compile {
val reactiveMongoJson = "uk.gov.hmrc" %% "reactivemongo-json" % "1.5.0"
val reactiveMongo = "org.reactivemongo" %% "reactivemongo" % "0.10.5.0.akka23"
val reactiveMongoJson = "uk.gov.hmrc" %% "reactivemongo-json" % "2.1.1"
val reactiveMongo = "org.reactivemongo" %% "reactivemongo" % "0.11.3"
val playJson = "com.typesafe.play" %% "play-json" % "2.3.9" % "provided"
val nscalaTime = "com.github.nscala-time" %% "nscala-time" % "1.8.0"
val logback = "ch.qos.logback" % "logback-classic" % "1.1.2"
Expand Down
53 changes: 34 additions & 19 deletions src/main/scala/uk/gov/hmrc/mongo/ReactiveRepository.scala
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package uk.gov.hmrc.mongo

import org.slf4j.LoggerFactory
import ch.qos.logback.classic.Logger
import org.slf4j.LoggerFactory
import play.api.libs.json.{Format, JsObject, Json}
import reactivemongo.api.commands._
import reactivemongo.api.indexes.Index
import play.api.libs.json.{Format, Json}
import reactivemongo.api.DB
import reactivemongo.core.commands.{Count, LastError}
import reactivemongo.api.{DB, ReadPreference}
import reactivemongo.core.commands.Count
import reactivemongo.json.ImplicitBSONHandlers
import reactivemongo.json.collection.JSONCollection
import uk.gov.hmrc.mongo.json.ReactiveMongoFormats

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}


abstract class ReactiveRepository[A <: Any, ID <: Any](collectionName: String,
mongo: () => DB,
domainFormat: Format[A],
idFormat: Format[ID] = ReactiveMongoFormats.objectIdFormats,
mc: Option[JSONCollection] = None)
(implicit manifest: Manifest[A], mid: Manifest[ID])
extends Repository[A, ID] with Indexes {
(implicit manifest: Manifest[A], mid: Manifest[ID], ec : ExecutionContext)
extends Repository[A, ID] with Indexes with ImplicitBSONHandlers {

import play.api.libs.json.Json.JsValueWrapper
import scala.concurrent.ExecutionContext.Implicits.global
import reactivemongo.core.commands.GetLastError

implicit val domainFormatImplicit = domainFormat
implicit val idFormatImplicit = idFormat
Expand All @@ -35,33 +34,49 @@ abstract class ReactiveRepository[A <: Any, ID <: Any](collectionName: String,

ensureIndexes

protected val _Id = "_id"
protected def _id(id : ID) = Json.obj(_Id -> id)

override def find(query: (String, JsValueWrapper)*)(implicit ec: ExecutionContext): Future[List[A]] = {
collection.find(Json.obj(query: _*)).cursor[A].collect[List]()
collection.find(Json.obj(query: _*)).cursor[A](ReadPreference.secondaryPreferred).collect[List]() //TODO: pass in ReadPreference
}

override def findAll(implicit ec: ExecutionContext): Future[List[A]] = collection.find(Json.obj()).cursor[A].collect[List]()
override def findAll(readPreference: ReadPreference = ReadPreference.secondaryPreferred)(implicit ec: ExecutionContext): Future[List[A]] = {
collection.find(Json.obj()).cursor[A](readPreference).collect[List]()
}

override def findById(id: ID)(implicit ec: ExecutionContext): Future[Option[A]] = {
collection.find(Json.obj("_id" -> id)).one[A]
override def findById(id: ID, readPreference: ReadPreference = ReadPreference.secondaryPreferred)(implicit ec: ExecutionContext): Future[Option[A]] = {
collection.find(_id(id)).one[A](readPreference)
}

override def count(implicit ec: ExecutionContext): Future[Int] = mongo().command(Count(collection.name))

override def removeAll(implicit ec: ExecutionContext): Future[LastError] = collection.remove(Json.obj(), GetLastError(), false)
override def removeAll(writeConcern: WriteConcern = WriteConcern.Default)(implicit ec: ExecutionContext) = {
collection.remove(Json.obj(), writeConcern)
}

override def removeById(id: ID)(implicit ec: ExecutionContext): Future[LastError] = collection.remove(Json.obj("_id" -> id), GetLastError(), false)
override def removeById(id: ID, writeConcern: WriteConcern = WriteConcern.Default)(implicit ec: ExecutionContext) = {
collection.remove(_id(id), writeConcern)
}

override def remove(query: (String, JsValueWrapper)*)(implicit ec: ExecutionContext): Future[LastError] = {
collection.remove(Json.obj(query: _*), GetLastError(), false)
override def remove(query: (String, JsValueWrapper)*)(implicit ec: ExecutionContext) = {
collection.remove(Json.obj(query: _*), WriteConcern.Default) //TODO: pass in the WriteConcern
}

override def drop(implicit ec: ExecutionContext): Future[Boolean] = collection.drop.recover {
override def drop(implicit ec: ExecutionContext) = collection.drop.map(_ => true).recover[Boolean] {
case _ => false
}

override def save(entity: A)(implicit ec: ExecutionContext) = collection.save(entity)

override def insert(entity: A)(implicit ec: ExecutionContext) = collection.insert(entity)
override def insert(entity: A)(implicit ec: ExecutionContext) = {
domainFormat.writes(entity) match {
case d @ JsObject(_) => collection.insert(d)
case _ =>
Future.failed[WriteResult](new Exception("cannot write object"))
}
}


private def ensureIndex(index: Index)(implicit ec: ExecutionContext): Future[Boolean] = {
collection.indexesManager.ensure(index).recover {
Expand Down
17 changes: 10 additions & 7 deletions src/main/scala/uk/gov/hmrc/mongo/Repository.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmrc.mongo

import org.joda.time.{DateTime, DateTimeZone}
import reactivemongo.api.commands._
import reactivemongo.api.indexes.Index
import reactivemongo.core.commands.LastError

Expand Down Expand Up @@ -30,24 +31,26 @@ trait CurrentTime {

trait Repository[A <: Any, ID <: Any] extends CurrentTime {

def findAll(implicit ec: ExecutionContext): Future[List[A]] = ???
import reactivemongo.api.ReadPreference

def findById(id: ID)(implicit ec: ExecutionContext): Future[Option[A]] = ???
def findAll(readPreference: ReadPreference)(implicit ec: ExecutionContext): Future[List[A]] = ???

def findById(id: ID, readPreference: ReadPreference)(implicit ec: ExecutionContext): Future[Option[A]] = ???

def find(query: (scala.Predef.String, play.api.libs.json.Json.JsValueWrapper)*)(implicit ec: ExecutionContext): Future[List[A]] = ???

def count(implicit ec: ExecutionContext): Future[Int] = ???

def removeAll(implicit ec: ExecutionContext): Future[LastError] = ???
def removeAll(writeConcern: WriteConcern)(implicit ec: ExecutionContext): Future[WriteResult] = ???

def removeById(id: ID)(implicit ec: ExecutionContext): Future[LastError] = ???
def removeById(id: ID, writeConcern: WriteConcern)(implicit ec: ExecutionContext): Future[WriteResult] = ???

def remove(query: (scala.Predef.String, play.api.libs.json.Json.JsValueWrapper)*)(implicit ec: ExecutionContext): Future[LastError] = ???
def remove(query: (scala.Predef.String, play.api.libs.json.Json.JsValueWrapper)*)(implicit ec: ExecutionContext): Future[WriteResult] = ???

def drop(implicit ec: ExecutionContext): Future[Boolean] = ???

def save(entity: A)(implicit ec: ExecutionContext): Future[LastError] = ???
def save(entity: A)(implicit ec: ExecutionContext): Future[WriteResult] = ???

def insert(entity: A)(implicit ec: ExecutionContext): Future[LastError] = ???
def insert(entity: A)(implicit ec: ExecutionContext): Future[WriteResult] = ???

}
5 changes: 3 additions & 2 deletions src/main/scala/uk/gov/hmrc/mongo/geospatial/Geospatial.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.hmrc.mongo.geospatial


import reactivemongo.api.ReadPreference
import uk.gov.hmrc.mongo.ReactiveRepository

trait Geospatial[A, ID] {
Expand All @@ -15,7 +16,7 @@ trait Geospatial[A, ID] {

lazy val geo2DSphericalIndex = Index(Seq((LocationField, Geo2DSpherical)), Some("geo2DSphericalIdx"))

def nearPoint(lon: Double, lat: Double, limit: Int = 100)(implicit ec: ExecutionContext) = collection.find(
def nearPoint(lon: Double, lat: Double, limit: Int = 100, readPreference: ReadPreference = ReadPreference.secondaryPreferred)(implicit ec: ExecutionContext) = collection.find(
Json.obj(
LocationField -> Json.obj(
"$near" -> Json.obj(
Expand All @@ -26,5 +27,5 @@ trait Geospatial[A, ID] {
)
)
)
).cursor[A].collect[List](limit)
).cursor[A](readPreference).collect[List](limit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ trait ReactiveMongoFormats {


implicit val localDateWrite: Writes[LocalDate] = new Writes[LocalDate] {
def writes(date: LocalDate): JsValue = Json.obj(
"$date" -> date.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis
def writes(localDate: LocalDate): JsValue = Json.obj(
"$date" -> localDate.toDateTimeAtStartOfDay(DateTimeZone.UTC).getMillis
)
}

Expand Down Expand Up @@ -47,8 +47,8 @@ trait ReactiveMongoFormats {


implicit val objectIdWrite: Writes[BSONObjectID] = new Writes[BSONObjectID] {
def writes(oid: BSONObjectID): JsValue = Json.obj(
"$oid" -> oid.stringify
def writes(objectId: BSONObjectID): JsValue = Json.obj(
"$oid" -> objectId.stringify
)
}

Expand Down
14 changes: 7 additions & 7 deletions src/test/scala/uk/gov/hmrc/mongo/AtomicUpdateSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class AtomicUpdateSpec extends WordSpec with Matchers with MongoSpecSupport with
val repositoryWithIdOverride = new AtomicTestRepositoryWithIdOverride

override def beforeEach() {
await(repository.removeAll)
await(repositoryWithIdOverride.removeAll)
await(repository.removeAll())
await(repositoryWithIdOverride.removeAll())
}

"Atomic operations" should {
Expand All @@ -55,7 +55,7 @@ class AtomicUpdateSpec extends WordSpec with Matchers with MongoSpecSupport with
val result2=await(insertRecord(a2))
result2.updateType shouldBe a [Saved[_]]

val result: List[AtomicTestObject] = await(repository.findAll)
val result: List[AtomicTestObject] = await(repository.findAll())

result.size shouldBe 2
result.head.name shouldBe(a1.name)
Expand All @@ -75,7 +75,7 @@ class AtomicUpdateSpec extends WordSpec with Matchers with MongoSpecSupport with
val result2=await(insertRecordWithIdOverride(a2, "idAtomic"))
result2.updateType shouldBe a [Saved[_]]

val result: List[AtomicTestObjectWithIdOverride] = await(repositoryWithIdOverride.findAll)
val result: List[AtomicTestObjectWithIdOverride] = await(repositoryWithIdOverride.findAll())

result.size shouldBe 2
result.head.name shouldBe(a1.name)
Expand All @@ -99,7 +99,7 @@ class AtomicUpdateSpec extends WordSpec with Matchers with MongoSpecSupport with
)
atomicResult.updateType shouldBe a [Updated[_]]

val result: List[AtomicTestObject] = await(repository.findAll)
val result: List[AtomicTestObject] = await(repository.findAll())
result.size shouldBe 1
result.head shouldBe updateRecord
}
Expand All @@ -119,7 +119,7 @@ class AtomicUpdateSpec extends WordSpec with Matchers with MongoSpecSupport with
)
atomicResult.updateType shouldBe a [Updated[_]]

val result: List[AtomicTestObject] = await(repository.findAll)
val result: List[AtomicTestObject] = await(repository.findAll())
result.size shouldBe 1
result.head shouldBe updateRecord
}
Expand All @@ -138,7 +138,7 @@ class AtomicUpdateSpec extends WordSpec with Matchers with MongoSpecSupport with
)
atomicResult.updateType shouldBe a [Updated[_]]

val result: List[AtomicTestObject] = await(repository.findAll)
val result: List[AtomicTestObject] = await(repository.findAll())
result.size shouldBe 1
result.head shouldBe updateRecord
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/uk/gov/hmrc/mongo/MongoSpecSupport.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package uk.gov.hmrc.mongo

import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.BSONDocument
import reactivemongo.core.commands.LastError

import scala.concurrent.duration._
import reactivemongo.api.FailoverStrategy
import reactivemongo.api.collections.default.BSONCollection

trait MongoSpecSupport {

Expand Down
10 changes: 6 additions & 4 deletions src/test/scala/uk/gov/hmrc/mongo/ReactiveRepositorySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import reactivemongo.bson.BSONObjectID
import reactivemongo.core.errors.DatabaseException
import uk.gov.hmrc.mongo.json.{ReactiveMongoFormats, TupleFormats}

import scala.concurrent.ExecutionContext

case class NestedModel(a: String, b: String)

case class TestObject(aField: String,
Expand Down Expand Up @@ -46,7 +48,7 @@ object TestObject {
}
}

class SimpleTestRepository(implicit mc: MongoConnector)
class SimpleTestRepository(implicit mc: MongoConnector, ec: ExecutionContext)
extends ReactiveRepository[TestObject, BSONObjectID]("simpleTestRepository", mc.db, TestObject.formats, ReactiveMongoFormats.objectIdFormats) {

override def indexes = Seq(
Expand All @@ -55,7 +57,7 @@ class SimpleTestRepository(implicit mc: MongoConnector)
)
}

class FailingIndexesTestRepository(implicit mc: MongoConnector)
class FailingIndexesTestRepository(implicit mc: MongoConnector, ec: ExecutionContext)
extends ReactiveRepository[TestObject, BSONObjectID]("failingIndexesTestRepository", mc.db, TestObject.formats, ReactiveMongoFormats.objectIdFormats) {

override def indexes = Seq(
Expand All @@ -69,7 +71,7 @@ class ReactiveRepositorySpec extends WordSpec with Matchers with MongoSpecSuppor
val uniqueIndexRepository = new FailingIndexesTestRepository

override def beforeEach() {
await(repository.removeAll)
await(repository.removeAll())
}

"findAll" should {
Expand All @@ -96,7 +98,7 @@ class ReactiveRepositorySpec extends WordSpec with Matchers with MongoSpecSuppor

await(created) shouldBe 3

val result: List[TestObject] = await(repository.findAll)
val result: List[TestObject] = await(repository.findAll())
result.size shouldBe 3
result should contain(e1)
result should contain(e2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ case class Place(loc : Coordinates, id: BSONObjectID = BSONObjectID.generate)
object Place{

val formats = ReactiveMongoFormats.mongoEntity({
import Coordinates.formats
import BSONObjectIdFormats._
import ReactiveMongoFormats.objectIdFormats

Json.format[Place]
})
}


class GeospatialTestRepository(implicit mc: MongoConnector)
class GeospatialTestRepository(implicit mc: MongoConnector, ec: ExecutionContext)
extends ReactiveRepository[Place, BSONObjectID]("geospatialTestRepository", mc.db, Place.formats, ReactiveMongoFormats.objectIdFormats)
with Geospatial[Place, BSONObjectID]{

Expand Down

0 comments on commit 5b6c92d

Please sign in to comment.