-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
QuadStingray
committed
Mar 31, 2023
1 parent
5f3134d
commit 1fe1d8b
Showing
14 changed files
with
177 additions
and
33 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Pagination | ||
|
||
In many cases you want to have the possibility to paginate over the response of your query result. MongoCamp MongoDB Driver support the pagination over Aggregation and Filter Response. As well there is an comfort methode to have an foreach over the whole Response, but with pagination to have a lower memory footprint. | ||
|
||
## Aggregation Pagination | ||
|
||
::: warning | ||
The Pagination over an aggregation pipeline supports only the response of `Document`, also if you use an case class MongoDAO you will got an `Document` back. | ||
::: | ||
|
||
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationAggregationSpec.scala#aggregation-pagination | ||
|
||
## Find Pagination | ||
|
||
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.scala#filter-pagination | ||
|
||
## Foreach over Pagination result | ||
|
||
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#aggregation-foreach |
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,21 @@ | ||
# Lucene Query | ||
|
||
MongoCamp Mongo Driver support the usage of [Lucene Query](https://lucene.apache.org/) to search in the MongoDb. | ||
|
||
## Usage | ||
Fist you have the add the lucene-queryparser Dependency to your build.sbt (without the provided marker). | ||
|
||
<<< @/../build.sbt#lucene-dependency{scala} | ||
|
||
### Explicit Usage | ||
The LuceneConverter has the methods to parse a String to and `Query` and a other to the document conversion. | ||
|
||
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSpec.scala#lucene-parser-with-explicit | ||
|
||
### Implicit Usage | ||
Like the Map to Bson conversion there is also an implicit method to convert `Query` to find Bson. | ||
|
||
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSpec.scala#lucene-parser-with-implicit | ||
|
||
## Read More | ||
[Lucene Cheatsheet](https://www.lucenetutorial.com/lucene-query-syntax.html) |
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,16 +1,18 @@ | ||
package dev.mongocamp.driver.mongodb | ||
|
||
import org.bson.conversions.Bson | ||
import org.mongodb.scala.model.Sorts.{ ascending, descending, orderBy } | ||
import org.mongodb.scala.model.Sorts.{ascending, descending, orderBy} | ||
|
||
object Sort extends Sort | ||
|
||
trait Sort { | ||
|
||
def sortByKey(key: String, sortAscending: Boolean = true): Bson = | ||
if (sortAscending) | ||
if (sortAscending) { | ||
orderBy(ascending(key)) | ||
else | ||
} | ||
else { | ||
orderBy(descending(key)) | ||
} | ||
|
||
} |
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
20 changes: 20 additions & 0 deletions
20
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.mongocamp.driver.mongodb.pagination | ||
|
||
trait MongoPagination[A <: Any] { | ||
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 = { | ||
var currentPageNumber = 1 | ||
val rowsPerPage = if (rows < 1) Int.MaxValue else rows | ||
val maxPages = Math.ceil(pagination.countResult.toDouble / rowsPerPage).toInt | ||
while (currentPageNumber > 0 && rowsPerPage > 0 && currentPageNumber <= maxPages) { | ||
val page = pagination.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
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
64 changes: 64 additions & 0 deletions
64
src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.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,64 @@ | ||
package dev.mongocamp.driver.mongodb.pagination | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import dev.mongocamp.driver.MongoImplicits | ||
import dev.mongocamp.driver.mongodb.Aggregate._ | ||
import dev.mongocamp.driver.mongodb._ | ||
import dev.mongocamp.driver.mongodb.dao.PersonSpecification | ||
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{ | ||
|
||
"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) | ||
|
||
pageFemale.paginationInfo.allCount mustEqual 98 | ||
|
||
var i = 0 | ||
|
||
MongoPagination.foreach(paginationFemale, 5) { person => | ||
{ | ||
logger.trace(person.toString) | ||
i = i + 1 | ||
} | ||
} | ||
i mustEqual 98 | ||
// #endregion filter-foreach | ||
|
||
} | ||
|
||
"support with aggregation" in { | ||
// #region aggregation-foreach | ||
val filterStage: Bson = filter(and(equal("gender", "female"), notNullFilter("balance"))) | ||
|
||
val sortStage: Bson = sort(sortByKey("age")) | ||
|
||
val pipeline = List(filterStage, sortStage) | ||
|
||
val pagination = MongoPaginatedAggregation(PersonDAO, pipeline, allowDiskUse = true) | ||
|
||
val page = pagination.paginate(1, 10) | ||
|
||
page.paginationInfo.allCount mustEqual 98 | ||
var i = 0 | ||
MongoPagination.foreach(pagination, 5) { element => | ||
{ | ||
logger.trace(element.toJson()) | ||
i = i + 1 | ||
} | ||
} | ||
i mustEqual 98 | ||
// #endregion aggregation-foreach | ||
|
||
} | ||
|
||
} | ||
|
||
} |