FoldWrite
A batch call for inputs that must be applied in order to one key's state, in a store with conditional writes: read the state once, apply the batch's inputs to it in order, in memory, and write the result once, only if the store still holds the state that was read. A failed read or write, a conflict included, retries the whole round trip.
Each input gets its own outcome, computed when it is applied, from the state at that point: an input that changes how later ones apply, such as one closing a period, does so inside the fold. An input rejected on its own merits fails no other. A store that writes the result in two steps, a commit and a projection, uses ProjectedWrite. step sees the state as read, which includes inputs that an earlier attempt already applied (a write can land and its response be lost): it should answer such an input without applying it again, so that retrying is safe (see docs/decisions/0005).
Use it as the call and the extract of Ferry.perKey:
val write = FoldWrite(id = (e: Entry) => e.id, read = store.read, step = apply, write = store.writeIf, retry = policy)
Ferry.perKey(config, (e: Entry) => e.entity)(write.call)(write.outcome)
Value parameters
- id
-
Identifies an input. Inputs of one batch with the same id are applied once, and share the first one's outcome.
- read
-
Reads a key's state, including whatever makes the write conditional, such as a version.
- retry
-
When to retry the round trip after
readorwritefailed. - step
-
Applies one input to the state: the next state and the input's outcome. It must be pure; it runs again on retry.
- write
-
Writes
afterforkeyif the store still holdsbefore, and fails otherwise. Called only when the batch changed the state (after != before).
Attributes
- Source
- FoldWrite.scala
- Graph
-
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass Any