Skip to content

Commit

Permalink
refactor: foreach moved to pagination trait
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadStingray committed Mar 31, 2023
1 parent 1fe1d8b commit 54dd250
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
6 changes: 5 additions & 1 deletion docs/documentation/collection/pagination.md
Expand Up @@ -16,4 +16,8 @@ The Pagination over an aggregation pipeline supports only the response of `Docum

## Foreach over Pagination result

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#aggregation-foreach
### With default row count
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#foreach-default-rows

### With specific row count
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#foreach-with-rows
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "mongodb-driver",
"organization": "dev.mongocamp",
"version": "2.5.5.snapshot",
"version": "2.6.0",
"author": "info@mongocamp.dev",
"license": "Apache-2.0",
"repository": {
Expand Down
17 changes: 11 additions & 6 deletions src/main/resources/reference.conf
@@ -1,8 +1,13 @@

dev.mongocamp.mongodb.sync {
maxWait = 600
syncColumnLastSync = "_lastSync"
syncColumnLastUpdate = "_lastUpdate"
writeSyncLogOnMaster = false
syncLogTableName = "mongo-sync-log"
dev.mongocamp.mongodb {
sync {
maxWait = 600
syncColumnLastSync = "_lastSync"
syncColumnLastUpdate = "_lastUpdate"
writeSyncLogOnMaster = false
syncLogTableName = "mongo-sync-log"
}
pagination {
rows = 100
}
}
@@ -1,20 +1,23 @@
package dev.mongocamp.driver.mongodb.pagination
import dev.mongocamp.driver.mongodb.database.ConfigHelper

trait MongoPagination[A <: Any] {
trait MongoPagination[A <: Any] extends ConfigHelper {
def paginate(page: Long, rows: Long): PaginationResult[A]
def countResult: Long
}
object MongoPagination {
def foreach[A <: Any](pagination: MongoPagination[A], rows: Int = 50)(a: A => Unit): Unit = {

def foreach(a: A => Unit): Unit = {
val rows = intConfig(configPath = "dev.mongocamp.mongodb.pagination", key = "rows")
foreach(rows)(a)
}
def foreach(rows: Int)(a: A => Unit): Unit = {
var currentPageNumber = 1
val rowsPerPage = if (rows < 1) Int.MaxValue else rows
val maxPages = Math.ceil(pagination.countResult.toDouble / rowsPerPage).toInt
val rowsPerPage = if (rows < 1) Int.MaxValue else rows
val maxPages = Math.ceil(countResult.toDouble / rowsPerPage).toInt
while (currentPageNumber > 0 && rowsPerPage > 0 && currentPageNumber <= maxPages) {
val page = pagination.paginate(currentPageNumber, rowsPerPage)
val page = paginate(currentPageNumber, rowsPerPage)
page.databaseObjects.foreach(a)
currentPageNumber += 1
}

}

}
Expand Up @@ -9,12 +9,11 @@ import dev.mongocamp.driver.mongodb.test.TestDatabase._
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.model.Aggregates.{filter, sort}
import org.mongodb.scala.model.Filters.{and, equal}
class PaginationIterationSpec extends PersonSpecification with MongoImplicits with LazyLogging{
class PaginationIterationSpec extends PersonSpecification with MongoImplicits with LazyLogging {

"Pagination Iteration" should {

"support with Filter" in {
// #region filter-foreach
val paginationFemale = MongoPaginatedFilter(PersonDAO, Map("gender" -> "female"), sortByKey("name"))

val pageFemale = paginationFemale.paginate(1, 10)
Expand All @@ -23,19 +22,19 @@ class PaginationIterationSpec extends PersonSpecification with MongoImplicits wi

var i = 0

MongoPagination.foreach(paginationFemale, 5) { person =>
// #region foreach-with-rows
paginationFemale.foreach(5) { person =>
{
logger.trace(person.toString)
i = i + 1
}
}
i mustEqual 98
// #endregion filter-foreach
// #endregion foreach-with-rows

}

"support with aggregation" in {
// #region aggregation-foreach
val filterStage: Bson = filter(and(equal("gender", "female"), notNullFilter("balance")))

val sortStage: Bson = sort(sortByKey("age"))
Expand All @@ -47,15 +46,17 @@ class PaginationIterationSpec extends PersonSpecification with MongoImplicits wi
val page = pagination.paginate(1, 10)

page.paginationInfo.allCount mustEqual 98

// #region foreach-default-rows
var i = 0
MongoPagination.foreach(pagination, 5) { element =>
pagination.foreach { element =>
{
logger.trace(element.toJson())
i = i + 1
}
}
i mustEqual 98
// #endregion aggregation-foreach
// #endregion foreach-default-rows

}

Expand Down

0 comments on commit 54dd250

Please sign in to comment.