Skip to content

Commit 182f294

Browse files
authored
Merge pull request #3130 from idodeclare/feature/settings_helper
Extract SettingsHelper from SearchHelper
2 parents 2a2d1e3 + 135114a commit 182f294

File tree

2 files changed

+156
-77
lines changed

2 files changed

+156
-77
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
22+
* Portions copyright (c) 2011 Jens Elkner.
23+
* Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
24+
*/
25+
package org.opengrok.indexer.search;
26+
27+
import org.apache.lucene.index.IndexReader;
28+
import org.opengrok.indexer.configuration.Project;
29+
import org.opengrok.indexer.index.IndexAnalysisSettings3;
30+
import org.opengrok.indexer.index.IndexAnalysisSettingsAccessor;
31+
import org.opengrok.indexer.index.IndexedSymlink;
32+
33+
import java.io.IOException;
34+
import java.util.Collections;
35+
import java.util.Comparator;
36+
import java.util.HashMap;
37+
import java.util.Map;
38+
import java.util.TreeMap;
39+
40+
/**
41+
* Represents a helper class for accessing settings.
42+
* @author Jens Elkner
43+
*/
44+
public class SettingsHelper {
45+
46+
private final IndexReader reader;
47+
48+
/**
49+
* Key is Project name or empty string for null Project.
50+
*/
51+
private Map<String, IndexAnalysisSettings3> mappedAnalysisSettings;
52+
53+
/**
54+
* Key is Project name or empty string for null Project. Map is ordered by
55+
* canonical length (ASC) and then canonical value (ASC).
56+
*/
57+
private Map<String, Map<String, IndexedSymlink>> mappedIndexedSymlinks;
58+
59+
public SettingsHelper(IndexReader reader) {
60+
if (reader == null) {
61+
throw new IllegalArgumentException("reader is null");
62+
}
63+
this.reader = reader;
64+
}
65+
66+
/**
67+
* Gets any mapped symlinks (after having called {@link #getSettings(String)}).
68+
* @return either a defined map or {@code null}
69+
*/
70+
public Map<String, IndexedSymlink> getSymlinks(String projectName) throws IOException {
71+
getSettings(projectName);
72+
String projectKey = projectName != null ? projectName : "";
73+
Map<String, IndexedSymlink> indexSymlinks = mappedIndexedSymlinks.get(projectKey);
74+
if (indexSymlinks != null) {
75+
return Collections.unmodifiableMap(indexSymlinks);
76+
}
77+
return null;
78+
}
79+
80+
/**
81+
* Gets the persisted tabSize via {@link #getSettings(String)} if
82+
* available or returns the {@code proj} tabSize if available -- or zero.
83+
* @param proj a defined instance or {@code null} if no project is active
84+
* @return tabSize
85+
* @throws IOException if an I/O error occurs querying the initialized
86+
* reader
87+
*/
88+
public int getTabSize(Project proj) throws IOException {
89+
String projectName = proj != null ? proj.getName() : null;
90+
IndexAnalysisSettings3 settings = getSettings(projectName);
91+
int tabSize;
92+
if (settings != null && settings.getTabSize() != null) {
93+
tabSize = settings.getTabSize();
94+
} else {
95+
tabSize = proj != null ? proj.getTabSize() : 0;
96+
}
97+
return tabSize;
98+
}
99+
100+
/**
101+
* Gets the settings for a specified project.
102+
* @param projectName a defined instance or {@code null} if no project is
103+
* active (or empty string to mean the same thing)
104+
* @return a defined instance or {@code null} if none is found
105+
* @throws IOException if an I/O error occurs querying the initialized reader
106+
*/
107+
public IndexAnalysisSettings3 getSettings(String projectName) throws IOException {
108+
if (mappedAnalysisSettings == null) {
109+
IndexAnalysisSettingsAccessor dao = new IndexAnalysisSettingsAccessor();
110+
IndexAnalysisSettings3[] setts = dao.read(reader, Short.MAX_VALUE);
111+
map(setts);
112+
}
113+
114+
String projectKey = projectName != null ? projectName : "";
115+
return mappedAnalysisSettings.get(projectKey);
116+
}
117+
118+
private void map(IndexAnalysisSettings3[] setts) {
119+
120+
Map<String, IndexAnalysisSettings3> settingsMap = new HashMap<>();
121+
Map<String, Map<String, IndexedSymlink>> symlinksMap = new HashMap<>();
122+
123+
for (IndexAnalysisSettings3 settings : setts) {
124+
String projectName = settings.getProjectName();
125+
String projectKey = projectName != null ? projectName : "";
126+
settingsMap.put(projectKey, settings);
127+
symlinksMap.put(projectKey, mapSymlinks(settings));
128+
}
129+
mappedAnalysisSettings = settingsMap;
130+
mappedIndexedSymlinks = symlinksMap;
131+
}
132+
133+
private Map<String, IndexedSymlink> mapSymlinks(IndexAnalysisSettings3 settings) {
134+
135+
Map<String, IndexedSymlink> res = new TreeMap<>(
136+
Comparator.comparingInt(String::length).thenComparing(o -> o));
137+
for (IndexedSymlink entry : settings.getIndexedSymlinks().values()) {
138+
res.put(entry.getCanonical(), entry);
139+
}
140+
return res;
141+
}
142+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/web/SearchHelper.java

+14-77
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@
3030
import java.nio.file.Path;
3131
import java.nio.file.Paths;
3232
import java.util.ArrayList;
33-
import java.util.Comparator;
34-
import java.util.HashMap;
3533
import java.util.List;
3634
import java.util.Map;
3735
import java.util.Set;
3836
import java.util.SortedSet;
39-
import java.util.TreeMap;
4037
import java.util.TreeSet;
4138
import java.util.logging.Level;
4239
import java.util.logging.Logger;
@@ -66,12 +63,11 @@
6663
import org.opengrok.indexer.configuration.Project;
6764
import org.opengrok.indexer.configuration.RuntimeEnvironment;
6865
import org.opengrok.indexer.configuration.SuperIndexSearcher;
69-
import org.opengrok.indexer.index.IndexAnalysisSettings3;
70-
import org.opengrok.indexer.index.IndexAnalysisSettingsAccessor;
7166
import org.opengrok.indexer.index.IndexDatabase;
7267
import org.opengrok.indexer.index.IndexedSymlink;
7368
import org.opengrok.indexer.logger.LoggerFactory;
7469
import org.opengrok.indexer.search.QueryBuilder;
70+
import org.opengrok.indexer.search.SettingsHelper;
7571
import org.opengrok.indexer.search.Summarizer;
7672
import org.opengrok.indexer.search.context.Context;
7773
import org.opengrok.indexer.search.context.HistoryContext;
@@ -83,7 +79,6 @@
8379
* complexity from UI design.
8480
*
8581
* @author Jens Elkner
86-
* @version $Revision$
8782
*/
8883
public class SearchHelper {
8984

@@ -219,16 +214,7 @@ public class SearchHelper {
219214
*/
220215
public static final String PARSE_ERROR_MSG = "Unable to parse your query: ";
221216

222-
/**
223-
* Key is Project name or empty string for null Project.
224-
*/
225-
private Map<String, IndexAnalysisSettings3> mappedAnalysisSettings;
226-
227-
/**
228-
* Key is Project name or empty string for null Project. Map is ordered by
229-
* canonical length (ASC) and then canonical value (ASC).
230-
*/
231-
private Map<String, Map<String, IndexedSymlink>> mappedIndexedSymlinks;
217+
private SettingsHelper settingsHelper;
232218

233219
/**
234220
* User readable description for file types. Only those listed in
@@ -271,7 +257,7 @@ public SearchHelper prepareExec(SortedSet<String> projects) {
271257
return this;
272258
}
273259

274-
mappedAnalysisSettings = null;
260+
settingsHelper = null;
275261
// the Query created by the QueryBuilder
276262
try {
277263
indexDir = new File(dataRoot, IndexDatabase.INDEX_DIR);
@@ -624,22 +610,15 @@ public int searchSingle(File file) throws IOException,
624610
}
625611

626612
/**
627-
* Gets the persisted tabSize via {@link #getSettings(java.lang.String)} if
628-
* available or returns the {@code proj} tabSize if available -- or zero.
613+
* Gets the persisted tabSize via {@link SettingsHelper} for the active
614+
* reader.
629615
* @param proj a defined instance or {@code null} if no project is active
630616
* @return tabSize
631617
* @throws IOException if an I/O error occurs querying the active reader
632618
*/
633619
public int getTabSize(Project proj) throws IOException {
634-
String projectName = proj != null ? proj.getName() : null;
635-
IndexAnalysisSettings3 settings = getSettings(projectName);
636-
int tabSize;
637-
if (settings != null && settings.getTabSize() != null) {
638-
tabSize = settings.getTabSize();
639-
} else {
640-
tabSize = proj != null ? proj.getTabSize() : 0;
641-
}
642-
return tabSize;
620+
ensureSettingsHelper();
621+
return settingsHelper.getTabSize(proj);
643622
}
644623

645624
/**
@@ -661,12 +640,10 @@ public String getPrimeRelativePath(String project, String relativePath)
661640
}
662641
File absolute = new File(sourceRoot + relativePath);
663642

664-
getSettings(project);
665-
666-
Map<String, IndexedSymlink> indexedSymlinks;
667-
if (mappedIndexedSymlinks != null && (indexedSymlinks =
668-
mappedIndexedSymlinks.get(project)) != null) {
669-
643+
ensureSettingsHelper();
644+
settingsHelper.getSettings(project);
645+
Map<String, IndexedSymlink> indexedSymlinks = settingsHelper.getSymlinks(project);
646+
if (indexedSymlinks != null) {
670647
String canonical = absolute.getCanonicalFile().getPath();
671648
for (IndexedSymlink entry : indexedSymlinks.values()) {
672649
if (canonical.equals(entry.getCanonical())) {
@@ -686,49 +663,9 @@ public String getPrimeRelativePath(String project, String relativePath)
686663
return relativePath;
687664
}
688665

689-
/**
690-
* Gets the settings for a specified project, querying the active reader
691-
* upon the first call after {@link #prepareExec(java.util.SortedSet)}.
692-
* @param projectName a defined instance or {@code null} if no project is
693-
* active (or empty string to mean the same thing)
694-
* @return a defined instance or {@code null} if none is found
695-
* @throws IOException if an I/O error occurs querying the active reader
696-
*/
697-
private IndexAnalysisSettings3 getSettings(String projectName)
698-
throws IOException {
699-
if (mappedAnalysisSettings == null) {
700-
IndexAnalysisSettingsAccessor dao =
701-
new IndexAnalysisSettingsAccessor();
702-
IndexAnalysisSettings3[] setts = dao.read(reader, Short.MAX_VALUE);
703-
map(setts);
666+
private void ensureSettingsHelper() {
667+
if (settingsHelper == null) {
668+
settingsHelper = new SettingsHelper(reader);
704669
}
705-
706-
String k = projectName != null ? projectName : "";
707-
return mappedAnalysisSettings.get(k);
708-
}
709-
710-
private void map(IndexAnalysisSettings3[] setts) {
711-
712-
Map<String, IndexAnalysisSettings3> settingsMap = new HashMap<>();
713-
Map<String, Map<String, IndexedSymlink>> symlinksMap = new HashMap<>();
714-
715-
for (IndexAnalysisSettings3 settings : setts) {
716-
String k = settings.getProjectName() != null ?
717-
settings.getProjectName() : "";
718-
settingsMap.put(k, settings);
719-
symlinksMap.put(k, mapSymlinks(settings));
720-
}
721-
mappedAnalysisSettings = settingsMap;
722-
mappedIndexedSymlinks = symlinksMap;
723-
}
724-
725-
private Map<String, IndexedSymlink> mapSymlinks(IndexAnalysisSettings3 settings) {
726-
727-
Map<String, IndexedSymlink> res = new TreeMap<>(
728-
Comparator.comparingInt(String::length).thenComparing(o -> o));
729-
for (IndexedSymlink entry : settings.getIndexedSymlinks().values()) {
730-
res.put(entry.getCanonical(), entry);
731-
}
732-
return res;
733670
}
734671
}

0 commit comments

Comments
 (0)