Skip to content

Commit

Permalink
Make SSTableExport Return Instead of Exit (#597)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Guo <[email protected]>
  • Loading branch information
d-guo and Daniel Guo authored Dec 10, 2024
1 parent 8cf1eb0 commit 422495f
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/java/org/apache/cassandra/tools/SSTableExport.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,13 @@ public static void export(Descriptor desc, String[] excludes) throws IOException
* @param args command lines arguments
* @throws ConfigurationException on configuration failure (wrong params given)
*/
public static void main(String[] args) throws ConfigurationException, IOException
public static void main(String[] args) throws ConfigurationException
{
System.exit(mainInternal(args));
}

@SuppressWarnings("resource")
public static int mainInternal(String[] args) throws ConfigurationException
{
String usage = String.format("Usage: %s <sstable> [-k key [-k key [...]] -x key [-x key [...]]]%n", SSTableExport.class.getName());

Expand All @@ -456,15 +462,15 @@ public static void main(String[] args) throws ConfigurationException, IOExceptio
{
System.err.println(e1.getMessage());
System.err.println(usage);
System.exit(1);
return 1;
}


if (cmd.getArgs().length != 1)
{
System.err.println("You must supply exactly one sstable or directory");
System.err.println(usage);
System.exit(1);
return 1;
}

Util.initDatabaseDescriptor();
Expand All @@ -476,7 +482,7 @@ public static void main(String[] args) throws ConfigurationException, IOExceptio
if ((keys != null) && (keys.length > 0) && (prefix != null))
{
System.err.println("Cannot specify keys and prefix at the same time");
System.exit(1);
return 1;
}

File fileOrDirectory = new File(cmd.getArgs()[0]);
Expand All @@ -500,20 +506,22 @@ public static void main(String[] args) throws ConfigurationException, IOExceptio
i++;
String ssTableFileName = file.getAbsolutePath();
printStream.printf("\"%s\":", file.getName());
handleSingleSsTableFile(ssTableFileName, keys, excludes, prefix, printStream);
int res = handleSingleSsTableFile(ssTableFileName, keys, excludes, prefix, printStream);
if (res != 0) {
return res;
}
}
printStream.println("}");
}
else
{
handleSingleSsTableFile(fileOrDirectory.getAbsolutePath(), keys, excludes, prefix, printStream);
return handleSingleSsTableFile(fileOrDirectory.getAbsolutePath(), keys, excludes, prefix, printStream);
}
}

System.exit(0);
return 0;
}

private static void handleSingleSsTableFile(String ssTableFileName, String[] keys, String[] excludes, String prefix, PrintStream printStream)
private static int handleSingleSsTableFile(String ssTableFileName, String[] keys, String[] excludes, String prefix, PrintStream printStream)
{
Descriptor descriptor = Descriptor.fromFilename(ssTableFileName);

Expand All @@ -522,7 +530,7 @@ private static void handleSingleSsTableFile(String ssTableFileName, String[] key
{
System.err.println(String.format("Filename %s references to nonexistent keyspace: %s!",
ssTableFileName, descriptor.ksname));
System.exit(1);
return 1;
}
Keyspace.setInitialized();
Keyspace keyspace = Keyspace.open(descriptor.ksname);
Expand All @@ -545,7 +553,7 @@ private static void handleSingleSsTableFile(String ssTableFileName, String[] key
{
System.err.println(String.format("The provided table is not part of this cassandra keyspace: keyspace = %s, table = %s",
descriptor.ksname, descriptor.cfname));
System.exit(1);
return 1;
}

try
Expand All @@ -569,6 +577,7 @@ else if (prefix != null)
// throwing exception outside main with broken pipe causes windows cmd to hang
e.printStackTrace(System.err);
}
return 0;
}

private static void writeJSON(PrintStream out, Object value)
Expand Down

0 comments on commit 422495f

Please sign in to comment.