From 28a5a5bee5ff8006ae3473fb5ff41fde71424297 Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Sat, 08 Dec 2018 22:22:38 +0000
Subject: [PATCH] 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.

---
 /dev/null                                                                    |   22 -----------
 borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java |   26 ++++++++-----
 borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java |   37 ++++++++++++------
 borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java   |   18 ++-------
 4 files changed, 44 insertions(+), 59 deletions(-)

diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveListCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveListCache.java
deleted file mode 100644
index 6f3a38a..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveListCache.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import de.micromata.borgbutler.json.borg.ArchiveList;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-
-public class ArchiveListCache extends AbstractCache<ArchiveList> {
-    private static Logger log = LoggerFactory.getLogger(ArchiveListCache.class);
-    public static final String CACHE_ARCHIVE_LISTS_BASENAME = "archive-lists";
-
-    /**
-     * Needed by jackson for deserialization.
-     */
-    ArchiveListCache() {
-    }
-
-    ArchiveListCache(File cacheDir) {
-        super(cacheDir, CACHE_ARCHIVE_LISTS_BASENAME);
-    }
-}
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
index 8664817..5e307e6 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
+++ b/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>>() {
+        }));
     }
 }
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java
similarity index 79%
rename from borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
rename to borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.java
index 0b70220..0b8ef19 100644
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
+++ b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/EntityCache.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) {
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java
deleted file mode 100644
index 2e41324..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoInfoCache.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import de.micromata.borgbutler.json.borg.RepoInfo;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-
-public class RepoInfoCache extends AbstractCache<RepoInfo> {
-    private static Logger log = LoggerFactory.getLogger(RepoInfoCache.class);
-    public static final String CACHE_REPOS_BASENAME = "repos";
-
-    /**
-     * Needed by jackson for deserialization.
-     */
-    RepoInfoCache() {
-    }
-
-    RepoInfoCache(File cacheDir) {
-        super(cacheDir, CACHE_REPOS_BASENAME);
-    }
-}
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoListCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoListCache.java
deleted file mode 100644
index 4851278..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoListCache.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import de.micromata.borgbutler.json.borg.RepoList;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-
-public class RepoListCache extends AbstractCache<RepoList> {
-    private static Logger log = LoggerFactory.getLogger(RepoListCache.class);
-    public static final String CACHE_REPO_LISTS_BASENAME = "repo-lists";
-
-    /**
-     * Needed by jackson for deserialization.
-     */
-    RepoListCache() {
-    }
-
-    RepoListCache(File cacheDir) {
-        super(cacheDir, CACHE_REPO_LISTS_BASENAME);
-    }
-}
diff --git a/borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java b/borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java
index 0b1bfef..20dd109 100644
--- a/borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java
+++ b/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);

--
Gitblit v1.10.0