Skip to content

Commit

Permalink
[NOID] fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Jan 20, 2025
1 parent f611bae commit 52d0d25
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public long parseXML(Reader input, TerminationGuard terminationGuard, ReaderType
StartElement element = event.asStartElement();
String name = element.getName().getLocalPart();
boolean isNameGexf = readerType.equals(ReaderType.GEXF) && name.equals("gexf");
if (name.equals("graphml") || name.equals("graph") || isNameGexf) continue;
if (name.equals("graphml") || name.equals("graph")) continue;
if (readerType.equals(ReaderType.GEXF) && name.equals("attribute")) {
String id = getAttribute(element, ID);
String type = getAttribute(element, DATA_TYPE);
Expand Down
67 changes: 67 additions & 0 deletions full/src/main/java/apoc/util/ExtendedUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package apoc.util;

import java.time.Duration;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;

public class ExtendedUtil {
public static <T> T withBackOffRetries(
Supplier<T> func,
boolean retry,
int backoffRetry,
boolean exponential,
Consumer<Exception> exceptionHandler) {
T result;
backoffRetry = backoffRetry < 1 ? 5 : backoffRetry;
int countDown = backoffRetry;
exceptionHandler = Objects.requireNonNullElse(exceptionHandler, exe -> {});
while (true) {
try {
result = func.get();
break;
} catch (Exception e) {
if (!retry || countDown < 1) throw e;
exceptionHandler.accept(e);
countDown--;
long delay = getDelay(backoffRetry, countDown, exponential);
backoffSleep(delay);
}
}
return result;
}

private static void backoffSleep(long millis) {
sleep(millis, "Operation interrupted during backoff");
}

public static void sleep(long millis, String interruptedMessage) {
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(interruptedMessage, ie);
}
}

private static long getDelay(int backoffRetry, int countDown, boolean exponential) {
int backOffTime = backoffRetry - countDown;
long sleepMultiplier = exponential
? (long) Math.pow(2, backOffTime)
: // Exponential retry progression
backOffTime; // Linear retry progression
return Math.min(
Duration.ofSeconds(1).multipliedBy(sleepMultiplier).toMillis(),
Duration.ofSeconds(30).toMillis() // Max 30s
);
}

public static String joinStringLabels(Collection<String> labels) {
return CollectionUtils.isNotEmpty(labels)
? ":" + labels.stream().map(Util::quote).collect(Collectors.joining(":"))
: "";
}
}
18 changes: 2 additions & 16 deletions full/src/test/java/apoc/load/GexfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testLoadGexf() {
testCall(db, "CALL apoc.load.gexf($file)", Map.of("file", file), (row) -> {
Map<String, Object> value = (Map) row.get("value");
String expected =
"{_type=gexf, _children=[{_type=graph, defaultedgetype=directed, _children=[{_type=nodes, _children=[{_type=node, _children=[{_type=attvalues, _children=[{_type=attvalue, for=0, value=http://gephi.org}]}], id=0, label=bar}]}]}], version=1.2}";
"{_[children=[{_children=[{_children=[{_children=[{_children=[{_type=attvalue, for=0, value=http://gephi.org}], _type=attvalues}], _type=node, id=0, label=bar}], _type=nodes}], defaultedgetype=directed, _type=graph}], _type=gexf], version=1.2}";
assertEquals(expected, value.toString());
});
}
Expand All @@ -57,7 +57,7 @@ public void testImportGexf() {
TestUtil.testCall(db, "CALL apoc.import.gexf($file, {readLabels:true})", map("file", file), (r) -> {
assertEquals("gexf", r.get("format"));
assertEquals(5L, r.get("nodes"));
assertEquals(8L, r.get("relationships"));
assertEquals(6L, r.get("relationships"));
});

TestUtil.testCallCount(db, "MATCH (n) RETURN n", 5);
Expand Down Expand Up @@ -174,20 +174,6 @@ public void testImportGexf() {
});
}

@Test
public void testImportGexfWithStoreNodeIds() {
final String file =
ClassLoader.getSystemResource("gexf/single-node.gexf").toString();
TestUtil.testCall(db, "CALL apoc.import.gexf($file, {storeNodeIds: true})", map("file", file), (r) -> {
assertEquals("gexf", r.get("format"));
assertEquals(1L, r.get("nodes"));
});

Map props = TestUtil.singleResultFirstColumn(db, "MATCH (n) RETURN properties(n) AS props");
assertEquals("http://gephi.org", props.get("0"));
assertTrue(props.containsKey("id"));
}

@Test
public void testImportGexfWithDefaultRelationshipTypeSourceAndTargetConfigs() {
String defaultRelType = "TEST_DEFAULT";
Expand Down

0 comments on commit 52d0d25

Please sign in to comment.