Skip to content

Commit d4765d8

Browse files
idodeclaretarzanek
authored andcommitted
Issue #2156 : speed up JarAnalyzer
1 parent 1ae5498 commit d4765d8

File tree

3 files changed

+95
-75
lines changed

3 files changed

+95
-75
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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) 2018, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opensolaris.opengrok.analysis.executables;
25+
26+
import java.io.StringWriter;
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
/**
31+
* Represents a package-private container for {@link StringWriter} instances
32+
* used when aggregating content for a jar's class files for the jar's indexed
33+
* fields.
34+
*/
35+
class JFieldBuilder {
36+
private final Map<String, StringWriter> fieldBuilders = new HashMap<>();
37+
38+
boolean hasField(String fieldName) {
39+
return fieldBuilders.containsKey(fieldName);
40+
}
41+
42+
StringWriter write(String fieldName) {
43+
StringWriter res = fieldBuilders.getOrDefault(fieldName, null);
44+
if (res != null) {
45+
return res;
46+
}
47+
res = new StringWriter();
48+
fieldBuilders.put(fieldName, res);
49+
return res;
50+
}
51+
}

src/org/opensolaris/opengrok/analysis/executables/JarAnalyzer.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.BufferedInputStream;
2727
import java.io.IOException;
28+
import java.io.StringWriter;
2829
import java.io.Writer;
2930
import java.util.zip.ZipEntry;
3031
import java.util.zip.ZipInputStream;
@@ -45,6 +46,9 @@
4546
*/
4647
public class JarAnalyzer extends FileAnalyzer {
4748

49+
private static final String[] FIELD_NAMES = new String[]
50+
{QueryBuilder.FULL, QueryBuilder.REFS, QueryBuilder.DEFS};
51+
4852
protected JarAnalyzer(FileAnalyzerFactory factory) {
4953
super(factory);
5054
}
@@ -53,16 +57,16 @@ protected JarAnalyzer(FileAnalyzerFactory factory) {
5357
* Gets a version number to be used to tag processed documents so that
5458
* re-analysis can be re-done later if a stored version number is different
5559
* from the current implementation.
56-
* @return 20180112_00
60+
* @return 20180612_00
5761
*/
5862
@Override
5963
protected int getSpecializedVersionNo() {
60-
return 20180112_00; // Edit comment above too!
64+
return 20180612_00; // Edit comment above too!
6165
}
6266

6367
@Override
6468
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
65-
StringBuilder fout = new StringBuilder();
69+
JFieldBuilder jfbuilder = new JFieldBuilder();
6670
try (ZipInputStream zis = new ZipInputStream(src.getStream())) {
6771
ZipEntry entry;
6872
while ((entry = zis.getNextEntry()) != null) {
@@ -74,23 +78,9 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
7478
xrefOut.append("</b>");
7579
}
7680

77-
/**
78-
* If a FULL field exists already, then append to it.
79-
*/
80-
useExtantValue(fout, doc, QueryBuilder.FULL);
81-
fout.append("// ");
82-
fout.append(ename);
83-
fout.append("\n");
84-
85-
/**
86-
* Unlike other analyzers, which rely on the full content
87-
* existing to be accessed at a file system location identified
88-
* by PATH, *.jar and *.class files have virtual content which
89-
* is stored here (Store.YES) for analyzer convenience.
90-
*/
91-
String fstr = fout.toString();
92-
doc.add(new OGKTextField(QueryBuilder.FULL, fstr, Store.YES));
93-
fout.setLength(0);
81+
StringWriter fout = jfbuilder.write(QueryBuilder.FULL);
82+
fout.write(ename);
83+
fout.write("\n");
9484

9585
FileAnalyzerFactory fac = AnalyzerGuru.find(ename);
9686
if (fac instanceof JavaClassAnalyzerFactory) {
@@ -99,18 +89,17 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
9989
}
10090
JavaClassAnalyzer jca =
10191
(JavaClassAnalyzer) fac.getAnalyzer();
102-
jca.analyze(doc, new BufferedInputStream(zis), xrefOut);
92+
jca.analyze(doc, new BufferedInputStream(zis), xrefOut,
93+
jfbuilder);
10394
}
10495
}
10596
}
106-
}
10797

108-
private static void useExtantValue(StringBuilder accum, Document doc,
109-
String field) {
110-
String extantValue = doc.get(field);
111-
if (extantValue != null) {
112-
doc.removeFields(field);
113-
accum.append(extantValue);
98+
for (String name : FIELD_NAMES) {
99+
if (jfbuilder.hasField(name)) {
100+
String fstr = jfbuilder.write(name).toString();
101+
doc.add(new OGKTextField(name, fstr, Store.NO));
102+
}
114103
}
115104
}
116105
}

