Skip to content

Commit

Permalink
Refacto event bus to allow topic use (#3)
Browse files Browse the repository at this point in the history
* Refacto event bus to allow topic use

Signed-off-by: Paul Bui-Quang <[email protected]>
  • Loading branch information
pl-buiquang authored and geofjamg committed Jan 5, 2020
1 parent db43442 commit 12c386f
Show file tree
Hide file tree
Showing 43 changed files with 204 additions and 217 deletions.
1 change: 1 addition & 0 deletions afs-cassandra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.cassandraunit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ private NodeInfo createNode(UUID nodeUuid, UUID parentNodeUuid, String name, Str
.value(CMI, genericMetadata.getInts())
.value(CMB, genericMetadata.getBooleans()));
}
pushEvent(new NodeCreated(nodeUuid.toString(), parentNodeUuid != null ? parentNodeUuid.toString() : null), NODE_CREATED);
pushEvent(new NodeCreated(nodeUuid.toString(), parentNodeUuid != null ? parentNodeUuid.toString() : null), APPSTORAGE_NODE_TOPIC);
return new NodeInfo(nodeUuid.toString(), name, nodePseudoClass, description, creationTime, creationTime, version, genericMetadata);
}

Expand Down Expand Up @@ -499,7 +499,7 @@ public void setMetadata(String nodeId, NodeGenericMetadata genericMetadata) {
}

getSession().execute(batchStatement);
pushEvent(new NodeMetadataUpdated(nodeId, newMetadata), NODE_DESCRIPTION_UPDATED);
pushEvent(new NodeMetadataUpdated(nodeId, newMetadata), APPSTORAGE_NODE_TOPIC);
}

@Override
Expand Down Expand Up @@ -536,7 +536,7 @@ public void renameNode(String nodeId, String name) {
getSession().execute(update(CHILDREN_BY_NAME_AND_CLASS).with(set(NAME, name))
.where(eq(ID, nodeUuid)));
});
pushEvent(new NodeNameUpdated(nodeId, name), NODE_NAME_UPDATED);
pushEvent(new NodeNameUpdated(nodeId, name), APPSTORAGE_NODE_TOPIC);
}

private static UUID checkNodeId(String nodeId) {
Expand Down Expand Up @@ -597,15 +597,15 @@ private void setAttribute(String nodeId, String attributeName, String childAttri
@Override
public void setDescription(String nodeId, String description) {
setAttribute(nodeId, DESCRIPTION, CHILD_DESCRIPTION, description);
pushEvent(new NodeDescriptionUpdated(nodeId, description), NODE_DESCRIPTION_UPDATED);
pushEvent(new NodeDescriptionUpdated(nodeId, description), APPSTORAGE_NODE_TOPIC);
}

@Override
public void setConsistent(String nodeId) {
// flush buffer to keep change order
changeBuffer.flush();
setAttribute(nodeId, CONSISTENT, CHILD_CONSISTENT, true);
pushEvent(new NodeConsistent(nodeId), NODE_CONSISTENT);
pushEvent(new NodeConsistent(nodeId), APPSTORAGE_NODE_TOPIC);
}

@Override
Expand Down Expand Up @@ -760,14 +760,14 @@ public void setParentNode(String nodeId, String newParentNodeId) {
.value(CMB, nodeInfo.getGenericMetadata().getBooleans()));
getSession().execute(batchStatement);

pushEvent(new ParentChanged(nodeId), PARENT_CHANGED);
pushEvent(new ParentChanged(nodeId), APPSTORAGE_NODE_TOPIC);
}

@Override
public String deleteNode(String nodeId) {
UUID nodeUuid = checkNodeId(nodeId);
UUID parentNodeUuid = deleteNode(nodeUuid);
pushEvent(new NodeRemoved(nodeId, String.valueOf(parentNodeUuid)), NODE_REMOVED);
pushEvent(new NodeRemoved(nodeId, String.valueOf(parentNodeUuid)), APPSTORAGE_NODE_TOPIC);
return parentNodeUuid != null ? parentNodeUuid.toString() : null;
}

Expand Down Expand Up @@ -998,8 +998,7 @@ public OutputStream writeBinaryData(String nodeId, String name) {
Objects.requireNonNull(name);
// flush buffer to keep change order
changeBuffer.flush();
pushEvent(new NodeDataUpdated(nodeId, name),
String.valueOf(NODE_DATA_UPDATED));
pushEvent(new NodeDataUpdated(nodeId, name), APPSTORAGE_NODE_TOPIC);
return new BinaryDataOutputStream(nodeUuid, name);
}

Expand Down Expand Up @@ -1041,8 +1040,7 @@ private void removeData(UUID nodeUuid, String name, List<Statement> statements)
.from(NODE_DATA_NAMES)
.where(eq(ID, nodeUuid))
.and(eq(NAME, name)));
pushEvent(new NodeDataRemoved(nodeUuid.toString(), name),
String.valueOf(NODE_DATA_REMOVED));
pushEvent(new NodeDataRemoved(nodeUuid.toString(), name), APPSTORAGE_NODE_TOPIC);
}

