-
Notifications
You must be signed in to change notification settings - Fork 778
Extract SettingsHelper from SearchHelper #3130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
opengrok-indexer/src/main/java/org/opengrok/indexer/search/SettingsHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* CDDL HEADER START | ||
* | ||
* The contents of this file are subject to the terms of the | ||
* Common Development and Distribution License (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* | ||
* See LICENSE.txt included in this distribution for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing Covered Code, include this CDDL HEADER in each | ||
* file and include the License file at LICENSE.txt. | ||
* If applicable, add the following below this CDDL HEADER, with the | ||
* fields enclosed by brackets "[]" replaced with your own identifying | ||
* information: Portions Copyright [yyyy] [name of copyright owner] | ||
* | ||
* CDDL HEADER END | ||
*/ | ||
|
||
/* | ||
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. | ||
* Portions copyright (c) 2011 Jens Elkner. | ||
* Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>. | ||
*/ | ||
package org.opengrok.indexer.search; | ||
|
||
import org.apache.lucene.index.IndexReader; | ||
import org.opengrok.indexer.configuration.Project; | ||
import org.opengrok.indexer.index.IndexAnalysisSettings3; | ||
import org.opengrok.indexer.index.IndexAnalysisSettingsAccessor; | ||
import org.opengrok.indexer.index.IndexedSymlink; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* Represents a helper class for accessing settings. | ||
* @author Jens Elkner | ||
*/ | ||
public class SettingsHelper { | ||
|
||
private final IndexReader reader; | ||
|
||
/** | ||
* Key is Project name or empty string for null Project. | ||
*/ | ||
private Map<String, IndexAnalysisSettings3> mappedAnalysisSettings; | ||
|
||
/** | ||
* Key is Project name or empty string for null Project. Map is ordered by | ||
* canonical length (ASC) and then canonical value (ASC). | ||
*/ | ||
private Map<String, Map<String, IndexedSymlink>> mappedIndexedSymlinks; | ||
|
||
public SettingsHelper(IndexReader reader) { | ||
if (reader == null) { | ||
throw new IllegalArgumentException("reader is null"); | ||
} | ||
this.reader = reader; | ||
} | ||
|
||
/** | ||
* Gets any mapped symlinks (after having called {@link #getSettings(String)}). | ||
* @return either a defined map or {@code null} | ||
*/ | ||
public Map<String, IndexedSymlink> getSymlinks(String projectName) { | ||
if (mappedIndexedSymlinks == null) { | ||
throw new IllegalStateException("getSettings() not yet called"); | ||
} | ||
|
||
String k = projectName != null ? projectName : ""; | ||
Map<String, IndexedSymlink> indexSymlinks = mappedIndexedSymlinks.get(k); | ||
if (indexSymlinks != null) { | ||
return Collections.unmodifiableMap(indexSymlinks); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Gets the persisted tabSize via {@link #getSettings(String)} if | ||
* available or returns the {@code proj} tabSize if available -- or zero. | ||
* @param proj a defined instance or {@code null} if no project is active | ||
* @return tabSize | ||
* @throws IOException if an I/O error occurs querying the initialized | ||
* reader | ||
*/ | ||
public int getTabSize(Project proj) throws IOException { | ||
String projectName = proj != null ? proj.getName() : null; | ||
IndexAnalysisSettings3 settings = getSettings(projectName); | ||
int tabSize; | ||
if (settings != null && settings.getTabSize() != null) { | ||
tabSize = settings.getTabSize(); | ||
} else { | ||
tabSize = proj != null ? proj.getTabSize() : 0; | ||
} | ||
return tabSize; | ||
} | ||
|
||
/** | ||
* Gets the settings for a specified project. | ||
* @param projectName a defined instance or {@code null} if no project is | ||
* active (or empty string to mean the same thing) | ||
* @return a defined instance or {@code null} if none is found | ||
* @throws IOException if an I/O error occurs querying the initialized reader | ||
*/ | ||
public IndexAnalysisSettings3 getSettings(String projectName) throws IOException { | ||
if (mappedAnalysisSettings == null) { | ||
IndexAnalysisSettingsAccessor dao = new IndexAnalysisSettingsAccessor(); | ||
IndexAnalysisSettings3[] setts = dao.read(reader, Short.MAX_VALUE); | ||
map(setts); | ||
} | ||
|
||
String k = projectName != null ? projectName : ""; | ||
return mappedAnalysisSettings.get(k); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. k is not very descriptive There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revised |
||
} | ||
|
||
private void map(IndexAnalysisSettings3[] setts) { | ||
|
||
Map<String, IndexAnalysisSettings3> settingsMap = new HashMap<>(); | ||
Map<String, Map<String, IndexedSymlink>> symlinksMap = new HashMap<>(); | ||
|
||
for (IndexAnalysisSettings3 settings : setts) { | ||
String projectName = settings.getProjectName(); | ||
String k = projectName != null ? projectName : ""; | ||
settingsMap.put(k, settings); | ||
symlinksMap.put(k, mapSymlinks(settings)); | ||
} | ||
mappedAnalysisSettings = settingsMap; | ||
mappedIndexedSymlinks = symlinksMap; | ||
} | ||
|
||
private Map<String, IndexedSymlink> mapSymlinks(IndexAnalysisSettings3 settings) { | ||
|
||
Map<String, IndexedSymlink> res = new TreeMap<>( | ||
Comparator.comparingInt(String::length).thenComparing(o -> o)); | ||
for (IndexedSymlink entry : settings.getIndexedSymlinks().values()) { | ||
res.put(entry.getCanonical(), entry); | ||
} | ||
return res; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it problem to call it here
getSettings()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No doesn't seem so