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

Commit

Permalink
Adding time and number of db-file options to CleanupOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 11, 2014
1 parent a740db9 commit badb05d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
39 changes: 39 additions & 0 deletions syncany-lib/src/main/java/org/syncany/config/to/CleanupTO.java
@@ -0,0 +1,39 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.config.to;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

/**
* @author pim-arch
*
*/
@Root(name="cleanup", strict=false)
public class CleanupTO {
@Element(name="lastTimeCleaned", required = false)
private long lastTimeCleaned = 0;

public long getLastTimeCleaned() {
return lastTimeCleaned;
}

public void setLastTimeCleaned(long lastTimeCleanup) {
this.lastTimeCleaned = lastTimeCleanup;
}
}
Expand Up @@ -31,9 +31,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.simpleframework.xml.core.Persister;
import org.syncany.chunk.Chunk;
import org.syncany.chunk.MultiChunk;
import org.syncany.config.Config;
import org.syncany.config.to.CleanupTO;
import org.syncany.database.DatabaseVersion;
import org.syncany.database.DatabaseVersionHeader;
import org.syncany.database.DatabaseVersionHeader.DatabaseVersionType;
Expand Down Expand Up @@ -326,7 +328,11 @@ private boolean hasRemoteChanges() throws Exception {
}

private void mergeRemoteFiles() throws IOException, StorageException {

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

return;
}


// Retrieve all database versions
Expand Down Expand Up @@ -410,7 +416,7 @@ private void mergeRemoteFiles() throws IOException, StorageException {

// Update stats
result.setMergedDatabaseFilesCount(allToDeleteDatabaseFiles.size());

setLastTimeCleaned(System.currentTimeMillis()/1000);


}
Expand All @@ -433,4 +439,28 @@ private Map<String, List<DatabaseRemoteFile>> retrieveAllRemoteDatabaseFiles() t

return allDatabaseRemoteFilesMap;
}

private long getLastTimeCleaned() {
try {
CleanupTO cleanupTO = (new Persister()).read(CleanupTO.class, config.getCleanupFile());
return cleanupTO.getLastTimeCleaned();
}
catch (Exception e) {
logger.log(Level.INFO, "Something went wrong with reading cleanup.xml, assuming never cleaned." + e.getMessage());
return 0;
}
}

private void setLastTimeCleaned(long lastTimeCleaned) {
CleanupTO cleanupTO = new CleanupTO();
cleanupTO.setLastTimeCleaned(lastTimeCleaned);
try {
(new Persister()).write(cleanupTO, config.getCleanupFile());
}
catch (Exception e) {
// Not doing anything else, because the worst that could happen is that cleanup is run an extra time
logger.log(Level.INFO, "Something went wrong with writing cleanup.xml." + e.getMessage());
}

}
}
Expand Up @@ -260,20 +260,13 @@ private void runSync() throws Exception {
notifyChanges = true;
}

// Run cleanup
boolean lastCleanupExpired = cleanupLastRun.get() + options.getCleanupInterval() < System.currentTimeMillis();
boolean enoughUpsForCleanup = upCount.get() > CleanupOperation.MAX_KEEP_DATABASE_VERSIONS;

CleanupOperationResult cleanupOperationResult = new CleanupOperation(config, options.getCleanupOptions()).execute();

if (lastCleanupExpired || enoughUpsForCleanup) {
CleanupOperationResult cleanupOperationResult = new CleanupOperation(config, options.getCleanupOptions()).execute();

if (cleanupOperationResult.getResultCode() == CleanupResultCode.OK) {
notifyChanges = true;

cleanupLastRun.set(System.currentTimeMillis());
upCount.set(0);
}
if (cleanupOperationResult.getResultCode() == CleanupResultCode.OK) {
notifyChanges = true;
}


// Fire change event if up and/or cleanup
if (notifyChanges) {
Expand Down

0 comments on commit badb05d

Please sign in to comment.