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

Kai Reinhard
09.32.2018 4ece88750272b3e2fc0bbccec7202d2faa1115b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package de.micromata.borgbutler.cache;
 
import de.micromata.borgbutler.BorgCommands;
import de.micromata.borgbutler.config.BorgRepoConfig;
import de.micromata.borgbutler.config.Configuration;
import de.micromata.borgbutler.config.ConfigurationHandler;
import de.micromata.borgbutler.json.borg.Archive1;
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;
import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 
public class CacheTest {
    private static Logger log = LoggerFactory.getLogger(CacheTest.class);
 
    @Test
    void repoCacheTest() {
        ConfigurationHandler configHandler = ConfigurationHandler.getInstance();
        configHandler.read();
        Configuration config = ConfigurationHandler.getConfiguration();
        if (config.getRepoConfigs().size() == 0) {
            log.info("No repos configured. Please configure repos first in: " + configHandler.getConfigFile().getAbsolutePath());
            return;
        }
        ButlerCache butlerCache = ButlerCache.getInstance();
        //butlerCache.removeAllCacheFiles();
        butlerCache.read();
        {
            RepoInfoCache repoInfoCache = ButlerCache.getInstance().getRepoInfoCache();
            if (repoInfoCache.getElements().size() != config.getRepoConfigs().size()) {
                refreshRepoInfoCache(config, repoInfoCache);
            }
            assertEquals(config.getRepoConfigs().size(), repoInfoCache.getElements().size());
        }
        {
            RepoListCache repoListCache = ButlerCache.getInstance().getRepoListCache();
            if (repoListCache.getElements().size() != config.getRepoConfigs().size()) {
                refreshRepoListCache(config, repoListCache);
            }
            assertEquals(config.getRepoConfigs().size(), repoListCache.getElements().size());
        }
        List<BorgRepoConfig> repoConfigs = ConfigurationHandler.getConfiguration().getRepoConfigs();
        Archive1 archive = null;
        BorgRepoConfig repoConfig = null;
        if (CollectionUtils.isNotEmpty(repoConfigs)) {
            repoConfig = repoConfigs.get(0);
            RepoList repoList = ButlerCache.getInstance().getRepoListCache().get(repoConfig, repoConfig.getRepo());
            if (repoList != null && CollectionUtils.isNotEmpty(repoList.getArchives())) {
                archive = repoList.getArchives().get(0);
            }
        }
        {/*
            List<BorgRepoConfig> repoConfigs = ConfigurationHandler.getConfiguration().getRepoConfigs();
            if (CollectionUtils.isNotEmpty(repoConfigs)) {
                BorgRepoConfig repoConfig = repoConfigs.get(0);
                RepoList repoList = ButlerCache.getRepoListCache().get(repoConfig.getRepo());
                if (repoList != null && CollectionUtils.isNotEmpty(repoList.getArchives())) {
                    Archive1 archive = repoList.getArchives().get(0);
                    if (archive != null) {
                        ArchiveList list = ButlerCache.getArchiveListCache().get(archive.getArchive());
                        ArchiveList list = BorgCommands.info(repoConfig, archive.getArchive());
                        log.info(list.toString());
                    }
                }
            }*/
        }
        {/*
            if (archive != null) {
                String json = BorgCommands.list(repoConfig, archive.getArchive());
                log.info(json);
            }*/
        }
        butlerCache.save();
    }
 
    private void refreshRepoInfoCache(Configuration config, RepoInfoCache repoInfoCache) {
        for (BorgRepoConfig repo : config.getRepoConfigs()) {
            log.info("Processing repo info '" + repo + "'");
            RepoInfo repoInfo = BorgCommands.info(repo);
            repoInfoCache.upsert(repo, repoInfo);
            repoInfo = repoInfoCache.get(repo, repoInfo.getRepository().getId());
            assertNotNull(repoInfo);
        }
    }
 
    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(repo, repoList);
            repoList = repoListCache.get(repo, repoList.getRepository().getId());
            assertNotNull(repoList);
        }
    }
 
    private void refresArchiveListCache(Configuration config, RepoListCache repoListCache) {
        for (BorgRepoConfig repo : config.getRepoConfigs()) {
            log.info("Processing repo list '" + repo + "'");
            RepoList repoList = BorgCommands.list(repo);
            repoListCache.upsert(repo, repoList);
            repoList = repoListCache.get(repo, repoList.getRepository().getId());
            assertNotNull(repoList);
        }
    }
}