Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Various style/small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 11, 2014
1 parent 07a2eef commit ff5d798
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
38 changes: 34 additions & 4 deletions syncany-cli/src/main/java/org/syncany/cli/CleanupCommand.java
Expand Up @@ -18,6 +18,10 @@
package org.syncany.cli;

import static java.util.Arrays.asList;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
Expand Down Expand Up @@ -49,12 +53,12 @@ public CleanupOperationOptions parseOptions(String[] operationArgs) throws Excep
OptionParser parser = new OptionParser();
parser.allowsUnrecognizedOptions();

OptionSpec<Void> optionForce = parser.acceptsAll(asList("F", "force"));
OptionSpec<Void> optionForce = parser.accepts("force");
OptionSpec<Void> optionNoDatabaseMerge = parser.acceptsAll(asList("M", "no-database-merge"));
OptionSpec<Void> optionNoOldVersionRemoval = parser.acceptsAll(asList("V", "no-version-remove"));
OptionSpec<Integer> optionKeepVersions = parser.acceptsAll(asList("k", "keep-versions")).withRequiredArg().ofType(Integer.class);
OptionSpec<Long> optionSecondsBetweenCleanups = parser.acceptsAll(asList("t", "time-between-cleanups")).withRequiredArg().ofType(Long.class);
OptionSpec<Integer> optionMaxDatabaseFiles = parser.acceptsAll(asList("d", "max-database-files")).withRequiredArg().ofType(Integer.class);
OptionSpec<String> optionSecondsBetweenCleanups = parser.acceptsAll(asList("t", "time-between-cleanups")).withRequiredArg().ofType(String.class);
OptionSpec<Integer> optionMaxDatabaseFiles = parser.acceptsAll(asList("x", "max-database-files")).withRequiredArg().ofType(Integer.class);

OptionSet options = parser.parse(operationArgs);

Expand All @@ -80,7 +84,7 @@ public CleanupOperationOptions parseOptions(String[] operationArgs) throws Excep

// -t=<count>, --time-between-cleanups=<count>
if (options.has(optionSecondsBetweenCleanups)) {
long secondsBetweenCleanups = options.valueOf(optionSecondsBetweenCleanups);
long secondsBetweenCleanups = parseTimePeriod(options.valueOf(optionSecondsBetweenCleanups));

if (secondsBetweenCleanups < 0) {
throw new Exception("Invalid value for --time-between-cleanups="+secondsBetweenCleanups+"; must be >= 0");
Expand Down Expand Up @@ -168,4 +172,30 @@ private void printResults(CleanupOperationResult operationResult) {
throw new RuntimeException("Invalid result code: " + operationResult.getResultCode().toString());
}
}

private long parseTimePeriod(String period) {
Pattern relativeDatePattern = Pattern.compile("(\\d+(?:[.,]\\d+)?)(mo|[smhdwy])");
Matcher relativeDateMatcher = relativeDatePattern.matcher(period);

relativeDateMatcher.reset();
long periodSeconds = 0;

while (relativeDateMatcher.find()) {
double time = Double.parseDouble(relativeDateMatcher.group(1));
String unitStr = relativeDateMatcher.group(2).toLowerCase();
int unitMultiplier = 0;

if (unitStr.startsWith("mo")) { unitMultiplier = 30*24*60*60; } // must be before "m"
else if (unitStr.startsWith("s")) { unitMultiplier = 1; }
else if (unitStr.startsWith("m")) { unitMultiplier = 60; }
else if (unitStr.startsWith("h")) { unitMultiplier = 60*60; }
else if (unitStr.startsWith("d")) { unitMultiplier = 24*60*60; }
else if (unitStr.startsWith("w")) { unitMultiplier = 7*24*60*60; }
else if (unitStr.startsWith("y")) { unitMultiplier = 365*24*60*60; }

periodSeconds += (long) ((double)time*unitMultiplier);
}

return periodSeconds;
}
}
6 changes: 5 additions & 1 deletion syncany-lib/src/main/java/org/syncany/config/Config.java
Expand Up @@ -55,12 +55,16 @@ public class Config {
public static final String DIR_CACHE = "cache";
public static final String DIR_DATABASE = "db";
public static final String DIR_LOG = "logs";
public static final String DIR_STATE = "state";
// Files in .syncany
public static final String FILE_CONFIG = "config.xml";
public static final String FILE_REPO = "syncany";
public static final String FILE_MASTER = "master";
// File in client root
public static final String FILE_IGNORE = ".syignore";
// File in .syncany/db
public static final String FILE_DATABASE = "local.db";
public static final String DIR_STATE = "state";
// Files in .syncany/state
public static final String FILE_PORT = "port.xml";
public static final String FILE_CLEANUP = "cleanup.xml";

Expand Down
Expand Up @@ -329,7 +329,8 @@ private boolean hasRemoteChanges() throws Exception {
}

private void mergeRemoteFiles() throws IOException, StorageException {
if (options.isForce() || getLastTimeCleaned() + options.getMinSecondsBetweenCleanups() > System.currentTimeMillis()/1000) {
boolean cleanedRecently = getLastTimeCleaned() + options.getMinSecondsBetweenCleanups() > System.currentTimeMillis()/1000;
if (!options.isForce() && cleanedRecently) {
logger.log(Level.INFO, "- Merge remote files: Not necessary, has been done recently");

return;
Expand All @@ -347,7 +348,8 @@ private void mergeRemoteFiles() throws IOException, StorageException {
}

// A client will merge databases if the number of databases exceeds the maximum number per client times the amount of clients
if (options.isForce() || numberOfDatabaseFiles <= options.getMaxDatabaseFiles()*allDatabaseFilesMap.keySet().size()) {
boolean notTooManyDatabaseFiles = numberOfDatabaseFiles <= options.getMaxDatabaseFiles()*allDatabaseFilesMap.keySet().size();
if (!options.isForce() && notTooManyDatabaseFiles) {
logger.log(Level.INFO, "- Merge remote files: Not necessary ({0} database files, max. {1})", new Object[] {
numberOfDatabaseFiles, options.getMaxDatabaseFiles()*allDatabaseFilesMap.keySet().size() });
return;
Expand Down

0 comments on commit ff5d798

Please sign in to comment.