package org.apache.cassandra.tools;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.List;
import org.apache.cassandra.config.Config;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.io.sstable.Component;
import org.apache.cassandra.io.sstable.Descriptor;
import org.apache.cassandra.service.ActiveRepairService;
public class SSTableRepairedAtSetter
{
public static void main(final String[] args) throws IOException
{
PrintStream out = System.out;
if (args.length == 0)
{
out.println("This command should be run with Cassandra stopped!");
out.println("Usage: sstablerepairedset [--is-repaired | --is-unrepaired] [-f <sstable-list> | <sstables>]");
System.exit(1);
}
if (args.length < 3 || !args[0].equals("--really-set") || (!args[1].equals("--is-repaired") && !args[1].equals("--is-unrepaired")))
{
out.println("This command should be run with Cassandra stopped, otherwise you will get very strange behavior");
out.println("Verify that Cassandra is not running and then execute the command like this:");
out.println("Usage: sstablerepairedset --really-set [--is-repaired | --is-unrepaired] [-f <sstable-list> | <sstables>]");
System.exit(1);
}
Util.initDatabaseDescriptor();
boolean setIsRepaired = args[1].equals("--is-repaired");
List<String> fileNames;
if (args[2].equals("-f"))
{
fileNames = Files.readAllLines(Paths.get(args[3]), Charset.defaultCharset());
}
else
{
fileNames = Arrays.asList(args).subList(2, args.length);
}
for (String fname: fileNames)
{
Descriptor descriptor = Descriptor.fromFilename(fname);
if (descriptor.version.hasRepairedAt())
{
if (setIsRepaired)
{
FileTime f = Files.getLastModifiedTime(new File(descriptor.filenameFor(Component.DATA)).toPath());
descriptor.getMetadataSerializer().mutateRepairedAt(descriptor, f.toMillis());
}
else
{
descriptor.getMetadataSerializer().mutateRepairedAt(descriptor, ActiveRepairService.UNREPAIRED_SSTABLE);
}
}
else
{
System.err.println("SSTable " + fname + " does not have repaired property, run upgradesstables");
}
}
}
}