This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Prism service for plugin support #160
Open
whimxiqal
wants to merge
3
commits into
prism:master
Choose a base branch
from
whimxiqal:prism-service
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/helion3/prism/api/services/PrismService.java
This file contains hidden or 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,39 @@ | ||
| package com.helion3.prism.api.services; | ||
|
|
||
| import org.spongepowered.api.command.CommandSource; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
|
|
||
| public interface PrismService { | ||
|
|
||
| /** | ||
| * Queries all events matching the conditions in the {@link Request} and restores the | ||
| * original states within every event. | ||
| * | ||
| * @param source The source requesting the rollback maneuver | ||
| * @param conditions The collection of all parameters with which to filter out logged events | ||
| * @throws Exception if something unexpected happens | ||
| */ | ||
| void rollback(@Nonnull CommandSource source, @Nonnull Request conditions) throws Exception; | ||
|
|
||
| /** | ||
| * Queries all events matching the conditions in the {@link Request} and restores the | ||
| * final states within every event. | ||
| * | ||
| * @param source The source requesting the restoration maneuver | ||
| * @param conditions The collection of all parameters with which to filter out logged events | ||
| * @throws Exception if something unexpected happens | ||
| */ | ||
| void restore(@Nonnull CommandSource source, @Nonnull Request conditions) throws Exception; | ||
|
|
||
| /** | ||
| * Queries all events matching the conditions in the {@link Request} and sends | ||
| * the command source the matching information. | ||
| * | ||
| * @param source The source requesting the information | ||
| * @param conditions The collection of all parameters with which to filter out logged events | ||
| * @throws Exception if something unexpected happens | ||
| */ | ||
| void lookup(@Nonnull CommandSource source, @Nonnull Request conditions) throws Exception; | ||
|
|
||
| } | ||
129 changes: 129 additions & 0 deletions
129
src/main/java/com/helion3/prism/api/services/PrismServiceImpl.java
This file contains hidden or 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,129 @@ | ||
| package com.helion3.prism.api.services; | ||
|
|
||
| import com.helion3.prism.api.flags.Flag; | ||
| import com.helion3.prism.api.query.ConditionGroup; | ||
| import com.helion3.prism.api.query.FieldCondition; | ||
| import com.helion3.prism.api.query.MatchRule; | ||
| import com.helion3.prism.api.query.QuerySession; | ||
| import com.helion3.prism.api.query.Sort; | ||
| import com.helion3.prism.commands.ApplierCommand; | ||
| import com.helion3.prism.util.AsyncUtil; | ||
| import com.helion3.prism.util.DataQueries; | ||
| import org.spongepowered.api.command.CommandSource; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class PrismServiceImpl implements PrismService { | ||
|
|
||
| @Override | ||
| public void rollback(@Nonnull CommandSource source, @Nonnull Request conditions) { | ||
| QuerySession session = buildSession(source, conditions); | ||
| session.addFlag(Flag.NO_GROUP); | ||
| ApplierCommand.runApplier(session, Sort.NEWEST_FIRST); | ||
| } | ||
|
|
||
| @Override | ||
| public void restore(@Nonnull CommandSource source, @Nonnull Request conditions) { | ||
| QuerySession session = buildSession(source, conditions); | ||
| session.addFlag(Flag.NO_GROUP); | ||
| ApplierCommand.runApplier(session, Sort.OLDEST_FIRST); | ||
| } | ||
|
|
||
| @Override | ||
| public void lookup(@Nonnull CommandSource source, @Nonnull Request conditions) { | ||
| AsyncUtil.lookup(buildSession(source, conditions)); | ||
| } | ||
|
|
||
| private QuerySession buildSession(CommandSource source, Request conditions) { | ||
| final QuerySession session = new QuerySession(source); | ||
| com.helion3.prism.api.query.Query query = session.newQuery(); | ||
|
|
||
| ConditionGroup eventConditionGroup = new ConditionGroup(ConditionGroup.Operator.OR); | ||
| conditions.getEvents().forEach(event -> | ||
| eventConditionGroup.add(FieldCondition.of( | ||
| DataQueries.EventName, | ||
| MatchRule.EQUALS, | ||
| event.getId()))); | ||
| if (!eventConditionGroup.getConditions().isEmpty()) { | ||
| query.addCondition(eventConditionGroup); | ||
| } | ||
|
|
||
| ConditionGroup targetConditionGroup = new ConditionGroup(ConditionGroup.Operator.OR); | ||
| conditions.getTargets().forEach(target -> | ||
| targetConditionGroup.add(FieldCondition.of( | ||
| DataQueries.Target, | ||
| MatchRule.EQUALS, | ||
| Pattern.compile(target.replace("_", " "))))); | ||
| if (!targetConditionGroup.getConditions().isEmpty()) { | ||
| query.addCondition(targetConditionGroup); | ||
| } | ||
|
|
||
| ConditionGroup playerConditionGroup = new ConditionGroup(ConditionGroup.Operator.OR); | ||
| conditions.getPlayerUuids().forEach(uuid -> | ||
| playerConditionGroup.add(FieldCondition.of( | ||
| DataQueries.Player, | ||
| MatchRule.EQUALS, | ||
| uuid.toString()))); | ||
| if (!playerConditionGroup.getConditions().isEmpty()) { | ||
| query.addCondition(playerConditionGroup); | ||
| } | ||
|
|
||
| ConditionGroup worldConditionGroup = new ConditionGroup(ConditionGroup.Operator.OR); | ||
| conditions.getWorldUuids().forEach(uuid -> | ||
| worldConditionGroup.add(FieldCondition.of( | ||
| DataQueries.Location.then(DataQueries.WorldUuid), | ||
| MatchRule.EQUALS, | ||
| uuid.toString()))); | ||
| if (!worldConditionGroup.getConditions().isEmpty()) { | ||
| query.addCondition(worldConditionGroup); | ||
| } | ||
|
|
||
| conditions.getxRange().ifPresent(range -> { | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.X, | ||
| MatchRule.GREATER_THAN_EQUAL, | ||
| range.lowerEndpoint())); | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.X, | ||
| MatchRule.LESS_THAN_EQUAL, | ||
| range.upperEndpoint())); | ||
| }); | ||
| conditions.getyRange().ifPresent(range -> { | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.Y, | ||
| MatchRule.GREATER_THAN_EQUAL, | ||
| range.lowerEndpoint())); | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.Y, | ||
| MatchRule.LESS_THAN_EQUAL, | ||
| range.upperEndpoint())); | ||
| }); | ||
| conditions.getzRange().ifPresent(range -> { | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.Z, | ||
| MatchRule.GREATER_THAN_EQUAL, | ||
| range.lowerEndpoint())); | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.Z, | ||
| MatchRule.LESS_THAN_EQUAL, | ||
| range.upperEndpoint())); | ||
| }); | ||
| conditions.getEarliest().ifPresent(earliest -> | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.Created, | ||
| MatchRule.GREATER_THAN_EQUAL, | ||
| earliest))); | ||
| conditions.getLatest().ifPresent(latest -> | ||
| query.addCondition(FieldCondition.of( | ||
| DataQueries.Created, | ||
| MatchRule.LESS_THAN_EQUAL, | ||
| latest))); | ||
| conditions.getFlags() | ||
| .stream() | ||
| .filter(o -> o instanceof Flag) | ||
| .map(o -> (Flag) o) | ||
| .forEach(session::addFlag); | ||
| return session; | ||
| } | ||
| } |
203 changes: 203 additions & 0 deletions
203
src/main/java/com/helion3/prism/api/services/Request.java
This file contains hidden or 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,203 @@ | ||
| package com.helion3.prism.api.services; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.collect.Range; | ||
| import com.google.common.collect.Sets; | ||
| import com.helion3.prism.api.data.PrismEvent; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import java.io.Serializable; | ||
| import java.util.Date; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * A store for all filtering information necessary to locate logged events. | ||
| */ | ||
| public final class Request implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = 4369983541428028962L; | ||
|
|
||
| /** | ||
| * Create a {@link Request.Builder} to construct a {@link Request} to use in a {@link PrismService}. | ||
| * | ||
| * @return a builder | ||
| */ | ||
| @Nonnull | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| private final Set<PrismEvent> events; | ||
| private final Set<String> targets; | ||
| private final Set<UUID> playerUuids; | ||
| private final Set<UUID> worldUuids; | ||
| private final Range<Integer> xRange; | ||
| private final Range<Integer> yRange; | ||
| private final Range<Integer> zRange; | ||
| private final Date earliest; | ||
| private final Date latest; | ||
|
|
||
| private final Set<Object> flags; | ||
|
|
||
| private Request(@Nonnull Set<PrismEvent> events, | ||
| @Nonnull Set<String> targets, | ||
| @Nonnull Set<UUID> playerUuids, | ||
| @Nonnull Set<UUID> worldUuids, | ||
| Range<Integer> xRange, | ||
| Range<Integer> yRange, | ||
| Range<Integer> zRange, | ||
| Date earliest, | ||
| Date latest, | ||
| @Nonnull Set<Object> flags) { | ||
| this.events = events; | ||
| this.targets = targets; | ||
| this.playerUuids = playerUuids; | ||
| this.worldUuids = worldUuids; | ||
| this.xRange = xRange; | ||
| this.yRange = yRange; | ||
| this.zRange = zRange; | ||
| this.earliest = earliest; | ||
| this.latest = latest; | ||
| this.flags = flags; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Set<PrismEvent> getEvents() { | ||
| return events; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Set<String> getTargets() { | ||
| return targets; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Set<UUID> getPlayerUuids() { | ||
| return playerUuids; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Set<UUID> getWorldUuids() { | ||
| return worldUuids; | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Optional<Range<Integer>> getxRange() { | ||
| return Optional.ofNullable(xRange); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Optional<Range<Integer>> getyRange() { | ||
| return Optional.ofNullable(yRange); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Optional<Range<Integer>> getzRange() { | ||
| return Optional.ofNullable(zRange); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Optional<Date> getEarliest() { | ||
| return Optional.ofNullable(earliest); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Optional<Date> getLatest() { | ||
| return Optional.ofNullable(latest); | ||
| } | ||
|
|
||
| @Nonnull | ||
| public Set<Object> getFlags() { | ||
| return flags; | ||
| } | ||
|
|
||
| public static class Builder { | ||
|
|
||
| private Set<PrismEvent> events = Sets.newHashSet(); | ||
| private Set<String> targets = Sets.newHashSet(); | ||
| private Set<UUID> playerUuids = Sets.newHashSet(); | ||
| private Set<UUID> worldUuids = Sets.newHashSet(); | ||
| private Range<Integer> xRange = null; | ||
| private Range<Integer> yRange = null; | ||
| private Range<Integer> zRange = null; | ||
| private Date earliest = null; | ||
| private Date latest = null; | ||
| private Set<Object> flags = Sets.newHashSet(); | ||
|
|
||
| private Builder() { | ||
| } | ||
|
|
||
| public Request build() { | ||
| return new Request(events, targets, playerUuids, worldUuids, xRange, yRange, zRange, earliest, latest, flags); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder addEvent(@Nonnull PrismEvent event) { | ||
| Preconditions.checkNotNull(event); | ||
| this.events.add(event); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder addTarget(@Nonnull String target) { | ||
| Preconditions.checkNotNull(target); | ||
| this.targets.add(target); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder addPlayerUuid(@Nonnull UUID playerUuid) { | ||
| Preconditions.checkNotNull(playerUuid); | ||
| this.playerUuids.add(playerUuid); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder addWorldUuid(@Nonnull UUID worldUuid) { | ||
| Preconditions.checkNotNull(worldUuid); | ||
| this.worldUuids.add(worldUuid); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder setxRange(int lower, int upper) { | ||
| this.xRange = Range.closed(lower, upper); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder setyRange(int lower, int upper) { | ||
| this.yRange = Range.closed(lower, upper); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder setzRange(int lower, int upper) { | ||
| this.zRange = Range.closed(lower, upper); | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder setEarliest(Date earliest) { | ||
| this.earliest = earliest; | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder setLatest(Date latest) { | ||
| this.latest = latest; | ||
| return this; | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public Builder addFlag(@Nonnull Object flag) { | ||
| Preconditions.checkNotNull(flag); | ||
| this.flags.add(flag); | ||
| return this; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@viveleroi Do you want more methods listed in this Service? What would the be the optimal API to expose to developers? These are just the ones I need for my plugin.