private void removeAllData(UUID nodeUuid, List<Statement> statements) {
Expand Down Expand Up @@ -1082,7 +1080,7 @@ public boolean removeData(String nodeId, String name) {
public void createTimeSeries(String nodeId, TimeSeriesMetadata metadata) {
changeBuffer.createTimeSeries(nodeId, metadata);
pushEvent(new TimeSeriesCreated(nodeId, metadata.getName()),
String.valueOf(TIME_SERIES_CREATED));
String.valueOf(APPSTORAGE_TIMESERIES_TOPIC));
}

@Override
Expand Down Expand Up @@ -1248,15 +1246,13 @@ public Map<String, List<StringDataChunk>> getStringTimeSeriesData(String nodeId,
@Override
public void addDoubleTimeSeriesData(String nodeId, int version, String timeSeriesName, List<DoubleDataChunk> chunks) {
changeBuffer.addDoubleTimeSeriesData(nodeId, version, timeSeriesName, chunks);
pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName),
String.valueOf(TIME_SERIES_DATA_UPDATED));
pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), APPSTORAGE_TIMESERIES_TOPIC);
}

@Override
public void addStringTimeSeriesData(String nodeId, int version, String timeSeriesName, List<StringDataChunk> chunks) {
changeBuffer.addStringTimeSeriesData(nodeId, version, timeSeriesName, chunks);
pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName),
String.valueOf(TIME_SERIES_DATA_UPDATED));
pushEvent(new TimeSeriesDataUpdated(nodeId, timeSeriesName), APPSTORAGE_TIMESERIES_TOPIC);
}

private void clearTimeSeries(UUID nodeUuid, List<Statement> statements) {
Expand Down Expand Up @@ -1308,8 +1304,7 @@ public void clearTimeSeries(String nodeId) {
for (Statement statement : statements) {
getSession().execute(statement);
}
pushEvent(new TimeSeriesCleared(nodeUuid.toString()),
String.valueOf(TIME_SERIES_CLEARED));
pushEvent(new TimeSeriesCleared(nodeUuid.toString()), APPSTORAGE_TIMESERIES_TOPIC);
}

@Override
Expand All @@ -1332,9 +1327,9 @@ public void addDependency(String nodeId, String name, String toNodeId) {
.value(FROM_ID, nodeUuid));
getSession().execute(batchStatement);
pushEvent(new DependencyAdded(nodeId, name),
String.valueOf(DEPENDENCY_ADDED));
String.valueOf(APPSTORAGE_DEPENDENCY_TOPIC));
pushEvent(new BackwardDependencyAdded(toNodeId, name),
String.valueOf(BACKWARD_DEPENDENCY_ADDED));
String.valueOf(APPSTORAGE_DEPENDENCY_TOPIC));
}

