From 21a22ed14deb572f82eb10eba7e7d0c15ed40009 Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Wed, 11 Aug 2021 15:14:20 +0800 Subject: [PATCH 1/2] support schema-api to return all schema of a graph Change-Id: I356336d46349bbc9ad42d7964013ea9147fdd986 --- .../baidu/hugegraph/api/schema/SchemaAPI.java | 68 +++++++++++++++++++ .../hugegraph/io/HugeGraphSONModule.java | 1 - .../com/baidu/hugegraph/api/ApiTestSuite.java | 1 + .../baidu/hugegraph/api/SchemaApiTest.java | 40 +++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/api/schema/SchemaAPI.java create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/api/SchemaApiTest.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/schema/SchemaAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/schema/SchemaAPI.java new file mode 100644 index 0000000000..378005e812 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/schema/SchemaAPI.java @@ -0,0 +1,68 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api.schema; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.annotation.security.RolesAllowed; +import javax.inject.Singleton; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; + +import org.slf4j.Logger; + +import com.baidu.hugegraph.HugeGraph; +import com.baidu.hugegraph.api.API; +import com.baidu.hugegraph.core.GraphManager; +import com.baidu.hugegraph.schema.SchemaManager; +import com.baidu.hugegraph.util.Log; +import com.codahale.metrics.annotation.Timed; + +@Path("graphs/{graph}/schema") +@Singleton +public class SchemaAPI extends API { + + private static final Logger LOG = Log.logger(SchemaAPI.class); + + @GET + @Timed + @Produces(APPLICATION_JSON_WITH_CHARSET) + @RolesAllowed({"admin", "$owner=$graph $action=schema_read"}) + public String list(@Context GraphManager manager, + @PathParam("graph") String graph) { + LOG.debug("Graph [{}] list all schema", graph); + + HugeGraph g = graph(manager, graph); + SchemaManager schema = g.schema(); + + Map> schemaMap = new LinkedHashMap<>(4); + schemaMap.put("propertykeys", schema.getPropertyKeys()); + schemaMap.put("vertexlabels", schema.getVertexLabels()); + schemaMap.put("edgelabels", schema.getEdgeLabels()); + schemaMap.put("indexlabels", schema.getIndexLabels()); + + return manager.serializer(g).writeMap(schemaMap); + } +} diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java index 5c1d60fd16..a350f7d812 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/io/HugeGraphSONModule.java @@ -583,7 +583,6 @@ public void serialize(Blob blob, JsonGenerator jsonGenerator, } } - private static class BlobDeserializer extends StdDeserializer { public BlobDeserializer() { diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/ApiTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/ApiTestSuite.java index 7810f15fb9..ff926de515 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/ApiTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/ApiTestSuite.java @@ -32,6 +32,7 @@ VertexLabelApiTest.class, EdgeLabelApiTest.class, IndexLabelApiTest.class, + SchemaApiTest.class, VertexApiTest.class, EdgeApiTest.class, TaskApiTest.class, diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/api/SchemaApiTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/SchemaApiTest.java new file mode 100644 index 0000000000..34291bc8e7 --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/api/SchemaApiTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 HugeGraph Authors + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.baidu.hugegraph.api; + +import javax.ws.rs.core.Response; + +import org.junit.Test; + +public class SchemaApiTest extends BaseApiTest { + + private static String path = "/graphs/hugegraph/schema"; + + @Test + public void testGet() { + Response r = client().get(path); + String content = assertResponseStatus(200, r); + + assertJsonContains(content, "propertykeys"); + assertJsonContains(content, "vertexlabels"); + assertJsonContains(content, "edgelabels"); + assertJsonContains(content, "indexlabels"); + } +} From c73fea28f750776afb6c3001e2b5b763db41507e Mon Sep 17 00:00:00 2001 From: zhangyi51 Date: Mon, 16 Aug 2021 16:11:13 +0800 Subject: [PATCH 2/2] upgrade api version to 0.66 Change-Id: I37ca09dcedfd81612f470ef8661effe35323c223 --- hugegraph-api/pom.xml | 2 +- .../src/main/java/com/baidu/hugegraph/version/ApiVersion.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/hugegraph-api/pom.xml b/hugegraph-api/pom.xml index f6a3236b0c..5746c80a37 100644 --- a/hugegraph-api/pom.xml +++ b/hugegraph-api/pom.xml @@ -153,7 +153,7 @@ - 0.65.0.0 + 0.66.0.0 diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java index 29e76a93e4..c0e2b5288a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/version/ApiVersion.java @@ -115,10 +115,11 @@ public final class ApiVersion { * [0.63] Issue-1500: Add user-login RESTful API * [0.64] Issue-1504: Add auth-project RESTful API * [0.65] Issue-1506: Support olap property key + * [0.66] Issue-1567: Support get schema RESTful API */ // The second parameter of Version.of() is for IDE running without JAR - public static final Version VERSION = Version.of(ApiVersion.class, "0.65"); + public static final Version VERSION = Version.of(ApiVersion.class, "0.66"); public static final void check() { // Check version of hugegraph-core. Firstly do check from version 0.3