Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hugegraph-api code checkstyle #1845

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions hugegraph-api/src/main/java/com/baidu/hugegraph/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public class API {
public static final String ACTION_ELIMINATE = "eliminate";
public static final String ACTION_CLEAR = "clear";

private static final Meter succeedMeter =
private static final Meter SUCCEED_METER =
MetricsUtil.registerMeter(API.class, "commit-succeed");
private static final Meter illegalArgErrorMeter =
private static final Meter ILLEGAL_ARG_ERROR_METER =
MetricsUtil.registerMeter(API.class, "illegal-arg");
private static final Meter expectedErrorMeter =
private static final Meter EXPECTED_ERROR_METER =
MetricsUtil.registerMeter(API.class, "expected-error");
private static final Meter unknownErrorMeter =
private static final Meter UNKNOWN_ERROR_METER =
MetricsUtil.registerMeter(API.class, "unknown-error");

public static HugeGraph graph(GraphManager manager, String graph) {
Expand Down Expand Up @@ -96,19 +96,19 @@ public static <R> R commit(HugeGraph g, Callable<R> callable) {
try {
R result = callable.call();
g.tx().commit();
succeedMeter.mark();
SUCCEED_METER.mark();
return result;
} catch (IllegalArgumentException | NotFoundException |
ForbiddenException e) {
illegalArgErrorMeter.mark();
ILLEGAL_ARG_ERROR_METER.mark();
rollback.accept(null);
throw e;
} catch (RuntimeException e) {
expectedErrorMeter.mark();
EXPECTED_ERROR_METER.mark();
rollback.accept(e);
throw e;
} catch (Throwable e) {
unknownErrorMeter.mark();
UNKNOWN_ERROR_METER.mark();
rollback.accept(e);
// TODO: throw the origin exception 'e'
throw new HugeException("Failed to commit", e);
Expand Down Expand Up @@ -171,7 +171,9 @@ protected static Map<String, Object> parseProperties(String properties) {
Map<String, Object> props = null;
try {
props = JsonUtil.fromJson(properties, Map.class);
} catch (Exception ignored) {}
} catch (Exception ignored) {
// ignore
}

// If properties is the string "null", props will be null
E.checkArgument(props != null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public void delete(@Context GraphManager manager,
throw new IllegalArgumentException("Invalid project id: " + id);
}
}

public static boolean isAddGraph(String action) {
return ACTION_ADD_GRAPH.equals(action);
}
Expand Down Expand Up @@ -267,6 +268,6 @@ public void checkUpdate() {
this.description != null,
"Must specify 'graphs' or 'description' " +
"field that need to be updated");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public class BatchAPI extends API {
private static final Logger LOG = Log.logger(BatchAPI.class);

// NOTE: VertexAPI and EdgeAPI should share a counter
private static final AtomicInteger batchWriteThreads = new AtomicInteger(0);
private static final AtomicInteger BATCH_WRITE_THREADS = new AtomicInteger(0);

static {
MetricsUtil.registerGauge(RestServer.class, "batch-write-threads",
() -> batchWriteThreads.intValue());
BATCH_WRITE_THREADS::intValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align with RestServer

}

private final Meter batchMeter;
Expand All @@ -64,24 +64,24 @@ public BatchAPI() {
public <R> R commit(HugeConfig config, HugeGraph g, int size,
Callable<R> callable) {
int maxWriteThreads = config.get(ServerOptions.MAX_WRITE_THREADS);
int writingThreads = batchWriteThreads.incrementAndGet();
int writingThreads = BATCH_WRITE_THREADS.incrementAndGet();
if (writingThreads > maxWriteThreads) {
batchWriteThreads.decrementAndGet();
BATCH_WRITE_THREADS.decrementAndGet();
throw new HugeException("The rest server is too busy to write");
}

LOG.debug("The batch writing threads is {}", batchWriteThreads);
LOG.debug("The batch writing threads is {}", BATCH_WRITE_THREADS);
try {
R result = commit(g, callable);
this.batchMeter.mark(size);
return result;
} finally {
batchWriteThreads.decrementAndGet();
BATCH_WRITE_THREADS.decrementAndGet();
}
}

@JsonIgnoreProperties(value = {"type"})
protected static abstract class JsonElement implements Checkable {
protected abstract static class JsonElement implements Checkable {

@JsonProperty("id")
public Object id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
@Singleton
public class GremlinAPI extends API {

private static final Histogram gremlinInputHistogram =
private static final Histogram GREMLIN_INPUT_HISTOGRAM =
MetricsUtil.registerHistogram(GremlinAPI.class, "gremlin-input");
private static final Histogram gremlinOutputHistogram =
private static final Histogram GREMLIN_OUTPUT_HISTOGRAM =
MetricsUtil.registerHistogram(GremlinAPI.class, "gremlin-output");

private static final Set<String> FORBIDDEN_REQUEST_EXCEPTIONS =
Expand Down Expand Up @@ -99,14 +99,14 @@ public Response post(@Context HugeConfig conf,
// .build();
String auth = headers.getHeaderString(HttpHeaders.AUTHORIZATION);
Response response = this.client().doPostRequest(auth, request);
gremlinInputHistogram.update(request.length());
gremlinOutputHistogram.update(response.getLength());
GREMLIN_INPUT_HISTOGRAM.update(request.length());
GREMLIN_OUTPUT_HISTOGRAM.update(response.getLength());
return transformResponseIfNeeded(response);
}

@GET
@Timed
@Compress(buffer=(1024 * 40))
@Compress(buffer = (1024 * 40))
@Produces(APPLICATION_JSON_WITH_CHARSET)
public Response get(@Context HugeConfig conf,
@Context HttpHeaders headers,
Expand All @@ -115,8 +115,8 @@ public Response get(@Context HugeConfig conf,
String query = uriInfo.getRequestUri().getRawQuery();
MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
Response response = this.client().doGetRequest(auth, params);
gremlinInputHistogram.update(query.length());
gremlinOutputHistogram.update(response.getLength());
GREMLIN_INPUT_HISTOGRAM.update(query.length());
GREMLIN_OUTPUT_HISTOGRAM.update(response.getLength());
return transformResponseIfNeeded(response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class GremlinAPI extends API {

private static final int MAX_NAME_LENGTH = 256;

private static final Histogram gremlinJobInputHistogram =
private static final Histogram GREMLIN_JOB_INPUT_HISTOGRAM =
MetricsUtil.registerHistogram(GremlinAPI.class, "gremlin-input");

@POST
Expand All @@ -79,7 +79,7 @@ public Map<String, Id> post(@Context GraphManager manager,
GremlinRequest request) {
LOG.debug("Graph [{}] schedule gremlin job: {}", graph, request);
checkCreatingBody(request);
gremlinJobInputHistogram.update(request.gremlin.length());
GREMLIN_JOB_INPUT_HISTOGRAM.update(request.gremlin.length());

HugeGraph g = graph(manager, graph);
request.aliase(graph, "graph");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ public APIProfiles() {

public void put(APICategory category, APIProfile profile) {
Map<String, List<APIProfile>> categories;
categories = this.apis.computeIfAbsent(category.dir,
k -> new TreeMap<>());
categories = this.apis.computeIfAbsent(category.dir, k -> new TreeMap<>());
List<APIProfile> profiles = categories.computeIfAbsent(
category.category,
k -> new ArrayList<>());
category.category, k -> new ArrayList<>());
profiles.add(profile);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public String update(@Context GraphManager manager,

HugeGraph g = graph(manager, graph);
IndexLabel.Builder builder = jsonIndexLabel.convert2Builder(g);
IndexLabel IndexLabel = append ? builder.append() : builder.eliminate();
return manager.serializer(g).writeIndexlabel(mapIndexLabel(IndexLabel));
IndexLabel indexLabel = append ? builder.append() : builder.eliminate();
return manager.serializer(g).writeIndexlabel(mapIndexLabel(indexLabel));
}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public String get(@Context GraphManager manager,
double similarity;
try (JaccardSimilarTraverser traverser =
new JaccardSimilarTraverser(g)) {
similarity = traverser.jaccardSimilarity(sourceId, targetId, dir,
similarity = traverser.jaccardSimilarity(sourceId, targetId, dir,
edgeLabel, maxDegree);
}
return JsonUtil.toJson(ImmutableMap.of("jaccard_similarity",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,33 @@

public interface HugeAuthenticator extends Authenticator {

public static final String KEY_USERNAME =
CredentialGraphTokens.PROPERTY_USERNAME;
public static final String KEY_PASSWORD =
CredentialGraphTokens.PROPERTY_PASSWORD;
public static final String KEY_TOKEN = "token";
public static final String KEY_ROLE = "role";
public static final String KEY_ADDRESS = "address";
public static final String KEY_PATH = "path";
String KEY_USERNAME = CredentialGraphTokens.PROPERTY_USERNAME;
String KEY_PASSWORD = CredentialGraphTokens.PROPERTY_PASSWORD;
String KEY_TOKEN = "token";
String KEY_ROLE = "role";
String KEY_ADDRESS = "address";
String KEY_PATH = "path";

public static final String USER_SYSTEM = "system";
public static final String USER_ADMIN = "admin";
public static final String USER_ANONY = AuthenticatedUser.ANONYMOUS_USERNAME;
String USER_SYSTEM = "system";
String USER_ADMIN = "admin";
String USER_ANONY = AuthenticatedUser.ANONYMOUS_USERNAME;

public static final RolePermission ROLE_NONE = RolePermission.none();
public static final RolePermission ROLE_ADMIN = RolePermission.admin();
RolePermission ROLE_NONE = RolePermission.none();
RolePermission ROLE_ADMIN = RolePermission.admin();

public static final String VAR_PREFIX = "$";
public static final String KEY_OWNER = VAR_PREFIX + "owner";
public static final String KEY_DYNAMIC = VAR_PREFIX + "dynamic";
public static final String KEY_ACTION = VAR_PREFIX + "action";
String VAR_PREFIX = "$";
String KEY_OWNER = VAR_PREFIX + "owner";
String KEY_DYNAMIC = VAR_PREFIX + "dynamic";
String KEY_ACTION = VAR_PREFIX + "action";

public void setup(HugeConfig config);
void setup(HugeConfig config);

public UserWithRole authenticate(String username, String password,
String token);
public AuthManager authManager();
UserWithRole authenticate(String username, String password, String token);

AuthManager authManager();

@Override
public default void setup(final Map<String, Object> config) {
default void setup(final Map<String, Object> config) {
E.checkState(config != null,
"Must provide a 'config' in the 'authentication'");
String path = (String) config.get("tokens");
Expand All @@ -82,7 +80,7 @@ public default void setup(final Map<String, Object> config) {
}

@Override
public default User authenticate(final Map<String, String> credentials)
default User authenticate(final Map<String, String> credentials)
throws AuthenticationException {
HugeGraphAuthProxy.resetContext();

Expand Down Expand Up @@ -114,21 +112,17 @@ public default User authenticate(final Map<String, String> credentials)
}

@Override
public default boolean requireAuthentication() {
default boolean requireAuthentication() {
return true;
}

public default boolean verifyRole(RolePermission role) {
if (role == ROLE_NONE || role == null) {
return false;
} else {
return true;
}
default boolean verifyRole(RolePermission role) {
return role != ROLE_NONE && role != null;
}

public void initAdminUser(String password) throws Exception;
void initAdminUser(String password) throws Exception;

public static HugeAuthenticator loadAuthenticator(HugeConfig conf) {
static HugeAuthenticator loadAuthenticator(HugeConfig conf) {
String authClass = conf.get(ServerOptions.AUTHENTICATOR);
if (authClass.isEmpty()) {
return null;
Expand All @@ -149,7 +143,7 @@ public static HugeAuthenticator loadAuthenticator(HugeConfig conf) {
return authenticator;
}

public static class User extends AuthenticatedUser {
class User extends AuthenticatedUser {

public static final User ADMIN = new User(USER_ADMIN, ROLE_ADMIN);
public static final User ANONYMOUS = new User(USER_ANONY, ROLE_ADMIN);
Expand Down Expand Up @@ -254,7 +248,7 @@ public static class UserJson {
}
}

public static class RolePerm {
class RolePerm {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to keep public static class


@JsonProperty("roles") // graph -> action -> resource
private Map<String, Map<HugePermission, Object>> roles;
Expand Down Expand Up @@ -394,7 +388,7 @@ public static boolean match(Object role, RolePermission grant,
}
}

public static class RequiredPerm {
class RequiredPerm {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer to keep public static class


@JsonProperty("owner")
private String owner;
Expand Down
Loading