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

java: implement checkstyle #4599

Merged
merged 10 commits into from
Feb 12, 2019
Merged
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
1 change: 1 addition & 0 deletions java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
21 changes: 21 additions & 0 deletions java/checkstyle-suppression.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
<suppress files="[\\/]generated-sources[\\/]" checks=".*"/>

<suppress checks="JavadocParagraph" files=".*[\\/]src[\\/](test|main)[\\/].*\.java"/>
<suppress checks="AbbreviationAsWordInName" files=".*[\\/]src[\\/](test|main)[\\/].*\.java"/>
<suppress checks="JavadocMethod" files=".*[\\/]src[\\/](test|main)[\\/].*\.java"/>
<suppress checks="SummaryJavadoc" files=".*[\\/]src[\\/](test|main)[\\/].*\.java"/>
<suppress checks="OverloadMethodsDeclarationOrder" files=".*[\\/]src[\\/](test|main)[\\/].*\.java"/>

<suppress checks="LocalVariableName" files="StringUtils.java" />
<suppress checks="ParameterName" files="StringUtils.java" />
<suppress checks="VariableDeclarationUsageDistance" files="StringUtils.java" />

<suppress checks="MissingSwitchDefault" files="VitessResultSet.java|VitessPreparedStatement.java" />
</suppressions>
54 changes: 27 additions & 27 deletions java/client/src/main/java/io/vitess/client/Context.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2017 Google Inc.
*
*
* Licensed 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.
Expand All @@ -16,31 +16,41 @@

package io.vitess.client;

import javax.annotation.Nullable;
import io.vitess.proto.Vtrpc.CallerID;

import org.joda.time.Duration;
import org.joda.time.Instant;

import io.vitess.proto.Vtrpc.CallerID;
import javax.annotation.Nullable;

