Skip to content

Commit

Permalink
fix: supported passing of map to remote edge creation
Browse files Browse the repository at this point in the history
Issue #1569
  • Loading branch information
lvca committed Apr 27, 2024
1 parent fdc9d8b commit 2699aa0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion engine/src/main/java/com/arcadedb/graph/GraphEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ else if (direction == Vertex.DIRECTION.IN)
throw new IllegalArgumentException("Invalid direction");
}

public static void setProperties(final MutableDocument edge, final Object[] properties) {
public static void setProperties(final MutableEdge edge, final Object[] properties) {
if (properties != null)
if (properties.length == 1 && properties[0] instanceof Map) {
// GET PROPERTIES FROM THE MAP
Expand Down
25 changes: 22 additions & 3 deletions network/src/main/java/com/arcadedb/remote/RemoteVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,41 @@ public RID moveTo(final String targetType, final String targetBucket) {
}

public MutableEdge newEdge(final String edgeType, final Identifiable toVertex, final boolean bidirectional,
final Object... properties) {
Object... properties) {
if (!bidirectional)
throw new UnsupportedOperationException("Creating unidirectional edges is not supported from remote database");

StringBuilder query = new StringBuilder(
"create edge `" + edgeType + "` from " + vertex.getIdentity() + " to " + toVertex.getIdentity());
if (properties.length > 0) {
if (properties != null && properties.length > 0) {
query.append(" set ");

if (properties.length == 1 && properties[0] instanceof Map) {
// GET PROPERTIES FROM THE MAP
final Map<String, Object> map = (Map<String, Object>) properties[0];

properties = new Object[map.size() * 2];
int i = 0;
for (final Map.Entry<String, Object> entry : map.entrySet()) {
properties[i++] = entry.getKey();
properties[i++] = entry.getValue();
}

} else {
if (properties.length % 2 != 0)
throw new IllegalArgumentException("Properties must be an even number as pairs of name, value");
}

for (int i = 0; i < properties.length; i += 2) {
final String propName = (String) properties[i];
final Object propValue = properties[i + 1];

if (i > 0)
query.append(", ");

query.append(propName + " = ");
query.append("`");
query.append(propName);
query.append("` = ");

if (propValue instanceof String)
query.append("'");
Expand Down
17 changes: 16 additions & 1 deletion server/src/test/java/com/arcadedb/remote/RemoteDatabaseIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,26 @@ public void simpleTxGraph() throws Exception {

// CREATE DOCUMENT VIA API
final MutableVertex jay = database.newVertex("Character").set("name", "Jay").save();
Assertions.assertTrue(jay instanceof RemoteMutableVertex);

Assertions.assertNotNull(jay);
Assertions.assertEquals("Jay", jay.getString("name"));
Assertions.assertNotNull(jay.getIdentity());
jay.save();

Assertions.assertNotNull(jay);
Assertions.assertEquals("Jay", jay.getString("name"));
Assertions.assertNotNull(jay.getIdentity());
jay.save();


// CREATE DOCUMENT VIA API
final Map<String, Object> map = Map.of("on", "today", "for", "5 days");
Edge edge = jay.newEdge(EDGE1_TYPE_NAME, jay, true, map).save();
Assertions.assertTrue(edge instanceof RemoteMutableEdge);
Assertions.assertEquals("today", edge.get("on"));
Assertions.assertEquals("5 days", edge.get("for"));

// TEST DELETION AND LOOKUP
jay.delete();
try {
Expand Down Expand Up @@ -192,7 +207,7 @@ public void simpleTxGraph() throws Exception {
// CREATE EDGE WITH COMMAND
result = database.command("SQL", "create edge " + EDGE1_TYPE_NAME + " from " + rid1 + " to " + rid2);
Assertions.assertTrue(result.hasNext());
final Edge edge = result.next().getEdge().get();
edge = result.next().getEdge().get();

edge.toMap();
edge.toJSON();
Expand Down

0 comments on commit 2699aa0

Please sign in to comment.