Skip to content

Commit

Permalink
ZOOKEEPER-3039: TxnLogToolkit uses Scanner badly
Browse files Browse the repository at this point in the history
Fixed by creating a single Scanner for all queries in the main() method.

Author: Andor Molnar <[email protected]>

Reviewers: [email protected]

Closes #517 from anmolnar/ZOOKEEPER-3039 and squashes the following commits:

a35e2e2 [Andor Molnar] ZOOKEEPER-3039. Optimize imports
a196443 [Andor Molnar] ZOOKEEPER-3039. Use the same Scanner for all queries

Change-Id: Icf66888d4e6ad902615ed8ffde58a5a8fdd41237
  • Loading branch information
anmolnar authored and phunt committed May 10, 2018
1 parent 6e64125 commit 2fa315b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.text.DateFormat;
import java.util.Date;
import java.util.Scanner;
Expand Down Expand Up @@ -98,7 +96,7 @@ Options getOptions() {
*/
public static void main(String[] args) throws Exception {
try (final TxnLogToolkit lt = parseCommandLine(args)) {
lt.dump(new InputStreamReader(System.in));
lt.dump(new Scanner(System.in));
lt.printStat();
} catch (TxnLogToolkitParseException e) {
System.err.println(e.getMessage() + "\n");
Expand Down Expand Up @@ -131,7 +129,7 @@ public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileNam
}
}

public void dump(Reader input) throws Exception {
public void dump(Scanner scanner) throws Exception {
crcFixed = 0;

FileHeader fhdr = new FileHeader();
Expand Down Expand Up @@ -172,7 +170,7 @@ public void dump(Reader input) throws Exception {
if (recoveryMode) {
if (!force) {
printTxn(bytes, "CRC ERROR");
if (askForFix(input)) {
if (askForFix(scanner)) {
crcValue = crc.getValue();
++crcFixed;
}
Expand Down Expand Up @@ -201,19 +199,17 @@ public void dump(Reader input) throws Exception {
}
}

private boolean askForFix(Reader input) throws TxnLogToolkitException {
try (Scanner scanner = new Scanner(input)) {
while (true) {
System.out.print("Would you like to fix it (Yes/No/Abort) ? ");
char answer = Character.toUpperCase(scanner.next().charAt(0));
switch (answer) {
case 'Y':
return true;
case 'N':
return false;
case 'A':
throw new TxnLogToolkitException(0, "Recovery aborted.");
}
private boolean askForFix(Scanner scanner) throws TxnLogToolkitException {
while (true) {
System.out.print("Would you like to fix it (Yes/No/Abort) ? ");
char answer = Character.toUpperCase(scanner.next().charAt(0));
switch (answer) {
case 'Y':
return true;
case 'N':
return false;
case 'A':
throw new TxnLogToolkitException(0, "Recovery aborted.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -138,7 +138,7 @@ public void testRecoveryInteractiveMode() throws Exception {
TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), false);

// Act
lt.dump(new StringReader("y\n"));
lt.dump(new Scanner("y\n"));

// Assert
String output = outContent.toString();
Expand Down

0 comments on commit 2fa315b

Please sign in to comment.