From 0c50069ac877873f511e6d698cc8d3e78a4cd608 Mon Sep 17 00:00:00 2001
From: Kai Reinhard <K.Reinhard@micromata.de>
Date: Mon, 10 Dec 2018 07:27:17 +0000
Subject: [PATCH] Old cache mechanism removed.
---
/dev/null | 46 ---------------
borgbutler-server/src/main/java/de/micromata/borgbutler/server/Main.java | 2
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java | 61 ++++----------------
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java | 14 +---
4 files changed, 16 insertions(+), 107 deletions(-)
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/Main.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/Main.java
deleted file mode 100644
index 32314c8..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/Main.java
+++ /dev/null
@@ -1,16 +0,0 @@
-package de.micromata.borgbutler;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class Main {
- private static Logger log = LoggerFactory.getLogger(Main.class);
-
- public static void main(String[] args) {
- new Main().start(args);
- }
-
- private void start(String[] args) {
- log.info("Hello Borgbutler...");
- }
-}
\ No newline at end of file
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
deleted file mode 100644
index 4072ebd..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractCache.java
+++ /dev/null
@@ -1,171 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import de.micromata.borgbutler.config.Definitions;
-import de.micromata.borgbutler.json.JsonUtils;
-import lombok.Getter;
-import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
-import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.io.IOUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.*;
-import java.util.Date;
-
-public abstract class AbstractCache {
- private static Logger log = LoggerFactory.getLogger(AbstractCache.class);
- private static final String CACHE_FILE_PREFIX = "cache-";
- private static final String CACHE_FILE_EXTENSION = "json";
- private static final String CACHE_FILE_ZIP_EXTENSION = ".gz";
-
- @JsonIgnore
- protected File cacheFile;
- @JsonIgnore
- @Getter
- private boolean compress;
- @Getter
- @JsonIgnore
- private CacheState state = CacheState.INITIAL;
- @Getter
- private Date lastModified;
- @Getter
- private Date created;
-
- /**
- * Removes all entries (doesn't effect the cache files!).
- */
- public void clear() {
- state = CacheState.INITIAL;
- }
-
- /**
- * Calls {@link #clear()} and removes the cache files. Therefore a new creation of this cache is forced.
- */
- public void clearAndReset() {
- synchronized (this) {
- if (cacheFile.exists()) {
- log.info("Clearing cache and deleting cache file (recreation is forced): " + cacheFile.getAbsolutePath());
- cacheFile.delete();
- }
- clear();
- }
- }
-
- protected void setDirty() {
- state = CacheState.DIRTY;
- }
-
- public void read() {
- if (state == CacheState.LOADING_FROM_CACHE_FILE) {
- // Already in progress, nothing to do.
- synchronized (this) {
- // Wait and do nothing.
- return;
- }
- }
- synchronized (this) {
- try {
- state = CacheState.LOADING_FROM_CACHE_FILE;
- if (!cacheFile.exists()) {
- // Cache file doesn't exist. Nothing to read.
- state = CacheState.DIRTY; // Needed to save cache to file.
- return;
- }
- log.info("Parsing cache file '" + cacheFile.getAbsolutePath() + "'.");
- String json;
- if (compress) {
- try (GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(cacheFile))) {
- StringWriter writer = new StringWriter();
- IOUtils.copy(in, writer, Definitions.STD_CHARSET);
- json = writer.toString();
- }
- } else {
- json = FileUtils.readFileToString(cacheFile, Definitions.STD_CHARSET);
- }
- AbstractCache readCache = JsonUtils.fromJson(this.getClass(), json);
- if (readCache != null) {
- this.lastModified = readCache.lastModified;
- this.created = readCache.created;
- update(readCache);
- this.state = CacheState.SAVED; // State of cache is updated from cache file.
- } else {
- log.error("Error while parsing cache: " + cacheFile.getAbsolutePath());
- this.state = CacheState.DIRTY; // Needed to save cache to file.
- }
- } catch (IOException ex) {
- log.error("Error while trying to read cache file '" + cacheFile.getAbsolutePath() + "': "
- + ex.getMessage(), ex);
- this.state = CacheState.DIRTY; // Needed to save cache to file.
- }
- }
- }
-
- protected abstract void update(AbstractCache readCache);
-
- public void save() {
- if (this.state == CacheState.SAVED || this.state == CacheState.INITIAL) {
- log.info("Cache file is up to date (nothing to save): " + cacheFile);
- return;
- }
- if (state == CacheState.SAVING) {
- // Already in progress, nothing to do.
- synchronized (this) {
- // Wait and do nothing.
- return;
- }
- }
- synchronized (this) {
- log.info("Saving to cache file: " + cacheFile);
- if (created == null) {
- created = lastModified = new Date();
- } else {
- lastModified = new Date();
- }
- String json = JsonUtils.toJson(this);
- try {
- if (this.compress) {
- try (GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(cacheFile))) {
- IOUtils.copy(new StringReader(json), out, Definitions.STD_CHARSET);
- }
- } else {
- FileUtils.write(cacheFile, json, Definitions.STD_CHARSET);
- }
- this.state = CacheState.SAVED;
- } catch (IOException ex) {
- log.error("Error while trying to write cache file '" + cacheFile.getAbsolutePath() + "': "
- + ex.getMessage(), ex);
- this.state = CacheState.DIRTY;
- }
- }
- }
-
- /**
- * Needed by jackson for deserialization.
- */
- AbstractCache() {
- }
-
- AbstractCache(File cacheDir, String cacheFilename) {
- this(cacheDir, cacheFilename, false);
- }
-
- AbstractCache(File cacheDir, String cacheFilename, boolean zip) {
- this.compress = zip;
- String filename = CACHE_FILE_PREFIX + cacheFilename + "." + CACHE_FILE_EXTENSION;
- if (this.compress)
- filename = filename + CACHE_FILE_ZIP_EXTENSION;
- cacheFile = new File(cacheDir, filename);
- this.state = CacheState.INITIAL;
- }
-
- public static boolean isCacheFile(File file) {
- String filename = file.getName();
- String extension = FilenameUtils.getExtension(filename);
- return filename.startsWith(CACHE_FILE_PREFIX) &&
- (extension.equals(CACHE_FILE_EXTENSION)
- || extension.equals(CACHE_FILE_EXTENSION + CACHE_FILE_ZIP_EXTENSION));
- }
-}
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractElementsCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractElementsCache.java
deleted file mode 100644
index 93dc1bb..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/AbstractElementsCache.java
+++ /dev/null
@@ -1,76 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import de.micromata.borgbutler.config.BorgRepoConfig;
-import lombok.Getter;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-import java.util.HashMap;
-import java.util.Map;
-
-public abstract class AbstractElementsCache<T> extends AbstractCache {
- private static Logger log = LoggerFactory.getLogger(AbstractElementsCache.class);
-
- @Getter
- protected Map<String, T> elements = new HashMap<>();
-
- public T get(BorgRepoConfig repoConfig, String identifier) {
- if (identifier == null) {
- return null;
- }
- if (getState() == CacheState.INITIAL) {
- read();
- }
- for (T element : elements.values()) {
- if (matches(element, identifier)) {
- return element;
- }
- }
- return load(repoConfig, identifier);
- }
-
- protected abstract T load(BorgRepoConfig repoConfig, String identifier);
-
- protected abstract boolean matches(T element, String identifier);
-
- protected abstract String getIdentifier(T element);
-
- protected abstract void updateFrom(T dest, T source);
-
- /**
- * Removes all entries (doesn't effect the cache files!).
- */
- public void clear() {
- elements.clear();
- super.clear();
- }
-
- public void upsert(BorgRepoConfig repoConfig, T element) {
- T existingElement = get(repoConfig, getIdentifier(element));
- if (existingElement == null) {
- elements.put(getIdentifier(element), element);
- } else {
- updateFrom(existingElement, element);
- }
- setDirty();
- }
-
- protected void update(AbstractCache readCache) {
- this.elements = ((AbstractElementsCache)readCache).elements;
- }
-
- /**
- * Needed by jackson for deserialization.
- */
- AbstractElementsCache() {
- }
-
- AbstractElementsCache(File cacheDir, String cacheFilename) {
- super(cacheDir, cacheFilename);
- }
-
- AbstractElementsCache(File cacheDir, String cacheFilename, boolean compress) {
- super(cacheDir, cacheFilename, compress);
- }
-}
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFileListCache.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFileListCache.java
deleted file mode 100644
index c60288d..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFileListCache.java
+++ /dev/null
@@ -1,58 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import de.micromata.borgbutler.BorgCommands;
-import de.micromata.borgbutler.config.BorgRepoConfig;
-import de.micromata.borgbutler.json.borg.Archive;
-import de.micromata.borgbutler.json.borg.FilesystemItem;
-import de.micromata.borgbutler.utils.ReplaceUtils;
-import lombok.Getter;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-import java.util.List;
-
-class ArchiveFileListCache extends AbstractCache {
- private static Logger log = LoggerFactory.getLogger(ArchiveFileListCache.class);
- public static final String CACHE_ARCHIVE_LISTS_BASENAME = "archive-content-";
-
- @JsonIgnore
- @Getter
- private Archive archive;
- @JsonProperty
- private List<FilesystemItem> content;
-
- public List<FilesystemItem> getContent(BorgRepoConfig repoConfig) {
- if (content == null) {
- read();
- }
- if (content == null) {
- this.content = BorgCommands.list(repoConfig, archive);
- save();
- }
- List<FilesystemItem> result = this.content;
- if (content != null && content.size() > 100000) {
- // Don't waste the memory space...
- this.content = null;
- }
- return result;
- }
-
- protected void update(AbstractCache readCache) {
- this.content = ((ArchiveFileListCache) readCache).content;
- }
-
- /**
- * Needed by jackson for deserialization.
- */
- ArchiveFileListCache() {
- }
-
- ArchiveFileListCache(File cacheDir, BorgRepoConfig repoConfig, Archive archive) {
- super(cacheDir, ReplaceUtils.encodeFilename(CACHE_ARCHIVE_LISTS_BASENAME + repoConfig.getName() + "-" + archive.getArchive(),
- true), true);
- this.archive = archive;
- }
-}
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 a2ec7d9..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveListCache.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import de.micromata.borgbutler.BorgCommands;
-import de.micromata.borgbutler.config.BorgRepoConfig;
-import de.micromata.borgbutler.json.borg.ArchiveInfo;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-
-public class ArchiveListCache extends AbstractElementsCache<ArchiveInfo> {
- private static Logger log = LoggerFactory.getLogger(ArchiveListCache.class);
- public static final String CACHE_ARCHIVE_LISTS_BASENAME = "archive-lists";
-
- @Override
- protected ArchiveInfo load(BorgRepoConfig repoConfig, String identifier) {
- ArchiveInfo archiveInfo = BorgCommands.info(repoConfig, identifier);
- this.elements.put(getIdentifier(archiveInfo), archiveInfo);
- return archiveInfo;
- }
-
- @Override
- protected boolean matches(ArchiveInfo element, String identifier) {
- return element.matches(identifier);
- }
-
- @Override
- protected String getIdentifier(ArchiveInfo element) {
- return element.getRepository().getId();
- }
-
- @Override
- protected void updateFrom(ArchiveInfo dest, ArchiveInfo source) {
- dest.updateFrom(source);
- }
-
- /**
- * 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 15a5334..b3ae45e 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
@@ -4,11 +4,7 @@
import de.micromata.borgbutler.BorgCommands;
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.ConfigurationHandler;
-import de.micromata.borgbutler.json.borg.Archive;
-import de.micromata.borgbutler.json.borg.FilesystemItem;
-import de.micromata.borgbutler.json.borg.RepoInfo;
-import de.micromata.borgbutler.json.borg.Repository;
-import lombok.Getter;
+import de.micromata.borgbutler.json.borg.*;
import org.apache.commons.jcs.access.CacheAccess;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@@ -25,11 +21,7 @@
private JCSCache jcsCache = JCSCache.getInstance();
private CacheAccess<String, RepoInfo> repoInfoCacheAccess;
- @Getter
- private RepoListCache repoListCache;
- private ArchiveListCache archiveListCache;
- private List<AbstractElementsCache> caches;
- private List<ArchiveFileListCache> archiveFileListCaches;
+ private CacheAccess<String, RepoList> repoListCacheAccess;
@JsonIgnore
private File cacheDir;
@@ -38,12 +30,6 @@
return instance;
}
- public void save() {
- for (AbstractElementsCache cache : caches) {
- cache.save();
- }
- }
-
public Repository getRepository(String idOrName) {
RepoInfo repoInfo = getRepoInfo(idOrName);
if (repoInfo == null) {
@@ -77,38 +63,19 @@
return repositories;
}
- public List<FilesystemItem> getArchiveContent(BorgRepoConfig repoConfig, Archive archive) {
+ public RepoList getRepoList(String idOrName) {
+ BorgRepoConfig repoConfig = ConfigurationHandler.getConfiguration().getRepoConfig(idOrName);
+ ArchiveInfo archiveInfo = BorgCommands.info(repoConfig, repoConfig.getRepo());
+ RepoList repoList = BorgCommands.list(repoConfig);
+ return null;
+ }
+
+ public List<FilesystemItem> getArchiveContent_(BorgRepoConfig repoConfig, Archive archive) {
if (archive == null || StringUtils.isBlank(archive.getArchive())) {
return null;
}
- ArchiveFileListCache cache = null;
- for (ArchiveFileListCache existingCache : archiveFileListCaches) {
- if (archive.equals(existingCache.getArchive())) {
- // Cache is already known:
- cache = existingCache;
- break;
- }
- }
- if (cache == null) {
- cache = new ArchiveFileListCache(cacheDir, repoConfig, archive);
- }
- return cache.getContent(repoConfig);
- }
-
- /**
- * Removes all cache files and clears all caches.
- */
- public void removeAllCacheFiles() {
- File[] files = cacheDir.listFiles();
- for (File file : files) {
- if (AbstractElementsCache.isCacheFile(file)) {
- log.info("Deleting cache file: " + file.getAbsolutePath());
- file.delete();
- }
- }
- for (AbstractElementsCache cache : caches) {
- cache.clear();
- }
+ List<FilesystemItem> content = BorgCommands.list(repoConfig, archive);
+ return content;
}
private ButlerCache() {
@@ -117,10 +84,6 @@
log.info("Creating cache dir: " + cacheDir.getAbsolutePath());
cacheDir.mkdir();
}
- caches = new ArrayList<>();
- caches.add(repoListCache = new RepoListCache(cacheDir));
- caches.add(archiveListCache = new ArchiveListCache(cacheDir));
- archiveFileListCaches = new ArrayList<>();
this.repoInfoCacheAccess = jcsCache.getJCSCache();
}
}
diff --git a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/CacheState.java b/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/CacheState.java
deleted file mode 100644
index 87a6771..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/CacheState.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-/**
- * ,
- * PARSING - , DIRTY - , SAVED -
- */
-public enum CacheState {
- /**
- * On startup or directly after {@link AbstractCache#clear()} is called. (not yet read).
- */
- INITIAL,
- /**
- * Cache is been loaded (with borg command).
- */
- LOADING_FROM_BORG,
- /**
- * Cache is beeing loaded from filesystem.
- */
- LOADING_FROM_CACHE_FILE,
- /**
- * Modifications done but not yet written.
- */
- DIRTY,
- /**
- * Save to cache file is in progress.
- */
- SAVING,
- /**
- * Content has been written to file, no modifications done after. Cache file is up-to-date.
- */
- SAVED
-
-}
\ No newline at end of file
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 d09ea39..0000000
--- a/borgbutler-core/src/main/java/de/micromata/borgbutler/cache/RepoListCache.java
+++ /dev/null
@@ -1,46 +0,0 @@
-package de.micromata.borgbutler.cache;
-
-import de.micromata.borgbutler.BorgCommands;
-import de.micromata.borgbutler.config.BorgRepoConfig;
-import de.micromata.borgbutler.json.borg.RepoList;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-
-public class RepoListCache extends AbstractElementsCache<RepoList> {
- private static Logger log = LoggerFactory.getLogger(RepoListCache.class);
- public static final String CACHE_REPO_LISTS_BASENAME = "repo-lists";
-
- @Override
- protected RepoList load(BorgRepoConfig repoConfig, String identifier) {
- RepoList repoList = BorgCommands.list(repoConfig);
- this.elements.put(getIdentifier(repoList), repoList);
- return repoList;
- }
-
- @Override
- protected boolean matches(RepoList element, String identifier) {
- return element.matches(identifier);
- }
-
- @Override
- protected String getIdentifier(RepoList element) {
- return element.getRepository().getId();
- }
-
- @Override
- protected void updateFrom(RepoList dest, RepoList source) {
- dest.updateFrom(source);
- }
-
- /**
- * 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 e944dfe..4e07239 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
@@ -3,17 +3,11 @@
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.Configuration;
import de.micromata.borgbutler.config.ConfigurationHandler;
-import de.micromata.borgbutler.json.borg.Archive;
-import de.micromata.borgbutler.json.borg.FilesystemItem;
import de.micromata.borgbutler.json.borg.RepoInfo;
-import de.micromata.borgbutler.json.borg.RepoList;
-import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.List;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -36,7 +30,7 @@
}
assertEquals(config.getRepoConfigs().size(), ButlerCache.getInstance().getAllRepositories().size());
}
- {
+/* {
for (BorgRepoConfig repoConfig : ConfigurationHandler.getConfiguration().getRepoConfigs()) {
RepoList repoList = ButlerCache.getInstance().getRepoListCache().get(repoConfig, repoConfig.getRepo());
}
@@ -51,7 +45,7 @@
if (repoList != null && CollectionUtils.isNotEmpty(repoList.getArchives())) {
archive = repoList.getArchives().get(0);
}
- }
+ }*/
{/*
List<BorgRepoConfig> repoConfigs = ConfigurationHandler.getConfiguration().getRepoConfigs();
if (CollectionUtils.isNotEmpty(repoConfigs)) {
@@ -67,13 +61,13 @@
}
}*/
}
+ /*
{
if (archive != null) {
List<FilesystemItem> content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive);
log.info("Number of items (content) of archive: " + content.size());
content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive);
}
- }
- butlerCache.save();
+ }*/
}
}
diff --git a/borgbutler-server/src/main/java/de/micromata/borgbutler/server/Main.java b/borgbutler-server/src/main/java/de/micromata/borgbutler/server/Main.java
index 3963b57..2bcd91d 100644
--- a/borgbutler-server/src/main/java/de/micromata/borgbutler/server/Main.java
+++ b/borgbutler-server/src/main/java/de/micromata/borgbutler/server/Main.java
@@ -1,6 +1,5 @@
package de.micromata.borgbutler.server;
-import de.micromata.borgbutler.cache.ButlerCache;
import de.micromata.borgbutler.server.jetty.JettyServer;
import de.micromata.borgbutler.server.user.SingleUserManager;
import de.micromata.borgbutler.server.user.UserManager;
@@ -105,7 +104,6 @@
}
log.info("Shutting down BorgButler web server...");
server.stop();
- ButlerCache.getInstance().save();
}
private static void printHelp(Options options) {
--
Gitblit v1.10.0