Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add abstract streaming actor for persistence ID and sequence number
Signed-off-by: Cai Yufei <Yufei.Cai@bosch-si.com>
- Loading branch information
Showing
5 changed files
with
111 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...org/eclipse/ditto/services/utils/persistence/mongo/AbstractPersistenceStreamingActor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2017 Bosch Software Innovations GmbH. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-2.0/index.php | ||
* | ||
* Contributors: | ||
* Bosch Software Innovations GmbH - initial contribution | ||
*/ | ||
package org.eclipse.ditto.services.utils.persistence.mongo; | ||
|
||
import org.eclipse.ditto.services.models.things.commands.sudo.SudoStreamModifiedEntities; | ||
import org.eclipse.ditto.services.utils.akka.streaming.AbstractStreamingActor; | ||
|
||
import akka.NotUsed; | ||
import akka.actor.ActorRef; | ||
import akka.actor.ActorRefProvider; | ||
import akka.contrib.persistence.mongodb.DittoJavaDslMongoReadJournal; | ||
import akka.stream.javadsl.Source; | ||
|
||
/** | ||
* Actor that streams information about persisted entities modified in a time window in the past. | ||
*/ | ||
public abstract class AbstractPersistenceStreamingActor<T> | ||
extends AbstractStreamingActor<SudoStreamModifiedEntities, T> { | ||
|
||
private final ActorRefProvider actorRefProvider = getContext().system().provider(); | ||
|
||
/** | ||
* Returns the journal to query for entities modified in a time window in the past. | ||
* | ||
* @return The journal. | ||
*/ | ||
protected abstract DittoJavaDslMongoReadJournal getJournal(); | ||
|
||
/** | ||
* Transforms persistence ID and sequence number into an object to stream to the recipient actor. | ||
* | ||
* @param pid The persistence ID. | ||
* @param sequenceNumber The latest sequence number. | ||
* @return Element to stream to the recipient actor. | ||
*/ | ||
protected abstract T createElement(final String pid, final long sequenceNumber); | ||
|
||
@Override | ||
protected final Class<SudoStreamModifiedEntities> getCommandClass() { | ||
return SudoStreamModifiedEntities.class; | ||
} | ||
|
||
@Override | ||
protected final ActorRef getElementRecipient(final SudoStreamModifiedEntities command) { | ||
return actorRefProvider.resolveActorRef(command.getElementRecipient()); | ||
} | ||
|
||
@Override | ||
protected final ActorRef getStatusRecipient(final SudoStreamModifiedEntities command) { | ||
return actorRefProvider.resolveActorRef(command.getStatusRecipient()); | ||
} | ||
|
||
@Override | ||
protected final int getElementsPerSecond(final SudoStreamModifiedEntities command) { | ||
return command.getElementsPerSecond(); | ||
} | ||
|
||
@Override | ||
protected final Source<T, NotUsed> createSource(final SudoStreamModifiedEntities command) { | ||
return getJournal().sequenceNumbersOfPidsByDuration(command.getTimespan(), command.getOffset()) | ||
.map(pidWithSeqNr -> createElement(pidWithSeqNr.persistenceId(), pidWithSeqNr.sequenceNr())); | ||
} | ||
} |