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

Kai Reinhard
08.22.2018 28a5a5bee5ff8006ae3473fb5ff41fde71424297
Tried to merge all caches to one EntityCache (failed due to Jackson-marshalling/unmarshalling problems with elements). I ended up in LinkedHashMaps instead of PoJos.
3 files deleted
1 files renamed
2 files modified
147 ■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveListCache.java 22 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java 37 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java 26 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java 22 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoListCache.java 22 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java 18 ●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveListCache.java
File was deleted
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
@@ -1,6 +1,10 @@
package de.micromata.borgbutler.cache;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.type.TypeReference;
import de.micromata.borgbutler.json.borg.ArchiveList;
import de.micromata.borgbutler.json.borg.RepoInfo;
import de.micromata.borgbutler.json.borg.RepoList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -13,9 +17,10 @@
    public static final String CACHE_DIR_NAME = ".borgbutler";
    private static ButlerCache instance = new ButlerCache();
    private RepoInfoCache repoInfoCache;
    private RepoListCache repoListCache;
    private List<AbstractCache> caches;
    private EntityCache<RepoInfo> repoInfoCache;
    private EntityCache<RepoList> repoListCache;
    private EntityCache<ArchiveList> archiveListCache;
    private List<EntityCache> caches;
    @JsonIgnore
    private File cacheDir;
