2016-04-02 04:13:25 -06:00
|
|
|
package lila.db
|
|
|
|
|
2019-11-29 11:08:13 -07:00
|
|
|
import scala.collection.Factory
|
2016-04-02 04:13:25 -06:00
|
|
|
|
|
|
|
import reactivemongo.api._
|
2019-11-29 11:08:13 -07:00
|
|
|
import reactivemongo.api.bson._
|
2016-04-02 04:13:25 -06:00
|
|
|
|
|
|
|
trait CursorExt { self: dsl =>
|
|
|
|
|
2016-07-21 06:41:34 -06:00
|
|
|
// Can be refactor as CursorProducer
|
2020-05-05 22:11:15 -06:00
|
|
|
implicit final class ExtendCursor[A: BSONDocumentReader](val c: Cursor[A])(implicit
|
|
|
|
ec: scala.concurrent.ExecutionContext
|
2019-12-13 18:17:43 -07:00
|
|
|
) {
|
2016-04-02 04:13:25 -06:00
|
|
|
|
|
|
|
// like collect, but with stopOnError defaulting to false
|
2019-11-29 11:08:13 -07:00
|
|
|
def gather[M[_]](upTo: Int = Int.MaxValue)(implicit cbf: Factory[A, M[A]]): Fu[M[A]] =
|
|
|
|
c.collect[M](upTo, Cursor.ContOnError[M[A]]())
|
2016-04-02 04:13:25 -06:00
|
|
|
|
2021-08-17 15:19:45 -06:00
|
|
|
private def list(limit: Option[Int]): Fu[List[A]] = gather[List](limit | Int.MaxValue)
|
2016-04-02 04:13:25 -06:00
|
|
|
|
|
|
|
def list(limit: Int): Fu[List[A]] = list(limit.some)
|
|
|
|
|
|
|
|
def list(): Fu[List[A]] = list(none)
|
|
|
|
|
|
|
|
// like headOption, but with stopOnError defaulting to false
|
2019-12-13 07:30:20 -07:00
|
|
|
def uno: Fu[Option[A]] =
|
|
|
|
c.collect[Iterable](
|
2020-06-24 03:37:18 -06:00
|
|
|
1,
|
|
|
|
Cursor.ContOnError[Iterable[A]]()
|
|
|
|
).map(_.headOption)
|
2016-04-02 04:13:25 -06:00
|
|
|
}
|
|
|
|
}
|