forked from oracle/opengrok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsHelper.java
142 lines (125 loc) · 5.19 KB
/
SettingsHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* 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) throws IOException {
getSettings(projectName);
String projectKey = projectName != null ? projectName : "";
Map<String, IndexedSymlink> indexSymlinks = mappedIndexedSymlinks.get(projectKey);
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 projectKey = projectName != null ? projectName : "";
return mappedAnalysisSettings.get(projectKey);
}
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 projectKey = projectName != null ? projectName : "";
settingsMap.put(projectKey, settings);
symlinksMap.put(projectKey, 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;
}
}