Skip to content

Commit

Permalink
#1989 add JsonSerializerTest, add new detach method to document to fi…
Browse files Browse the repository at this point in the history
…lter hidden properties, add tests to HTTPGraphIT, fix Projection
  • Loading branch information
robfrank committed Feb 19, 2025
1 parent 1ab5d92 commit 651d7bb
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 52 deletions.
26 changes: 12 additions & 14 deletions e2e/src/test/java/com/arcadedb/e2e/RemoteDatabaseJavaApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import com.arcadedb.graph.Vertex;
import com.arcadedb.query.sql.executor.ResultSet;
import com.arcadedb.remote.RemoteDatabase;
import com.arcadedb.serializer.json.JSONObject;
import com.arcadedb.utility.CollectionUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -47,16 +45,14 @@ void tearDown() {
database.close();
}



@Test
void createUnidirectionalEdge() {

database.command("sql", "create vertex type Person");
database.command("sql", "create edge type FriendOf");

MutableVertex me = database.newVertex("Person").set("name", "me") .save();
MutableVertex you = database.newVertex("Person").set("name", "you") .save();
MutableVertex me = database.newVertex("Person").set("name", "me").save();
MutableVertex you = database.newVertex("Person").set("name", "you").save();

MutableEdge friendOf = me.newEdge("FriendOf", you, false).save();

Expand All @@ -67,20 +63,23 @@ void createUnidirectionalEdge() {
assertThat(e).isEqualTo(friendOf);
});

database.query("sql", "select expand(out('FriendOf')) from Person where name = 'me'").stream().forEach(r -> {
assertThat(r.<String>getProperty("name")).isEqualTo("you");
});
}

