Skip to content

Commit 54dd250

Browse files
author
QuadStingray
committed
refactor: foreach moved to pagination trait
1 parent 1fe1d8b commit 54dd250

File tree

5 files changed

+36
-23
lines changed

5 files changed

+36
-23
lines changed

docs/documentation/collection/pagination.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ The Pagination over an aggregation pipeline supports only the response of `Docum
1616

1717
## Foreach over Pagination result
1818

19-
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#aggregation-foreach
19+
### With default row count
20+
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#foreach-default-rows
21+
22+
### With specific row count
23+
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#foreach-with-rows

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongodb-driver",
33
"organization": "dev.mongocamp",
4-
"version": "2.5.5.snapshot",
4+
"version": "2.6.0",
55
"author": "info@mongocamp.dev",
66
"license": "Apache-2.0",
77
"repository": {

src/main/resources/reference.conf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11

2-
dev.mongocamp.mongodb.sync {
3-
maxWait = 600
4-
syncColumnLastSync = "_lastSync"
5-
syncColumnLastUpdate = "_lastUpdate"
6-
writeSyncLogOnMaster = false
7-
syncLogTableName = "mongo-sync-log"
2+
dev.mongocamp.mongodb {
3+
sync {
4+
maxWait = 600
5+
syncColumnLastSync = "_lastSync"
6+
syncColumnLastUpdate = "_lastUpdate"
7+
writeSyncLogOnMaster = false
8+
syncLogTableName = "mongo-sync-log"
9+
}
10+
pagination {
11+
rows = 100
12+
}
813
}
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package dev.mongocamp.driver.mongodb.pagination
2+
import dev.mongocamp.driver.mongodb.database.ConfigHelper
23

3-
trait MongoPagination[A <: Any] {
4+
trait MongoPagination[A <: Any] extends ConfigHelper {
45
def paginate(page: Long, rows: Long): PaginationResult[A]
56
def countResult: Long
6-
}
7-
object MongoPagination {
8-
def foreach[A <: Any](pagination: MongoPagination[A], rows: Int = 50)(a: A => Unit): Unit = {
7+
8+
def foreach(a: A => Unit): Unit = {
9+
val rows = intConfig(configPath = "dev.mongocamp.mongodb.pagination", key = "rows")
10+
foreach(rows)(a)
11+
}
12+
def foreach(rows: Int)(a: A => Unit): Unit = {
913
var currentPageNumber = 1
10-
val rowsPerPage = if (rows < 1) Int.MaxValue else rows
11-
val maxPages = Math.ceil(pagination.countResult.toDouble / rowsPerPage).toInt
14+
val rowsPerPage = if (rows < 1) Int.MaxValue else rows
15+
val maxPages = Math.ceil(countResult.toDouble / rowsPerPage).toInt
1216
while (currentPageNumber > 0 && rowsPerPage > 0 && currentPageNumber <= maxPages) {
13-
val page = pagination.paginate(currentPageNumber, rowsPerPage)
17+
val page = paginate(currentPageNumber, rowsPerPage)
1418
page.databaseObjects.foreach(a)
1519
currentPageNumber += 1
1620
}
17-
1821
}
1922

2023
}

src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import dev.mongocamp.driver.mongodb.test.TestDatabase._
99
import org.mongodb.scala.bson.conversions.Bson
1010
import org.mongodb.scala.model.Aggregates.{filter, sort}
1111
import org.mongodb.scala.model.Filters.{and, equal}
12-
class PaginationIterationSpec extends PersonSpecification with MongoImplicits with LazyLogging{
12+
class PaginationIterationSpec extends PersonSpecification with MongoImplicits with LazyLogging {
1313

1414
"Pagination Iteration" should {
1515

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

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

2423
var i = 0
2524

26-
MongoPagination.foreach(paginationFemale, 5) { person =>
25+
// #region foreach-with-rows
26+
paginationFemale.foreach(5) { person =>
2727
{
2828
logger.trace(person.toString)
2929
i = i + 1
3030
}
3131
}
3232
i mustEqual 98
33-
// #endregion filter-foreach
33+
// #endregion foreach-with-rows
3434

3535
}
3636

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

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

4948
page.paginationInfo.allCount mustEqual 98
49+
50+
// #region foreach-default-rows
5051
var i = 0
51-
MongoPagination.foreach(pagination, 5) { element =>
52+
pagination.foreach { element =>
5253
{
5354
logger.trace(element.toJson())
5455
i = i + 1
5556
}
5657
}
5758
i mustEqual 98
58-
// #endregion aggregation-foreach
59+
// #endregion foreach-default-rows
5960

6061
}
6162

0 commit comments

Comments
 (0)