Skip to content

Commit

Permalink
apacheGH-40831: [Java] Adding Spotless to Tools module (apache#42058)
Browse files Browse the repository at this point in the history
### Rationale for this change

Applying Java code style and formatting options to Tools module.

### What changes are included in this PR?

Java code formatting via spotless plugin has been enabled.

### Are these changes tested?

Yes, but doesn't involve test cases, the plugin itself corrects.

### Are there any user-facing changes?

No

* GitHub Issue: apache#40831

Authored-by: Laurent Goujon <laurent@apache.org>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
laurentgo authored Jun 10, 2024
1 parent 8710bb2 commit 2f4354d
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 212 deletions.
5 changes: 5 additions & 0 deletions java/tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ under the License.
<name>Arrow Tools</name>
<description>Java applications for working with Arrow ValueVectors.</description>

<properties>
<checkstyle.config.location>dev/checkstyle/checkstyle-spotless.xml</checkstyle.config.location>
<spotless.java.excludes>none</spotless.java.excludes>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.arrow</groupId>
Expand Down
26 changes: 6 additions & 20 deletions java/tools/src/main/java/org/apache/arrow/tools/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.tools;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.util.Preconditions;
Expand All @@ -30,26 +28,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Simple server that echoes back data received.
*/
/** Simple server that echoes back data received. */
public class EchoServer {
private static final Logger LOGGER = LoggerFactory.getLogger(EchoServer.class);
private final ServerSocket serverSocket;
private boolean closed = false;

/**
* Constructs a new instance that binds to the given port.
*/
/** Constructs a new instance that binds to the given port. */
public EchoServer(int port) throws IOException {
LOGGER.debug("Starting echo server.");
serverSocket = new ServerSocket(port);
LOGGER.debug("Running echo server on port: " + port());
}

/**
* Main method to run the server, the first argument is an optional port number.
*/
/** Main method to run the server, the first argument is an optional port number. */
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
Expand All @@ -64,9 +56,7 @@ public int port() {
return serverSocket.getLocalPort();
}

/**
* Starts the main server event loop.
*/
/** Starts the main server event loop. */
public void run() throws IOException {
try {
Socket clientSocket = null;
Expand Down Expand Up @@ -98,19 +88,15 @@ public void close() throws IOException {
serverSocket.close();
}

/**
* Handler for each client connection to the server.
*/
/** Handler for each client connection to the server. */
public static class ClientConnection implements AutoCloseable {
public final Socket socket;

public ClientConnection(Socket socket) {
this.socket = socket;
}

/**
* Reads a record batch off the socket and writes it back out.
*/
/** Reads a record batch off the socket and writes it back out. */
public void run() throws IOException {
// Read the entire input stream and write it back
try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE)) {
Expand Down
18 changes: 6 additions & 12 deletions java/tools/src/main/java/org/apache/arrow/tools/FileRoundtrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VectorSchemaRoot;
Expand All @@ -37,9 +35,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Application that verifies data can be round-tripped through a file.
*/
/** Application that verifies data can be round-tripped through a file. */
public class FileRoundtrip {
private static final Logger LOGGER = LoggerFactory.getLogger(FileRoundtrip.class);
private final Options options;
Expand All @@ -50,7 +46,6 @@ public class FileRoundtrip {
this.options = new Options();
this.options.addOption("i", "in", true, "input file");
this.options.addOption("o", "out", true, "output file");

}

public static void main(String[] args) {
Expand Down Expand Up @@ -80,18 +75,18 @@ int run(String[] args) {
File outFile = validateFile("output", outFileName);

try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
FileInputStream fileInputStream = new FileInputStream(inFile);
ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(),
allocator)) {
FileInputStream fileInputStream = new FileInputStream(inFile);
ArrowFileReader arrowReader =
new ArrowFileReader(fileInputStream.getChannel(), allocator)) {

VectorSchemaRoot root = arrowReader.getVectorSchemaRoot();
Schema schema = root.getSchema();
LOGGER.debug("Input file size: " + inFile.length());
LOGGER.debug("Found schema: " + schema);

try (FileOutputStream fileOutputStream = new FileOutputStream(outFile);
ArrowFileWriter arrowWriter = new ArrowFileWriter(root, arrowReader,
fileOutputStream.getChannel())) {
ArrowFileWriter arrowWriter =
new ArrowFileWriter(root, arrowReader, fileOutputStream.getChannel())) {
arrowWriter.start();
while (true) {
if (!arrowReader.loadNextBatch()) {
Expand All @@ -117,5 +112,4 @@ private int fatalError(String message, Throwable e) {
LOGGER.error(message, e);
return 1;
}

}
20 changes: 7 additions & 13 deletions java/tools/src/main/java/org/apache/arrow/tools/FileToStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.ipc.ArrowFileReader;
import org.apache.arrow.vector.ipc.ArrowStreamWriter;

/**
* Converts an Arrow file to an Arrow stream. The file should be specified as the
* first argument and the output is written to standard out.
* Converts an Arrow file to an Arrow stream. The file should be specified as the first argument and
* the output is written to standard out.
*/
public class FileToStream {
private FileToStream() {
}
private FileToStream() {}

/**
* Reads an Arrow file from in and writes it back to out.
*/
/** Reads an Arrow file from in and writes it back to out. */
public static void convert(FileInputStream in, OutputStream out) throws IOException {
BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
try (ArrowFileReader reader = new ArrowFileReader(in.getChannel(), allocator)) {
Expand All @@ -61,8 +56,8 @@ public static void convert(FileInputStream in, OutputStream out) throws IOExcept
}

/**
* Main method. The first arg is the file path. The second, optional argument,
* is an output file location (defaults to standard out).
* Main method. The first arg is the file path. The second, optional argument, is an output file
* location (defaults to standard out).
*/
public static void main(String[] args) throws IOException {
if (args.length != 1 && args.length != 2) {
Expand All @@ -71,8 +66,7 @@ public static void main(String[] args) throws IOException {
}

FileInputStream in = new FileInputStream(new File(args[0]));
OutputStream out = args.length == 1 ?
System.out : new FileOutputStream(new File(args[1]));
OutputStream out = args.length == 1 ? System.out : new FileOutputStream(new File(args[1]));

convert(in, out);
}
Expand Down
67 changes: 33 additions & 34 deletions java/tools/src/main/java/org/apache/arrow/tools/Integration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.tools;

import java.io.File;
Expand All @@ -25,7 +24,6 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.arrow.compression.CommonsCompressionFactory;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
Expand All @@ -47,9 +45,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Application for cross language integration testing.
*/
/** Application for cross language integration testing. */
public class Integration {
private static final Logger LOGGER = LoggerFactory.getLogger(Integration.class);
private final Options options;
Expand All @@ -58,13 +54,11 @@ public class Integration {
this.options = new Options();
this.options.addOption("a", "arrow", true, "arrow file");
this.options.addOption("j", "json", true, "json file");
this.options.addOption("c", "command", true, "command to execute: " + Arrays.toString(Command
.values()));
this.options.addOption(
"c", "command", true, "command to execute: " + Arrays.toString(Command.values()));
}

/**
* Main method.
*/
/** Main method. */
public static void main(String[] args) {
try {
new Integration().run(args);
Expand Down Expand Up @@ -123,28 +117,29 @@ private Command toCommand(String commandName) {
try {
return Command.valueOf(commandName);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Unknown command: " + commandName + " expected one of " +
Arrays.toString(Command.values()));
throw new IllegalArgumentException(
"Unknown command: "
+ commandName
+ " expected one of "
+ Arrays.toString(Command.values()));
}
}

/**
* Commands (actions) the application can perform.
*/
/** Commands (actions) the application can perform. */
enum Command {
ARROW_TO_JSON(true, false) {
@Override
public void execute(File arrowFile, File jsonFile) throws IOException {
try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
FileInputStream fileInputStream = new FileInputStream(arrowFile);
ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(),
allocator)) {
FileInputStream fileInputStream = new FileInputStream(arrowFile);
ArrowFileReader arrowReader =
new ArrowFileReader(fileInputStream.getChannel(), allocator)) {
VectorSchemaRoot root = arrowReader.getVectorSchemaRoot();
Schema schema = root.getSchema();
LOGGER.debug("Input file size: " + arrowFile.length());
LOGGER.debug("Found schema: " + schema);
try (JsonFileWriter writer = new JsonFileWriter(jsonFile, JsonFileWriter.config()
.pretty(true))) {
try (JsonFileWriter writer =
new JsonFileWriter(jsonFile, JsonFileWriter.config().pretty(true))) {
writer.start(schema, arrowReader);
for (ArrowBlock rbBlock : arrowReader.getRecordBlocks()) {
if (!arrowReader.loadRecordBatch(rbBlock)) {
Expand All @@ -161,15 +156,15 @@ public void execute(File arrowFile, File jsonFile) throws IOException {
@Override
public void execute(File arrowFile, File jsonFile) throws IOException {
try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
JsonFileReader reader = new JsonFileReader(jsonFile, allocator)) {
JsonFileReader reader = new JsonFileReader(jsonFile, allocator)) {
Schema schema = reader.start();
LOGGER.debug("Input file size: " + jsonFile.length());
LOGGER.debug("Found schema: " + schema);
try (FileOutputStream fileOutputStream = new FileOutputStream(arrowFile);
VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator);
// TODO json dictionaries
ArrowFileWriter arrowWriter = new ArrowFileWriter(root, reader, fileOutputStream
.getChannel())) {
VectorSchemaRoot root = VectorSchemaRoot.create(schema, allocator);
// TODO json dictionaries
ArrowFileWriter arrowWriter =
new ArrowFileWriter(root, reader, fileOutputStream.getChannel())) {
arrowWriter.start();
while (reader.read(root)) {
arrowWriter.writeBatch();
Expand All @@ -184,10 +179,11 @@ public void execute(File arrowFile, File jsonFile) throws IOException {
@Override
public void execute(File arrowFile, File jsonFile) throws IOException {
try (BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
JsonFileReader jsonReader = new JsonFileReader(jsonFile, allocator);
FileInputStream fileInputStream = new FileInputStream(arrowFile);
ArrowFileReader arrowReader = new ArrowFileReader(fileInputStream.getChannel(),
allocator, CommonsCompressionFactory.INSTANCE)) {
JsonFileReader jsonReader = new JsonFileReader(jsonFile, allocator);
FileInputStream fileInputStream = new FileInputStream(arrowFile);
ArrowFileReader arrowReader =
new ArrowFileReader(
fileInputStream.getChannel(), allocator, CommonsCompressionFactory.INSTANCE)) {
Schema jsonSchema = jsonReader.start();
VectorSchemaRoot arrowRoot = arrowReader.getVectorSchemaRoot();
Schema arrowSchema = arrowRoot.getSchema();
Expand Down Expand Up @@ -221,9 +217,14 @@ public void execute(File arrowFile, File jsonFile) throws IOException {
boolean hasMoreJSON = jsonRoot != null;
boolean hasMoreArrow = iterator.hasNext();
if (hasMoreJSON || hasMoreArrow) {
throw new IllegalArgumentException("Unexpected RecordBatches. Total: " + totalBatches +
" J:" + hasMoreJSON + " " +
"A:" + hasMoreArrow);
throw new IllegalArgumentException(
"Unexpected RecordBatches. Total: "
+ totalBatches
+ " J:"
+ hasMoreJSON
+ " "
+ "A:"
+ hasMoreArrow);
}
}
}
Expand All @@ -238,7 +239,5 @@ public void execute(File arrowFile, File jsonFile) throws IOException {
}

public abstract void execute(File arrowFile, File jsonFile) throws IOException;

}

}
Loading

0 comments on commit 2f4354d

Please sign in to comment.