IndexPropertyUtil.java 11.9 KB
/*
 * 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);
        }
    }
}