mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
19.27.2018 231d2d5e08e8b567f9d4e1cf206465170c598b09
Updating cache info about archive file lists works now.
3 files modified
67 ■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java 19 ●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java 28 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/data/ArchiveShortInfo.java 20 ●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java
@@ -3,6 +3,7 @@
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.data.Archive;
import de.micromata.borgbutler.data.FileSystemFilter;
import de.micromata.borgbutler.data.Repository;
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
import de.micromata.borgbutler.utils.ReplaceUtils;
import lombok.Getter;
@@ -31,10 +32,10 @@
    private long FILES_EXPIRE_TIME = 7 * 24 * 3660 * 1000; // Expires after 7 days.
    public void save(BorgRepoConfig repoConfig, Archive archive, List<BorgFilesystemItem> filesystemItems) {
        File file = getFile(repoConfig, archive);
        if (CollectionUtils.isEmpty(filesystemItems)) {
            return;
        }
        File file = getFile(repoConfig, archive);
        log.info("Saving archive content as file list: " + file.getAbsolutePath());
        try (ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(new GzipCompressorOutputStream(new FileOutputStream(file))))) {
            outputStream.writeObject(filesystemItems.size());
@@ -49,12 +50,12 @@
    }
    /**
     * @param repoConfig
     * @param repository
     * @param archive
     * @return true, if the content of the archive is already cached, otherwise false.
     */
    public boolean contains(BorgRepoConfig repoConfig, Archive archive) {
        File file = getFile(repoConfig, archive);
    public boolean contains(Repository repository, Archive archive) {
        File file = getFile(repository, archive);
        return file.exists();
    }
@@ -220,9 +221,17 @@
        }
    }
    File getFile(Repository repository, Archive archive) {
        return getFile(repository.getName(), archive);
    }
    File getFile(BorgRepoConfig repoConfig, Archive archive) {
        return getFile(repoConfig.getRepo(), archive);
    }
   private File getFile(String repo, Archive archive) {
        return new File(cacheDir, ReplaceUtils.encodeFilename(CACHE_ARCHIVE_LISTS_BASENAME + archive.getTime()
                        + "-" + repoConfig.getRepo() + "-" + archive.getName() + CACHE_FILE_GZIP_EXTENSION,
                        + "-" + repo + "-" + archive.getName() + CACHE_FILE_GZIP_EXTENSION,
                true));
    }
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
@@ -5,6 +5,7 @@
import de.micromata.borgbutler.config.Configuration;
import de.micromata.borgbutler.config.ConfigurationHandler;
import de.micromata.borgbutler.data.Archive;
import de.micromata.borgbutler.data.ArchiveShortInfo;
import de.micromata.borgbutler.data.FileSystemFilter;
import de.micromata.borgbutler.data.Repository;
import de.micromata.borgbutler.json.borg.BorgFilesystemItem;
@@ -20,7 +21,7 @@
import java.util.List;
public class ButlerCache {
    private static Logger log = LoggerFactory.getLogger(ButlerCache.class);
    private Logger log = LoggerFactory.getLogger(ButlerCache.class);
    public static final String CACHE_DIR_NAME = "cache";
    private static ButlerCache instance = new ButlerCache();
@@ -123,10 +124,12 @@
            return null;
        }
        if (repository.isArchivesLoaded()) {
            updateArchivesCacheStatusAndShortInfos(repository);
            return repository;
        }
        BorgRepoConfig repoConfig = ConfigurationHandler.getConfiguration().getRepoConfig(repository.getName());
        BorgCommands.list(repoConfig, repository);
        updateArchivesCacheStatusAndShortInfos(repository);
        return repository;
    }
@@ -174,9 +177,11 @@
        }
        if (!forceReload && archive.hasInfoData()) {
            // borg info archive was already called.
            updateArchivesCacheStatusAndShortInfos(repository);
            return archive;
        }
        BorgCommands.info(repoConfig, archive, repository);
        updateArchivesCacheStatusAndShortInfos(repository);
        return archive;
    }
@@ -199,6 +204,27 @@
    }
    /**
     * Updates for all archives of the given repository the cache status ({@link Archive#isFileListAlreadyCached()} and
     * updates also the list of {@link ArchiveShortInfo} for all archives of the given repository.
     *
     * @param repository
     */
    private void updateArchivesCacheStatusAndShortInfos(Repository repository) {
        if (repository == null || repository.getArchives() == null) {
            return;
        }
        List<ArchiveShortInfo> archiveInfoList = new ArrayList<>();
        for (Archive archive : repository.getArchives()) {
            archive.setFileListAlreadyCached(archiveFilelistCache.contains(repository, archive));
            archiveInfoList.add(new ArchiveShortInfo(archive));
        }
        for (Archive archive : repository.getArchives()) {
            // ArchiveInfoList for comparing current archives with one of all other archives.
            archive.setArchiveShortInfoList(archiveInfoList);
        }
    }
    /**
     * @param archiveId
     * @param forceLoad If false, the file list will only get if not yet loaded.
     * @param filter    If not null, the result will be filtered.
borgbutler-core/src/main/java/de/micromata/borgbutler/data/ArchiveShortInfo.java
@@ -1,9 +1,5 @@
package de.micromata.borgbutler.data;
import de.micromata.borgbutler.json.borg.BorgArchiveLimits;
import de.micromata.borgbutler.json.borg.BorgArchiveStats;
import de.micromata.borgbutler.json.borg.BorgCache;
import de.micromata.borgbutler.json.borg.BorgEncryption;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
@@ -39,12 +35,16 @@
    @Setter
    private boolean fileListAlreadyCached;
    /**
     *
     * @return repoName::archiveName
     */
    public String getBorgIdentifier() {
        return repoName + "::" + name;
    public ArchiveShortInfo() {
    }
    public ArchiveShortInfo(Archive archive) {
        this.id = archive.getId();
        this.name = archive.getName();
        this.repoId = archive.getRepoId();
        this.time = archive.getTime();
        this.fileListAlreadyCached = archive.isFileListAlreadyCached();
    }
    /**