/**
* Context is an immutable object that carries per-request info.
*
* <p>RPC frameworks like gRPC have their own Context implementations that
* allow propagation of deadlines, cancellation, and end-user credentials
* across RPC boundaries (between client and server). Since these
* framework-specific Context implementations are not compatible with one
* another, we provide our own Context class that wraps common features.
* allow propagation of deadlines, cancellation, and end-user credentials across RPC boundaries
* (between client and server). Since these framework-specific Context implementations are not
* compatible with one another, we provide our own Context class that wraps common features.
*
* <p>In gRPC and other frameworks, the current Context is maintained in
* thread-local storage, so it's implicitly available to any method that
* needs it. In this Vitess client library, we pass Context as an explicit
* parameter to methods that need it. This allows us to defer enforcement
* of the specified request constraints until the request reaches the
* underlying framework-specific Vitess client implementation, at which point
* the native Context class can be used.
* thread-local storage, so it's implicitly available to any method that needs it. In this Vitess
* client library, we pass Context as an explicit parameter to methods that need it. This allows us
* to defer enforcement of the specified request constraints until the request reaches the
* underlying framework-specific Vitess client implementation, at which point the native Context
* class can be used.
*/
public class Context {

private static final Context DEFAULT_CONTEXT = new Context();
private Instant deadline;
private CallerID callerId;

private Context() {
}

private Context(Instant deadline, CallerID callerId) {
this.deadline = deadline;
this.callerId = callerId;
}

// getDefault returns an empty context.
public static Context getDefault() {
Expand All @@ -57,8 +67,8 @@ public Context withDeadline(Instant deadline) {
}

/**
* withDeadlineAfter returns a derived context with a maximum deadline
* specified relative to the current time.
* withDeadlineAfter returns a derived context with a maximum deadline specified relative to the
* current time.
*/
public Context withDeadlineAfter(Duration duration) {
return withDeadline(Instant.now().plus(duration));
Expand Down Expand Up @@ -90,14 +100,4 @@ public Duration getTimeout() {
public CallerID getCallerId() {
return callerId;
}

private Instant deadline;
private CallerID callerId;

private Context() {}

private Context(Instant deadline, CallerID callerId) {
this.deadline = deadline;
this.callerId = callerId;
}
}
70 changes: 36 additions & 34 deletions java/client/src/main/java/io/vitess/client/Proto.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2017 Google Inc.
*
*
* Licensed 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.
Expand All @@ -21,6 +21,19 @@
import com.google.common.collect.Iterables;
import com.google.common.primitives.UnsignedLong;
import com.google.protobuf.ByteString;

import io.vitess.client.cursor.Cursor;
import io.vitess.client.cursor.CursorWithError;
import io.vitess.client.cursor.SimpleCursor;
import io.vitess.proto.Query;
import io.vitess.proto.Query.BindVariable;
import io.vitess.proto.Query.BoundQuery;
import io.vitess.proto.Query.QueryResult;
import io.vitess.proto.Vtgate.BoundKeyspaceIdQuery;
import io.vitess.proto.Vtgate.BoundShardQuery;
import io.vitess.proto.Vtgate.ExecuteEntityIdsRequest.EntityId;
import io.vitess.proto.Vtrpc.RPCError;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
Expand All @@ -34,25 +47,28 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

import io.vitess.client.cursor.Cursor;
import io.vitess.client.cursor.CursorWithError;
import io.vitess.client.cursor.SimpleCursor;
import io.vitess.proto.Query;
import io.vitess.proto.Query.BindVariable;
import io.vitess.proto.Query.BoundQuery;
import io.vitess.proto.Query.QueryResult;
import io.vitess.proto.Vtgate.BoundKeyspaceIdQuery;
import io.vitess.proto.Vtgate.BoundShardQuery;
import io.vitess.proto.Vtgate.ExecuteEntityIdsRequest.EntityId;
import io.vitess.proto.Vtrpc.RPCError;
import javax.annotation.Nullable;

/**
* Proto contains methods for working with Vitess protobuf messages.
*/
public class Proto {

public static final Function<byte[], ByteString> BYTE_ARRAY_TO_BYTE_STRING =
new Function<byte[], ByteString>() {
@Override
public ByteString apply(byte[] from) {
return ByteString.copyFrom(from);
}
};
public static final Function<Map.Entry<byte[], ?>, EntityId> MAP_ENTRY_TO_ENTITY_KEYSPACE_ID =
new Function<Map.Entry<byte[], ?>, EntityId>() {
@Override
public EntityId apply(Map.Entry<byte[], ?> entry) {
return buildEntityId(entry.getKey(), entry.getValue());
}
};
private static final int MAX_DECIMAL_UNIT = 30;

/**
Expand Down Expand Up @@ -133,7 +149,7 @@ public static int getErrno(@Nullable String errorMessage) {
}
try {
return Integer.parseInt(errorMessage.substring(start, end));
} catch (NumberFormatException e) {
} catch (NumberFormatException exc) {
return 0;
}
}
Expand Down Expand Up @@ -256,34 +272,20 @@ public static List<Cursor> toCursorList(List<QueryResult> queryResults) {
return builder.build();
}

public static List<CursorWithError> fromQueryResponsesToCursorList(List<Query.ResultWithError> resultWithErrorList) {
public static List<CursorWithError> fromQueryResponsesToCursorList(
List<Query.ResultWithError> resultWithErrorList) {
ImmutableList.Builder<CursorWithError> builder = new ImmutableList.Builder<CursorWithError>();
for (Query.ResultWithError resultWithError : resultWithErrorList) {
builder.add(new CursorWithError(resultWithError));
}
return builder.build();
}

public static final Function<byte[], ByteString> BYTE_ARRAY_TO_BYTE_STRING =
new Function<byte[], ByteString>() {
@Override
public ByteString apply(byte[] from) {
return ByteString.copyFrom(from);
}
};

public static final Function<Map.Entry<byte[], ?>, EntityId> MAP_ENTRY_TO_ENTITY_KEYSPACE_ID =
new Function<Map.Entry<byte[], ?>, EntityId>() {
@Override
public EntityId apply(Map.Entry<byte[], ?> entry) {
return buildEntityId(entry.getKey(), entry.getValue());
}
};

/**
* Represents a type and value in the type system used in query.proto.
*/
protected static class TypedValue {

Query.Type type;
ByteString value;

Expand All @@ -300,7 +302,7 @@ protected static class TypedValue {
this.type = Query.Type.VARBINARY;
this.value = ByteString.copyFrom((byte[]) value);
} else if (value instanceof Integer || value instanceof Long || value instanceof Short
|| value instanceof Byte ) {
|| value instanceof Byte) {
// Int32, Int64, Short, Byte
this.type = Query.Type.INT64;
this.value = ByteString.copyFromUtf8(value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@
import java.io.File;

public class RefreshableVTGateConnection extends VTGateConnection {
private final File keystoreFile;
private final File truststoreFile;
private volatile long keystoreMtime;
private volatile long truststoreMtime;

public RefreshableVTGateConnection(RpcClient client,
String keystorePath,
String truststorePath) {
super(client);
this.keystoreFile = new File(keystorePath);
this.truststoreFile = new File(truststorePath);
this.keystoreMtime = this.keystoreFile.exists() ? this.keystoreFile.lastModified() : 0;
this.truststoreMtime = this.truststoreFile.exists() ? this.truststoreFile.lastModified() : 0;
}
private final File keystoreFile;
private final File truststoreFile;
private volatile long keystoreMtime;
private volatile long truststoreMtime;

public RefreshableVTGateConnection(RpcClient client,
String keystorePath,
String truststorePath) {
super(client);
this.keystoreFile = new File(keystorePath);
this.truststoreFile = new File(truststorePath);
this.keystoreMtime = this.keystoreFile.exists() ? this.keystoreFile.lastModified() : 0;
this.truststoreMtime = this.truststoreFile.exists() ? this.truststoreFile.lastModified() : 0;
}

public boolean checkKeystoreUpdates() {
long keystoreMtime = keystoreFile.exists() ? keystoreFile.lastModified() : 0;
long truststoreMtime = truststoreFile.exists() ? truststoreFile.lastModified() : 0;
boolean modified = false;
if (keystoreMtime > this.keystoreMtime) {
modified = true;
this.keystoreMtime = keystoreMtime;
}
if (truststoreMtime > this.truststoreMtime) {
modified = true;
this.truststoreMtime = truststoreMtime;
}
return modified;
public boolean checkKeystoreUpdates() {
long keystoreMtime = keystoreFile.exists() ? keystoreFile.lastModified() : 0;
long truststoreMtime = truststoreFile.exists() ? truststoreFile.lastModified() : 0;
boolean modified = false;
if (keystoreMtime > this.keystoreMtime) {
modified = true;
this.keystoreMtime = keystoreMtime;
}
if (truststoreMtime > this.truststoreMtime) {
modified = true;
this.truststoreMtime = truststoreMtime;
}
return modified;
}
}
Loading