Persistence: replace initialState with emptyState (#25129)

This commit is contained in:
Richard Imaoka 2018-05-25 21:29:22 +09:00 committed by Arnout Engelen
parent 8eb7b1ea81
commit 84d53d1ad1
19 changed files with 37 additions and 37 deletions

View file

@ -45,7 +45,7 @@ object ClusterShardingPersistenceSpec {
def persistentActor(entityId: String): Behavior[Command] =
PersistentBehaviors.receive[Command, String, String](
entityId,
initialState = "",
emptyState = "",
commandHandler = (_, state, cmd) cmd match {
case Add(s) Effect.persist(s)
case Get(replyTo)

View file

@ -36,7 +36,7 @@ object ClusterSingletonPersistenceSpec {
val persistentActor: Behavior[Command] =
PersistentBehaviors.receive[Command, String, String](
persistenceId = "TheSingleton",
initialState = "",
emptyState = "",
commandHandler = (_, state, cmd) cmd match {
case Add(s) Effect.persist(s)
case Get(replyTo)

View file

@ -42,7 +42,7 @@ The event and state are only used internally.
The components that make up a PersistentBehavior are:
* `persistenceId` is the unique identifier for the persistent actor.
* `initialState` defines the `State` when the entity is first created e.g. a Counter would start with 0 as state.
* `emptyState` defines the `State` when the entity is first created e.g. a Counter would start with 0 as state.
* `commandHandler` defines how to handle command, resulting in Effects e.g. persisting events, stopping the persistent actor.
* `eventHandler` updates the current state when an event has been persisted.

View file

@ -20,7 +20,7 @@ import akka.persistence.typed.internal.EventsourcedBehavior._
*
* In this behavior the recovery process is initiated.
* We try to obtain a snapshot from the configured snapshot store,
* and if it exists, we use it instead of the `initialState`.
* and if it exists, we use it instead of the initial `emptyState`.
*
* Once snapshot recovery is done (or no snapshot was selected),
* recovery of events continues in [[EventsourcedReplayingEvents]].
@ -96,7 +96,7 @@ private[akka] class EventsourcedReplayingSnapshot[C, E, S](override val setup: E
def onSnapshotterResponse(response: SnapshotProtocol.Response): Behavior[InternalProtocol] = {
response match {
case LoadSnapshotResult(sso, toSnr)
var state: S = setup.initialState
var state: S = setup.emptyState
val seqNr: Long = sso match {
case Some(SelectedSnapshot(metadata, snapshot))

View file

@ -27,7 +27,7 @@ private[persistence] final class EventsourcedSetup[C, E, S](
val context: ActorContext[InternalProtocol],
val timers: TimerScheduler[InternalProtocol],
val persistenceId: String,
val initialState: S,
val emptyState: S,
val commandHandler: PersistentBehaviors.CommandHandler[C, E, S],
val eventHandler: (S, E) S,
val writerIdentity: WriterIdentity,

View file

@ -33,7 +33,7 @@ private[akka] object PersistentBehaviorImpl {
@InternalApi
private[akka] final case class PersistentBehaviorImpl[Command, Event, State](
persistenceId: String,
initialState: State,
emptyState: State,
commandHandler: PersistentBehaviors.CommandHandler[Command, Event, State],
eventHandler: (State, Event) State,
journalPluginId: Option[String] = None,
@ -59,7 +59,7 @@ private[akka] final case class PersistentBehaviorImpl[Command, Event, State](
ctx,
timers,
persistenceId,
initialState,
emptyState,
commandHandler,
eventHandler,
WriterIdentity.newIdentity(),

View file

@ -29,12 +29,12 @@ abstract class PersistentBehavior[Command, Event, State >: Null](val persistence
protected final def Effect: EffectFactories[Command, Event, State] = EffectFactory.asInstanceOf[EffectFactories[Command, Event, State]]
/**
* Implement by returning the initial state object.
* Implement by returning the initial empty state object.
* This object will be passed into this behaviors handlers, until a new state replaces it.
*
* Also known as "zero state" or "neutral state".
*/
protected def initialState: State
protected def emptyState: State
/**
* Implement by handling incoming commands and return an `Effect()` to persist or signal other effects
@ -147,7 +147,7 @@ abstract class PersistentBehavior[Command, Event, State >: Null](val persistence
scaladsl.PersistentBehaviors.receive[Command, Event, State](
persistenceId,
initialState,
emptyState,
(c, state, cmd) commandHandler()(c.asJava, state, cmd).asInstanceOf[EffectImpl[Event, State]],
eventHandler()(_, _))
.onRecoveryCompleted((ctx, state) onRecoveryCompleted(ctx.asJava, state))

View file

@ -24,10 +24,10 @@ object PersistentBehaviors {
*/
def receive[Command, Event, State](
persistenceId: String,
initialState: State,
emptyState: State,
commandHandler: CommandHandler[Command, Event, State],
eventHandler: (State, Event) State): PersistentBehavior[Command, Event, State] =
PersistentBehaviorImpl(persistenceId, initialState, commandHandler, eventHandler)
PersistentBehaviorImpl(persistenceId, emptyState, commandHandler, eventHandler)
/**
* The `CommandHandler` defines how to act on commands.

View file

@ -89,7 +89,7 @@ public class PersistentActorCompileOnlyTest {
//#behavior
public static PersistentBehavior<SimpleCommand, SimpleEvent, SimpleState> pb = new PersistentBehavior<SimpleCommand, SimpleEvent, SimpleState>("p1") {
@Override
public SimpleState initialState() {
public SimpleState emptyState() {
return new SimpleState();
}
@ -150,7 +150,7 @@ public class PersistentActorCompileOnlyTest {
private PersistentBehavior<MyCommand, MyEvent, ExampleState> pa = new PersistentBehavior<MyCommand, MyEvent, ExampleState>("pa") {
@Override
public ExampleState initialState() {
public ExampleState emptyState() {
return new ExampleState();
}
@ -259,7 +259,7 @@ public class PersistentActorCompileOnlyTest {
}
@Override
public EventsInFlight initialState() {
public EventsInFlight emptyState() {
return new EventsInFlight(0, Collections.emptyMap());
}

View file

@ -299,7 +299,7 @@ public class PersistentActorJavaDslTest extends JUnitSuite {
}
@Override
public State initialState() {
public State emptyState() {
return emptyState;
}

View file

@ -29,7 +29,7 @@ public class BasicPersistentBehaviorsTest {
}
@Override
public State initialState() {
public State emptyState() {
return new State();
}

View file

@ -170,7 +170,7 @@ public class InDepthPersistentBehaviorTest {
}
@Override
public BlogState initialState() {
public BlogState emptyState() {
return new BlogState(Optional.empty(), false);
}

View file

@ -29,7 +29,7 @@ object ManyRecoveriesSpec {
latch: Option[TestLatch]): PersistentBehavior[Cmd, Evt, String] =
PersistentBehaviors.receive[Cmd, Evt, String](
persistenceId = name,
initialState = "",
emptyState = "",
commandHandler = CommandHandler.command {
case Cmd(s) Effect.persist(Evt(s)).andThen(_ probe.ref ! s"$name-$s")
},

View file

@ -44,7 +44,7 @@ object RecoveryPermitterSpec {
throwOnRecovery: Boolean = false): Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State](
persistenceId = name,
initialState = EmptyState,
emptyState = EmptyState,
commandHandler = CommandHandler.command {
case StopActor Effect.stop
case command commandProbe.ref ! command; Effect.none

View file

@ -29,7 +29,7 @@ object OptionalSnapshotStoreSpec {
name: String = UUID.randomUUID().toString) =
PersistentBehaviors.receive[Command, Event, State](
persistenceId = name,
initialState = State(),
emptyState = State(),
commandHandler = CommandHandler.command {
_ Effect.persist(Event()).andThen(probe.ref ! _)
},

View file

@ -45,7 +45,7 @@ object PersistentActorCompileOnlyTest {
val simpleBehavior: PersistentBehavior[SimpleCommand, SimpleEvent, ExampleState] =
PersistentBehaviors.receive[SimpleCommand, SimpleEvent, ExampleState](
persistenceId = "sample-id-1",
initialState = ExampleState(Nil),
emptyState = ExampleState(Nil),
commandHandler = commandHandler,
eventHandler = eventHandler)
//#behavior
@ -66,7 +66,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[MyCommand, MyEvent, ExampleState](
persistenceId = "sample-id-1",
initialState = ExampleState(Nil),
emptyState = ExampleState(Nil),
commandHandler = CommandHandler.command {
case Cmd(data, sender)
@ -110,7 +110,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, EventsInFlight](
persistenceId = "recovery-complete-id",
initialState = EventsInFlight(0, Map.empty),
emptyState = EventsInFlight(0, Map.empty),
commandHandler = (ctx: ActorContext[Command], state, cmd) cmd match {
case DoSideEffect(data)
@ -151,7 +151,7 @@ object PersistentActorCompileOnlyTest {
val b: Behavior[Command] = PersistentBehaviors.receive[Command, Event, Mood](
persistenceId = "myPersistenceId",
initialState = Happy,
emptyState = Happy,
commandHandler = CommandHandler.byState {
case Happy CommandHandler.command {
case Greet(whom)
@ -192,7 +192,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "asdf",
initialState = State(Nil),
emptyState = State(Nil),
commandHandler = CommandHandler.command {
case RegisterTask(task) Effect.persist(TaskRegistered(task))
case TaskDone(task) Effect.persist(TaskRemoved(task))
@ -219,7 +219,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "asdf",
initialState = State(Nil),
emptyState = State(Nil),
commandHandler = (ctx, _, cmd) cmd match {
case RegisterTask(task)
Effect.persist(TaskRegistered(task))
@ -282,7 +282,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, List[Id]](
persistenceId = "basket-1",
initialState = Nil,
emptyState = Nil,
commandHandler =
CommandHandler.byState(state
if (isFullyHydrated(basket, state)) (ctx, state, cmd)
@ -343,7 +343,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, Mood](
persistenceId = "myPersistenceId",
initialState = Sad,
emptyState = Sad,
commandHandler = (_, state, cmd)
cmd match {
case Greet(whom)
@ -379,7 +379,7 @@ object PersistentActorCompileOnlyTest {
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "myPersistenceId",
initialState = new State,
emptyState = new State,
commandHandler = CommandHandler.command {
case Enough
Effect.persist(Done)

View file

@ -109,7 +109,7 @@ object PersistentBehaviorSpec {
snapshotProbe: ActorRef[Try[Done]]): PersistentBehavior[Command, Event, State] = {
PersistentBehaviors.receive[Command, Event, State](
persistenceId,
initialState = State(0, Vector.empty),
emptyState = State(0, Vector.empty),
commandHandler = (ctx, state, cmd) cmd match {
case Increment
Effect.persist(Incremented(1))

View file

@ -18,7 +18,7 @@ object BasicPersistentBehaviorsCompileOnly {
val behavior: Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
emptyState = State(),
commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???)
//#structure
@ -27,7 +27,7 @@ object BasicPersistentBehaviorsCompileOnly {
val recoveryBehavior: Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
emptyState = State(),
commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???)
.onRecoveryCompleted { (ctx, state)
@ -39,7 +39,7 @@ object BasicPersistentBehaviorsCompileOnly {
val taggingBehavior: Behavior[Command] =
PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
emptyState = State(),
commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???
).withTagger(_ Set("tag1", "tag2"))
@ -49,7 +49,7 @@ object BasicPersistentBehaviorsCompileOnly {
//#wrapPersistentBehavior
val samplePersistentBehavior = PersistentBehaviors.receive[Command, Event, State](
persistenceId = "abc",
initialState = State(),
emptyState = State(),
commandHandler = (ctx, state, cmd) ???,
eventHandler = (state, evt) ???)
.onRecoveryCompleted { (ctx, state)

View file

@ -121,7 +121,7 @@ object InDepthPersistentBehaviorSpec {
def behavior(entityId: String): Behavior[BlogCommand] =
PersistentBehaviors.receive[BlogCommand, BlogEvent, BlogState](
persistenceId = "Blog-" + entityId,
initialState = BlogState.empty,
emptyState = BlogState.empty,
commandHandler,
eventHandler)
//#behavior