Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve MapRef docs #3829

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/std/mapref.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
id: mapref
title: MapRef
---

A total map from a key to a `Ref` of its value.

```scala mdoc:silent
trait MapRef[F[_], K, V] {

/**
* Access the reference for this Key
*/
def apply(k: K): Ref[F, V]
}
```

It is conceptually similar to a `Ref[F, Map[K, V]]`,
but with better ergonomics when working on a per key basis.
Note, however, that it does not support atomic updates to multiple keys.

Additionally, some implementations also provide less contention,
since all operations are performed over individual key-value pairs,
those could be sharded.
Thus, multiple concurrent updates may be executed independently to each other;
as long as their keys belong to different shards.
BalmungSan marked this conversation as resolved.
Show resolved Hide resolved

### In-Memory database

This is probably one of the most common uses of this tool.
BalmungSan marked this conversation as resolved.
Show resolved Hide resolved

```scala mdoc:reset:silent
//> using lib "org.typelevel::cats-effect:3.4.11"
BalmungSan marked this conversation as resolved.
Show resolved Hide resolved

import cats.effect.IO
import cats.effect.std.MapRef

type Id
type Data

trait DatabaseClient[F[_]] {
def getDataById(id: Id): F[Option[Data]]
def upsertData(id: Id, data: Data): F[Unit]
}

object DatabaseClient {
def inMemory: IO[DatabaseClient[IO]] =
MapRef.ofShardedImmutableMap[IO, Id, Data](
shardCount = 5 // Arbitrary number of shards just for demonstration.
).map { mapRef =>
new DatabaseClient[IO] {
override def getDataById(id: Id): IO[Option[Data]] =
mapRef(id).get

override def upsertData(id: Id, data: Data): IO[Unit] =
mapRef(id).update(_ => Some(data))
}
}
}
```
6 changes: 5 additions & 1 deletion kernel/shared/src/main/scala/cats/effect/kernel/Ref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import cats.syntax.all._
* mutable data as `AtomicReference#compareAndSet` and friends are dependent upon object
* reference equality.
*
* See also `cats.effect.std.AtomicCell` class from `cats-effect-std` for an alternative.
* See also `cats.effect.std.AtomicCell` class from `cats-effect-std` for an alternative that
* ensures exclusive access and effectual updates.
*
* If your contents are an immutable `Map[K, V]`, and all your operations are per-key, consider
* using `cats.effect.std.MapRef`.
*/
abstract class Ref[F[_], A] extends RefSource[F, A] with RefSink[F, A] {

Expand Down
11 changes: 9 additions & 2 deletions std/shared/src/main/scala/cats/effect/std/MapRef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean

/**
* This is a total map from K to Ref[F, V]. This allows us to use the Ref API backed by a
* ConcurrentHashMap or similar.
* This is a total map from `K` to `Ref[F, V]`.
*
* It is conceptually similar to a `Ref[F, Map[K, V]]`, but with better ergonomics when working
* on a per key basis. Note, however, that it does not support atomic updates to multiple keys.
*
* Additionally, some implementations also provide less contention, since all operations are
* performed over individual key-value pairs, those could be sharded. Thus, multiple concurrent
* updates may be executed independently to each other; as long as their keys belong to
* different shards.
*/
trait MapRef[F[_], K, V] extends Function1[K, Ref[F, V]] {

Expand Down