@Test
void createTypesAndDataWithSqlScripts() {
//this is a test-double of HTTPGraphIT.testOneEdgePerTx test
database.command( "sqlscript",
database.command("sqlscript",
"""
CREATE VERTEX TYPE Photos;
CREATE VERTEX TYPE Users;
CREATE EDGE TYPE HasUploaded;""");

database.command( "sql", "CREATE VERTEX Users SET id = 'u1111'");
database.command("sql", "CREATE VERTEX Users SET id = 'u1111'");

database.command( "sqlscript",
database.command("sqlscript",
"""
BEGIN;
LET photo = CREATE VERTEX Photos SET id = "p12345", name = "download1.jpg";
Expand All @@ -89,7 +88,7 @@ void createTypesAndDataWithSqlScripts() {
COMMIT RETRY 30;
RETURN $photo;""");

database.command( "sqlscript",
database.command("sqlscript",
"""
BEGIN;
LET photo = CREATE VERTEX Photos SET id = "p2222", name = "download2.jpg";
Expand All @@ -98,7 +97,7 @@ void createTypesAndDataWithSqlScripts() {
COMMIT RETRY 30;
RETURN $photo;""");

database.command( "sqlscript",
database.command("sqlscript",
"""
BEGIN;LET photo = CREATE VERTEX Photos SET id = "p5555", name = "download3.jpg";
LET user = SELECT FROM Users WHERE id = "u1111";
Expand All @@ -108,9 +107,8 @@ void createTypesAndDataWithSqlScripts() {

ResultSet resultSet = database.command("sql",
"""
SELECT id
FROM ( SELECT expand( outE('HasUploaded') )
FROM Users WHERE id = "u1111" )""");
SELECT expand( out('HasUploaded') ) FROM Users WHERE id = "u1111"
""");

assertThat(resultSet.stream()).hasSize(3);
}
Expand Down
6 changes: 6 additions & 0 deletions engine/src/main/java/com/arcadedb/database/BaseDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ public Document asDocument(final boolean loadContent) {
return this;
}

@Override
public DetachedDocument detach() {
return new DetachedDocument(this);
}

@Override
public DetachedDocument detach(boolean filterHiddenProperties) {
return new DetachedDocument(this, filterHiddenProperties);
}

@Override
public String getString(final String propertyName) {
return (String) Type.convert(database, get(propertyName), String.class);
Expand Down
20 changes: 19 additions & 1 deletion engine/src/main/java/com/arcadedb/database/DetachedDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
*/
package com.arcadedb.database;

import com.arcadedb.schema.Property;
import com.arcadedb.serializer.json.JSONObject;

import java.util.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Detached document instances are generated from a document and can be accessed outside a transaction.
Expand All @@ -29,16 +34,29 @@
*/
public class DetachedDocument extends ImmutableDocument {
private Map<String, Object> map;
private boolean filterHiddenProperties = false;

protected DetachedDocument(final BaseDocument source) {
super(null, source.type, source.rid, null);
init(source);
}

protected DetachedDocument(final BaseDocument source, boolean filterHiddenProperties) {
super(null, source.type, source.rid, null);
this.filterHiddenProperties = filterHiddenProperties;
init(source);
}

private void init(final Document sourceDocument) {
this.map = new LinkedHashMap<>();
final Map<String, Object> sourceMap = sourceDocument.propertiesAsMap();
for (final Map.Entry<String, Object> entry : sourceMap.entrySet()) {

if (filterHiddenProperties) {
Property property = sourceDocument.getType().getPropertyIfExists(entry.getKey());
if (property != null && property.isHidden())
continue;
}
Object value = entry.getValue();

if (value instanceof List list) {
Expand Down
2 changes: 2 additions & 0 deletions engine/src/main/java/com/arcadedb/database/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public interface Document extends Record {

DetachedDocument detach();

DetachedDocument detach(boolean filterHiddenProperties);

boolean has(String propertyName);

Object get(String propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void init(final CommandContext context) {
}

public InternalExecutionPlan createExecutionPlan(final CommandContext context, final boolean useCache) {
final DatabaseInternal db = context.getDatabase();
final DatabaseInternal db= context.getDatabase();
if (useCache && !context.isProfiling() && statement.executionPlanCanBeCached()) {
final ExecutionPlan plan = db.getExecutionPlanCache().get(statement.getOriginalStatement(), context);
if (plan != null)
Expand Down
16 changes: 3 additions & 13 deletions engine/src/main/java/com/arcadedb/query/sql/parser/Projection.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public Result calculateSingle(final CommandContext context, final Result record)
if (excludes.contains(alias) ||
(doc.getType().existsProperty(alias) &&
doc.getType().getProperty(alias).isHidden())) {

continue;
continue;
}
}
Object value = item.convert(record.getProperty(alias));
Expand All @@ -141,17 +140,8 @@ public Result calculateSingle(final CommandContext context, final Result record)
result.setProperty("@type", doc.getType().getName());
}

// System.out.println(" isActive = " + context.getDatabase().getTransaction().isActive());
// MutableDocument modified = doc.modify();
// excludes.forEach(modified::remove);
// doc.getType().getProperties().stream()
// .filter(Property::isHidden)
// .map(Property::getName)
// .forEach(modified::remove);

// System.out.println("result = " + result);
// result.setElement(modified);
// System.out.println("result.getElement() = " + result.getElement());
Document detached = doc.detach(true);
result.setElement(detached);
});
} else {
result.setProperty(item.getProjectionAliasAsString(), item.execute(record, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public class JsonGraphSerializer extends JsonSerializer {

private boolean expandVertexEdges = false;
private JSONObject sharedJson = null;
private boolean includeMetadata = true;

private JsonGraphSerializer() {
}

public static JsonGraphSerializer createJsonGraphSerializer() {
return new JsonGraphSerializer();
}

public JSONObject serializeGraphElement(final Document document) {
if (sharedJson != null)
Expand All @@ -59,7 +67,7 @@ public JSONObject serializeGraphElement(final Document document, final JSONObjec
object.put("r", rid.toString());
object.put("t", document.getTypeName());

for (final Map.Entry<String, Object> prop : document.toMap(false).entrySet()) {
for (final Map.Entry<String, Object> prop : document.toMap(includeMetadata).entrySet()) {
Object value = prop.getValue();

if (value != null) {
Expand Down Expand Up @@ -91,15 +99,6 @@ else if (value.equals(Double.NEGATIVE_INFINITY) || value.equals(Float.NEGATIVE_I
return object;
}

public boolean isExpandVertexEdges() {
return expandVertexEdges;
}

public JsonGraphSerializer setExpandVertexEdges(final boolean expandVertexEdges) {
this.expandVertexEdges = expandVertexEdges;
return this;
}

private void setMetadata(final Document document, final JSONObject object) {
if (document instanceof Vertex vertex1) {
final Vertex vertex = vertex1;
Expand All @@ -126,8 +125,22 @@ private void setMetadata(final Document document, final JSONObject object) {
}
}

public boolean isExpandVertexEdges() {
return expandVertexEdges;
}

public JsonGraphSerializer setExpandVertexEdges(final boolean expandVertexEdges) {
this.expandVertexEdges = expandVertexEdges;
return this;
}

public JsonGraphSerializer setSharedJson(final JSONObject json) {
sharedJson = json;
return this;
}

public JsonGraphSerializer setIncludeMetadata(final boolean includeMetadata) {
this.includeMetadata = includeMetadata;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public class JsonSerializer {
private boolean useVertexEdgeSize = true;
private boolean useCollectionSizeForEdges = true;

JsonSerializer() {
}

public static JsonSerializer createJsonSerializer() {
return new JsonSerializer();
}

public JSONObject serializeDocument(final Document document) {
final Database database = document.getDatabase();
final JSONObject object = new JSONObject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.arcadedb.query.sql.executor;

import com.arcadedb.TestHelper;
import com.arcadedb.database.Document;
import com.arcadedb.database.MutableDocument;
import com.arcadedb.exception.CommandSQLParsingException;
import com.arcadedb.exception.ValidationException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import static org.assertj.core.api.Assertions.assertThat;

public class SerializerTest extends TestHelper {
public class BinarySerializerTest extends TestHelper {

@Test
public void testVarNumber() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.arcadedb.serializer;

import com.arcadedb.TestHelper;
import com.arcadedb.graph.MutableEdge;
import com.arcadedb.graph.MutableVertex;
import com.arcadedb.schema.DocumentType;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class JsonGraphSerializerTest extends TestHelper {

private JsonGraphSerializer jsonGraphSerializer;
private MutableEdge edge;
private MutableVertex vertex1;
private MutableVertex vertex2;

@BeforeEach
void setUp() {
jsonGraphSerializer = JsonGraphSerializer.createJsonGraphSerializer();

database.transaction(() -> {
DocumentType vertexType = database.getSchema().createVertexType("TestVertexType");
DocumentType edgeType = database.getSchema().createEdgeType("TestEdgeType");

vertex1 = database.newVertex("TestVertexType").save();
vertex2 = database.newVertex("TestVertexType").save();

edge = vertex1.newEdge("TestEdgeType", vertex2, true).save();
});
}

@Test
void testSerializeEdge() {

String json = jsonGraphSerializer.serializeGraphElement(edge).toString();

assertThat(JsonPath.<String>read(json, "$.p.@rid")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.p.@type")).isEqualTo("TestEdgeType");
assertThat(JsonPath.<String>read(json, "$.p.@cat")).isEqualTo("e");
assertThat(JsonPath.<String>read(json, "$.p.@in")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.p.@out")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.r")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.t")).isEqualTo("TestEdgeType");
assertThat(JsonPath.<String>read(json, "$.i")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.o")).isNotEmpty();

}

@Test
void testSerializeEdgeWithoutMetadata() {
jsonGraphSerializer.setIncludeMetadata(false);

String json = jsonGraphSerializer.serializeGraphElement(edge).toString();

//nometadata
assertThat(JsonPath.<Map<?, ?>>read(json, "$.p")).isEmpty();

assertThat(JsonPath.<String>read(json, "$.r")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.t")).isEqualTo("TestEdgeType");
assertThat(JsonPath.<String>read(json, "$.i")).isNotEmpty();
assertThat(JsonPath.<String>read(json, "$.o")).isNotEmpty();

}

}
Loading

0 comments on commit 651d7bb

Please sign in to comment.