-
Notifications
You must be signed in to change notification settings - Fork 409
/
IndexName.java
309 lines (285 loc) · 10.8 KB
/
IndexName.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.jackrabbit.oak.plugins.index.search.spi.query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.PathUtils;
import org.apache.jackrabbit.oak.plugins.index.IndexConstants;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An index name, which possibly contains two version numbers: the product
* version number, and the customer version number.
*
* The format of an index node name is:
* - The name of the index,
* - optionally a dash ('-') and the product version number,
* - optionally "-custom-" and the customer version number.
*
* If the node name doesn't contain version numbers / dashes, then version 0 is
* assumed (for both the product version number and customer version number).
*/
public class IndexName implements Comparable<IndexName> {
private final static Logger LOG = LoggerFactory.getLogger(IndexName.class);
// already logged index names
private static final HashSet<String> LOGGED_WARN = new HashSet<>();
// when LOGGED_WARN will be cleared
private static long nextLogWarnClear;
private final String nodeName;
private final String baseName;
private final boolean isVersioned;
private final int productVersion;
private final int customerVersion;
private final boolean isLegal;
/**
* Parse the node name. Both node names with version and without version are
* supported.
*
* @param nodeName the node name (starting from root; e.g. "/oak:index/lucene")
* @return the index name object
*/
public static IndexName parse(final String nodeName) {
String baseName = nodeName;
int index = baseName.lastIndexOf('-');
if (index < 0) {
return new IndexName(nodeName, true);
}
String last = baseName.substring(index + 1);
baseName = baseName.substring(0, index);
try {
int v1 = Integer.parseInt(last);
if (!baseName.endsWith("-custom")) {
return new IndexName(nodeName, baseName, v1, 0);
}
baseName = baseName.substring(0,
baseName.length() - "-custom".length());
index = baseName.lastIndexOf('-');
if (index < 0) {
return new IndexName(nodeName, baseName, 0, v1);
}
last = baseName.substring(index + 1);
baseName = baseName.substring(0, index);
int v2 = Integer.parseInt(last);
return new IndexName(nodeName, baseName, v2, v1);
} catch (NumberFormatException e) {
long now = System.currentTimeMillis();
if (nextLogWarnClear < now) {
LOGGED_WARN.clear();
// clear again each 5 minutes
nextLogWarnClear = now + 5 * 60 * 1000;
}
if (LOGGED_WARN.add(nodeName)) {
LOG.warn("Index name format error: " + nodeName);
}
return new IndexName(nodeName, false);
}
}
private IndexName(String nodeName, boolean isLegal) {
// not versioned
this.nodeName = nodeName;
this.baseName = nodeName;
this.isVersioned = false;
this.productVersion = 0;
this.customerVersion = 0;
this.isLegal = isLegal;
}
private IndexName(String nodeName, String baseName, int productVersion, int customerVersion) {
// versioned
this.nodeName = nodeName;
this.baseName = baseName;
this.isVersioned = true;
this.productVersion = productVersion;
this.customerVersion = customerVersion;
this.isLegal = true;
}
public String toString() {
return nodeName +
" base=" + baseName +
(isVersioned ? " versioned": "") +
" product=" + productVersion +
" custom=" + customerVersion +
(isLegal ? "" : " illegal");
}
@Override
public int compareTo(IndexName o) {
int comp = baseName.compareTo(o.baseName);
if (comp != 0) {
return comp;
}
comp = Integer.compare(productVersion, o.productVersion);
if (comp != 0) {
return comp;
}
return Integer.compare(customerVersion, o.customerVersion);
}
/**
* Get the latest index name object that matches this index base name, and
* is customized.
*
* @param all the list of all indexes
* @return the lastest customized index, or null if none
*/
public IndexName getLatestCustomized(List<IndexName> all) {
IndexName latest = null;
for (IndexName n : all) {
if (n.baseName.equals(baseName)) {
if (n.customerVersion > 0) {
if (latest == null || n.compareTo(latest) > 0) {
latest = n;
}
}
}
}
return latest;
}
/**
* Get the latest product index that matches this index base name.
*
* @param all the list of all indexes
* @return the latest product index, or null if none
*/
public IndexName getLatestProduct(List<IndexName> all) {
IndexName latest = null;
for (IndexName n : all) {
if (n.baseName.equals(baseName)) {
if (compareTo(n) > 0 && n.customerVersion == 0) {
if (latest == null || n.compareTo(latest) > 0) {
latest = n;
}
}
}
}
return latest;
}
/**
* Filter out index that are replaced by another index with the same base
* name but newer version.
*
* Indexes without a version number in the name are always used, except if
* there is an active index with the same base name but a newer version.
*
* Active indexes have a hidden ":oak:mount-" node, which means they are
* indexed in the read-only node store.
*
* @param indexPaths the set of index paths
* @param rootState the root node state (used to find hidden nodes)
* @return the filtered list
*/
public static Collection<String> filterReplacedIndexes(Collection<String> indexPaths, NodeState rootState, boolean checkIsActive) {
HashMap<String, IndexName> latestVersions = new HashMap<String, IndexName>();
for (String p : indexPaths) {
IndexName indexName = IndexName.parse(p);
if (indexName.isVersioned && checkIsActive) {
// which might not be a good idea - instead, it should check if the composite node store is used
// (but how?)
if (!isIndexActive(p, rootState)) {
// the index is inactive, so not used
continue;
}
}
IndexName stored = latestVersions.get(indexName.baseName);
if (stored == null || stored.compareTo(indexName) < 0) {
// no old version, or old version is smaller: replace
latestVersions.put(indexName.baseName, indexName);
}
}
ArrayList<String> result = new ArrayList<>(latestVersions.size());
for (IndexName n : latestVersions.values()) {
result.add(n.nodeName);
}
return result;
}
public String nextCustomizedName() {
return baseName + "-" + productVersion + "-custom-" + (customerVersion + 1);
}
private static boolean isIndexActive(String indexPath, NodeState rootState) {
// An index is active if it has a hidden child node that starts with ":oak:mount-",
// OR if it is an active merged index
try {
NodeState indexNode = rootState;
for (String e : PathUtils.elements(indexPath)) {
indexNode = indexNode.getChildNode(e);
}
for (String c : indexNode.getChildNodeNames()) {
if (c.startsWith(":oak:mount-")) {
return true;
}
}
return isIndexActiveMerged(indexNode, rootState);
} catch (StackOverflowError e) {
LOG.error("Fail to check index activeness for {} due to StackOverflowError", indexPath, e);
return true;
}
}
private static boolean isIndexActiveMerged(NodeState indexNode, NodeState rootState) {
// An index is an active merged index if it has the property "merges",
// and that property points to index definitions,
// and each of those indexes is either active, disabled, or removed.
PropertyState ps = indexNode.getProperty("merges");
if (ps == null) {
return false;
}
if (ps.getType() != Type.STRING && ps.getType() != Type.STRINGS) {
return false;
}
for (int i = 0; i < ps.count(); i++) {
String merges = ps.getValue(Type.STRING, i);
NodeState mergeNode = rootState;
for (String e : PathUtils.elements(merges)) {
mergeNode = mergeNode.getChildNode(e);
}
if (!mergeNode.exists()) {
continue;
}
String indexType = mergeNode.getString(IndexConstants.TYPE_PROPERTY_NAME);
if (IndexConstants.TYPE_DISABLED.equals(indexType)) {
continue;
}
if (isIndexActive(merges, rootState)) {
continue;
}
return false;
}
return true;
}
public String getNodeName() {
return nodeName;
}
public int getCustomerVersion() {
return customerVersion;
}
public int getProductVersion() {
return productVersion;
}
public String getBaseName() {
return baseName;
}
public boolean isVersioned() {
return isVersioned;
}
public boolean isLegal() {
return isLegal;
}
}