• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oracle / opengrok / #3650

24 Oct 2023 03:07PM UTC coverage: 66.012% (-8.4%) from 74.444%
#3650

push

vladak
refactory repository history check

9 of 9 new or added lines in 1 file covered. (100.0%)

38668 of 58577 relevant lines covered (66.01%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.17
/opengrok-indexer/src/main/java/org/opengrok/indexer/history/AbstractCache.java
1
/*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License (the "License").
6
 * You may not use this file except in compliance with the License.
7
 *
8
 * See LICENSE.txt included in this distribution for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing Covered Code, include this CDDL HEADER in each
12
 * file and include the License file at LICENSE.txt.
13
 * If applicable, add the following below this CDDL HEADER, with the
14
 * fields enclosed by brackets "[]" replaced with your own identifying
15
 * information: Portions Copyright [yyyy] [name of copyright owner]
16
 *
17
 * CDDL HEADER END
18
 */
19

20
/*
21
 * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
22
 */
23
package org.opengrok.indexer.history;
24

25
import org.opengrok.indexer.configuration.RuntimeEnvironment;
26
import org.opengrok.indexer.util.ForbiddenSymlinkException;
27
import org.opengrok.indexer.util.TandemPath;
28

29
import java.io.File;
30
import java.io.IOException;
31
import java.util.ArrayList;
32
import java.util.Collection;
33
import java.util.List;
34
import java.util.logging.Level;
35

36
/**
37
 * Class to hold code shared between various cache implementations,
38
 * notably {@link FileHistoryCache} and {@link FileAnnotationCache}.
39
 */
40
public abstract class AbstractCache implements Cache {
1✔
41

42

43

44
    public boolean hasCacheForFile(File file) throws CacheException {
45
        try {
46
            return getCachedFile(file).exists();
1✔
47
        } catch (CacheException ex) {
×
48
            throw new CacheException(ex);
×
49
        }
50
    }
51

52
    /**
53
     * Get a <code>File</code> object describing the cache file.
54
     *
55
     * @param file the file to find the cache for
56
     * @return file that might contain cached object for <code>file</code>
57
     * @throws CacheException on error
58
     */
59
    File getCachedFile(File file) throws CacheException {
60

61
        RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1✔
62

63
        StringBuilder sb = new StringBuilder();
1✔
64
        sb.append(env.getDataRootPath());
1✔
65
        sb.append(File.separatorChar);
1✔
66
        sb.append(getCacheDirName());
1✔
67

68
        try {
69
            String add = env.getPathRelativeToSourceRoot(file);
1✔
70
            if (add.length() == 0) {
1✔
71
                add = File.separator;
×
72
            }
73
            sb.append(add);
1✔
74
        } catch (ForbiddenSymlinkException | IOException e) {
×
75
            throw new CacheException("Failed to get path relative to source root for " + file, e);
×
76
        }
1✔
77

78
        String suffix = getCacheFileSuffix();
1✔
79
        if (suffix != null && !suffix.isEmpty()) {
1✔
80
            return new File(TandemPath.join(sb.toString(), suffix));
×
81
        }
82

83
        return new File(sb.toString());
1✔
84
    }
85

86
    public List<String> clearCache(Collection<RepositoryInfo> repositories) {
87
        List<String> clearedRepos = new ArrayList<>();
×
88

89
        for (RepositoryInfo repo : repositories) {
×
90
            try {
91
                this.clear(repo);
×
92
                clearedRepos.add(repo.getDirectoryNameRelative());
×
93
                LOGGER.log(Level.INFO, "{1} cache for ''{0}'' cleared.",
×
94
                        new Object[]{repo.getDirectoryName(), this.getInfo()});
×
95
            } catch (CacheException e) {
×
96
                LOGGER.log(Level.WARNING,
×
97
                        "Clearing cache for repository {0} failed: {1}",
98
                        new Object[]{repo.getDirectoryName(), e.getLocalizedMessage()});
×
99
            }
×
100
        }
×
101

102
        return clearedRepos;
×
103
    }
104

105
    /**
106
     * Attempt to delete file with its parent.
107
     * @param file file to delete
108
     */
109
    static void clearWithParent(File file) {
110
        File parent = file.getParentFile();
1✔
111

112
        if (!file.delete() && file.exists()) {
1✔
113
            LOGGER.log(Level.WARNING, "Failed to remove obsolete cache-file: {0}", file.getAbsolutePath());
×
114
        }
115

116
        if (parent.delete()) {
1✔
117
            LOGGER.log(Level.FINE, "Removed empty cache dir:{0}", parent.getAbsolutePath());
1✔
118
        }
119
    }
1✔
120

121
    public void clearFile(String path) {
122
        File historyFile;
123
        try {
124
            historyFile = getCachedFile(new File(RuntimeEnvironment.getInstance().getSourceRootPath() + path));
1✔
125
        } catch (CacheException ex) {
×
126
            LOGGER.log(Level.WARNING, String.format("cannot get cached file for file '%s'"
×
127
                    + " - the cache entry will not be cleared", path), ex);
128
            return;
×
129
        }
1✔
130

131
        clearWithParent(historyFile);
1✔
132
    }
1✔
133

134
    public String getCacheFileSuffix() {
135
        return "";
1✔
136
    }
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc