-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add mongo sink * add mongo update and delete operations * document mongo update and delete operations. * Seperate sink and flow.
- Loading branch information
1 parent
5a0d8a5
commit 52f5c06
Showing
7 changed files
with
429 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
mongodb/src/main/scala/akka/stream/alpakka/mongodb/scaladsl/DocumentUpdate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.mongodb.scaladsl | ||
|
||
import org.mongodb.scala.bson.conversions.Bson | ||
|
||
/** | ||
* | ||
* @param filter a document describing the query filter, which may not be null. This can be of any type for which a { @code Codec} is | ||
* registered | ||
* @param update a document describing the update, which may not be null. The update to apply must include only update operators. This | ||
* can be of any type for which a { @code Codec} is registered | ||
*/ | ||
final case class DocumentUpdate(filter: Bson, update: Bson) |
116 changes: 116 additions & 0 deletions
116
mongodb/src/main/scala/akka/stream/alpakka/mongodb/scaladsl/MongoFlow.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.mongodb.scaladsl | ||
|
||
import akka.stream.scaladsl.Flow | ||
import akka.NotUsed | ||
import org.mongodb.scala.bson.conversions.Bson | ||
import org.mongodb.scala.model.UpdateOptions | ||
import org.mongodb.scala.result.{DeleteResult, UpdateResult} | ||
import org.mongodb.scala.{Document, MongoCollection} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
object MongoFlow { | ||
|
||
/** | ||
* A [[Flow]] that will insert documents into a collection. | ||
* @param parallelism number of documents to insert in parallel. | ||
* @param collection mongo db collection to insert to. | ||
*/ | ||
def insertOne(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Flow[Document, Document, NotUsed] = | ||
Flow[Document] | ||
.mapAsync(parallelism)(doc => collection.insertOne(doc).toFuture().map(_ => doc)) | ||
|
||
/** | ||
* A [[Flow]] that will insert batches documents into a collection. | ||
* @param parallelism number of batches of documents to insert in parallel. | ||
* @param collection mongo db collection to insert to. | ||
*/ | ||
def insertMany(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Flow[Seq[Document], Seq[Document], NotUsed] = | ||
Flow[Seq[Document]].mapAsync(parallelism)(docs => collection.insertMany(docs).toFuture().map(_ => docs)) | ||
|
||
/** | ||
* A [[Flow]] that will update documents as defined by a [[DocumentUpdate]]. | ||
* | ||
* @param parallelism the number of documents to update in parallel. | ||
* @param collection the mongo db collection to update. | ||
* @param maybeUpdateOptions optional additional [[UpdateOptions]] | ||
*/ | ||
def updateOne( | ||
parallelism: Int, | ||
collection: MongoCollection[Document], | ||
maybeUpdateOptions: Option[UpdateOptions] = None | ||
)(implicit executionContext: ExecutionContext): Flow[DocumentUpdate, (UpdateResult, DocumentUpdate), NotUsed] = | ||
maybeUpdateOptions match { | ||
case None => | ||
Flow[DocumentUpdate].mapAsync(parallelism)( | ||
documentUpdate => | ||
collection.updateOne(documentUpdate.filter, documentUpdate.update).toFuture().map(_ -> documentUpdate) | ||
) | ||
case Some(options) => | ||
Flow[DocumentUpdate].mapAsync(parallelism)( | ||
documentUpdate => | ||
collection | ||
.updateOne(documentUpdate.filter, documentUpdate.update, options) | ||
.toFuture() | ||
.map(_ -> documentUpdate) | ||
) | ||
} | ||
|
||
/** | ||
* A [[Flow]] that will update many documents as defined by a [[DocumentUpdate]]. | ||
* | ||
* @param parallelism the number of documents to update in parallel. | ||
* @param collection the mongo db collection to update. | ||
* @param maybeUpdateOptions optional additional [[UpdateOptions]] | ||
*/ | ||
def updateMany( | ||
parallelism: Int, | ||
collection: MongoCollection[Document], | ||
maybeUpdateOptions: Option[UpdateOptions] = None | ||
)(implicit executionContext: ExecutionContext): Flow[DocumentUpdate, (UpdateResult, DocumentUpdate), NotUsed] = | ||
maybeUpdateOptions match { | ||
case None => | ||
Flow[DocumentUpdate].mapAsync(parallelism)( | ||
documentUpdate => | ||
collection.updateMany(documentUpdate.filter, documentUpdate.update).toFuture().map(_ -> documentUpdate) | ||
) | ||
case Some(options) => | ||
Flow[DocumentUpdate].mapAsync(parallelism)( | ||
documentUpdate => | ||
collection | ||
.updateMany(documentUpdate.filter, documentUpdate.update, options) | ||
.toFuture() | ||
.map(_ -> documentUpdate) | ||
) | ||
} | ||
|
||
/** | ||
* A [[Flow]] that will delete individual documents as defined by a [[Bson]] filter query. | ||
* | ||
* @param parallelism the number of documents to delete in parallel. | ||
* @param collection the mongo db collection to update. | ||
*/ | ||
def deleteOne(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Flow[Bson, (DeleteResult, Bson), NotUsed] = | ||
Flow[Bson].mapAsync(parallelism)(bson => collection.deleteOne(bson).toFuture().map(_ -> bson)) | ||
|
||
/** | ||
* A [[Flow]] that will delete many documents as defined by a [[Bson]] filter query. | ||
* | ||
* @param parallelism the number of documents to delete in parallel. | ||
* @param collection the mongo db collection to update. | ||
*/ | ||
def deleteMany(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Flow[Bson, (DeleteResult, Bson), NotUsed] = | ||
Flow[Bson].mapAsync(parallelism)(bson => collection.deleteMany(bson).toFuture().map(_ -> bson)) | ||
} |
87 changes: 87 additions & 0 deletions
87
mongodb/src/main/scala/akka/stream/alpakka/mongodb/scaladsl/MongoSink.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.mongodb.scaladsl | ||
|
||
import akka.stream.scaladsl.{Keep, Sink} | ||
import akka.Done | ||
import org.mongodb.scala.bson.conversions.Bson | ||
import org.mongodb.scala.model.UpdateOptions | ||
import org.mongodb.scala.{Document, MongoCollection} | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
object MongoSink { | ||
|
||
/** | ||
* A [[Sink]] that will insert documents into a collection. | ||
* @param parallelism number of documents to insert in parallel. | ||
* @param collection mongo db collection to insert to. | ||
*/ | ||
def insertOne(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Sink[Document, Future[Done]] = | ||
MongoFlow.insertOne(parallelism, collection).toMat(Sink.ignore)(Keep.right) | ||
|
||
/** | ||
* A [[Sink]] that will insert batches of documents into a collection. | ||
* @param parallelism number of batches of documents to insert in parallel. | ||
* @param collection mongo db collection to insert to. | ||
*/ | ||
def insertMany(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Sink[Seq[Document], Future[Done]] = | ||
MongoFlow.insertMany(parallelism, collection).toMat(Sink.ignore)(Keep.right) | ||
|
||
/** | ||
* A [[Sink]] that will update documents as defined by a [[DocumentUpdate]]. | ||
* | ||
* @param parallelism the number of documents to update in parallel. | ||
* @param collection the mongo db collection to update. | ||
* @param maybeUpdateOptions optional additional [[UpdateOptions]] | ||
*/ | ||
def updateOne( | ||
parallelism: Int, | ||
collection: MongoCollection[Document], | ||
maybeUpdateOptions: Option[UpdateOptions] = None | ||
)(implicit executionContext: ExecutionContext): Sink[DocumentUpdate, Future[Done]] = | ||
MongoFlow.updateOne(parallelism, collection, maybeUpdateOptions).toMat(Sink.ignore)(Keep.right) | ||
|
||
/** | ||
* A [[Sink]] that will update many documents as defined by a [[DocumentUpdate]]. | ||
* | ||
* @param parallelism the number of documents to update in parallel. | ||
* @param collection the mongo db collection to update. | ||
* @param maybeUpdateOptions optional additional [[UpdateOptions]] | ||
*/ | ||
def updateMany( | ||
parallelism: Int, | ||
collection: MongoCollection[Document], | ||
maybeUpdateOptions: Option[UpdateOptions] = None | ||
)(implicit executionContext: ExecutionContext): Sink[DocumentUpdate, Future[Done]] = | ||
MongoFlow.updateMany(parallelism, collection, maybeUpdateOptions).toMat(Sink.ignore)(Keep.right) | ||
|
||
/** | ||
* A [[Sink]] that will delete individual documents as defined by a [[Bson]] filter query. | ||
* | ||
* @param parallelism the number of documents to delete in parallel. | ||
* @param collection the mongo db collection to update. | ||
*/ | ||
def deleteOne(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Sink[Bson, Future[Done]] = | ||
MongoFlow.deleteOne(parallelism, collection).toMat(Sink.ignore)(Keep.right) | ||
|
||
/** | ||
* A [[Sink]] that will delete many documents as defined by a [[Bson]] filter query. | ||
* | ||
* @param parallelism the number of documents to delete in parallel. | ||
* @param collection the mongo db collection to update. | ||
*/ | ||
def deleteMany(parallelism: Int, collection: MongoCollection[Document])( | ||
implicit executionContext: ExecutionContext | ||
): Sink[Bson, Future[Done]] = | ||
MongoFlow.deleteMany(parallelism, collection).toMat(Sink.ignore)(Keep.right) | ||
|
||
} |
Oops, something went wrong.