IndexPropertyUtil.java
11.9 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.google.common.base.Preconditions
* com.google.common.base.Strings
* javax.annotation.Nonnull
* javax.annotation.Nullable
* org.apache.jackrabbit.oak.api.Tree
* org.apache.jackrabbit.oak.api.Type
* org.apache.jackrabbit.oak.plugins.index.IndexUtils
* org.apache.jackrabbit.oak.plugins.tree.TreeFactory
* org.apache.jackrabbit.oak.spi.state.EqualsDiff
* org.apache.jackrabbit.oak.spi.state.NodeBuilder
* org.apache.jackrabbit.oak.spi.state.NodeState
*/
package com.adobe.granite.repository.impl;
import com.adobe.granite.repository.impl.lucene.util.LuceneAggregate;
import com.adobe.granite.repository.impl.lucene.util.LuceneAggregateDefinition;
import com.adobe.granite.repository.impl.lucene.util.LuceneAggregateInclude;
import com.adobe.granite.repository.impl.lucene.util.LuceneIndexProperty;
import com.adobe.granite.repository.impl.lucene.util.LuceneIndexRule;
import com.adobe.granite.repository.impl.lucene.util.LucenePropertyIndexDefinition;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.jackrabbit.oak.api.Tree;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.plugins.index.IndexUtils;
import org.apache.jackrabbit.oak.plugins.tree.TreeFactory;
import org.apache.jackrabbit.oak.spi.state.EqualsDiff;
import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
import org.apache.jackrabbit.oak.spi.state.NodeState;
final class IndexPropertyUtil {
static final String DISABLED_INDEX_TYPE = "disabled";
static final String OAK_INDEX_PATH = "/oak:index/";
private IndexPropertyUtil() {
}
private static boolean isDisabled(@Nonnull NodeState indexDef) {
Preconditions.checkNotNull((Object)indexDef);
return "disabled".equals(indexDef.getString("type"));
}
private static boolean shouldReindex(@Nonnull NodeState before, @Nonnull NodeState after) {
Preconditions.checkNotNull((Object)before);
Preconditions.checkNotNull((Object)after);
return !EqualsDiff.equals((NodeState)after, (NodeState)before);
}
private static void checkAndIssueReindex(@Nonnull NodeState before, @Nonnull NodeBuilder indexDef) {
Preconditions.checkNotNull((Object)before);
Preconditions.checkNotNull((Object)indexDef);
if (IndexPropertyUtil.shouldReindex(before, indexDef.getNodeState())) {
indexDef.setProperty("reindex", (Object)true);
}
}
protected static void luceneProperty(@Nonnull NodeBuilder index, @Nonnull String indexName, @Nullable LuceneAggregateDefinition aggregates, @Nonnull LucenePropertyIndexDefinition definition) {
IndexPropertyUtil.luceneProperty(index, indexName, false, null, null, aggregates, definition);
}
protected static void luceneProperty(@Nonnull NodeBuilder index, @Nonnull String indexName, boolean evaluatePathRestrictions, @Nullable List<String> includedPaths, @Nullable List<String> excludedPaths, @Nullable LuceneAggregateDefinition aggregates, @Nonnull LucenePropertyIndexDefinition definition) {
Set<LuceneIndexRule> indexRules;
Preconditions.checkNotNull((Object)index);
Preconditions.checkArgument((boolean)(!Strings.isNullOrEmpty((String)indexName)), (Object)"indexName cannot be null or empty");
Preconditions.checkNotNull((Object)definition);
NodeBuilder builder = index.child(indexName);
Tree indexDef = TreeFactory.createTree((NodeBuilder)builder);
NodeState before = builder.getNodeState();
if (IndexPropertyUtil.isDisabled(before)) {
return;
}
indexDef.setProperty("jcr:primaryType", (Object)"oak:QueryIndexDefinition", Type.NAME);
indexDef.setProperty("compatVersion", (Object)2, Type.LONG);
indexDef.setProperty("type", (Object)"lucene", Type.STRING);
indexDef.setProperty("async", (Object)"async", Type.STRING);
indexDef.setProperty("indexPath", (Object)("/oak:index/" + indexName), Type.STRING);
indexDef.setProperty("evaluatePathRestrictions", (Object)evaluatePathRestrictions, Type.BOOLEAN);
if (!Strings.isNullOrEmpty((String)definition.getName())) {
indexDef.setProperty("name", (Object)definition.getName());
}
if (includedPaths != null && !includedPaths.isEmpty()) {
indexDef.setProperty("includedPaths", includedPaths, Type.STRINGS);
}
if (excludedPaths != null && !excludedPaths.isEmpty()) {
indexDef.setProperty("excludedPaths", excludedPaths, Type.STRINGS);
}
if (null != aggregates && !aggregates.getAggregates().isEmpty()) {
Tree aggregatesTree = indexDef.addChild("aggregates");
aggregatesTree.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
aggregatesTree.setOrderableChildren(true);
Set<LuceneAggregate> ags = aggregates.getAggregates();
for (LuceneAggregate a : ags) {
if (a.getIncludes().isEmpty()) continue;
Tree aggregateNode = aggregatesTree.addChild(a.getNode());
aggregateNode.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
aggregateNode.setOrderableChildren(true);
for (LuceneAggregateInclude i : a.getIncludes()) {
Tree includeNode = aggregateNode.addChild(i.getNodeName());
includeNode.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
includeNode.setOrderableChildren(true);
includeNode.setProperty("path", (Object)i.getPath(), Type.STRING);
includeNode.setProperty("relativeNode", (Object)i.isRelativeNode(), Type.BOOLEAN);
if (IndexPropertyUtil.isNullOrEmpty(i.getPrimaryType())) continue;
includeNode.setProperty("primaryType", (Object)i.getPrimaryType(), Type.STRING);
}
}
}
if ((indexRules = definition.getIndexRules()) != null) {
Tree irTree = indexDef.addChild("indexRules");
irTree.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
irTree.setOrderableChildren(true);
for (LuceneIndexRule rule : indexRules) {
Tree irNode = irTree.addChild(rule.getNode());
irNode.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
irNode.setOrderableChildren(true);
Set<LuceneIndexProperty> properties = rule.getProperties();
if (properties == null) continue;
Tree propsNode = irNode.addChild("properties");
propsNode.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
propsNode.setOrderableChildren(true);
for (LuceneIndexProperty prop : properties) {
Tree p = propsNode.addChild(prop.getNodeName());
p.setProperty("jcr:primaryType", (Object)"nt:unstructured", Type.NAME);
p.setProperty("propertyIndex", (Object)prop.isPropertyIndex(), Type.BOOLEAN);
p.setProperty("name", (Object)prop.getProperty(), Type.STRING);
p.setProperty("ordered", (Object)prop.isOrdered(), Type.BOOLEAN);
if (prop.isAnalyzed()) {
p.setProperty("analyzed", (Object)prop.isAnalyzed(), Type.BOOLEAN);
}
if (prop.getType() != null) {
p.setProperty("type", (Object)prop.getType(), Type.STRING);
}
if (prop.isUseInSuggest()) {
p.setProperty("useInSuggest", (Object)true, Type.BOOLEAN);
}
if (prop.isUseInSpellcheck()) {
p.setProperty("useInSpellcheck", (Object)prop.isUseInSpellcheck(), Type.BOOLEAN);
}
if (prop.isNullCheckEnabled()) {
p.setProperty("nullCheckEnabled", (Object)prop.isNullCheckEnabled(), Type.BOOLEAN);
}
if (!prop.isNodeScopeIndex()) continue;
p.setProperty("nodeScopeIndex", (Object)prop.isNodeScopeIndex(), Type.BOOLEAN);
}
}
}
IndexPropertyUtil.checkAndIssueReindex(before, builder);
}
static void property(NodeBuilder index, String indexName, String propertyName) {
IndexPropertyUtil.property(index, indexName, propertyName, null);
}
protected static void property(NodeBuilder index, String indexName, String propertyName, String declaringNodeType, boolean reindexOnChange) {
NodeState beforeState = index.child(indexName).getNodeState();
if ("disabled".equals(beforeState.getString("type"))) {
return;
}
NodeBuilder indexBuilder = IndexPropertyUtil.getNewIndexDefinition(index, indexName, propertyName, declaringNodeType);
boolean reindex = false;
if (!beforeState.exists()) {
reindex = true;
} else if (reindexOnChange && !EqualsDiff.equals((NodeState)indexBuilder.getNodeState(), (NodeState)beforeState)) {
reindex = true;
}
if (reindex) {
indexBuilder.setProperty("reindex", (Object)true);
}
}
protected static void property(NodeBuilder index, String indexName, String propertyName, String declaringNodeType) {
IndexPropertyUtil.property(index, indexName, propertyName, declaringNodeType, true);
}
private static NodeBuilder getNewIndexDefinition(NodeBuilder index, String indexName, String propertyName, String declaringNodeType) {
Set<String> declaringType = null;
if (declaringNodeType != null) {
declaringType = Collections.singleton(declaringNodeType);
}
return IndexUtils.createIndexDefinition((NodeBuilder)index, (String)indexName, (boolean)false, (boolean)false, Collections.singleton(propertyName), declaringType);
}
private static void property(NodeBuilder index, String indexName, String propertyName, long entryCount) {
if (!index.hasChildNode(indexName)) {
NodeBuilder b = IndexUtils.createIndexDefinition((NodeBuilder)index, (String)indexName, (boolean)true, (boolean)false, Collections.singleton(propertyName), (Collection)null);
b.setProperty("entryCount", (Object)entryCount);
}
}
@Deprecated
protected static void ordered(NodeBuilder index, String indexName, String propertyName, String declaringNodeType, String direction) {
NodeState beforeState = index.child(indexName).getNodeState();
if ("disabled".equals(beforeState.getString("type"))) {
return;
}
NodeBuilder indexBuilder = IndexPropertyUtil.getNewIndexDefinition(index, indexName, propertyName, declaringNodeType);
indexBuilder.setProperty("type", (Object)"ordered");
indexBuilder.setProperty("async", (Object)"async");
if (direction != null) {
indexBuilder.setProperty("direction", (Object)direction);
}
if (!EqualsDiff.equals((NodeState)indexBuilder.getNodeState(), (NodeState)beforeState)) {
indexBuilder.setProperty("reindex", (Object)true);
}
}
static boolean isNullOrEmpty(Object value) {
return value == null || value.toString().trim().length() == 0;
}
static void disableOrderedOrProperty(@Nonnull NodeBuilder index, @Nonnull String indexNode) {
NodeBuilder b = ((NodeBuilder)Preconditions.checkNotNull((Object)index)).getChildNode((String)Preconditions.checkNotNull((Object)indexNode));
if (b.exists() && ("property".equals(b.getString("type")) || "ordered".equals(b.getString("type")))) {
b.setProperty("type", (Object)"disabled", Type.STRING);
}
}
}