Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: foreach moved to pagination trait
- Loading branch information
QuadStingray
committed
Mar 31, 2023
1 parent
1fe1d8b
commit 54dd250
Showing
5 changed files
with
36 additions
and
23 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
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
19 changes: 11 additions & 8 deletions
19
src/main/scala/dev/mongocamp/driver/mongodb/pagination/MongoPagination.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 |
---|---|---|
@@ -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 | ||
} | ||
|
||
} | ||
|
||
} |
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