longevity

A Persistence Framework for Scala and NoSQL

View project on GitHub

retrieval by query

Once we have constructed our query, we can iterate over the results using Repo.queryToIterator:

import longevity.persistence.PState
import scala.concurrent.Future

val blog: Blog = getBlogFromSomewhere()

val recentPosts: Future[Iterator[PState[BlogPost]]] = repo.queryToIterator {
  import com.github.nscala_time.time.Imports._
  import BlogPost.queryDsl._
  import BlogPost.props._
  blogUri eqs blog.blogUri and postDate gt DateTime.now - 1.week
}

This approach is non-reactive; the resulting iterator will be blocking. If we are okay with receiving the entire results at once, we can use Repo.queryToVector, which returns an F[Vector[PState[P]]]:

import longevity.persistence.PState
import scala.concurrent.Future

val blog: Blog = getBlogFromSomewhere()

val recentPosts: Future[Vector[PState[BlogPost]]] = blogPostRepo.queryToVector {
  import com.github.nscala_time.time.Imports._
  import BlogPost.queryDsl._
  import BlogPost.props._
  blogUri eqs blog.blogUri and postDate gt DateTime.now - 1.week
}

Clearly, this approach is not going to work for result sets that are very large. For one thing, they are all going to have to fit into memory at the same time. For another thing, you will have to wait for the complete result set before you can start processing the results. In these situations, you may want to stream the results instead, as we discuss in the next section.

prev: limits and offsets
up: queries
next: stream by query