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

Commit

Permalink
Enabling options in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 11, 2014
1 parent badb05d commit 9183604
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
28 changes: 28 additions & 0 deletions syncany-cli/src/main/java/org/syncany/cli/CleanupCommand.java
Expand Up @@ -49,12 +49,18 @@ public CleanupOperationOptions parseOptions(String[] operationArgs) throws Excep
OptionParser parser = new OptionParser();
parser.allowsUnrecognizedOptions();

OptionSpec<Void> optionForce = parser.acceptsAll(asList("F", "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);

OptionSet options = parser.parse(operationArgs);

// -F, --force
operationOptions.setForce(options.has(optionForce));

// -M, --no-database-merge
operationOptions.setMergeRemoteFiles(!options.has(optionNoDatabaseMerge));

Expand All @@ -72,6 +78,28 @@ public CleanupOperationOptions parseOptions(String[] operationArgs) throws Excep
operationOptions.setKeepVersionsCount(options.valueOf(optionKeepVersions));
}

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

if (secondsBetweenCleanups < 0) {
throw new Exception("Invalid value for --time-between-cleanups="+secondsBetweenCleanups+"; must be >= 0");
}

operationOptions.setMinSecondsBetweenCleanups(secondsBetweenCleanups);
}

// -d=<count>, --max-database-files=<count>
if (options.has(optionMaxDatabaseFiles)) {
int maxDatabaseFiles = options.valueOf(optionMaxDatabaseFiles);

if (maxDatabaseFiles < 1) {
throw new Exception("Invalid value for --max-database-files="+maxDatabaseFiles+"; must be >= 1");
}

operationOptions.setMaxDatabaseFiles(maxDatabaseFiles);
}

// Parse 'status' options
operationOptions.setStatusOptions(parseStatusOptions(operationArgs));

Expand Down
Expand Up @@ -144,6 +144,7 @@ public CleanupOperationResult execute() throws Exception {

removeLostMultiChunks();

setLastTimeCleaned(System.currentTimeMillis()/1000);
finishOperation();
return updateResultCode(result);
}
Expand Down Expand Up @@ -328,7 +329,7 @@ private boolean hasRemoteChanges() throws Exception {
}

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

return;
Expand All @@ -347,7 +348,7 @@ 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 (numberOfDatabaseFiles <= options.getMaxDatabaseFiles()*allDatabaseFilesMap.keySet().size()) {
if (options.isForce() || numberOfDatabaseFiles <= options.getMaxDatabaseFiles()*allDatabaseFilesMap.keySet().size()) {
logger.log(Level.INFO, "- Merge remote files: Not necessary ({0} database files, max. {1})", new Object[] {
numberOfDatabaseFiles, options.getMaxDatabaseFiles()*allDatabaseFilesMap.keySet().size() });

Expand Down Expand Up @@ -416,7 +417,7 @@ private void mergeRemoteFiles() throws IOException, StorageException {

// Update stats
result.setMergedDatabaseFilesCount(allToDeleteDatabaseFiles.size());
setLastTimeCleaned(System.currentTimeMillis()/1000);



}
Expand Down Expand Up @@ -455,6 +456,7 @@ private void setLastTimeCleaned(long lastTimeCleaned) {
CleanupTO cleanupTO = new CleanupTO();
cleanupTO.setLastTimeCleaned(lastTimeCleaned);
try {
logger.log(Level.INFO, "Writing cleanup.xml");
(new Persister()).write(cleanupTO, config.getCleanupFile());
}
catch (Exception e) {
Expand Down
Expand Up @@ -25,6 +25,9 @@ public class CleanupOperationOptions implements OperationOptions {
@Element(name = "status", required = false)
private StatusOperationOptions statusOptions = new StatusOperationOptions();

@Element(required = false)
private boolean force = false;

@Element(required = false)
private boolean mergeRemoteFiles = true;

Expand All @@ -38,7 +41,7 @@ public class CleanupOperationOptions implements OperationOptions {
private int maxDatabaseFiles = 15;

@Element(required = false)
private int minSecondsBetweenCleanups = 10800;
private long minSecondsBetweenCleanups = 10800;

// TODO [medium] Implement multichunk repackaging

Expand Down Expand Up @@ -85,11 +88,19 @@ public int getMaxDatabaseFiles() {
return maxDatabaseFiles;
}

public void setMinSecondsBetweenCleanups(int minSecondsBetweenCleanups) {
public void setMinSecondsBetweenCleanups(long minSecondsBetweenCleanups) {
this.minSecondsBetweenCleanups = minSecondsBetweenCleanups;
}

public int getMinSecondsBetweenCleanups() {
public long getMinSecondsBetweenCleanups() {
return minSecondsBetweenCleanups;
}

public boolean isForce() {
return force;
}

public void setForce(boolean force) {
this.force = force;
}
}

0 comments on commit 9183604

Please sign in to comment.