private List<UUID> getDependencyNodeUuids(UUID nodeUuid) {
Expand Down Expand Up @@ -1423,8 +1418,8 @@ public void removeDependency(String nodeId, String name, String toNodeId) {
.and(eq(FROM_ID, nodeUuid)));

getSession().execute(batchStatement);
pushEvent(new DependencyRemoved(nodeId, name), DEPENDENCY_REMOVED);
pushEvent(new BackwardDependencyRemoved(toNodeId, name), BACKWARD_DEPENDENCY_REMOVED);
pushEvent(new DependencyRemoved(nodeId, name), APPSTORAGE_DEPENDENCY_TOPIC);
pushEvent(new BackwardDependencyRemoved(toNodeId, name), APPSTORAGE_DEPENDENCY_TOPIC);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions afs-core/src/main/java/com/powsybl/afs/AppFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.powsybl.afs.storage.AppStorage;
import com.powsybl.afs.storage.EventsBus;
import com.powsybl.afs.storage.NodeInfo;

import java.util.Objects;
Expand Down Expand Up @@ -73,6 +74,10 @@ AppStorage getStorage() {
return storage;
}

public EventsBus getEventBus() {
return storage.getEventsBus();
}

public Folder getRootFolder() {
return new Folder(new FileCreationContext(rootNodeInfo.get(), storage, this));
}
Expand Down
17 changes: 5 additions & 12 deletions afs-core/src/main/java/com/powsybl/afs/ProjectFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
package com.powsybl.afs;

import com.powsybl.afs.storage.NodeInfo;
import com.powsybl.afs.storage.events.AppStorageListener;
import com.powsybl.afs.storage.events.DependencyEvent;
import com.powsybl.afs.storage.events.NodeEvent;
import com.powsybl.afs.storage.events.*;
import com.powsybl.commons.util.WeakListenerList;

import java.util.List;
Expand All @@ -23,24 +21,19 @@
*/
public class ProjectFile extends ProjectNode {

private static final String DEPENDENCY_ADDED = "DEPENDENCY_ADDED";
private static final String DEPENDENCY_REMOVED = "DEPENDENCY_REMOVED";
private static final String BACKWARD_DEPENDENCY_ADDED = "BACKWARD_DEPENDENCY_ADDED";
private static final String BACKWARD_DEPENDENCY_REMOVED = "BACKWARD_DEPENDENCY_REMOVED";

private final WeakListenerList<ProjectFileListener> listeners = new WeakListenerList<>();

private final AppStorageListener l = eventList -> {
for (NodeEvent event : eventList.getEvents()) {
if (event.getId().equals(getId())) {
switch (event.getType()) {
case DEPENDENCY_ADDED:
case DEPENDENCY_REMOVED:
case DependencyAdded.TYPENAME:
case DependencyRemoved.TYPENAME:
listeners.notify(listener -> listener.dependencyChanged(((DependencyEvent) event).getDependencyName()));
break;

case BACKWARD_DEPENDENCY_ADDED:
case BACKWARD_DEPENDENCY_REMOVED:
case BackwardDependencyAdded.TYPENAME:
case BackwardDependencyRemoved.TYPENAME:
listeners.notify(listener -> listener.backwardDependencyChanged(((DependencyEvent) event).getDependencyName()));
break;

Expand Down
6 changes: 2 additions & 4 deletions afs-core/src/main/java/com/powsybl/afs/ProjectFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,19 @@ public class ProjectFolder extends ProjectNode implements FolderBase<ProjectNode

public static final String PSEUDO_CLASS = "projectFolder";
public static final int VERSION = 0;
private static final String NODE_CREATED = "NODE_CREATED";
private static final String NODE_REMOVED = "NODE_REMOVED";

private final WeakListenerList<ProjectFolderListener> listeners = new WeakListenerList<>();

private final AppStorageListener l = eventList -> {
for (NodeEvent event : eventList.getEvents()) {
switch (event.getType()) {
case NODE_CREATED:
case NodeCreated.TYPENAME:
if (getId().equals(((NodeCreated) event).getParentId())) {
listeners.notify(listener -> listener.childAdded(event.getId()));
}
break;

case NODE_REMOVED:
case NodeRemoved.TYPENAME:
if (getId().equals(((NodeRemoved) event).getParentId())) {
listeners.notify(listener -> listener.childRemoved(event.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
public abstract class AbstractScript<T extends AbstractScript> extends ProjectFile implements StorableScript {

private static final String NODE_DATA_UPDATED = "NODE_DATA_UPDATED";

private static final String INCLUDED_SCRIPTS_DEPENDENCY_NAME = "scriptIncludes";
private static final String DEFAULT_SCRIPTS_DELIMITER = "\n\n";
private final String scriptContentName;
Expand All @@ -45,7 +43,7 @@ public AbstractScript(ProjectFileCreationContext context, int codeVersion, Strin

private void processEvents(List<NodeEvent> events, String nodeId, List<ScriptListener> listeners) {
for (NodeEvent event : events) {
if (NODE_DATA_UPDATED.equals(event.getType())) {
if (NodeDataUpdated.TYPENAME.equals(event.getType())) {
NodeDataUpdated dataUpdated = (NodeDataUpdated) event;
if (dataUpdated.getId().equals(nodeId) && scriptContentName.equals(dataUpdated.getDataName())) {
for (ScriptListener listener : listeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
*/
public class ImportedCaseBuilder implements ProjectFileBuilder<ImportedCase> {

private static final String CASE_IMPORTED = "CASE_IMPORTED";

private final ProjectFileBuildContext context;

private final ExportersLoader exportersLoader;
Expand Down Expand Up @@ -158,7 +156,7 @@ public ImportedCase build() {
importersLoader);

context.getStorage().getEventsBus().pushEvent(new CaseImported(info.getId(),
context.getFolderInfo().getId(), ic.getPath().toString()), CASE_IMPORTED);
context.getFolderInfo().getId(), ic.getPath().toString()), CaseImported.TYPENAME);

return ic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
public class ModificationScriptBuilder implements ProjectFileBuilder<ModificationScript> {

private static final String SCRIPT_MODIFIED = "SCRIPT_MODIFIED";

private final ProjectFileBuildContext context;

private String name;
Expand Down Expand Up @@ -89,7 +87,7 @@ public ModificationScript build() {
ModificationScript modificationScript = new ModificationScript(new ProjectFileCreationContext(info, context.getStorage(), context.getProject()));

context.getStorage().getEventsBus().pushEvent(new ScriptModified(info.getId(),
context.getFolderInfo().getId(), modificationScript.getPath().toString()), SCRIPT_MODIFIED);
context.getFolderInfo().getId(), modificationScript.getPath().toString()), ScriptModified.TYPENAME);

return modificationScript;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
public class VirtualCaseBuilder implements ProjectFileBuilder<VirtualCase> {

private static final String VIRTUAL_CASE_CREATED = "VIRTUAL_CASE_CREATED";

private final ProjectFileBuildContext context;

private String name;
Expand Down Expand Up @@ -96,7 +94,7 @@ public VirtualCase build() {
VirtualCase virtualCase = new VirtualCase(new ProjectFileCreationContext(info, context.getStorage(), context.getProject()));

context.getStorage().getEventsBus().pushEvent(new VirtualCaseCreated(info.getId(),
context.getFolderInfo().getId(), virtualCase.getPath().toString()), VIRTUAL_CASE_CREATED);
context.getFolderInfo().getId(), virtualCase.getPath().toString()), VirtualCaseCreated.TYPENAME);

return virtualCase;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
public class CaseImported extends BusinessEvent {

private static final String CASE_IMPORTED = "CASE_IMPORTED";
public static final String TYPENAME = "CASE_IMPORTED";

@JsonCreator
public CaseImported(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId,
@JsonProperty("path") String path) {
super(id, parentId, path, CASE_IMPORTED);
super(id, parentId, path, TYPENAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
public class ScriptModified extends BusinessEvent {

private static final String SCRIPT_MODIFIED = "SCRIPT_MODIFIED";
public static final String TYPENAME = "SCRIPT_MODIFIED";

@JsonCreator
public ScriptModified(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId, @JsonProperty("path") String path) {
super(id, parentId, path, SCRIPT_MODIFIED);
super(id, parentId, path, TYPENAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/
public class VirtualCaseCreated extends BusinessEvent {

private static final String VIRTUAL_CASE_CREATED = "VIRTUAL_CASE_CREATED";
public static final String TYPENAME = "VIRTUAL_CASE_CREATED";

@JsonCreator
public VirtualCaseCreated(@JsonProperty("id") String id, @JsonProperty("parentId") String parentId,
@JsonProperty("path") String path) {
super(id, parentId, path, VIRTUAL_CASE_CREATED);
super(id, parentId, path, TYPENAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.powsybl.afs.ext.base.events.CaseImported;
import com.powsybl.afs.ext.base.events.ScriptModified;
import com.powsybl.afs.ext.base.events.VirtualCaseCreated;
import com.powsybl.afs.storage.events.*;
import org.junit.Test;

import java.io.IOException;
Expand All @@ -26,7 +25,7 @@ public class EventsTest {
public void caseImportedTest() throws IOException {
CaseImported caseImported = new CaseImported("a", "b", Paths.get("/tmp/foo").toString());
assertEquals("a", caseImported.getId());
assertEquals("CASE_IMPORTED", caseImported.getType());
assertEquals(CaseImported.TYPENAME, caseImported.getType());
assertEquals("b", caseImported.getParentId());
assertNotNull(caseImported.toString());
assertEquals(Paths.get("/tmp/foo").toString(), caseImported.getPath());
Expand All @@ -41,7 +40,7 @@ public void caseImportedTest() throws IOException {
public void scriptModifiedTest() throws IOException {
ScriptModified scriptModified = new ScriptModified("a", "b", Paths.get("/tmp/foo").toString());
assertEquals("a", scriptModified.getId());
assertEquals("SCRIPT_MODIFIED", scriptModified.getType());
assertEquals(ScriptModified.TYPENAME, scriptModified.getType());
assertEquals("b", scriptModified.getParentId());
assertNotNull(scriptModified.toString());

Expand All @@ -56,7 +55,7 @@ public void scriptModifiedTest() throws IOException {
public void virtualCaseCreatedTest() throws IOException {
VirtualCaseCreated virtualCaseCreated = new VirtualCaseCreated("a", "b", Paths.get("/tmp/foo").toString());
assertEquals("a", virtualCaseCreated.getId());
assertEquals("VIRTUAL_CASE_CREATED", virtualCaseCreated.getType());
assertEquals(VirtualCaseCreated.TYPENAME, virtualCaseCreated.getType());
assertEquals("b", virtualCaseCreated.getParentId());
assertNotNull(virtualCaseCreated.toString());
assertEquals(Paths.get("/tmp/foo").toString(), virtualCaseCreated.getPath());
Expand Down
Loading

0 comments on commit 12c386f

Please sign in to comment.