From fe3f952a1d8fdf617a947d29156e497df3825cd0 Mon Sep 17 00:00:00 2001 From: liningrui Date: Thu, 23 May 2019 16:00:57 +0800 Subject: [PATCH 01/10] Add api white list and rate limiter for gc Change-Id: Ia7ac5dc5d7d91676701738f2a0ae5073f9ceb01e --- .../java/com/baidu/hugegraph/api/APIList.java | 170 ++++++++++++++++++ .../api/filter/LoadDetectFilter.java | 35 ++++ .../src/main/resources/hugegraph.properties | 8 +- 3 files changed, 209 insertions(+), 4 deletions(-) create mode 100644 hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java new file mode 100644 index 0000000000..fdcf206713 --- /dev/null +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java @@ -0,0 +1,170 @@ +/* + * + * * 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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.inject.Singleton; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; + +import org.apache.commons.lang3.StringUtils; +import org.apache.tinkerpop.shaded.jackson.annotation.JsonProperty; +import org.glassfish.jersey.server.model.Parameter.Source; +import org.glassfish.jersey.server.model.Resource; +import org.glassfish.jersey.server.model.ResourceMethod; + +import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.JsonUtil; +import com.codahale.metrics.annotation.Timed; + +@Path("/") +@Singleton +public class APIList { + + @GET + @Timed + @Produces(MediaType.APPLICATION_JSON) + public String showAll(@Context Application application) { + Map>> apiProfiles = new HashMap<>(); + + for (Class aClass : application.getClasses()) { + if (!isAnnotatedResourceClass(aClass)) { + continue; + } + + Resource resource = Resource.from(aClass); + String fullName = resource.getName(); + APICategory apiCategory = getCategory(fullName); + + Map> subApiProfiles; + subApiProfiles = apiProfiles.computeIfAbsent(apiCategory.category, + k -> new HashMap<>()); + List profiles = new ArrayList<>(); + subApiProfiles.put(apiCategory.subCategory, profiles); + + String url = resource.getPath(); + // List all methods of this resource + for (ResourceMethod rm : resource.getResourceMethods()) { + APIProfile profile = APIProfile.parse(url, rm); + profiles.add(profile); + } + // List all methods of this resource's child resources + for (Resource childResource : resource.getChildResources()) { + String childUrl = url + "/" + childResource.getPath(); + for (ResourceMethod rm : childResource.getResourceMethods()) { + APIProfile profile = APIProfile.parse(childUrl, rm); + profiles.add(profile); + } + } + } + return JsonUtil.toJson(apiProfiles); + } + + private static boolean isAnnotatedResourceClass(Class rc) { + if (rc.isAnnotationPresent(Path.class)) { + return true; + } + for (Class i : rc.getInterfaces()) { + if (i.isAnnotationPresent(Path.class)) { + return true; + } + } + return false; + } + + private static APICategory getCategory(String fullName) { + String[] parts = StringUtils.split(fullName, "."); + E.checkState(parts.length >= 2, "Invalid api name"); + String category = parts[parts.length - 2]; + String subCategory = parts[parts.length - 1]; + return new APICategory(category, subCategory); + } + + private static class APIProfile { + + @JsonProperty("url") + private final String url; + @JsonProperty("method") + private final String method; + @JsonProperty("parameters") + private final List parameters; + + public APIProfile(String url, String method, + List parameters) { + this.url = url; + this.method = method; + this.parameters = parameters; + } + + public static APIProfile parse(String url, ResourceMethod rm) { + String method = rm.getHttpMethod(); + List apiParameters = new ArrayList<>(); + for (org.glassfish.jersey.server.model.Parameter parameter : + rm.getInvocable().getParameters()) { + String sourceName = parameter.getSourceName(); + if (sourceName == null) { + continue; + } + if (parameter.getSource() == Source.PATH) { + continue; + } + String typeName = parameter.getType().getTypeName(); + Parameter apiParameter = new Parameter(sourceName, typeName); + apiParameters.add(apiParameter); + } + return new APIProfile(url, method, apiParameters); + } + + private static class Parameter { + + @JsonProperty("name") + private String name; + @JsonProperty("type") + private String type; + + public Parameter(String name, String type) { + this.name = name; + this.type = type; + } + } + } + + private static class APICategory { + + private String category; + private String subCategory; + + public APICategory(String category, String subCategory) { + this.category = category; + this.subCategory = subCategory; + } + } +} diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java index 272114a77e..89b2611857 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java @@ -19,24 +19,40 @@ package com.baidu.hugegraph.api.filter; +import java.util.List; +import java.util.Set; + import javax.inject.Singleton; import javax.ws.rs.ServiceUnavailableException; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.PreMatching; import javax.ws.rs.core.Context; +import javax.ws.rs.core.PathSegment; import javax.ws.rs.ext.Provider; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; import com.baidu.hugegraph.core.WorkLoad; import com.baidu.hugegraph.util.Bytes; +import com.baidu.hugegraph.util.E; +import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.RateLimiter; @Provider @Singleton @PreMatching public class LoadDetectFilter implements ContainerRequestFilter { + private static final Set WHITE_API_LIST = ImmutableSet.of( + "metrics", + "versions" + ); + + // Call gc every 30+ seconds if memory is low and request frequently + private static final RateLimiter GC_RATE_LIMITER = + RateLimiter.create(1.0 / 30); + @Context private javax.inject.Provider configProvider; @Context @@ -44,6 +60,10 @@ public class LoadDetectFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext context) { + if (isWhiteAPI(context)) { + return; + } + HugeConfig config = this.configProvider.get(); long minFreeMemory = config.get(ServerOptions.MIN_FREE_MEMORY); long allocatedMem = Runtime.getRuntime().totalMemory() - @@ -51,6 +71,7 @@ public void filter(ContainerRequestContext context) { long presumableFreeMem = (Runtime.getRuntime().maxMemory() - allocatedMem) / Bytes.MB; if (presumableFreeMem < minFreeMemory) { + gcIfNeeded(); throw new ServiceUnavailableException(String.format( "The server available memory %s(MB) is below than " + "threshold %s(MB) and can't process the request, " + @@ -69,4 +90,18 @@ public void filter(ContainerRequestContext context) { ServerOptions.MAX_WORKER_THREADS.name())); } } + + private static boolean isWhiteAPI(ContainerRequestContext context) { + List segments = context.getUriInfo().getPathSegments(); + E.checkArgument(segments.size() > 0, "Invalid request uri '%s'", + context.getUriInfo().getPath()); + String rootPath = segments.get(0).getPath(); + return WHITE_API_LIST.contains(rootPath); + } + + private static void gcIfNeeded() { + if (GC_RATE_LIMITER.tryAcquire(1)) { + System.gc(); + } + } } diff --git a/hugegraph-example/src/main/resources/hugegraph.properties b/hugegraph-example/src/main/resources/hugegraph.properties index fa596fb7f5..403ca84095 100644 --- a/hugegraph-example/src/main/resources/hugegraph.properties +++ b/hugegraph-example/src/main/resources/hugegraph.properties @@ -1,10 +1,10 @@ gremlin.graph=com.baidu.hugegraph.HugeFactory -backend=cassandra -serializer=cassandra +#backend=cassandra +#serializer=cassandra -#backend=rocksdb -#serializer=binary +backend=rocksdb +serializer=binary store=hugegraph rate_limit=0 From 94a95ae0f4eddd366c149fb3314c084938ae97d3 Mon Sep 17 00:00:00 2001 From: liningrui Date: Fri, 24 May 2019 19:15:47 +0800 Subject: [PATCH 02/10] add ProfileAPI Change-Id: Id16eef8f9c4b0b65689d64cb7a44beead2e0cc41 --- .../api/{APIList.java => ProfileAPI.java} | 52 +++++++++++++++++-- .../api/filter/LoadDetectFilter.java | 2 + 2 files changed, 49 insertions(+), 5 deletions(-) rename hugegraph-api/src/main/java/com/baidu/hugegraph/api/{APIList.java => ProfileAPI.java} (77%) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java similarity index 77% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java index fdcf206713..dd2dd6f818 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/APIList.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java @@ -24,8 +24,10 @@ import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import javax.inject.Singleton; import javax.ws.rs.GET; @@ -42,21 +44,60 @@ import org.glassfish.jersey.server.model.ResourceMethod; import com.baidu.hugegraph.util.E; +import com.baidu.hugegraph.util.InsertionOrderUtil; import com.baidu.hugegraph.util.JsonUtil; import com.codahale.metrics.annotation.Timed; @Path("/") @Singleton -public class APIList { +public class ProfileAPI { + + private static final String SERVICE = "hugegraph"; + private static final String DOC = "https://hugegraph.github.io/hugegraph-doc/"; + private static final String API_DOC = DOC + "clients/hugegraph-api.html"; + + private static String SERVER_PROFILES = null; + private static String API_PROFILES = null; @GET @Timed @Produces(MediaType.APPLICATION_JSON) + public String get(@Context Application application) { + if (SERVER_PROFILES != null) { + return SERVER_PROFILES; + } + + Map profiles = InsertionOrderUtil.newMap(); + profiles.put("service", SERVICE); + profiles.put("doc", DOC); + profiles.put("api-doc", API_DOC); + Set apis = new HashSet<>(); + for (Class aClass : application.getClasses()) { + if (!isAnnotatedPathClass(aClass)) { + continue; + } + Resource resource = Resource.from(aClass); + String fullName = resource.getName(); + APICategory apiCategory = getCategory(fullName); + apis.add(apiCategory.category); + } + profiles.put("apis", apis); + SERVER_PROFILES = JsonUtil.toJson(profiles); + return SERVER_PROFILES; + } + + @GET + @Path("apis") + @Timed + @Produces(MediaType.APPLICATION_JSON) public String showAll(@Context Application application) { - Map>> apiProfiles = new HashMap<>(); + if (API_PROFILES != null) { + return API_PROFILES; + } + Map>> apiProfiles = new HashMap<>(); for (Class aClass : application.getClasses()) { - if (!isAnnotatedResourceClass(aClass)) { + if (!isAnnotatedPathClass(aClass)) { continue; } @@ -85,10 +126,11 @@ public String showAll(@Context Application application) { } } } - return JsonUtil.toJson(apiProfiles); + API_PROFILES = JsonUtil.toJson(apiProfiles); + return API_PROFILES; } - private static boolean isAnnotatedResourceClass(Class rc) { + private static boolean isAnnotatedPathClass(Class rc) { if (rc.isAnnotationPresent(Path.class)) { return true; } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java index 89b2611857..317b2ec35d 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.java @@ -45,6 +45,8 @@ public class LoadDetectFilter implements ContainerRequestFilter { private static final Set WHITE_API_LIST = ImmutableSet.of( + "", + "apis", "metrics", "versions" ); From d4a370447c0052b79a4fffa14ef2f80b7bd66742 Mon Sep 17 00:00:00 2001 From: liningrui Date: Tue, 28 May 2019 10:08:36 +0800 Subject: [PATCH 03/10] tiny improve Change-Id: I9e36d146f2c9463754914f6b2868416254efe371 --- .../com/baidu/hugegraph/api/ProfileAPI.java | 51 ++++++------ .../baidu/hugegraph/type/define/DataType.java | 20 +++-- .../baidu/hugegraph/unit/UnitTestSuite.java | 2 + .../hugegraph/unit/core/DataTypeTest.java | 83 +++++++++++++++++++ 4 files changed, 120 insertions(+), 36 deletions(-) create mode 100644 hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java index dd2dd6f818..6375017d97 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java @@ -1,23 +1,20 @@ /* + * Copyright 2017 HugeGraph Authors * - * * 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. + * 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; @@ -62,7 +59,7 @@ public class ProfileAPI { @GET @Timed @Produces(MediaType.APPLICATION_JSON) - public String get(@Context Application application) { + public String getProfile(@Context Application application) { if (SERVER_PROFILES != null) { return SERVER_PROFILES; } @@ -72,11 +69,11 @@ public String get(@Context Application application) { profiles.put("doc", DOC); profiles.put("api-doc", API_DOC); Set apis = new HashSet<>(); - for (Class aClass : application.getClasses()) { - if (!isAnnotatedPathClass(aClass)) { + for (Class clazz : application.getClasses()) { + if (!isAnnotatedPathClass(clazz)) { continue; } - Resource resource = Resource.from(aClass); + Resource resource = Resource.from(clazz); String fullName = resource.getName(); APICategory apiCategory = getCategory(fullName); apis.add(apiCategory.category); @@ -90,18 +87,18 @@ public String get(@Context Application application) { @Path("apis") @Timed @Produces(MediaType.APPLICATION_JSON) - public String showAll(@Context Application application) { + public String showAllAPIs(@Context Application application) { if (API_PROFILES != null) { return API_PROFILES; } Map>> apiProfiles = new HashMap<>(); - for (Class aClass : application.getClasses()) { - if (!isAnnotatedPathClass(aClass)) { + for (Class clazz : application.getClasses()) { + if (!isAnnotatedPathClass(clazz)) { continue; } - Resource resource = Resource.from(aClass); + Resource resource = Resource.from(clazz); String fullName = resource.getName(); APICategory apiCategory = getCategory(fullName); @@ -134,8 +131,8 @@ private static boolean isAnnotatedPathClass(Class rc) { if (rc.isAnnotationPresent(Path.class)) { return true; } - for (Class i : rc.getInterfaces()) { - if (i.isAnnotationPresent(Path.class)) { + for (Class clazz : rc.getInterfaces()) { + if (clazz.isAnnotationPresent(Path.class)) { return true; } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/DataType.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/DataType.java index c39f1fdddb..c69e7c042a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/DataType.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/type/define/DataType.java @@ -123,24 +123,26 @@ public Number valueToNumber(V value) { } public Date valueToDate(V value) { + if (!this.isDate()) { + return null; + } if (value instanceof Date) { return (Date) value; - } - if (this.isDate()) { - if (value instanceof Number) { - return new Date(((Number) value).longValue()); - } else if (value instanceof String) { - return DateUtil.parse((String) value); - } + } else if (value instanceof Number) { + return new Date(((Number) value).longValue()); + } else if (value instanceof String) { + return DateUtil.parse((String) value); } return null; } public UUID valueToUUID(V value) { + if (!this.isUUID()) { + return null; + } if (value instanceof UUID) { return (UUID) value; - } - if (this.isUUID() && value instanceof String) { + } else if (value instanceof String) { return java.util.UUID.fromString((String) value); } return null; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java index f8109d4fa2..3780dc15cf 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/UnitTestSuite.java @@ -30,6 +30,7 @@ import com.baidu.hugegraph.unit.core.BackendMutationTest; import com.baidu.hugegraph.unit.core.CassandraTest; import com.baidu.hugegraph.unit.core.ConditionQueryFlattenTest; +import com.baidu.hugegraph.unit.core.DataTypeTest; import com.baidu.hugegraph.unit.core.DirectionsTest; import com.baidu.hugegraph.unit.core.EdgeIdTest; import com.baidu.hugegraph.unit.core.JsonUtilTest; @@ -47,6 +48,7 @@ CacheManagerTest.class, VersionTest.class, + DataTypeTest.class, DirectionsTest.class, SerialEnumTest.class, BackendMutationTest.class, diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java new file mode 100644 index 0000000000..09ae6574ca --- /dev/null +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java @@ -0,0 +1,83 @@ +/* + * 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.unit.core; + +import java.util.Date; +import java.util.UUID; + +import org.junit.Test; + +import com.baidu.hugegraph.testutil.Assert; +import com.baidu.hugegraph.testutil.Utils; +import com.baidu.hugegraph.type.define.DataType; + +public class DataTypeTest { + + @Test + public void testString() { + Assert.assertEquals("object", DataType.OBJECT.string()); + Assert.assertEquals("boolean", DataType.BOOLEAN.string()); + Assert.assertEquals("byte", DataType.BYTE.string()); + Assert.assertEquals("int", DataType.INT.string()); + Assert.assertEquals("long", DataType.LONG.string()); + Assert.assertEquals("float", DataType.FLOAT.string()); + Assert.assertEquals("double", DataType.DOUBLE.string()); + Assert.assertEquals("text", DataType.TEXT.string()); + Assert.assertEquals("blob", DataType.BLOB.string()); + Assert.assertEquals("date", DataType.DATE.string()); + Assert.assertEquals("uuid", DataType.UUID.string()); + } + + @Test + public void testValueToNumber() { + Assert.assertNull(DataType.BOOLEAN.valueToNumber(1)); + Assert.assertNull(DataType.INT.valueToNumber("not number")); + + Assert.assertEquals((byte) 1, DataType.BYTE.valueToNumber(1)); + Assert.assertEquals(1, DataType.INT.valueToNumber(1)); + Assert.assertEquals(1, DataType.INT.valueToNumber(1.0F)); + Assert.assertEquals(1, DataType.INT.valueToNumber("1")); + Assert.assertEquals(1L, DataType.LONG.valueToNumber(1)); + Assert.assertEquals(1.0F, DataType.FLOAT.valueToNumber(1)); + Assert.assertEquals(1.0D, DataType.DOUBLE.valueToNumber(1)); + } + + @Test + public void testValueToDate() { + Date date = Utils.date("2019-01-01 12:00:00"); + Assert.assertEquals(date, DataType.DATE.valueToDate(date)); + Assert.assertEquals(date, + DataType.DATE.valueToDate("2019-01-01 12:00:00")); + Assert.assertEquals(date, DataType.DATE.valueToDate(date.getTime())); + + Assert.assertNull(DataType.TEXT.valueToDate("2019-01-01 12:00:00")); + Assert.assertNull(DataType.DATE.valueToDate(true)); + } + + @Test + public void testValueToUUID() { + UUID uuid = UUID.randomUUID(); + Assert.assertEquals(uuid, DataType.UUID.valueToUUID(uuid)); + Assert.assertEquals(uuid, DataType.UUID.valueToUUID(uuid.toString())); + + Assert.assertNull(DataType.TEXT.valueToUUID("2019-01-01 12:00:00")); + Assert.assertNull(DataType.UUID.valueToUUID(true)); + } +} From 74a44c70f2d6865c95853a49e85924d813b51ad1 Mon Sep 17 00:00:00 2001 From: liningrui Date: Wed, 29 May 2019 10:21:29 +0800 Subject: [PATCH 04/10] tiny improve Change-Id: I7b2429217131e460c08da52a67a1356d670522d9 --- .../main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java index 09ae6574ca..8e541d6e0f 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/core/DataTypeTest.java @@ -49,11 +49,11 @@ public void testString() { public void testValueToNumber() { Assert.assertNull(DataType.BOOLEAN.valueToNumber(1)); Assert.assertNull(DataType.INT.valueToNumber("not number")); + Assert.assertNull(DataType.INT.valueToNumber(1.0F)); Assert.assertEquals((byte) 1, DataType.BYTE.valueToNumber(1)); Assert.assertEquals(1, DataType.INT.valueToNumber(1)); - Assert.assertEquals(1, DataType.INT.valueToNumber(1.0F)); - Assert.assertEquals(1, DataType.INT.valueToNumber("1")); + Assert.assertEquals(1, DataType.INT.valueToNumber((byte) 1)); Assert.assertEquals(1L, DataType.LONG.valueToNumber(1)); Assert.assertEquals(1.0F, DataType.FLOAT.valueToNumber(1)); Assert.assertEquals(1.0D, DataType.DOUBLE.valueToNumber(1)); From fd09828be7f36d9bb25689aa69f95ea3ea13c842 Mon Sep 17 00:00:00 2001 From: liningrui Date: Thu, 30 May 2019 15:28:33 +0800 Subject: [PATCH 05/10] move some API into profile package Change-Id: I4f0bc33115bf50486f9996e287cd4cc9a5edb268 --- .../src/main/java/com/baidu/hugegraph/api/API.java | 2 +- .../main/java/com/baidu/hugegraph/api/graph/BatchAPI.java | 2 +- .../com/baidu/hugegraph/api/{ => gremlin}/GremlinAPI.java | 5 +++-- .../main/java/com/baidu/hugegraph/api/job/GremlinAPI.java | 2 +- .../java/com/baidu/hugegraph/api/metrics/MetricsAPI.java | 6 +++--- .../com/baidu/hugegraph/api/{ => profile}/GraphsAPI.java | 3 ++- .../com/baidu/hugegraph/api/{ => profile}/ProfileAPI.java | 4 ++-- .../com/baidu/hugegraph/api/{ => profile}/VersionAPI.java | 3 ++- .../main/java/com/baidu/hugegraph/core/GraphManager.java | 4 ++-- .../baidu/hugegraph/{metric => metrics}/MetricsModule.java | 2 +- .../baidu/hugegraph/{metric => metrics}/MetricsUtil.java | 2 +- .../baidu/hugegraph/{metric => metrics}/ServerReporter.java | 2 +- .../baidu/hugegraph/{metric => metrics}/SystemMetrics.java | 2 +- 13 files changed, 21 insertions(+), 18 deletions(-) rename hugegraph-api/src/main/java/com/baidu/hugegraph/api/{ => gremlin}/GremlinAPI.java (97%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/api/{ => profile}/GraphsAPI.java (98%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/api/{ => profile}/ProfileAPI.java (98%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/api/{ => profile}/VersionAPI.java (95%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{metric => metrics}/MetricsModule.java (99%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{metric => metrics}/MetricsUtil.java (98%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{metric => metrics}/ServerReporter.java (99%) rename hugegraph-api/src/main/java/com/baidu/hugegraph/{metric => metrics}/SystemMetrics.java (99%) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java index 3f477f2409..48aa9fefc4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java @@ -35,7 +35,7 @@ import com.baidu.hugegraph.HugeGraph; import com.baidu.hugegraph.api.schema.Checkable; import com.baidu.hugegraph.core.GraphManager; -import com.baidu.hugegraph.metric.MetricsUtil; +import com.baidu.hugegraph.metrics.MetricsUtil; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.type.HugeType; import com.baidu.hugegraph.util.E; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/BatchAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/BatchAPI.java index bfa6a47d6c..517581da52 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/BatchAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/graph/BatchAPI.java @@ -29,7 +29,7 @@ import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; -import com.baidu.hugegraph.metric.MetricsUtil; +import com.baidu.hugegraph.metrics.MetricsUtil; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.util.Log; import com.codahale.metrics.Meter; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GremlinAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/gremlin/GremlinAPI.java similarity index 97% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/api/GremlinAPI.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/api/gremlin/GremlinAPI.java index 3973f4d1eb..361f334b02 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GremlinAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/gremlin/GremlinAPI.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.api; +package com.baidu.hugegraph.api.gremlin; import javax.inject.Singleton; import javax.ws.rs.Consumes; @@ -34,11 +34,12 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; +import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.api.filter.CompressInterceptor; import com.baidu.hugegraph.api.filter.CompressInterceptor.Compress; import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; -import com.baidu.hugegraph.metric.MetricsUtil; +import com.baidu.hugegraph.metrics.MetricsUtil; import com.codahale.metrics.Histogram; import com.codahale.metrics.annotation.Timed; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java index 46c3988cd5..1d710ba9d7 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java @@ -50,7 +50,7 @@ import com.baidu.hugegraph.core.GraphManager; import com.baidu.hugegraph.job.Job; import com.baidu.hugegraph.job.JobBuilder; -import com.baidu.hugegraph.metric.MetricsUtil; +import com.baidu.hugegraph.metrics.MetricsUtil; import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.traversal.optimize.HugeScriptTraversal; import com.baidu.hugegraph.util.E; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java index 629090196c..a886cb49b6 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/metrics/MetricsAPI.java @@ -38,9 +38,9 @@ import com.baidu.hugegraph.backend.store.BackendMetrics; import com.baidu.hugegraph.backend.tx.GraphTransaction; import com.baidu.hugegraph.core.GraphManager; -import com.baidu.hugegraph.metric.MetricsModule; -import com.baidu.hugegraph.metric.ServerReporter; -import com.baidu.hugegraph.metric.SystemMetrics; +import com.baidu.hugegraph.metrics.MetricsModule; +import com.baidu.hugegraph.metrics.ServerReporter; +import com.baidu.hugegraph.metrics.SystemMetrics; import com.baidu.hugegraph.util.InsertionOrderUtil; import com.baidu.hugegraph.util.JsonUtil; import com.baidu.hugegraph.util.Log; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/GraphsAPI.java similarity index 98% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/GraphsAPI.java index bd085bfbba..429505c54c 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/GraphsAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/GraphsAPI.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.api; +package com.baidu.hugegraph.api.profile; import java.io.File; import java.util.Map; @@ -40,6 +40,7 @@ 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.server.RestServer; import com.baidu.hugegraph.type.define.GraphMode; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java similarity index 98% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java index 6375017d97..d73af0daa2 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.api; +package com.baidu.hugegraph.api.profile; import java.util.ArrayList; import java.util.HashMap; @@ -67,7 +67,7 @@ public String getProfile(@Context Application application) { Map profiles = InsertionOrderUtil.newMap(); profiles.put("service", SERVICE); profiles.put("doc", DOC); - profiles.put("api-doc", API_DOC); + profiles.put("api_doc", API_DOC); Set apis = new HashSet<>(); for (Class clazz : application.getClasses()) { if (!isAnnotatedPathClass(clazz)) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/VersionAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/VersionAPI.java similarity index 95% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/api/VersionAPI.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/VersionAPI.java index ab849cc6dd..b4e0343b7d 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/VersionAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/VersionAPI.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.api; +package com.baidu.hugegraph.api.profile; import java.util.Map; @@ -27,6 +27,7 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; +import com.baidu.hugegraph.api.API; import com.baidu.hugegraph.version.ApiVersion; import com.baidu.hugegraph.version.CoreVersion; import com.codahale.metrics.annotation.Timed; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java index 5d52b8458b..7eb53b53b8 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/core/GraphManager.java @@ -44,8 +44,8 @@ import com.baidu.hugegraph.config.HugeConfig; import com.baidu.hugegraph.config.ServerOptions; import com.baidu.hugegraph.exception.NotSupportException; -import com.baidu.hugegraph.metric.MetricsUtil; -import com.baidu.hugegraph.metric.ServerReporter; +import com.baidu.hugegraph.metrics.MetricsUtil; +import com.baidu.hugegraph.metrics.ServerReporter; import com.baidu.hugegraph.serializer.JsonSerializer; import com.baidu.hugegraph.serializer.Serializer; import com.baidu.hugegraph.server.RestServer; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/MetricsModule.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/MetricsModule.java similarity index 99% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/metric/MetricsModule.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/MetricsModule.java index eecfe28065..8c0cc8ba67 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/MetricsModule.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/MetricsModule.java @@ -15,7 +15,7 @@ * under the License. */ -package com.baidu.hugegraph.metric; +package com.baidu.hugegraph.metrics; import java.io.IOException; import java.util.Arrays; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/MetricsUtil.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/MetricsUtil.java similarity index 98% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/metric/MetricsUtil.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/MetricsUtil.java index 5025f45361..576af23a88 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/MetricsUtil.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/MetricsUtil.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.metric; +package com.baidu.hugegraph.metrics; import org.apache.tinkerpop.gremlin.server.util.MetricManager; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/ServerReporter.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/ServerReporter.java similarity index 99% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/metric/ServerReporter.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/ServerReporter.java index cd53b3f005..956235edd4 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/ServerReporter.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/ServerReporter.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.metric; +package com.baidu.hugegraph.metrics; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/SystemMetrics.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/SystemMetrics.java similarity index 99% rename from hugegraph-api/src/main/java/com/baidu/hugegraph/metric/SystemMetrics.java rename to hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/SystemMetrics.java index 7de6c50f74..8d6c56fb07 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/metric/SystemMetrics.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/metrics/SystemMetrics.java @@ -17,7 +17,7 @@ * under the License. */ -package com.baidu.hugegraph.metric; +package com.baidu.hugegraph.metrics; import java.lang.management.ClassLoadingMXBean; import java.lang.management.GarbageCollectorMXBean; From a8ffe8ca652f773b775829205a4db07a50d98e94 Mon Sep 17 00:00:00 2001 From: liningrui Date: Thu, 30 May 2019 15:34:15 +0800 Subject: [PATCH 06/10] add vertion Change-Id: I3e4a28292fcd39cd463d9e9387643e9be1d784cd --- hugegraph-api/pom.xml | 2 +- .../java/com/baidu/hugegraph/api/profile/ProfileAPI.java | 3 +++ .../main/java/com/baidu/hugegraph/version/ApiVersion.java | 5 ++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/hugegraph-api/pom.xml b/hugegraph-api/pom.xml index 253b8a8074..fc27f55e2c 100644 --- a/hugegraph-api/pom.xml +++ b/hugegraph-api/pom.xml @@ -86,7 +86,7 @@ - 0.38.0.0 + 0.39.0.0 diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java index d73af0daa2..ecb25ae465 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java @@ -43,6 +43,8 @@ import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; import com.baidu.hugegraph.util.JsonUtil; +import com.baidu.hugegraph.version.ApiVersion; +import com.baidu.hugegraph.version.CoreVersion; import com.codahale.metrics.annotation.Timed; @Path("/") @@ -66,6 +68,7 @@ public String getProfile(@Context Application application) { Map profiles = InsertionOrderUtil.newMap(); profiles.put("service", SERVICE); + profiles.put("version", CoreVersion.VERSION.toString()); profiles.put("doc", DOC); profiles.put("api_doc", API_DOC); Set apis = new HashSet<>(); 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 c6438adc49..80656b1552 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 @@ -84,10 +84,13 @@ public final class ApiVersion { * [0.36] Issue-360: Support paging for scan api * [0.37] Issue-391: Add skip_super_node for shortest path * [0.38] Issue-274: Add personalrank and neighborrank RESTful API + * + * version 0.10: + * [0.39] Issue-522: Add profile RESTful API */ // The second parameter of Version.of() is for IDE running without JAR - public static final Version VERSION = Version.of(ApiVersion.class, "0.38"); + public static final Version VERSION = Version.of(ApiVersion.class, "0.39"); public static final void check() { // Check version of hugegraph-core. Firstly do check from version 0.3 From 88354e08821bcce28049b1e3792bf809ce2103a9 Mon Sep 17 00:00:00 2001 From: liningrui Date: Thu, 30 May 2019 16:49:03 +0800 Subject: [PATCH 07/10] remove unused import Change-Id: I7224a14eb6241f12d459e6263ff47acf93a59d2d --- .../main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java index ecb25ae465..de3d68b9f6 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java @@ -43,7 +43,6 @@ import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; import com.baidu.hugegraph.util.JsonUtil; -import com.baidu.hugegraph.version.ApiVersion; import com.baidu.hugegraph.version.CoreVersion; import com.codahale.metrics.annotation.Timed; From 56c31e3f42d36d3c4aa2aa27eb9b90a7fa34441f Mon Sep 17 00:00:00 2001 From: liningrui Date: Thu, 30 May 2019 18:13:12 +0800 Subject: [PATCH 08/10] improve Change-Id: I9ce487c6359ef1a4ed188c8638a7b3812ac27c4d --- .../hugegraph/api/profile/ProfileAPI.java | 102 ++++++++++-------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java index de3d68b9f6..c575368bf0 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java @@ -20,11 +20,11 @@ package com.baidu.hugegraph.api.profile; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeMap; import javax.inject.Singleton; import javax.ws.rs.GET; @@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.tinkerpop.shaded.jackson.annotation.JsonProperty; +import org.glassfish.jersey.server.model.Parameter; import org.glassfish.jersey.server.model.Parameter.Source; import org.glassfish.jersey.server.model.Resource; import org.glassfish.jersey.server.model.ResourceMethod; @@ -76,8 +77,7 @@ public String getProfile(@Context Application application) { continue; } Resource resource = Resource.from(clazz); - String fullName = resource.getName(); - APICategory apiCategory = getCategory(fullName); + APICategory apiCategory = getCategory(resource.getName()); apis.add(apiCategory.category); } profiles.put("apis", apis); @@ -94,34 +94,27 @@ public String showAllAPIs(@Context Application application) { return API_PROFILES; } - Map>> apiProfiles = new HashMap<>(); + APIProfiles apiProfiles = new APIProfiles(); for (Class clazz : application.getClasses()) { if (!isAnnotatedPathClass(clazz)) { continue; } Resource resource = Resource.from(clazz); - String fullName = resource.getName(); - APICategory apiCategory = getCategory(fullName); - - Map> subApiProfiles; - subApiProfiles = apiProfiles.computeIfAbsent(apiCategory.category, - k -> new HashMap<>()); - List profiles = new ArrayList<>(); - subApiProfiles.put(apiCategory.subCategory, profiles); + APICategory apiCategory = getCategory(resource.getName()); String url = resource.getPath(); // List all methods of this resource for (ResourceMethod rm : resource.getResourceMethods()) { APIProfile profile = APIProfile.parse(url, rm); - profiles.add(profile); + apiProfiles.put(apiCategory, profile); } // List all methods of this resource's child resources for (Resource childResource : resource.getChildResources()) { String childUrl = url + "/" + childResource.getPath(); for (ResourceMethod rm : childResource.getResourceMethods()) { APIProfile profile = APIProfile.parse(childUrl, rm); - profiles.add(profile); + apiProfiles.put(apiCategory, profile); } } } @@ -144,9 +137,29 @@ private static boolean isAnnotatedPathClass(Class rc) { private static APICategory getCategory(String fullName) { String[] parts = StringUtils.split(fullName, "."); E.checkState(parts.length >= 2, "Invalid api name"); - String category = parts[parts.length - 2]; - String subCategory = parts[parts.length - 1]; - return new APICategory(category, subCategory); + String dir = parts[parts.length - 2]; + String category = parts[parts.length - 1]; + return new APICategory(dir, category); + } + + private static class APIProfiles { + + @JsonProperty("apis") + private final Map>> apis; + + public APIProfiles() { + this.apis = new TreeMap<>(); + } + + public void put(APICategory category, APIProfile profile) { + Map> categories; + categories = this.apis.computeIfAbsent(category.dir, + k -> new TreeMap<>()); + List profiles = categories.computeIfAbsent( + category.category, + k -> new ArrayList<>()); + profiles.add(profile); + } } private static class APIProfile { @@ -156,56 +169,61 @@ private static class APIProfile { @JsonProperty("method") private final String method; @JsonProperty("parameters") - private final List parameters; + private final List parameters; public APIProfile(String url, String method, - List parameters) { + List parameters) { this.url = url; this.method = method; this.parameters = parameters; } - public static APIProfile parse(String url, ResourceMethod rm) { - String method = rm.getHttpMethod(); - List apiParameters = new ArrayList<>(); - for (org.glassfish.jersey.server.model.Parameter parameter : - rm.getInvocable().getParameters()) { - String sourceName = parameter.getSourceName(); - if (sourceName == null) { - continue; - } - if (parameter.getSource() == Source.PATH) { - continue; + public static APIProfile parse(String url, ResourceMethod resource) { + String method = resource.getHttpMethod(); + List params = new ArrayList<>(); + for (Parameter param : resource.getInvocable().getParameters()) { + if (param.getSource() == Source.QUERY) { + String name = param.getSourceName(); + String type = param.getType().getTypeName(); + String defaultValue = param.getDefaultValue(); + params.add(new ParamInfo(name, type, defaultValue)); + } else if (param.getSource() == Source.ENTITY) { + String type = param.getType().getTypeName(); + params.add(new ParamInfo("body", type)); } - String typeName = parameter.getType().getTypeName(); - Parameter apiParameter = new Parameter(sourceName, typeName); - apiParameters.add(apiParameter); } - return new APIProfile(url, method, apiParameters); + return new APIProfile(url, method, params); } - private static class Parameter { + private static class ParamInfo { @JsonProperty("name") - private String name; + private final String name; @JsonProperty("type") - private String type; + private final String type; + @JsonProperty("default_value") + private final String defaultValue; + + public ParamInfo(String name, String type) { + this(name, type, null); + } - public Parameter(String name, String type) { + public ParamInfo(String name, String type, String defaultValue) { this.name = name; this.type = type; + this.defaultValue = defaultValue; } } } private static class APICategory { - private String category; - private String subCategory; + private final String dir; + private final String category; - public APICategory(String category, String subCategory) { + public APICategory(String dir, String category) { + this.dir = dir; this.category = category; - this.subCategory = subCategory; } } } From e780e8d1f7af9e8a1858c5da1326061eae9e3682 Mon Sep 17 00:00:00 2001 From: liningrui Date: Thu, 30 May 2019 20:18:51 +0800 Subject: [PATCH 09/10] tiny improve Change-Id: I7ccd32b2afc16457caf73aae2073c5f23d3bf3fb --- .../hugegraph/api/profile/ProfileAPI.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java index c575368bf0..e3ec1297a7 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java @@ -20,11 +20,11 @@ package com.baidu.hugegraph.api.profile; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import java.util.TreeSet; import javax.inject.Singleton; import javax.ws.rs.GET; @@ -62,6 +62,7 @@ public class ProfileAPI { @Timed @Produces(MediaType.APPLICATION_JSON) public String getProfile(@Context Application application) { + // May init multi times by multi threads, but no effect on the results if (SERVER_PROFILES != null) { return SERVER_PROFILES; } @@ -71,14 +72,14 @@ public String getProfile(@Context Application application) { profiles.put("version", CoreVersion.VERSION.toString()); profiles.put("doc", DOC); profiles.put("api_doc", API_DOC); - Set apis = new HashSet<>(); + Set apis = new TreeSet<>(); for (Class clazz : application.getClasses()) { if (!isAnnotatedPathClass(clazz)) { continue; } Resource resource = Resource.from(clazz); - APICategory apiCategory = getCategory(resource.getName()); - apis.add(apiCategory.category); + APICategory apiCategory = APICategory.parse(resource.getName()); + apis.add(apiCategory.dir); } profiles.put("apis", apis); SERVER_PROFILES = JsonUtil.toJson(profiles); @@ -101,7 +102,7 @@ public String showAllAPIs(@Context Application application) { } Resource resource = Resource.from(clazz); - APICategory apiCategory = getCategory(resource.getName()); + APICategory apiCategory = APICategory.parse(resource.getName()); String url = resource.getPath(); // List all methods of this resource @@ -122,26 +123,18 @@ public String showAllAPIs(@Context Application application) { return API_PROFILES; } - private static boolean isAnnotatedPathClass(Class rc) { - if (rc.isAnnotationPresent(Path.class)) { + private static boolean isAnnotatedPathClass(Class clazz) { + if (clazz.isAnnotationPresent(Path.class)) { return true; } - for (Class clazz : rc.getInterfaces()) { - if (clazz.isAnnotationPresent(Path.class)) { + for (Class i : clazz.getInterfaces()) { + if (i.isAnnotationPresent(Path.class)) { return true; } } return false; } - private static APICategory getCategory(String fullName) { - String[] parts = StringUtils.split(fullName, "."); - E.checkState(parts.length >= 2, "Invalid api name"); - String dir = parts[parts.length - 2]; - String category = parts[parts.length - 1]; - return new APICategory(dir, category); - } - private static class APIProfiles { @JsonProperty("apis") @@ -225,5 +218,14 @@ public APICategory(String dir, String category) { this.dir = dir; this.category = category; } + + public static APICategory parse(String fullName) { + String[] parts = StringUtils.split(fullName, "."); + E.checkState(parts.length >= 2, "Invalid api name"); + String dir = parts[parts.length - 2]; + String category = parts[parts.length - 1]; + return new APICategory(dir, category); + } + } } From 5ab35f84f78b34b92a9facbf8329ba1708387b28 Mon Sep 17 00:00:00 2001 From: liningrui Date: Fri, 31 May 2019 14:03:25 +0800 Subject: [PATCH 10/10] remove redundant empty line Change-Id: I1be0bd5592ff21f7d5914b1042d599816f6020d0 --- .../main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java index e3ec1297a7..5860117f5b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/profile/ProfileAPI.java @@ -226,6 +226,5 @@ public static APICategory parse(String fullName) { String category = parts[parts.length - 1]; return new APICategory(dir, category); } - } }