Skip to content

Commit

Permalink
Fixes bwmcadams#28 - FindAndModify tests out, as does findAndRemove. …
Browse files Browse the repository at this point in the history
…Also added

Db/Coll level interfaces
  • Loading branch information
Brendan W. McAdams committed Jun 2, 2011
1 parent e468daa commit c51b67f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
29 changes: 29 additions & 0 deletions mongo-driver/src/main/scala/Collection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ class Collection(val name: String)(implicit val db: DB) extends Logging {

// TODO - getIndexInfo

/**
* Calls findAndModify in remove only mode with
* fields={}, sort={}, remove=true, getNew=false, upsert=false
* @param query
* @return the removed document
*/
def findAndRemove(query: BSONDocument = Document.empty) = db.findAndRemove(name)(query)

/**
* Finds the first document in the query and updates it.
* @param query query to match
* @param fields fields to be returned
* @param sort sort to apply before picking first document
* @param remove if true, document found will be removed
* @param update update to apply
* @param getNew if true, the updated document is returned, otherwise the old document is returned (or it would be lost forever) [ignored in remove]
* @param upsert do upsert (insert if document not present)
* @return the document
*/
def findAndModify(query: BSONDocument = Document.empty,
sort: BSONDocument = Document.empty,
remove: Boolean = false,
update: Option[Document] = None,
getNew: Boolean = false,
fields: BSONDocument = Document.empty,
upsert: Boolean = false)(callback: SingleDocQueryRequestFuture) =
db.findAndModify(name)(query, sort, remove, update, getNew, fields, upsert)(callback)


/**
*
*/
Expand Down
29 changes: 29 additions & 0 deletions mongo-driver/src/main/scala/DB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,35 @@ class DB(val name: String)(implicit val connection: MongoConnection) extends Log
// TODO - We can't allow free form getLastError due to the async nature.. it must be locked to the call


/**
* Calls findAndModify in remove only mode with
* fields={}, sort={}, remove=true, getNew=false, upsert=false
* @param query
* @return the removed document
*/
def findAndRemove(collection: String)(query: BSONDocument = Document.empty) = connection.findAndRemove(name)(collection)(query)

/**
* Finds the first document in the query and updates it.
* @param query query to match
* @param fields fields to be returned
* @param sort sort to apply before picking first document
* @param remove if true, document found will be removed
* @param update update to apply
* @param getNew if true, the updated document is returned, otherwise the old document is returned (or it would be lost forever) [ignored in remove]
* @param upsert do upsert (insert if document not present)
* @return the document
*/
def findAndModify(collection: String)(query: BSONDocument = Document.empty,
sort: BSONDocument = Document.empty,
remove: Boolean = false,
update: Option[Document] = None,
getNew: Boolean = false,
fields: BSONDocument = Document.empty,
upsert: Boolean = false)(callback: SingleDocQueryRequestFuture) =
connection.findAndModify(name)(collection)(query, sort, remove, update, getNew, fields, upsert)(callback)


/**
* Gets another database on the same server (without having to go up to connection)
* @param name Name of the database
Expand Down
26 changes: 23 additions & 3 deletions mongo-driver/src/test/scala/DirectConnectionSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DirectConnectionSpec extends Specification with Logging { def is =
endp^
"More detailed special commands" ^
"Support findAndModify" ! mongo(simpleFindAndModify)^
"Support findAndRemove" ! mongo(findAndRemoveTest)^
end

trait mongoConn extends AroundOutside[MongoConnection] {
Expand Down Expand Up @@ -243,20 +244,39 @@ class DirectConnectionSpec extends Specification with Logging { def is =
def simpleFindAndModify(conn: MongoConnection) = {
val mongo = conn("testHammersmith")("findModify")
mongo.dropCollection(){ success => }
mongo.insert(Document("name" -> "Next promo", "inprogress" -> false, "priority" -> 0, "tasks" -> Seq("select product", "add inventory", "do placement"))){}
mongo.insert(Document("name" -> "Biz report", "inprogress" -> false, "priority" -> 1, "tasks" -> Seq("run sales report", "email report"))){}
mongo.insert(Document("name" -> "Biz report", "inprogress" -> false, "priority" -> 2, "tasks" -> Seq("run marketing report", "email report"))){}

var found: BSONDocument = Document.empty
val startDate = new java.util.Date
mongo.findAndModify(query=Document("inprogress" -> false, "name" -> "Biz report"),
sort=Document("priority" -> -1),
update=Some(Document("$set" -> Document("inprogress" -> true, "started" -> startDate))),
getNew=true){ doc: BSONDocument =>
log.info("FAM Doc: %s", doc)
found = doc
}
found must eventually(havePairs("inprogress" -> true, "name" -> "Biz report", "started" -> startDate))
}

def findAndRemoveTest(conn: MongoConnection) = {
val mongo = conn("testHammersmith")("findRemove")
mongo.dropCollection(){ success => }
mongo.batchInsert((0 until 100).map(x => Document("x" -> x)): _*){}
var n: Int = -10
mongo.count((_n: Int) => n = _n)
n must eventually(beEqualTo(100))
var x: Int = -1
conn.findAndRemove("testHammersmith")("findModify")(){ doc: BSONDocument =>
mongo.findAndRemove(){ doc: BSONDocument =>
x = doc.as[Int]("x")
}
x must eventually(beEqualTo(0))
conn.findAndRemove("testHammersmith")("findModify")(){ doc: BSONDocument =>
mongo.findAndRemove(){ doc: BSONDocument =>
x = doc.as[Int]("x")
}
x must eventually(beEqualTo(1))
conn.findAndRemove("testHammersmith")("findModify")(){ doc: BSONDocument =>
mongo.findAndRemove(){ doc: BSONDocument =>
x = doc.as[Int]("x")
}
x must eventually(beEqualTo(2))
Expand Down
2 changes: 1 addition & 1 deletion project/build/HammersmithProject.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HammersmithProject(info: ProjectInfo)
// Connection Pooling
val commonsPool = "commons-pool" % "commons-pool" % "1.5.5"

val scalaj_collection = "org.scalaj" % "scalaj-collection_2.8.0" % "1.0"
val scalaj_collection = "org.scalaj" %% "scalaj-collection" % "1.1"
// Netty
val netty = "org.jboss.netty" % "netty" % "3.2.4.Final"

Expand Down

0 comments on commit c51b67f

Please sign in to comment.