Skip to content

Commit f52e5f1

Browse files
committed
use CBOR to serialize AnnotationData
fixes #4096
1 parent 2004480 commit f52e5f1

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

opengrok-indexer/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ Portions Copyright (c) 2020-2020, Lubos Kosco <[email protected]>.
194194
<artifactId>jackson-databind</artifactId>
195195
<version>${jackson.version}</version>
196196
</dependency>
197+
<dependency>
198+
<groupId>com.fasterxml.jackson.dataformat</groupId>
199+
<artifactId>jackson-dataformat-cbor</artifactId>
200+
<version>${jackson.version}</version>
201+
</dependency>
197202
<dependency>
198203
<groupId>com.fasterxml.jackson.core</groupId>
199204
<artifactId>jackson-annotations</artifactId>

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ File getCachedFile(File file) throws CacheException {
7575
throw new CacheException("Failed to get path relative to source root for " + file, e);
7676
}
7777

78-
return new File(TandemPath.join(sb.toString(), ".gz"));
78+
String suffix = getCacheFileSuffix();
79+
if (suffix != null && !suffix.isEmpty()) {
80+
return new File(TandemPath.join(sb.toString(), suffix));
81+
}
82+
83+
return new File(sb.toString());
7984
}
8085

8186
public List<String> clearCache(Collection<RepositoryInfo> repositories) {
@@ -125,4 +130,8 @@ public void clearFile(String path) {
125130

126131
clearWithParent(historyFile);
127132
}
133+
134+
public String getCacheFileSuffix() {
135+
return ".gz";
136+
}
128137
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,9 @@ public interface Cache {
103103
* @throws CacheException on error
104104
*/
105105
boolean isUpToDate(File file) throws CacheException;
106+
107+
/**
108+
* @return suffix used for the cache files
109+
*/
110+
String getCacheFileSuffix();
106111
}

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package org.opengrok.indexer.history;
2424

25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
import com.fasterxml.jackson.dataformat.cbor.databind.CBORMapper;
2527
import io.micrometer.core.instrument.Counter;
2628
import io.micrometer.core.instrument.MeterRegistry;
2729
import org.jetbrains.annotations.Nullable;
@@ -30,22 +32,15 @@
3032
import org.opengrok.indexer.logger.LoggerFactory;
3133
import org.opengrok.indexer.util.Statistics;
3234

33-
import java.beans.XMLDecoder;
34-
import java.io.BufferedInputStream;
3535
import java.io.File;
36-
import java.io.FileInputStream;
3736
import java.io.IOException;
38-
import java.io.InputStream;
3937
import java.util.logging.Level;
4038
import java.util.logging.Logger;
41-
import java.util.zip.GZIPInputStream;
4239

4340
public class FileAnnotationCache extends AbstractCache implements AnnotationCache {
4441

4542
private static final Logger LOGGER = LoggerFactory.getLogger(FileAnnotationCache.class);
4643

47-
private static final ClassLoader classLoader = new AnnotationDataClassLoader();
48-
4944
private Counter fileAnnotationCacheHits;
5045
private Counter fileAnnotationCacheMisses;
5146

@@ -65,18 +60,17 @@ public void initialize() {
6560
}
6661
}
6762

68-
private static XMLDecoder getDecoder(InputStream in) {
69-
return new XMLDecoder(in, null, null, classLoader);
70-
}
71-
7263
/**
7364
* Read annotation from a file.
7465
*/
7566
static Annotation readCache(File file) throws IOException {
76-
try (FileInputStream in = new FileInputStream(file);
77-
XMLDecoder d = getDecoder(new GZIPInputStream(new BufferedInputStream(in)))) {
78-
return new Annotation((AnnotationData) d.readObject());
79-
}
67+
ObjectMapper mapper = new CBORMapper();
68+
return new Annotation(mapper.readValue(file, AnnotationData.class));
69+
}
70+
71+
@Override
72+
public String getCacheFileSuffix() {
73+
return "";
8074
}
8175

8276
@VisibleForTesting
@@ -90,7 +84,10 @@ Annotation readAnnotation(File file) throws CacheException {
9084
}
9185

9286
try {
93-
return readCache(cacheFile);
87+
Statistics statistics = new Statistics();
88+
Annotation annotation = readCache(cacheFile);
89+
statistics.report(LOGGER, Level.FINEST, String.format("deserialized annotation from cache for '%s'", file));
90+
return annotation;
9491
} catch (IOException e) {
9592
throw new CacheException(String.format("failed to read annotation cache for '%s'", file), e);
9693
}
@@ -156,6 +153,11 @@ public Annotation get(File file, @Nullable String rev) throws CacheException {
156153
return annotation;
157154
}
158155

156+
private void writeCache(AnnotationData annotationData, File outfile) throws IOException {
157+
ObjectMapper mapper = new CBORMapper();
158+
mapper.writeValue(outfile, annotationData);
159+
}
160+
159161
public void store(File file, Annotation annotation) throws CacheException {
160162
if (annotation.getRevision() == null || annotation.getRevision().isEmpty()) {
161163
throw new CacheException(String.format("annotation for ''%s'' does not contain revision", file));
@@ -177,7 +179,7 @@ public void store(File file, Annotation annotation) throws CacheException {
177179

178180
Statistics statistics = new Statistics();
179181
try {
180-
CacheUtil.writeCache(annotation.annotationData, cacheFile);
182+
writeCache(annotation.annotationData, cacheFile);
181183
statistics.report(LOGGER, Level.FINEST, String.format("wrote annotation for '%s'", file),
182184
"cache.annotation.file.store.latency");
183185
} catch (IOException e) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ public Annotation annotate(File file, @Nullable String rev, boolean fallback) th
311311
return null;
312312
}
313313

314+
Statistics statistics = new Statistics();
314315
completeAnnotationWithHistory(file, annotation, repo);
316+
statistics.report(LOGGER, Level.FINEST, String.format("completed annotation with history for '%s'", file));
315317

316318
return annotation;
317319
}

0 commit comments

Comments
 (0)