22
22
*/
23
23
package org .opengrok .indexer .history ;
24
24
25
+ import com .fasterxml .jackson .databind .ObjectMapper ;
26
+ import com .fasterxml .jackson .dataformat .cbor .databind .CBORMapper ;
25
27
import io .micrometer .core .instrument .Counter ;
26
28
import io .micrometer .core .instrument .MeterRegistry ;
27
29
import org .jetbrains .annotations .Nullable ;
30
32
import org .opengrok .indexer .logger .LoggerFactory ;
31
33
import org .opengrok .indexer .util .Statistics ;
32
34
33
- import java .beans .XMLDecoder ;
34
- import java .io .BufferedInputStream ;
35
35
import java .io .File ;
36
- import java .io .FileInputStream ;
37
36
import java .io .IOException ;
38
- import java .io .InputStream ;
39
37
import java .util .logging .Level ;
40
38
import java .util .logging .Logger ;
41
- import java .util .zip .GZIPInputStream ;
42
39
43
40
public class FileAnnotationCache extends AbstractCache implements AnnotationCache {
44
41
45
42
private static final Logger LOGGER = LoggerFactory .getLogger (FileAnnotationCache .class );
46
43
47
- private static final ClassLoader classLoader = new AnnotationDataClassLoader ();
48
-
49
44
private Counter fileAnnotationCacheHits ;
50
45
private Counter fileAnnotationCacheMisses ;
51
46
@@ -65,18 +60,17 @@ public void initialize() {
65
60
}
66
61
}
67
62
68
- private static XMLDecoder getDecoder (InputStream in ) {
69
- return new XMLDecoder (in , null , null , classLoader );
70
- }
71
-
72
63
/**
73
64
* Read annotation from a file.
74
65
*/
75
66
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 "" ;
80
74
}
81
75
82
76
@ VisibleForTesting
@@ -90,7 +84,10 @@ Annotation readAnnotation(File file) throws CacheException {
90
84
}
91
85
92
86
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 ;
94
91
} catch (IOException e ) {
95
92
throw new CacheException (String .format ("failed to read annotation cache for '%s'" , file ), e );
96
93
}
@@ -156,6 +153,11 @@ public Annotation get(File file, @Nullable String rev) throws CacheException {
156
153
return annotation ;
157
154
}
158
155
156
+ private void writeCache (AnnotationData annotationData , File outfile ) throws IOException {
157
+ ObjectMapper mapper = new CBORMapper ();
158
+ mapper .writeValue (outfile , annotationData );
159
+ }
160
+
159
161
public void store (File file , Annotation annotation ) throws CacheException {
160
162
if (annotation .getRevision () == null || annotation .getRevision ().isEmpty ()) {
161
163
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 {
177
179
178
180
Statistics statistics = new Statistics ();
179
181
try {
180
- CacheUtil . writeCache (annotation .annotationData , cacheFile );
182
+ writeCache (annotation .annotationData , cacheFile );
181
183
statistics .report (LOGGER , Level .FINEST , String .format ("wrote annotation for '%s'" , file ),
182
184
"cache.annotation.file.store.latency" );
183
185
} catch (IOException e ) {
0 commit comments