src/org/opensolaris/opengrok/analysis/executables/JavaClassAnalyzer.java

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
import org.apache.bcel.classfile.ConstantDouble;
4242
import org.apache.bcel.classfile.ConstantFloat;
4343
import org.apache.bcel.classfile.ConstantInteger;
44-
import org.apache.bcel.classfile.ConstantInvokeDynamic;
4544
import org.apache.bcel.classfile.ConstantLong;
46-
import org.apache.bcel.classfile.ConstantMethodHandle;
4745
import org.apache.bcel.classfile.ConstantMethodType;
4846
import org.apache.bcel.classfile.ConstantModule;
4947
import org.apache.bcel.classfile.ConstantNameAndType;
@@ -60,9 +58,7 @@
6058
import org.apache.lucene.document.Field.Store;
6159
import org.opensolaris.opengrok.analysis.FileAnalyzer;
6260
import org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
63-
import org.opensolaris.opengrok.analysis.IteratorReader;
6461
import org.opensolaris.opengrok.analysis.OGKTextField;
65-
import org.opensolaris.opengrok.analysis.OGKTextVecField;
6662
import org.opensolaris.opengrok.analysis.StreamSource;
6763
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
6864
import org.opensolaris.opengrok.logger.LoggerFactory;
@@ -94,36 +90,36 @@ protected JavaClassAnalyzer(FileAnalyzerFactory factory) {
9490
* Gets a version number to be used to tag processed documents so that
9591
* re-analysis can be re-done later if a stored version number is different
9692
* from the current implementation.
97-
* @return 20180112_00
93+
* @return 20180612_00
9894
*/
9995
@Override
10096
protected int getSpecializedVersionNo() {
101-
return 20180112_00; // Edit comment above too!
97+
return 20180612_00; // Edit comment above too!
10298
}
10399

104100
@Override
105101
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
106102
try (InputStream in = src.getStream()) {
107-
analyze(doc, in, xrefOut);
103+
analyze(doc, in, xrefOut, null);
108104
}
109105
}
110106

111-
void analyze(Document doc, InputStream in, Writer xrefOut) throws IOException {
107+
void analyze(Document doc, InputStream in, Writer xrefOut,
108+
JFieldBuilder jfbuilder) throws IOException {
112109
List<String> defs = new ArrayList<>();
113110
List<String> refs = new ArrayList<>();
114111
List<String> full = new ArrayList<>();
115112

116-
StringWriter dout = new StringWriter();
117-
StringWriter rout = new StringWriter();
118-
StringWriter fout = new StringWriter();
119-
120-
/**
121-
* The JarAnalyzer uses JavaClassAnalyzer, so if a DEFS, REFS, or FULL
122-
* field exists already, then append to it.
123-
*/
124-
useExtantValue(dout, doc, QueryBuilder.DEFS);
125-
useExtantValue(rout, doc, QueryBuilder.REFS);
126-
useExtantValue(fout, doc, QueryBuilder.FULL);
113+
StringWriter dout, rout, fout;
114+
if (jfbuilder == null) {
115+
dout = new StringWriter();
116+
rout = new StringWriter();
117+
fout = new StringWriter();
118+
} else {
119+
dout = jfbuilder.write(QueryBuilder.DEFS);
120+
rout = jfbuilder.write(QueryBuilder.REFS);
121+
fout = jfbuilder.write(QueryBuilder.FULL);
122+
}
127123

128124
ClassParser classparser = new ClassParser(in,
129125
doc.get(QueryBuilder.PATH));
@@ -140,25 +136,20 @@ void analyze(Document doc, InputStream in, Writer xrefOut) throws IOException {
140136
}
141137
}
142138

143-
appendValues(dout, defs, "");
144-
appendValues(rout, refs, "");
145-
appendValues(fout, full, "// ");
139+
appendValues(dout, defs);
140+
appendValues(rout, refs);
141+
appendValues(fout, full);
146142

147-
/**
148-
* Unlike other analyzers, which rely on the full content existing to be
149-
* accessed at a file system location identified by PATH, *.class and
150-
* *.jar files have virtual content which is stored here (Store.YES) for
151-
* analyzer convenience.
152-
*/
143+
if (jfbuilder == null) {
144+
String dstr = dout.toString();
145+
doc.add(new OGKTextField(QueryBuilder.DEFS, dstr, Store.NO));
153146

154-
String dstr = dout.toString();
155-
doc.add(new OGKTextField(QueryBuilder.DEFS, dstr, Store.YES));
147+
String rstr = rout.toString();
148+
doc.add(new OGKTextField(QueryBuilder.REFS, rstr, Store.NO));
156149

157-
String rstr = rout.toString();
158-
doc.add(new OGKTextField(QueryBuilder.REFS, rstr, Store.YES));
159-
160-
String fstr = fout.toString();
161-
doc.add(new OGKTextField(QueryBuilder.FULL, fstr, Store.YES));
150+
String fstr = fout.toString();
151+
doc.add(new OGKTextField(QueryBuilder.FULL, fstr, Store.NO));
152+
}
162153
}
163154

164155

@@ -565,19 +556,8 @@ public String constantToString(Constant c, ConstantPool cp, int[] v)
565556
return str;
566557
}
567558

568-
private static void useExtantValue(StringWriter accum, Document doc,
569-
String field) {
570-
String extantValue = doc.get(field);
571-
if (extantValue != null) {
572-
doc.removeFields(field);
573-
accum.append(extantValue);
574-
}
575-
}
576-
577-
private static void appendValues(StringWriter accum, List<String> full,
578-
String lede) {
559+
private static void appendValues(StringWriter accum, List<String> full) {
579560
for (String fl : full) {
580-
accum.write(lede);
581561
accum.write(fl);
582562
accum.write(EOL);
583563
}

0 commit comments

Comments
 (0)