@@ -24,22 +29,26 @@
        return instance;
    }
    public static RepoInfoCache getRepoInfoCache() {
    public static EntityCache<RepoInfo> getRepoInfoCache() {
        return instance.repoInfoCache;
    }
    public static RepoListCache getRepoListCache() {
    public static EntityCache<RepoList> getRepoListCache() {
        return instance.repoListCache;
    }
    public static EntityCache<ArchiveList> getArchiveListCache() {
        return instance.archiveListCache;
    }
    public void read() {
        for (AbstractCache cache : caches) {
        for (EntityCache cache : caches) {
            cache.read();
        }
    }
    public void save() {
        for (AbstractCache cache : caches) {
        for (EntityCache cache : caches) {
            cache.save();
        }
    }
@@ -50,12 +59,12 @@
    public void removeAllCacheFiles() {
        File[] files = cacheDir.listFiles();
        for (File file : files) {
            if (AbstractCache.isCacheFile(file)) {
            if (EntityCache.isCacheFile(file)) {
                log.info("Deleting cache file: " + file.getAbsolutePath());
                file.delete();
            }
        }
        for (AbstractCache cache : caches) {
        for (EntityCache cache : caches) {
            cache.clear();
        }
    }
@@ -67,10 +76,12 @@
            log.info("Creating cache dir: " + cacheDir.getAbsolutePath());
            cacheDir.mkdir();
        }
        repoInfoCache = new RepoInfoCache(cacheDir);
        repoListCache = new RepoListCache(cacheDir);
        caches = new ArrayList<>();
        caches.add(repoInfoCache);
        caches.add(repoListCache);
        caches.add(repoInfoCache = new EntityCache<RepoInfo>(cacheDir, EntityCache.CACHE_REPO_INFOS_BASENAME, new TypeReference<List<RepoInfo>>() {
        }));
        caches.add(repoListCache = new EntityCache<>(cacheDir, EntityCache.CACHE_REPO_LISTS_BASENAME, new TypeReference<List<RepoList>>() {
        }));
        caches.add(archiveListCache = new EntityCache<>(cacheDir, EntityCache.CACHE_ARCHIVE_LISTS_BASENAME, new TypeReference<List<ArchiveList>>() {
        }));
    }
}
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java
File was renamed from borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
@@ -2,6 +2,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import de.micromata.borgbutler.json.JsonUtils;
import de.micromata.borgbutler.json.borg.RepositoryMatcher;
import lombok.Getter;
@@ -16,16 +17,20 @@
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractCache<T> {
    private static Logger log = LoggerFactory.getLogger(AbstractCache.class);
public class EntityCache<T> {
    private static Logger log = LoggerFactory.getLogger(EntityCache.class);
    private static final String CACHE_FILE_PREFIX = "cache-";
    private static final String CACHE_FILE_EXTENSION = "json";
    public static final String CACHE_REPO_INFOS_BASENAME = "repo-infos";
    public static final String CACHE_REPO_LISTS_BASENAME = "repo-lists";
    public static final String CACHE_ARCHIVE_LISTS_BASENAME = "archive-lists";
    @JsonIgnore
    protected File cacheFile;
    @Getter
    @JsonProperty
    private List<T> elements = new ArrayList<>();
    private TypeReference<T> typeReference;
    public T get(String identifier) {
        if (identifier == null) {
@@ -41,21 +46,21 @@
    public boolean matches(T element, String identifier) {
        if (!(element instanceof RepositoryMatcher)) {
            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + this.getClass());
            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + element.getClass());
        }
        return ((RepositoryMatcher) element).matches(identifier);
    }
    public String getIdentifier(T element) {
        if (!(element instanceof RepositoryMatcher)) {
            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + this.getClass());
            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + element.getClass());
        }
        return ((RepositoryMatcher)element).getRepository().getId();
    }
    public void updateFrom(T dest, T source) {
        if (!(dest instanceof RepositoryMatcher)) {
            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + this.getClass());
            throw new UnsupportedOperationException("matches not implemented, only available for RepositoryMatcher: " + dest.getClass());
        }
        ((RepositoryMatcher)dest).updateFrom(((RepositoryMatcher)source));
    }
@@ -84,9 +89,9 @@
            }
            log.info("Parsing cache file '" + cacheFile.getAbsolutePath() + "'.");
            String json = FileUtils.readFileToString(cacheFile, Charset.forName("UTF-8"));
            AbstractCache readCache = JsonUtils.fromJson(this.getClass(), json);
            if (readCache != null) {
                this.elements = readCache.elements;
            List<T> elements = (List<T>)JsonUtils.fromJson(typeReference, json);
            if (elements != null) {
                this.elements = elements;
            } else {
                log.error("Error while parsing cache: " + cacheFile.getAbsolutePath());
            }
@@ -110,11 +115,12 @@
    /**
     * Needed by jackson for deserialization.
     */
    AbstractCache() {
    EntityCache() {
    }
    AbstractCache(File cacheDir, String cacheFilename) {
    EntityCache(File cacheDir, String cacheFilename, TypeReference typeReference) {
        cacheFile = new File(cacheDir, CACHE_FILE_PREFIX + cacheFilename + "." + CACHE_FILE_EXTENSION);
        this.typeReference = typeReference;
    }
    public static boolean isCacheFile(File file) {
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java
File was deleted
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoListCache.java
File was deleted
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java
@@ -35,14 +35,14 @@
        //butlerCache.removeAllCacheFiles();
        butlerCache.read();
        {
            RepoInfoCache repoInfoCache = ButlerCache.getRepoInfoCache();
            EntityCache<RepoInfo> repoInfoCache = ButlerCache.getRepoInfoCache();
            if (repoInfoCache.getElements().size() != config.getRepoConfigs().size()) {
                refreshRepoInfoCache(config, repoInfoCache);
            }
            assertEquals(config.getRepoConfigs().size(), repoInfoCache.getElements().size());
        }
        {
            RepoListCache repoListCache = ButlerCache.getRepoListCache();
            EntityCache<RepoList> repoListCache = ButlerCache.getRepoListCache();
            if (repoListCache.getElements().size() != config.getRepoConfigs().size()) {
                refreshRepoListCache(config, repoListCache);
            }
@@ -65,7 +65,7 @@
        butlerCache.save();
    }
    private void refreshRepoInfoCache(Configuration config, RepoInfoCache repoInfoCache) {
    private void refreshRepoInfoCache(Configuration config, EntityCache<RepoInfo> repoInfoCache) {
        for (BorgRepoConfig repo : config.getRepoConfigs()) {
            log.info("Processing repo info '" + repo + "'");
            RepoInfo repoInfo = BorgCommands.info(repo);
@@ -75,17 +75,7 @@
        }
    }
    private void refreshRepoListCache(Configuration config, RepoListCache repoListCache) {
        for (BorgRepoConfig repo : config.getRepoConfigs()) {
            log.info("Processing repo list '" + repo + "'");
            RepoList repoList = BorgCommands.list(repo);
            repoListCache.upsert(repoList);
            repoList = repoListCache.get(repoList.getRepository().getId());
            assertNotNull(repoList);
        }
    }
    private void refresArchiveListCache(Configuration config, RepoListCache repoListCache) {
    private void refreshRepoListCache(Configuration config, EntityCache<RepoList> repoListCache) {
        for (BorgRepoConfig repo : config.getRepoConfigs()) {
            log.info("Processing repo list '" + repo + "'");
            RepoList repoList = BorgCommands.list(repo);