object UsageSection extends AnyFlatSpec with Matchers with Section
Introduction
Fetch is a library that allows your data fetches to be written in a concise, composable way while executing efficiently. You don't need to use any explicit concurrency construct but existing idioms: applicative for concurrency and monad for sequencing.
Oftentimes, our applications read and manipulate data from a variety of different sources such as databases, web services or file systems. These data sources are subject to latency, and we'd prefer to query them efficiently.
If we are just reading data, we can make a series of optimizations such as:
- batching requests to the same data source
- requesting independent data from different sources in parallel
- caching previously seen results
However, if we mix these optimizations with the code that fetches the data we may end up trading clarity for performance. Furthermore, we are mixing low-level (optimization) and high-level (business logic with the data we read) concerns.
Installation
To begin, add the following dependency to your SBT build file:
"com.47deg" %% "fetch" % "1.2.2"
Or, if using Scala.js:
"com.47deg" %%% "fetch" % "1.2.2"
Now you’ll have Fetch available in both Scala and Scala.js.
Usage
In order to tell Fetch how to retrieve data, we must implement the DataSource
typeclass.
import cats.effect.Concurrent import cats.data.NonEmptyList trait DataSource[F[_], Identity, Result]{ def data: Data[Identity, Result] def CF: Concurrent[F] def fetch(id: Identity): F[Option[Result]] /* `batch` is implemented in terms of `fetch` by default */ def batch(ids: NonEmptyList[Identity]): F[Map[Identity, Result]] }
It takes two type parameters:
Identity
: the identity we want to fetch (aUserId
if we were fetching users)Result
: the type of the data we retrieve (aUser
if we were fetching users)
There are two methods: fetch
and batch
. fetch
receives one identity and must return
a Concurrent
containing
an optional result. Returning an Option
Fetch can detect whether an identity couldn't be
fetched or no longer exists.
batch
method takes a non-empty list of identities and must return a Concurrent
containing
a map from identities to results. Accepting a list of identities gives Fetch the ability to batch
requests to the same data source, and returning a mapping from identities to results, Fetch can
detect whenever an identity couldn’t be fetched or no longer exists.
The data
method returns a Data[Identity, Result]
instance that Fetch uses to optimize requests to the
same data source, and is expected to return a singleton object
that extends Data[Identity, Result]
.
Writing your first data source
Now that we know about the DataSource
typeclass, let's write our first data source! We'll start by
implementing a data source for fetching users given their id.
The first thing we'll do is define the types for user ids and users.
type UserId = Int case class User(id: UserId, username: String)
We’ll simulate unpredictable latency with this function.
import cats.effect._ import cats.syntax.all._ def latency[F[_] : Concurrent](msg: String): F[Unit] = for { _ <- Sync[F].delay(println(s"--> [${Thread.currentThread.getId}] $msg")) _ <- Sync[F].delay(Thread.sleep(100)) _ <- Sync[F].delay(println(s"<-- [${Thread.currentThread.getId}] $msg")) } yield ()
And now we're ready to write our user data source; we'll emulate a database with an in-memory map.
import cats.data.NonEmptyList import cats.instances.list._ import fetch._ val userDatabase: Map[UserId, User] = Map( 1 -> User(1, "@one"), 2 -> User(2, "@two"), 3 -> User(3, "@three"), 4 -> User(4, "@four") ) object Users extends Data[UserId, User] { def name = "Users" def source[F[_] : Concurrent]: DataSource[F, UserId, User] = new DataSource[F, UserId, User] { override def data = Users override def CF = Concurrent[F] override def fetch(id: UserId): F[Option[User]] = latency[F](s"One User $id") >> CF.pure(userDatabase.get(id)) override def batch(ids: NonEmptyList[UserId]): F[Map[UserId, User]] = latency[F](s"Batch Users $ids") >> CF.pure(userDatabase.filterKeys(ids.toList.toSet).toMap) } }
Now that we have a data source we can write a function for fetching users
given an id, we just have to pass a UserId
as an argument to Fetch
.
def getUser[F[_] : Concurrent](id: UserId): Fetch[F, User] =
Fetch(id, Users.source)
Optional identities
If you want to create a Fetch that doesn’t fail if the identity is not found, you can use
Fetch#optional
instead of Fetch#apply
. Note that instead of a Fetch[F, A]
you will get a
Fetch[F, Option[A]]
.
def maybeGetUser[F[_] : Concurrent](id: UserId): Fetch[F, Option[User]] = Fetch.optional(id, Users.source)
Data sources that don’t support batching
If your data source doesn’t support batching, you can simply leave the batch
method unimplemented.
Note that it will use the fetch
implementation for requesting identities in parallel.
object Unbatched extends Data[Int, Int]{ def name = "Unbatched" def source[F[_] : Concurrent]: DataSource[F, Int, Int] = new DataSource[F, Int, Int]{ override def data = Unbatched override def CF = Concurrent[F] override def fetch(id: Int): F[Option[Int]] = CF.pure(Option(id)) } }
Batching individuals requests sequentially
The default batch
implementation run requests to the data source in parallel, but you can easily
override it. We can make batch
sequential using NonEmptyList.traverse
for fetching individual
identities.
object UnbatchedSeq extends Data[Int, Int]{ def name = "UnbatchedSeq" def source[F[_] : Concurrent]: DataSource[F, Int, Int] = new DataSource[F, Int, Int]{ override def data = UnbatchedSeq override def CF = Concurrent[F] override def fetch(id: Int): F[Option[Int]] = CF.pure(Option(id)) override def batch(ids: NonEmptyList[Int]): F[Map[Int, Int]] = ids.traverse( (id) => fetch(id).map(v => (id, v)) ).map(_.collect { case (i, Some(x)) => (i, x) }.toMap) } }
Data sources that only support batching
If your data source only supports querying it in batches, you can implement fetch
in terms of batch
.
object OnlyBatched extends Data[Int, Int]{ def name = "OnlyBatched" def source[F[_] : Concurrent]: DataSource[F, Int, Int] = new DataSource[F, Int, Int]{ override def data = OnlyBatched override def CF = Concurrent[F] override def fetch(id: Int): F[Option[Int]] = batch(NonEmptyList(id, List())).map(_.get(id)) override def batch(ids: NonEmptyList[Int]): F[Map[Int, Int]] = CF.pure(ids.map(x => (x, x)).toList.toMap) } }
- Alphabetic
- By Inheritance
- UsageSection
- Section
- Matchers
- Explicitly
- MatcherWords
- Tolerance
- AnyFlatSpec
- AnyFlatSpecLike
- Documenting
- Alerting
- Notifying
- Informing
- CanVerb
- MustVerb
- ShouldVerb
- TestRegistration
- TestSuite
- Suite
- Serializable
- Assertions
- TripleEquals
- TripleEqualsSupport
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class AWord extends AnyRef
- Definition Classes
- Matchers
- final class AnWord extends AnyRef
- Definition Classes
- Matchers
- sealed class AnyShouldWrapper[T] extends AnyRef
- Definition Classes
- Matchers
- final class BehaviorWord extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- class CheckingEqualizer[L] extends AnyRef
- Definition Classes
- TripleEqualsSupport
- sealed class Collected extends Serializable
- Attributes
- protected
- Definition Classes
- Matchers
- class DecidedByEquality[A] extends Equality[A]
- Definition Classes
- Explicitly
- class DecidedWord extends AnyRef
- Definition Classes
- Explicitly
- class DeterminedByEquivalence[T] extends Equivalence[T]
- Definition Classes
- Explicitly
- class DeterminedWord extends AnyRef
- Definition Classes
- Explicitly
- class Equalizer[L] extends AnyRef
- Definition Classes
- TripleEqualsSupport
- final class HavePropertyMatcherGenerator extends AnyRef
- Definition Classes
- Matchers
- final class IgnoreVerbString extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class IgnoreVerbStringTaggedAs extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class IgnoreWord extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class InAndIgnoreMethods extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class InAndIgnoreMethodsAfterTaggedAs extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class ItVerbString extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class ItVerbStringTaggedAs extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class ItWord extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class KeyWord extends AnyRef
- Definition Classes
- Matchers
- trait NoArgTest extends () => Outcome with TestData
- Attributes
- protected
- Definition Classes
- TestSuite
- final class PlusOrMinusWrapper[T] extends AnyRef
- Definition Classes
- Tolerance
- final class RegexWord extends AnyRef
- Definition Classes
- Matchers
- final class RegexWrapper extends AnyRef
- Definition Classes
- Matchers
- class ResultOfBeWordForAny[T] extends AnyRef
- Definition Classes
- Matchers
- sealed class ResultOfBeWordForCollectedAny[T] extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfBeWordForCollectedArray[T] extends ResultOfBeWordForCollectedAny[Array[T]]
- Definition Classes
- Matchers
- final class ResultOfCollectedAny[T] extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfContainWordForCollectedAny[T] extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfEndWithWordForCollectedString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfEndWithWordForString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfFullyMatchWordForCollectedString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfFullyMatchWordForString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfHaveWordForCollectedExtent[A] extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfHaveWordForExtent[A] extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfIncludeWordForCollectedString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfIncludeWordForString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfNotWordForCollectedAny[T] extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfStartWithWordForCollectedString extends AnyRef
- Definition Classes
- Matchers
- final class ResultOfStartWithWordForString extends AnyRef
- Definition Classes
- Matchers
- trait StringCanWrapperForVerb extends AnyRef
- Definition Classes
- CanVerb
- trait StringMustWrapperForVerb extends AnyRef
- Definition Classes
- MustVerb
- final class StringShouldWrapper extends AnyShouldWrapper[String] with org.scalatest.matchers.should.Matchers.StringShouldWrapperForVerb
- Definition Classes
- Matchers
- trait StringShouldWrapperForVerb extends AnyRef
- Definition Classes
- ShouldVerb
- class TheAfterWord extends AnyRef
- Definition Classes
- Explicitly
- final class TheSameInstanceAsPhrase extends AnyRef
- Definition Classes
- Matchers
- final class TheyVerbString extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class TheyVerbStringTaggedAs extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class TheyWord extends AnyRef
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- final class ValueWord extends AnyRef
- Definition Classes
- Matchers
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def !==[T](right: Spread[T]): TripleEqualsInvocationOnSpread[T]
- Definition Classes
- TripleEqualsSupport
- def !==(right: Null): TripleEqualsInvocation[Null]
- Definition Classes
- TripleEqualsSupport
- def !==[T](right: T): TripleEqualsInvocation[T]
- Definition Classes
- TripleEqualsSupport
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def <[T](right: T)(implicit arg0: Ordering[T]): ResultOfLessThanComparison[T]
- Definition Classes
- Matchers
- def <=[T](right: T)(implicit arg0: Ordering[T]): ResultOfLessThanOrEqualToComparison[T]
- Definition Classes
- Matchers
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def ===[T](right: Spread[T]): TripleEqualsInvocationOnSpread[T]
- Definition Classes
- TripleEqualsSupport
- def ===(right: Null): TripleEqualsInvocation[Null]
- Definition Classes
- TripleEqualsSupport
- def ===[T](right: T): TripleEqualsInvocation[T]
- Definition Classes
- TripleEqualsSupport
- def >[T](right: T)(implicit arg0: Ordering[T]): ResultOfGreaterThanComparison[T]
- Definition Classes
- Matchers
- def >=[T](right: T)(implicit arg0: Ordering[T]): ResultOfGreaterThanOrEqualToComparison[T]
- Definition Classes
- Matchers
- def a[T](implicit arg0: ClassTag[T]): ResultOfATypeInvocation[T]
- Definition Classes
- Matchers
- val a: AWord
- Definition Classes
- Matchers
- val after: TheAfterWord
- Definition Classes
- Explicitly
- def alert: Alerter
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike → Alerting
- def all(xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def all[K, V, JMAP[k, v] <: Map[k, v]](xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def all[K, V, MAP[k, v] <: GenMap[k, v]](xs: MAP[K, V])(implicit collecting: Collecting[(K, V), GenTraversable[(K, V)]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[(K, V)]
- Definition Classes
- Matchers
- def all[E, C[_]](xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- def allElementsOf[R](elements: GenTraversable[R]): ResultOfAllElementsOfApplication
- Definition Classes
- Matchers
- def allOf(firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfAllOfApplication
- Definition Classes
- Matchers
- def an[T](implicit arg0: ClassTag[T]): ResultOfAnTypeInvocation[T]
- Definition Classes
- Matchers
- val an: AnWord
- Definition Classes
- Matchers
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- macro def assert(condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: Position): Assertion
- Definition Classes
- Assertions
- macro def assert(condition: Boolean)(implicit prettifier: Prettifier, pos: Position): Assertion
- Definition Classes
- Assertions
- macro def assertCompiles(code: String)(implicit pos: Position): Assertion
- Definition Classes
- Assertions
- macro def assertDoesNotCompile(code: String)(implicit pos: Position): Assertion
- Definition Classes
- Assertions
- def assertResult(expected: Any)(actual: Any)(implicit prettifier: Prettifier, pos: Position): Assertion
- Definition Classes
- Assertions
- def assertResult(expected: Any, clue: Any)(actual: Any)(implicit prettifier: Prettifier, pos: Position): Assertion
- Definition Classes
- Assertions
- def assertThrows[T <: AnyRef](f: => Any)(implicit classTag: ClassTag[T], pos: Position): Assertion
- Definition Classes
- Assertions
- macro def assertTypeError(code: String)(implicit pos: Position): Assertion
- Definition Classes
- Assertions
- macro def assume(condition: Boolean, clue: Any)(implicit prettifier: Prettifier, pos: Position): Assertion
- Definition Classes
- Assertions
- macro def assume(condition: Boolean)(implicit prettifier: Prettifier, pos: Position): Assertion
- Definition Classes
- Assertions
- def atLeast(num: Int, xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def atLeast[K, V, JMAP[k, v] <: Map[k, v]](num: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def atLeast[K, V, MAP[k, v] <: GenMap[k, v]](num: Int, xs: MAP[K, V])(implicit collecting: Collecting[(K, V), GenTraversable[(K, V)]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[(K, V)]
- Definition Classes
- Matchers
- def atLeast[E, C[_]](num: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- def atLeastOneElementOf(elements: GenTraversable[Any]): ResultOfAtLeastOneElementOfApplication
- Definition Classes
- Matchers
- def atLeastOneOf(firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfAtLeastOneOfApplication
- Definition Classes
- Matchers
- def atMost(num: Int, xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def atMost[K, V, JMAP[k, v] <: Map[k, v]](num: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def atMost[K, V, MAP[k, v] <: GenMap[k, v]](num: Int, xs: MAP[K, V])(implicit collecting: Collecting[(K, V), GenTraversable[(K, V)]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[(K, V)]
- Definition Classes
- Matchers
- def atMost[E, C[_]](num: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- def atMostOneElementOf[R](elements: GenTraversable[R]): ResultOfAtMostOneElementOfApplication
- Definition Classes
- Matchers
- def atMostOneOf(firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfAtMostOneOfApplication
- Definition Classes
- Matchers
- def batching(res0: (User, User)): Assertion
If we combine two independent requests to the same data source, Fetch will automatically batch them together into a single request.
Batching
If we combine two independent requests to the same data source, Fetch will automatically batch them together into a single request. Applicative operations like the product of two fetches help us tell the library that those fetches are independent, and thus can be batched if they use the same data source:
Both ids (1 and 2) are requested in a single query to the data source when executing the fetch.
- val be: BeWord
- Definition Classes
- MatcherWords
- val behave: BehaveWord
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- val behavior: BehaviorWord
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- def between(from: Int, upTo: Int, xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def between[K, V, JMAP[k, v] <: Map[k, v]](from: Int, upTo: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def between[E, C[_]](from: Int, upTo: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- def caching(res0: (User, User)): Assertion
During the execution of a fetch, previously requested results are implicitly cached.
Caching
During the execution of a fetch, previously requested results are implicitly cached. This allows us to write fetches in a very modular way, asking for all the data they need as if it was in memory; furthermore, it also avoids re-fetching an identity that may have changed during the course of a fetch execution, which can lead to inconsistencies in the data.
val fetchCached: Fetch[(User, User)] = for { aUser <- getUser(1) anotherUser <- getUser(1) } yield (aUser, anotherUser)
As you can see, the
User
with id 1 is fetched only once in a single round-trip. The next time it was needed we used the cached versions, thus avoiding another request to the user data source. - def cancel(cause: Throwable)(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def cancel(message: String, cause: Throwable)(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def cancel(message: String)(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def cancel()(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def combiningData(res0: (Post, PostTopic)): Assertion
Now that we know about some of the optimizations that Fetch can perform to read data efficiently, let's look at how we can combine more than one data source.
Combining data from multiple sources
Now that we know about some of the optimizations that Fetch can perform to read data efficiently, let's look at how we can combine more than one data source.
Imagine that we are rendering a blog and have the following types for posts:
type PostId = Int case class Post(id: PostId, author: UserId, content: String)
As you can see, every
Post
has an author, but it refers to the author by its id. We'll implement a data source for retrieving a post given a post id.val postDatabase: Map[PostId, Post] = Map( 1 -> Post(1, 2, "An article"), 2 -> Post(2, 3, "Another article"), 3 -> Post(3, 4, "Yet another article") ) object Posts extends Data[PostId, Post] { def name = "Posts" def source[F[_] : Concurrent]: DataSource[F, PostId, Post] = new DataSource[F, PostId, Post] { override def data = Posts override def CF = Concurrent[F] override def fetch(id: PostId): F[Option[Post]] = latency[F](s"One Post $id") >> CF.pure(postDatabase.get(id)) override def batch(ids: NonEmptyList[PostId]): F[Map[PostId, Post]] = latency[F](s"Batch Posts $ids") >> CF.pure(postDatabase.filterKeys(ids.toList.toSet).toMap) } } def getPost[F[_] : Concurrent](id: PostId): Fetch[F, Post] = Fetch(id, Posts.source)
Apart from posts, we are going to add another data source: one for post topics.
type PostTopic = String
We'll implement a data source for retrieving a post topic given a post id.
object PostTopics extends Data[Post, PostTopic] { def name = "Post Topics" def source[F[_] : Concurrent]: DataSource[F, Post, PostTopic] = new DataSource[F, Post, PostTopic] { override def data = PostTopics override def CF = Concurrent[F] override def fetch(id: Post): F[Option[PostTopic]] = { val topic = if (id.id % 2 == 0) "monad" else "applicative" latency[F](s"One Post Topic $id") >> CF.pure(Option(topic)) } override def batch(ids: NonEmptyList[Post]): F[Map[Post, PostTopic]] = { val result = ids.toList.map(id => (id, if (id.id % 2 == 0) "monad" else "applicative")).toMap latency[F](s"Batch Post Topics $ids") >> CF.pure(result) } } } def getPostTopic[F[_] : Concurrent](post: Post): Fetch[F, PostTopic] = Fetch(post, PostTopics.source)
Now that we have multiple sources let's mix them in the same fetch. In the following example, we are fetching a post given its id and then fetching its topic. This data could come from entirely different places, but Fetch makes working with heterogeneous sources of data very easy.
- val compile: CompileWord
- Definition Classes
- MatcherWords
- val contain: ContainWord
- Definition Classes
- MatcherWords
- def convertEquivalenceToAToBConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: <:<[A, B]): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- def convertEquivalenceToBToAConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: <:<[B, A]): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- implicit def convertNumericToPlusOrMinusWrapper[T](pivot: T)(implicit arg0: Numeric[T]): PlusOrMinusWrapper[T]
- Definition Classes
- Tolerance
- implicit def convertSymbolToHavePropertyMatcherGenerator(symbol: Symbol)(implicit prettifier: Prettifier, pos: Position): HavePropertyMatcherGenerator
- Definition Classes
- Matchers
- implicit def convertToAnyShouldWrapper[T](o: T)(implicit pos: Position, prettifier: Prettifier): AnyShouldWrapper[T]
- Definition Classes
- Matchers
- def convertToCheckingEqualizer[T](left: T): CheckingEqualizer[T]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- implicit def convertToEqualizer[T](left: T): Equalizer[T]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- implicit def convertToInAndIgnoreMethods(resultOfStringPassedToVerb: ResultOfStringPassedToVerb): InAndIgnoreMethods
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- implicit def convertToInAndIgnoreMethodsAfterTaggedAs(resultOfTaggedAsInvocation: ResultOfTaggedAsInvocation): InAndIgnoreMethodsAfterTaggedAs
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- implicit def convertToRegexWrapper(o: Regex): RegexWrapper
- Definition Classes
- Matchers
- implicit def convertToStringCanWrapper(o: String)(implicit position: Position): StringCanWrapperForVerb
- Definition Classes
- CanVerb
- implicit def convertToStringMustWrapperForVerb(o: String)(implicit position: Position): StringMustWrapperForVerb
- Definition Classes
- MustVerb
- implicit def convertToStringShouldWrapper(o: String)(implicit pos: Position, prettifier: Prettifier): StringShouldWrapper
- Definition Classes
- Matchers
- implicit def convertToStringShouldWrapperForVerb(o: String)(implicit position: Position): StringShouldWrapperForVerb
- Definition Classes
- ShouldVerb
- def creatingAndRunning(res0: User): Assertion
Since we’lll use
IO
from thecats-effect
library to execute our fetches, we’ll need a runtime for executing ourIO
instances.Creating a runtime
Since we’lll use
IO
from thecats-effect
library to execute our fetches, we’ll need a runtime for executing ourIO
instances. This includes aContextShift[IO]
used for running theIO
instances and aTimer[IO]
that is used for scheduling, let’s go ahead and create them, we’ll use ajava.util.concurrent.ScheduledThreadPoolExecutor
with a few threads to run our fetches.import cats.effect._ import java.util.concurrent._ import scala.concurrent.ExecutionContext import scala.concurrent.duration._ val executor = new ScheduledThreadPoolExecutor(4) val executionContext: ExecutionContext = ExecutionContext.fromExecutor(executor) implicit val timer: Timer[IO] = IO.timer(executionContext) implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)
Creating and running a fetch
We are now ready to create and run fetches. Note the distinction between Fetch creation and execution. When we are creating
Fetch
values, we are just constructing a recipe of our data dependencies.def fetchUser[F[_] : Concurrent]: Fetch[F, User] = getUser(1)
A Fetch is just a value, and in order to be able to get its value we need to run it to an IO first.
import cats.effect.IO Fetch.run[IO](fetchUser)
We can now run the IO and see its result:
- val decided: DecidedWord
- Definition Classes
- Explicitly
- def deduplication(res0: (User, User)): Assertion
If two independent requests ask for the same identity, Fetch will detect it and deduplicate the id.
Deduplication
If two independent requests ask for the same identity, Fetch will detect it and deduplicate the id. Note that when running the fetch, the identity 1 is only requested once even when it is needed by both fetches.
- def defaultEquality[A]: Equality[A]
- Definition Classes
- TripleEqualsSupport
- val defined: DefinedWord
- Definition Classes
- MatcherWords
- def definedAt[T](right: T): ResultOfDefinedAt[T]
- Definition Classes
- Matchers
- val determined: DeterminedWord
- Definition Classes
- Explicitly
- val empty: EmptyWord
- Definition Classes
- MatcherWords
- val endWith: EndWithWord
- Definition Classes
- MatcherWords
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equal(o: Null): Matcher[AnyRef]
- Definition Classes
- Matchers
- def equal[T](spread: Spread[T]): Matcher[T]
- Definition Classes
- Matchers
- def equal(right: Any): MatcherFactory1[Any, Equality]
- Definition Classes
- MatcherWords
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def every(xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def every[K, V, JMAP[k, v] <: Map[k, v]](xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def every[K, V, MAP[k, v] <: Map[k, v]](xs: MAP[K, V])(implicit collecting: Collecting[(K, V), GenTraversable[(K, V)]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[(K, V)]
- Definition Classes
- Matchers
- def every[E, C[_]](xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- def exactly(num: Int, xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def exactly[K, V, JMAP[k, v] <: Map[k, v]](num: Int, xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def exactly[K, V, MAP[k, v] <: GenMap[k, v]](num: Int, xs: MAP[K, V])(implicit collecting: Collecting[(K, V), GenTraversable[(K, V)]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[(K, V)]
- Definition Classes
- Matchers
- def exactly[E, C[_]](num: Int, xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- final def execute(testName: String, configMap: ConfigMap, color: Boolean, durations: Boolean, shortstacks: Boolean, fullstacks: Boolean, stats: Boolean): Unit
- Definition Classes
- Suite
- val exist: ExistWord
- Definition Classes
- MatcherWords
- def expectedTestCount(filter: Filter): Int
- Definition Classes
- Suite
- def fail(cause: Throwable)(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def fail(message: String, cause: Throwable)(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def fail(message: String)(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def fail()(implicit pos: Position): Nothing
- Definition Classes
- Assertions
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- val fullyMatch: FullyMatchWord
- Definition Classes
- MatcherWords
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- val have: HaveWord
- Definition Classes
- MatcherWords
- val ignore: IgnoreWord
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- def inOrder(firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfInOrderApplication
- Definition Classes
- Matchers
- def inOrderElementsOf[R](elements: GenTraversable[R]): ResultOfInOrderElementsOfApplication
- Definition Classes
- Matchers
- def inOrderOnly[T](firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfInOrderOnlyApplication
- Definition Classes
- Matchers
- val include: IncludeWord
- Definition Classes
- MatcherWords
- def info: Informer
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike → Informing
- def intercept[T <: AnyRef](f: => Any)(implicit classTag: ClassTag[T], pos: Position): T
- Definition Classes
- Assertions
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- val it: ItWord
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- val key: KeyWord
- Definition Classes
- Matchers
- val length: LengthWord
- Definition Classes
- MatcherWords
- def lowPriorityTypeCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], ev: <:<[A, B]): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- def markup: Documenter
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike → Documenting
- val matchPattern: MatchPatternWord
- Definition Classes
- MatcherWords
- def message(expectedMessage: String): ResultOfMessageWordApplication
- Definition Classes
- Matchers
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def nestedSuites: IndexedSeq[Suite]
- Definition Classes
- Suite
- def no(xs: String)(implicit collecting: Collecting[Char, String], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Char]
- Definition Classes
- Matchers
- def no[K, V, JMAP[k, v] <: Map[k, v]](xs: JMAP[K, V])(implicit collecting: Collecting[Entry[K, V], JMAP[K, V]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[Entry[K, V]]
- Definition Classes
- Matchers
- def no[E, C[_]](xs: C[E])(implicit collecting: Collecting[E, C[E]], prettifier: Prettifier, pos: Position): ResultOfCollectedAny[E]
- Definition Classes
- Matchers
- def noElementsOf(elements: GenTraversable[Any]): ResultOfNoElementsOfApplication
- Definition Classes
- Matchers
- def noException(implicit pos: Position): NoExceptionWord
- Definition Classes
- MatcherWords
- def noneOf(firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfNoneOfApplication
- Definition Classes
- Matchers
- val not: NotWord
- Definition Classes
- MatcherWords
- def note: Notifier
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike → Notifying
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def of[T](implicit ev: ClassTag[T]): ResultOfOfTypeInvocation[T]
- Definition Classes
- Matchers
- def oneElementOf(elements: GenTraversable[Any]): ResultOfOneElementOfApplication
- Definition Classes
- Matchers
- def oneOf(firstEle: Any, secondEle: Any, remainingEles: Any*)(implicit pos: Position): ResultOfOneOfApplication
- Definition Classes
- Matchers
- def only(xs: Any*)(implicit pos: Position): ResultOfOnlyApplication
- Definition Classes
- Matchers
- def pending: Assertion with PendingStatement
- Definition Classes
- Assertions
- def pendingUntilFixed(f: => Unit)(implicit pos: Position): Assertion with PendingStatement
- Definition Classes
- Assertions
- val readable: ReadableWord
- Definition Classes
- MatcherWords
- val regex: RegexWord
- Definition Classes
- Matchers
- final def registerIgnoredTest(testText: String, testTags: Tag*)(testFun: => Any)(implicit pos: Position): Unit
- Definition Classes
- AnyFlatSpecLike → TestRegistration
- final def registerTest(testText: String, testTags: Tag*)(testFun: => Any)(implicit pos: Position): Unit
- Definition Classes
- AnyFlatSpecLike → TestRegistration
- def rerunner: Option[String]
- Definition Classes
- Suite
- def run(testName: Option[String], args: Args): Status
- Definition Classes
- AnyFlatSpecLike → Suite
- def runNestedSuites(args: Args): Status
- Attributes
- protected
- Definition Classes
- Suite
- def runTest(testName: String, args: Args): Status
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike → TestSuite → Suite
- def runTests(testName: Option[String], args: Args): Status
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike → Suite
- def sequence(res0: List[User]): Assertion
Besides
flatMap
for sequencing fetches and products for running them concurrently, Fetch provides a number of other combinators.Combinators
Besides
flatMap
for sequencing fetches and products for running them concurrently, Fetch provides a number of other combinators.Sequence
Whenever we have a list of fetches of the same type and want to run them concurrently, we can use the
sequence
combinator. It takes aList[Fetch[A]]
and gives you back aFetch[List[A]]
, batching the fetches to the same data source and running fetches to different sources in parallel. Note that thesequence
combinator is more general and works not only on lists but on any type that has a Traverse instance.Since
sequence
uses applicative operations internally, the library is able to perform optimizations across all the sequenced fetches.import cats.instances.list._ import cats.syntax.traverse._
- def sequencing(res0: (User, User)): Assertion
When we have two fetches that depend on each other, we can use
flatMap
to combine them.Sequencing
When we have two fetches that depend on each other, we can use
flatMap
to combine them. The most straightforward way is to use a for comprehension.When composing fetches with
flatMap
we are telling Fetch that the second one depends on the previous one, so it isn't able to make any optimizations. When running the below fetch, we will query the user data source in two rounds: one for the user with id 1 and another for the user with id 2. - implicit val shorthandSharedTestRegistrationFunction: StringVerbBehaveLikeInvocation
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- implicit val shorthandTestRegistrationFunction: StringVerbStringInvocation
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- val size: SizeWord
- Definition Classes
- MatcherWords
- val sorted: SortedWord
- Definition Classes
- MatcherWords
- val startWith: StartWithWord
- Definition Classes
- MatcherWords
- final val succeed: Assertion
- Definition Classes
- Assertions
- def suiteId: String
- Definition Classes
- Suite
- def suiteName: String
- Definition Classes
- Suite
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def tags: Map[String, Set[String]]
- Definition Classes
- AnyFlatSpecLike → Suite
- def testDataFor(testName: String, theConfigMap: ConfigMap): TestData
- Definition Classes
- AnyFlatSpecLike → Suite
- def testNames: Set[String]
- Definition Classes
- AnyFlatSpecLike → Suite
- def the[T](implicit arg0: ClassTag[T], pos: Position): ResultOfTheTypeInvocation[T]
- Definition Classes
- Matchers
- def theSameElementsAs(xs: GenTraversable[_]): ResultOfTheSameElementsAsApplication
- Definition Classes
- Matchers
- def theSameElementsInOrderAs(xs: GenTraversable[_]): ResultOfTheSameElementsInOrderAsApplication
- Definition Classes
- Matchers
- val theSameInstanceAs: TheSameInstanceAsPhrase
- Definition Classes
- Matchers
- val they: TheyWord
- Attributes
- protected
- Definition Classes
- AnyFlatSpecLike
- def thrownBy(fun: => Any): ResultOfThrownByApplication
- Definition Classes
- Matchers
- def toString(): String
- Definition Classes
- AnyFlatSpec → AnyRef → Any
- def traverse(res0: List[User]): Assertion
Another interesting combinator is
traverse
, which is the composition ofmap
andsequence
.Traverse
Another interesting combinator is
traverse
, which is the composition ofmap
andsequence
.All the optimizations made by
sequence
still apply when usingtraverse
. - val typeCheck: TypeCheckWord
- Definition Classes
- MatcherWords
- def typeCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], ev: <:<[B, A]): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- implicit def unconstrainedEquality[A, B](implicit equalityOfA: Equality[A]): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- val value: ValueWord
- Definition Classes
- Matchers
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- def withClue[T](clue: Any)(fun: => T): T
- Definition Classes
- Assertions
- def withFixture(test: NoArgTest): Outcome
- Attributes
- protected
- Definition Classes
- TestSuite
- val writable: WritableWord
- Definition Classes
- MatcherWords
Deprecated Value Members
- def conversionCheckedConstraint[A, B](implicit equivalenceOfA: Equivalence[A], cnv: (B) => A): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- Annotations
- @deprecated
- Deprecated
(Since version 3.1.0) The conversionCheckedConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.
- def convertEquivalenceToAToBConversionConstraint[A, B](equivalenceOfB: Equivalence[B])(implicit ev: (A) => B): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- Annotations
- @deprecated
- Deprecated
(Since version 3.1.0) The convertEquivalenceToAToBConversionConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.
- def convertEquivalenceToBToAConversionConstraint[A, B](equivalenceOfA: Equivalence[A])(implicit ev: (B) => A): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- Annotations
- @deprecated
- Deprecated
(Since version 3.1.0) The convertEquivalenceToBToAConversionConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.
- def lowPriorityConversionCheckedConstraint[A, B](implicit equivalenceOfB: Equivalence[B], cnv: (A) => B): CanEqual[A, B]
- Definition Classes
- TripleEquals → TripleEqualsSupport
- Annotations
- @deprecated
- Deprecated
(Since version 3.1.0) The lowPriorityConversionCheckedConstraint method has been deprecated and will be removed in a future version of ScalaTest. It is no longer needed now that the deprecation period of ConversionCheckedTripleEquals has expired. It will not be replaced.
- final val styleName: String
- Definition Classes
- AnyFlatSpecLike → Suite
- Annotations
- @deprecated
- Deprecated
(Since version 3.1.0) The styleName lifecycle method has been deprecated and will be removed in a future version of ScalaTest with no replacement.