Skip to content

Commit 0d2003a

Browse files
committed
introduce CacheException
1 parent 04da486 commit 0d2003a

18 files changed

+184
-152
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/AbstractCache.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@
3939
*/
4040
public abstract class AbstractCache implements Cache {
4141

42-
public boolean hasCacheForFile(File file) throws HistoryException {
42+
43+
44+
public boolean hasCacheForFile(File file) throws CacheException {
4345
try {
4446
return getCachedFile(file).exists();
45-
} catch (ForbiddenSymlinkException ex) {
46-
LOGGER.log(Level.FINER, ex.getMessage());
47-
return false;
47+
} catch (CacheException ex) {
48+
throw new CacheException(ex);
4849
}
4950
}
5051

@@ -53,8 +54,9 @@ public boolean hasCacheForFile(File file) throws HistoryException {
5354
*
5455
* @param file the file to find the cache for
5556
* @return file that might contain cached object for <code>file</code>
57+
* @throws CacheException on error
5658
*/
57-
File getCachedFile(File file) throws HistoryException, ForbiddenSymlinkException {
59+
File getCachedFile(File file) throws CacheException {
5860

5961
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
6062

@@ -69,8 +71,8 @@ File getCachedFile(File file) throws HistoryException, ForbiddenSymlinkException
6971
add = File.separator;
7072
}
7173
sb.append(add);
72-
} catch (IOException e) {
73-
throw new HistoryException("Failed to get path relative to source root for " + file, e);
74+
} catch (ForbiddenSymlinkException | IOException e) {
75+
throw new CacheException("Failed to get path relative to source root for " + file, e);
7476
}
7577

7678
return new File(TandemPath.join(sb.toString(), ".gz"));
@@ -85,7 +87,7 @@ public List<String> clearCache(Collection<RepositoryInfo> repositories) {
8587
clearedRepos.add(repo.getDirectoryNameRelative());
8688
LOGGER.log(Level.INFO, "{1} cache for ''{0}'' cleared.",
8789
new Object[]{repo.getDirectoryName(), this.getInfo()});
88-
} catch (HistoryException e) {
90+
} catch (CacheException e) {
8991
LOGGER.log(Level.WARNING,
9092
"Clearing cache for repository {0} failed: {1}",
9193
new Object[]{repo.getDirectoryName(), e.getLocalizedMessage()});
@@ -115,11 +117,9 @@ public void clearFile(String path) {
115117
File historyFile;
116118
try {
117119
historyFile = getCachedFile(new File(RuntimeEnvironment.getInstance().getSourceRootPath() + path));
118-
} catch (ForbiddenSymlinkException ex) {
119-
LOGGER.log(Level.FINER, ex.getMessage());
120-
return;
121-
} catch (HistoryException ex) {
122-
LOGGER.log(Level.WARNING, String.format("cannot get history file for file %s", path), ex);
120+
} catch (CacheException ex) {
121+
LOGGER.log(Level.WARNING, String.format("cannot get cached file for file '%s'"
122+
+ " - the cache entry will not be cleared", path), ex);
123123
return;
124124
}
125125

opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationCache.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,23 @@
2727
import java.io.File;
2828

2929
public interface AnnotationCache extends Cache {
30-
/**
31-
* Perform whatever initialization is needed for given cache implementation.
32-
*/
33-
void initialize();
34-
3530
/**
3631
* Retrieve annotation from cache.
3732
* @param file file under source root to get the annotation for
3833
* @param rev requested revision
3934
* @return {@link Annotation} object or <code>null</code>
40-
* @throws AnnotationException on error
35+
* @throws CacheException on error
4136
*/
4237
@Nullable
43-
Annotation get(File file, String rev) throws AnnotationException;
38+
Annotation get(File file, String rev) throws CacheException;
4439

4540
/**
4641
* Store annotation for file into cache.
4742
* @param file file under source root to store the annotation for
4843
* @param annotation {@link Annotation} object
49-
* @throws AnnotationException on error
44+
* @throws CacheException on error
5045
*/
51-
void store(File file, Annotation annotation) throws AnnotationException;
46+
void store(File file, Annotation annotation) throws CacheException;
5247

5348
/**
5449
* Clear annotation cache entry for given file.

opengrok-indexer/src/main/java/org/opengrok/indexer/history/AnnotationException.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,16 @@
2222
*/
2323
package org.opengrok.indexer.history;
2424

25-
import java.util.logging.Level;
26-
2725
public class AnnotationException extends Exception {
2826
private static final long serialVersionUID = 1L;
2927

30-
private final Level level;
31-
32-
/**
33-
* Construct a {@code HistoryException} with the specified message.
34-
*
35-
* @param msg the message string
36-
*/
37-
public AnnotationException(String msg) {
38-
this(msg, Level.WARNING);
39-
}
40-
41-
public AnnotationException(String msg, Level level) {
42-
super(msg);
43-
this.level = level;
44-
}
45-
4628
/**
4729
* Construct a {@code HistoryException} with the specified cause.
4830
*
4931
* @param cause the cause of the exception
5032
*/
5133
public AnnotationException(Throwable cause) {
5234
super(cause);
53-
this.level = Level.WARNING;
5435
}
5536

5637
/**
@@ -61,13 +42,5 @@ public AnnotationException(Throwable cause) {
6142
*/
6243
public AnnotationException(String msg, Throwable cause) {
6344
super(msg, cause);
64-
this.level = Level.WARNING;
65-
}
66-
67-
/**
68-
* @return log {@link Level} to be used when logging the exception
69-
*/
70-
public Level getLevel() {
71-
return level;
7245
}
7346
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/Cache.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
package org.opengrok.indexer.history;
2424

2525
import org.opengrok.indexer.logger.LoggerFactory;
26-
import org.opengrok.indexer.util.ForbiddenSymlinkException;
2726

2827
import java.io.File;
2928
import java.util.Collection;
@@ -34,6 +33,13 @@ public interface Cache {
3433

3534
Logger LOGGER = LoggerFactory.getLogger(Cache.class);
3635

36+
/**
37+
* Create and initialize an empty cache if one doesn't exist already.
38+
*
39+
* @throws CacheException if initialization fails
40+
*/
41+
void initialize() throws CacheException;
42+
3743
/**
3844
* Get a string with information about the cache.
3945
* @return a free form text string describing the history instance
@@ -44,9 +50,9 @@ public interface Cache {
4450
* Clear the cache for a repository.
4551
*
4652
* @param repository the repository whose cache to clear
47-
* @throws HistoryException if the cache couldn't be cleared
53+
* @throws CacheException if the cache couldn't be cleared
4854
*/
49-
void clear(RepositoryInfo repository) throws HistoryException;
55+
void clear(RepositoryInfo repository) throws CacheException;
5056

5157
/**
5258
* Optimize how the history is stored on disk. This method is typically
@@ -56,9 +62,9 @@ public interface Cache {
5662
* or reordering the disk image of the cache in order to optimize
5763
* performance or space usage.
5864
*
59-
* @throws HistoryException if an error happens during optimization
65+
* @throws CacheException if an error happens during optimization
6066
*/
61-
void optimize() throws HistoryException;
67+
void optimize() throws CacheException;
6268

6369
/**
6470
* Check whether this cache implementation can store history for the given repository.
@@ -89,14 +95,12 @@ public interface Cache {
8995
* @param file the file to check
9096
* @return {@code true} if the file is in the cache, {@code false} otherwise
9197
*/
92-
boolean hasCacheForFile(File file) throws HistoryException;
98+
boolean hasCacheForFile(File file) throws CacheException;
9399

94100
/**
95101
* @param file source file
96102
* @return whether cache entry for given file is fresh
97-
* @throws HistoryException on error
98-
* @throws ForbiddenSymlinkException on error
99-
* TODO: HistoryException -> CacheException
103+
* @throws CacheException on error
100104
*/
101-
boolean isUpToDate(File file) throws HistoryException, ForbiddenSymlinkException;
105+
boolean isUpToDate(File file) throws CacheException;
102106
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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) 2022, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.history;
24+
25+
import java.util.logging.Level;
26+
27+
/**
28+
* Exception thrown when retrieval or manipulation of cache data fails.
29+
*/
30+
public class CacheException extends Exception {
31+
private static final long serialVersionUID = 1L;
32+
33+
private final Level level;
34+
35+
/**
36+
* Construct a {@code HistoryException} with the specified message.
37+
*
38+
* @param msg the message string
39+
*/
40+
public CacheException(String msg) {
41+
this(msg, Level.WARNING);
42+
}
43+
44+
public CacheException(String msg, Level level) {
45+
super(msg);
46+
this.level = level;
47+
}
48+
49+
/**
50+
* Construct a {@code CacheException} with the specified cause.
51+
*
52+
* @param cause the cause of the exception
53+
*/
54+
public CacheException(Throwable cause) {
55+
super(cause);
56+
this.level = Level.WARNING;
57+
}
58+
59+
/**
60+
* Construct a {@code CacheException} with the specified message and cause.
61+
*
62+
* @param msg the message string
63+
* @param cause the cause of the exception
64+
*/
65+
public CacheException(String msg, Throwable cause) {
66+
super(msg, cause);
67+
this.level = Level.WARNING;
68+
}
69+
70+
/**
71+
* @return log {@link Level} to be used when logging the exception
72+
*/
73+
public Level getLevel() {
74+
return level;
75+
}
76+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/FileAnnotationCache.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.jetbrains.annotations.VisibleForTesting;
2929
import org.opengrok.indexer.Metrics;
3030
import org.opengrok.indexer.logger.LoggerFactory;
31-
import org.opengrok.indexer.util.ForbiddenSymlinkException;
3231
import org.opengrok.indexer.util.Statistics;
3332

3433
import java.beans.XMLDecoder;
@@ -81,19 +80,19 @@ static Annotation readCache(File file) throws IOException {
8180
}
8281

8382
@VisibleForTesting
84-
Annotation readAnnotation(File file) throws AnnotationException {
83+
Annotation readAnnotation(File file) throws CacheException {
8584
File cacheFile;
8685
try {
8786
cacheFile = getCachedFile(file);
88-
} catch (HistoryException | ForbiddenSymlinkException e) {
87+
} catch (CacheException e) {
8988
LOGGER.log(Level.WARNING, "failed to get annotation cache file", e);
9089
return null;
9190
}
9291

9392
try {
9493
return readCache(cacheFile);
9594
} catch (IOException e) {
96-
throw new AnnotationException(String.format("failed to read annotation cache for '%s'", file), e);
95+
throw new CacheException(String.format("failed to read annotation cache for '%s'", file), e);
9796
}
9897
}
9998

@@ -106,12 +105,12 @@ Annotation readAnnotation(File file) throws AnnotationException {
106105
public boolean isUpToDate(File file) {
107106
try {
108107
return get(file, null) != null;
109-
} catch (AnnotationException e) {
108+
} catch (CacheException e) {
110109
return false;
111110
}
112111
}
113112

114-
public Annotation get(File file, @Nullable String rev) throws AnnotationException {
113+
public Annotation get(File file, @Nullable String rev) throws CacheException {
115114
Annotation annotation = null;
116115
String latestRevision = LatestRevisionUtil.getLatestRevision(file);
117116
if (rev == null || (latestRevision != null && latestRevision.equals(rev))) {
@@ -157,23 +156,23 @@ public Annotation get(File file, @Nullable String rev) throws AnnotationExceptio
157156
return annotation;
158157
}
159158

160-
public void store(File file, Annotation annotation) throws AnnotationException {
159+
public void store(File file, Annotation annotation) throws CacheException {
161160
if (annotation.getRevision() == null || annotation.getRevision().isEmpty()) {
162-
throw new AnnotationException(String.format("annotation for ''%s'' does not contain revision", file));
161+
throw new CacheException(String.format("annotation for ''%s'' does not contain revision", file));
163162
}
164163

165164
File cacheFile;
166165
try {
167166
cacheFile = getCachedFile(file);
168-
} catch (ForbiddenSymlinkException | HistoryException e) {
167+
} catch (CacheException e) {
169168
LOGGER.log(Level.FINER, e.getMessage());
170169
return;
171170
}
172171

173172
File dir = cacheFile.getParentFile();
174173
// calling isDirectory() twice to prevent a race condition
175174
if (!dir.isDirectory() && !dir.mkdirs() && !dir.isDirectory()) {
176-
throw new AnnotationException("Unable to create cache directory '" + dir + "'.");
175+
throw new CacheException("Unable to create cache directory '" + dir + "'.");
177176
}
178177

179178
Statistics statistics = new Statistics();

0 commit comments

Comments
 (0)