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

Kai Reinhard
16.54.2018 afdb1082a7b5bf29be6c2dd21b593ef69562764a
Archive content from array to list.
4 files modified
64 ■■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java 15 ●●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java 29 ●●●● patch | view | raw | blame | history
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/ArchiveFilelistCacheTest.java 14 ●●●● patch | view | raw | blame | history
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java 6 ●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ArchiveFilelistCache.java
@@ -15,10 +15,7 @@
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.*;
/**
 * Cache for storing complete file lists of archives as gzipped files (using Java standard serialization for
@@ -61,7 +58,7 @@
     * @param archive
     * @return
     */
    public BorgFilesystemItem[] load(BorgRepoConfig repoConfig, Archive archive) {
    public List<BorgFilesystemItem> load(BorgRepoConfig repoConfig, Archive archive) {
        File file = getFile(repoConfig, archive);
        if (!file.exists()) {
            return null;
@@ -69,13 +66,13 @@
        return load(file);
    }
    public BorgFilesystemItem[] load(File file) {
    public List<BorgFilesystemItem> load(File file) {
        if (file.exists() == false) {
            log.error("File '" + file.getAbsolutePath() + "' doesn't exist. Can't get archive content files.");
            return null;
        }
        log.info("Loading archive content as file list from: " + file.getAbsolutePath());
        BorgFilesystemItem[] list = null;
        List<BorgFilesystemItem> list = null;
        try {
            // Set last modified time of file:
            Files.setAttribute(file.toPath(), "lastModifiedTime", FileTime.fromMillis(System.currentTimeMillis()));
@@ -89,11 +86,11 @@
                return null;
            }
            int size = (Integer) obj;
            list = new BorgFilesystemItem[size];
            list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                obj = inputStream.readObject();
                if (obj instanceof BorgFilesystemItem) {
                    list[i] = (BorgFilesystemItem) obj;
                    list.add((BorgFilesystemItem) obj);
                } else {
                    log.error("Can't load archive content. FilesystemItem expected, but received: "
                            + (obj != null ? obj.getClass() : "null")
borgbutler-core/src/main/java/de/micromata/borgbutler/cache/ButlerCache.java
@@ -179,16 +179,37 @@
        return archive;
    }
    public BorgFilesystemItem[] getArchiveContent(BorgRepoConfig repoConfig, Archive archive) {
    public List<BorgFilesystemItem> getArchiveContent(String archiveId) {
        Archive archive = null;
        outerLoop:
        for (Repository repository : getAllRepositories()) {
            if (repository.getArchives() != null) {
                for (Archive arch : repository.getArchives()) {
                    if (StringUtils.equals(archiveId, arch.getId())) {
                        archive = arch;
                        break outerLoop;
                    }
                }
            }
        }
        if (archive == null) {
            log.error("Can't find archive with id '" + archiveId + "'. May-be it doesn't exist or the archives of the target repository aren't yet loaded.");
            return null;
        }
        BorgRepoConfig repoConfig = ConfigurationHandler.getConfiguration().getRepoConfig(archive.getRepoId());
        return getArchiveContent(repoConfig, archive);
    }
    public List<BorgFilesystemItem> getArchiveContent(BorgRepoConfig repoConfig, Archive archive) {
        if (archive == null || StringUtils.isBlank(archive.getName())) {
            return null;
        }
        BorgFilesystemItem[] items = archiveFilelistCache.load(repoConfig, archive);
        List<BorgFilesystemItem> items = archiveFilelistCache.load(repoConfig, archive);
        if (items == null) {
            List<BorgFilesystemItem> list = BorgCommands.listArchiveContent(repoConfig, archive.getName());
            if (CollectionUtils.isNotEmpty(list)) {
                archiveFilelistCache.save(repoConfig, archive, list);
                items = list.toArray(new BorgFilesystemItem[0]);
                items = new ArrayList<>();
            }
        }
        if (items == null) {
@@ -197,7 +218,7 @@
        return items;
    }
    public BorgFilesystemItem[] getArchiveContent(File file) {
    public List<BorgFilesystemItem> getArchiveContent(File file) {
        return archiveFilelistCache.load(file);
    }
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/ArchiveFilelistCacheTest.java
@@ -34,11 +34,11 @@
        cache.save(repoConfig, archive, list);
        log.info("Saving done.");
        log.info("Loading items from out dir.");
        BorgFilesystemItem[] filesystemItems = cache.load(repoConfig, archive);
        log.info("Loading " + filesystemItems.length + " items done.");
        assertEquals(list.size(), filesystemItems.length);
        for (int i = 0; i < filesystemItems.length; i++) {
            assertEquals(list.get(i).getPath(), filesystemItems[i].getPath());
        List<BorgFilesystemItem> filesystemItems = cache.load(repoConfig, archive);
        log.info("Loading " + filesystemItems.size() + " items done.");
        assertEquals(list.size(), filesystemItems.size());
        for (int i = 0; i < filesystemItems.size(); i++) {
            assertEquals(list.get(i).getPath(), filesystemItems.get(i).getPath());
        }
        cache.removeAllCacheFiles();
    }
@@ -53,7 +53,7 @@
        Archive archive = createArchive("2018-12-09");
        assertNull(cache.load(repoConfig, archive));
        cache.save(repoConfig, archive, list);
        BorgFilesystemItem[] filesystemItems = cache.load(repoConfig, archive);
        List<BorgFilesystemItem> filesystemItems = cache.load(repoConfig, archive);
        assertNull(cache.load(repoConfig, archive));
        cache.removeAllCacheFiles();
    }
@@ -148,7 +148,7 @@
    private Archive createArchive(String time) throws Exception {
        Archive archive = new Archive();
        set(archive, "archive", "archive-" + time);
        set(archive, "name", "archive-" + time);
        set(archive, "time", time);
        return archive;
    }
borgbutler-core/src/test/java/de/micromata/borgbutler/cache/CacheTest.java
@@ -57,10 +57,10 @@
                assertNotNull(archive2);
                archive = ButlerCache.getInstance().getArchive(repoConfig.getRepo(), archive.getId());
                assertNotNull(archive2);
                BorgFilesystemItem[] content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive2);
                log.info("Number of items (content) of archive: " + content.length);
                List<BorgFilesystemItem> content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive2);
                log.info("Number of items (content) of archive: " + content.size());
                content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive2);
                log.info("Number of items (content) of archive: " + content.length);
                log.info("Number of items (content) of archive: " + content.size());
            }
        }
        ButlerCache.getInstance().shutdown();