Skip to content

Commit

Permalink
update transform history to store coordinated places as array list (#861
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dev-mlb authored Aug 1, 2024
1 parent de36e49 commit eec6690
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 61 deletions.
8 changes: 4 additions & 4 deletions src/main/java/emissary/core/BaseDataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,15 @@ public String whereAmI() {
@Nullable
@Override
public DirectoryEntry getLastPlaceVisited() {
String entry = history.lastVisit();
return entry == null ? null : new DirectoryEntry(entry);
TransformHistory.History entry = history.lastVisit();
return entry == null ? null : new DirectoryEntry(entry.getKey());
}

@Nullable
@Override
public DirectoryEntry getPenultimatePlaceVisited() {
String entry = history.penultimateVisit();
return entry == null ? null : new DirectoryEntry(entry);
TransformHistory.History entry = history.penultimateVisit();
return entry == null ? null : new DirectoryEntry(entry.getKey());
}

@Override
Expand Down
108 changes: 69 additions & 39 deletions src/main/java/emissary/core/TransformHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import emissary.directory.KeyManipulator;
import emissary.place.IServiceProviderPlace;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
Expand Down Expand Up @@ -72,7 +73,15 @@ public void append(final String key) {
* @param coordinated true if history entry is for informational purposes only
*/
public void append(String key, boolean coordinated) {
history.add(new History(key, coordinated));
if (coordinated) {
History last = lastVisit();
if (last != null) {
last.addCoordinated(key);
}
} else {
history.add(new History(key));
}

}

/**
Expand All @@ -97,10 +106,18 @@ public List<String> get() {
* @return List of places visited
*/
public List<String> get(boolean includeCoordinated) {
return history.stream()
.filter(x -> includeCoordinated || !x.wasCoordinated())
.map(History::getKey)
.collect(Collectors.toList());
if (includeCoordinated) {
List<String> keys = new ArrayList<>();
history.forEach(k -> {
keys.add(k.getKey());
keys.addAll(k.getCoordinated());
});
return keys;
} else {
return history.stream()
.map(History::getKey)
.collect(Collectors.toList());
}
}

/**
Expand All @@ -118,13 +135,11 @@ public List<History> getHistory() {
* @return last place visited
*/
@Nullable
public String lastVisit() {
List<String> historyList = get();
final int sz = historyList.size();
if (sz == 0) {
public History lastVisit() {
if (CollectionUtils.isEmpty(history)) {
return null;
}
return historyList.get(sz - 1);
return history.get(history.size() - 1);
}

/**
Expand All @@ -133,13 +148,11 @@ public String lastVisit() {
* @return second-to-last place visited
*/
@Nullable
public String penultimateVisit() {
List<String> historyList = get();
final int sz = historyList.size();
if (sz < 2) {
public History penultimateVisit() {
if (CollectionUtils.isEmpty(history) || history.size() < 2) {
return null;
}
return historyList.get(sz - 2);
return history.get(history.size() - 2);
}

/**
Expand All @@ -148,8 +161,8 @@ public String penultimateVisit() {
* @return true is place has been visited
*/
public boolean hasVisited(final String pattern) {
for (final String historyValue : get()) {
if (KeyManipulator.gmatch(historyValue, pattern)) {
for (final History hist : history) {
if (KeyManipulator.gmatch(hist.getKey(), pattern)) {
return true;
}
}
Expand All @@ -162,62 +175,79 @@ public boolean hasVisited(final String pattern) {
* @return true if not yet started
*/
public boolean beforeStart() {
List<String> historyList = get();
if (historyList.isEmpty()) {
if (history.isEmpty()) {
return true;
}
final String s = historyList.get(historyList.size() - 1);
final String s = history.get(history.size() - 1).getKey();
return s.contains(IServiceProviderPlace.SPROUT_KEY);
}

public int size() {
return size(false);
}

public int size(boolean includeCoordinated) {
int size = history.size();
if (includeCoordinated) {
size += (int) history.stream().mapToLong(h -> h.coordinated.size()).sum();
}
return size;
}

@Override
public String toString() {
final StringBuilder myOutput = new StringBuilder();
final String ls = System.getProperty("line.separator");
myOutput.append("transform history (").append(this.history.size()).append(") :").append(ls);
final String ls = System.lineSeparator();
myOutput.append("transform history (").append(size(true)).append(") :").append(ls);
history.forEach(x -> myOutput.append(x.toString()).append(ls));
return myOutput.toString();
}

public static class History {
String key;
boolean coordinated;
List<String> coordinated = new ArrayList<>();

/**
* Needed to support Kryo deserialization
*/
private History() {}

public History(String key) {
this(key, false);
}

public History(String key, boolean coordinated) {
this.key = key;
this.coordinated = coordinated;
}

public String getKey() {
return key;
return getKey(false);
}

public String getKeyNoUrl() {
return StringUtils.substringBefore(key, ".http");
public String getKey(boolean stripUrl) {
return stripUrl ? stripUrl(key) : key;
}

public boolean wasCoordinated() {
return coordinated;
public List<String> getCoordinated() {
return getCoordinated(false);
}

public List<String> getCoordinated(boolean stripUrl) {
return stripUrl ? coordinated.stream().map(History::stripUrl).collect(Collectors.toUnmodifiableList())
: Collections.unmodifiableList(coordinated);
}

public void addCoordinated(String key) {
coordinated.add(key);
}

protected static String stripUrl(String key) {
return StringUtils.substringBefore(key, ".http");
}

@Override
public String toString() {
String start;
if (wasCoordinated()) {
start = " +--> ";
} else {
start = " -> ";
StringBuilder hist = new StringBuilder(" -> " + getKey());
for (String coord : coordinated) {
hist.append(System.lineSeparator()).append(" +--> ").append(coord);
}
return start + getKey();
return hist.toString();
}
}
}
14 changes: 6 additions & 8 deletions src/main/java/emissary/util/PayloadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ public static String getPayloadDisplayString(final IBaseDataObject payload, fina
*/
public static String getPayloadDisplayString(final IBaseDataObject payload) {
final StringBuilder sb = new StringBuilder();
final List<TransformHistory.History> th = payload.getTransformHistory().getHistory();
final TransformHistory th = payload.getTransformHistory();
final String fileName = payload.getFilename();
final String fileType = payload.getFileType();
final List<String> currentForms = payload.getAllCurrentForms();
final Instant creationTimestamp = payload.getCreationTimestamp();

sb.append("\n").append("filename: ").append(fileName).append("\n").append(" creationTimestamp: ").append(creationTimestamp).append("\n")
.append(" currentForms: ").append(currentForms).append("\n").append(" filetype: ").append(fileType).append("\n")
.append(" transform history (").append(th.size()).append(") :").append("\n");
.append(" transform history (").append(th.size(true)).append(") :").append("\n");

// transform history output
String historyCase = configureHistoryCase(fileType);
Expand All @@ -130,13 +130,11 @@ public static String getPayloadDisplayString(final IBaseDataObject payload) {
.append(payload.getLastPlaceVisited())
.append("\n");
} else {
for (final TransformHistory.History h : th) {
sb.append(" ");
if (h.wasCoordinated()) {
sb.append(" ");
for (final TransformHistory.History h : th.getHistory()) {
sb.append(" ").append(h.getKey(historyCase.equals(NO_URL))).append("\n");
for (final String coord : h.getCoordinated(historyCase.equals(NO_URL))) {
sb.append(" ").append(coord).append("\n");
}
// check is NO_URL or not
sb.append(" ").append(historyCase.equals(NO_URL) ? h.getKeyNoUrl() : h.getKey()).append("\n");
}
}
return sb.toString();
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/emissary/core/TransformHistoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ void testAppend() {
th.append(key2);
assertEquals(2, th.getHistory().size());
assertTrue(th.get().contains(key2));
assertFalse(th.getHistory().get(1).wasCoordinated());
assertTrue(th.getHistory().get(1).getCoordinated().isEmpty());

// now append a coordinated value
th.append(key3, true);
assertEquals(3, th.getHistory().size());
assertEquals(2, th.getHistory().size());
assertFalse(th.get().contains(key3)); // does not include coordinated results
assertTrue(th.get(true).contains(key3));
assertTrue(th.getHistory().get(2).wasCoordinated());
assertEquals(key3, th.getHistory().get(1).getCoordinated().get(0));
}

@Test
Expand All @@ -91,17 +91,17 @@ void testVisitation() {
th.append(key1);
th.append(key2);
th.append(key3, true);
assertEquals(3, th.getHistory().size());
assertEquals(2, th.getHistory().size());

assertEquals(key2, th.lastVisit()); // skips the coordinated place
assertEquals(key1, th.penultimateVisit());
assertEquals(key2, th.lastVisit().getKey()); // skips the coordinated place
assertEquals(key1, th.penultimateVisit().getKey());

// set and check the history with the last place visited was not coordinated
th.append(key4);
assertEquals(4, th.getHistory().size());
assertEquals(3, th.getHistory().size());

assertEquals(key4, th.lastVisit());
assertEquals(key2, th.penultimateVisit()); // skips the coordinated place
assertEquals(key4, th.lastVisit().getKey());
assertEquals(key2, th.penultimateVisit().getKey()); // skips the coordinated place

// check if a place has been visited
assertFalse(th.hasVisited("*.ONE_THING.*.*")); // coordinated place is ignored
Expand Down Expand Up @@ -177,6 +177,6 @@ void testGetKeyNoUrl() {
TransformHistory th = new TransformHistory();
th.append(key1);

assertEquals("UNKNOWN.FILE_PICK_UP.INPUT", th.getHistory().get(0).getKeyNoUrl());
assertEquals("UNKNOWN.FILE_PICK_UP.INPUT", th.getHistory().get(0).getKey(true));
}
}

0 comments on commit eec6690

Please sign in to comment.