Search in sources :

Example 31 with ChildNodeEntry

use of org.apache.jackrabbit.oak.spi.state.ChildNodeEntry in project jackrabbit-oak by apache.

the class IndexCopier method copy.

/**
     * Copy all index definition and data from sourceRoot to targetRoot. The
     * indexing data is filtered to include only content related to the passed
     * list of paths.
     *
     * @param sourceRoot the source
     * @param targetRoot the target
     * @param includes indexing data for these paths will be copied
     */
public static void copy(NodeState sourceRoot, NodeBuilder targetRoot, Set<String> includes) {
    NodeState oakIndex = sourceRoot.getChildNode(INDEX_DEFINITIONS_NAME);
    NodeBuilder targetOakIndex = copySingleNode(oakIndex, targetRoot, INDEX_DEFINITIONS_NAME);
    for (ChildNodeEntry child : oakIndex.getChildNodeEntries()) {
        NodeState indexDef = child.getNodeState();
        String type = indexDef.getString(TYPE_PROPERTY_NAME);
        if (isEmpty(type)) {
            continue;
        }
        NodeBuilder targetIndexDef = copySingleNode(child, targetOakIndex);
        switch(type) {
            case "property":
                if (indexDef.getBoolean(UNIQUE_PROPERTY_NAME)) {
                    copyUniqueIndex(indexDef, targetIndexDef, includes);
                } else {
                    copyMirrorIndex(indexDef, targetIndexDef, includes);
                }
                break;
            case "counter":
                copyMirrorIndex(indexDef, targetIndexDef, includes);
            case "lucene":
                copyLuceneIndex(indexDef, targetIndexDef, includes);
            default:
                break;
        }
    }
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.oak.spi.state.ChildNodeEntry) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder)

Example 32 with ChildNodeEntry

use of org.apache.jackrabbit.oak.spi.state.ChildNodeEntry in project jackrabbit-oak by apache.

the class IndexCopier method copyUniqueIndex.

private static void copyUniqueIndex(NodeState indexDef, NodeBuilder targetIndexDef, Set<String> includes) {
    for (ChildNodeEntry childIndexNode : getIndexNodes(indexDef)) {
        NodeState indexNode = childIndexNode.getNodeState();
        NodeBuilder targetIndexNode = copySingleNode(indexNode, targetIndexDef, childIndexNode.getName());
        boolean anyAttrCopied = false;
        for (ChildNodeEntry attr : indexNode.getChildNodeEntries()) {
            Iterable<String> entries = attr.getNodeState().getStrings("entry");
            if (entries != null) {
                for (String e : entries) {
                    if (startsWithAny(e, includes)) {
                        copySingleNode(attr, targetIndexNode);
                        anyAttrCopied = true;
                    }
                }
            }
        }
        if (!anyAttrCopied) {
            targetIndexNode.remove();
        }
    }
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.oak.spi.state.ChildNodeEntry) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder)

Example 33 with ChildNodeEntry

use of org.apache.jackrabbit.oak.spi.state.ChildNodeEntry in project jackrabbit-oak by apache.

the class IndexCopier method copyMirrorIndex.

private static void copyMirrorIndex(NodeState indexDef, NodeBuilder targetIndexDef, Set<String> includes) {
    for (ChildNodeEntry childIndexNode : getIndexNodes(indexDef)) {
        NodeState indexNode = childIndexNode.getNodeState();
        NodeBuilder targetIndexNode = copySingleNode(indexNode, targetIndexDef, childIndexNode.getName());
        boolean anyAttrCopied = false;
        for (ChildNodeEntry attr : indexNode.getChildNodeEntries()) {
            NodeBuilder targetAttr = copySingleNode(attr, targetIndexNode);
            boolean copied = NodeStateCopier.builder().include(includes).copy(attr.getNodeState(), targetAttr);
            if (!copied) {
                targetAttr.remove();
            }
            anyAttrCopied = copied || anyAttrCopied;
        }
        if (!anyAttrCopied) {
            targetIndexNode.remove();
        }
    }
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.oak.spi.state.ChildNodeEntry) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder)

Example 34 with ChildNodeEntry

use of org.apache.jackrabbit.oak.spi.state.ChildNodeEntry in project jackrabbit-oak by apache.

the class SameNameSiblingsEditor method prepareChildDefsWithoutSns.

/**
     * Prepare a list of node definitions that doesn't allow having SNS children.
     *
     * @param root Repository root
     * @return a list of node definitions denying SNS children
     */
private static List<ChildTypeDef> prepareChildDefsWithoutSns(NodeState root) {
    List<ChildTypeDef> defs = new ArrayList<ChildTypeDef>();
    NodeState types = root.getChildNode(JCR_SYSTEM).getChildNode(JCR_NODE_TYPES);
    for (ChildNodeEntry typeEntry : types.getChildNodeEntries()) {
        NodeState type = typeEntry.getNodeState();
        TypePredicate typePredicate = new TypePredicate(root, typeEntry.getName());
        defs.addAll(parseResidualChildNodeDefs(root, type, typePredicate));
        defs.addAll(parseNamedChildNodeDefs(root, type, typePredicate));
    }
    return defs;
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.oak.spi.state.ChildNodeEntry) ArrayList(java.util.ArrayList) TypePredicate(org.apache.jackrabbit.oak.plugins.nodetype.TypePredicate)

Example 35 with ChildNodeEntry

use of org.apache.jackrabbit.oak.spi.state.ChildNodeEntry in project jackrabbit-oak by apache.

the class SegmentNodeState method compareAgainstBaseState.

@Override
public boolean compareAgainstBaseState(NodeState base, NodeStateDiff diff) {
    if (this == base || fastEquals(this, base)) {
        // no changes
        return true;
    } else if (base == EMPTY_NODE || !base.exists()) {
        // special case
        return EmptyNodeState.compareAgainstEmptyState(this, diff);
    } else if (!(base instanceof SegmentNodeState)) {
        // fallback
        return AbstractNodeState.compareAgainstBaseState(this, base, diff);
    }
    SegmentNodeState that = (SegmentNodeState) base;
    Template beforeTemplate = that.getTemplate();
    RecordId beforeId = that.getRecordId();
    Template afterTemplate = getTemplate();
    RecordId afterId = getRecordId();
    // Compare type properties
    if (!compareProperties(beforeTemplate.getPrimaryType(), afterTemplate.getPrimaryType(), diff)) {
        return false;
    }
    if (!compareProperties(beforeTemplate.getMixinTypes(), afterTemplate.getMixinTypes(), diff)) {
        return false;
    }
    // Compare other properties, leveraging the ordering
    int beforeIndex = 0;
    int afterIndex = 0;
    PropertyTemplate[] beforeProperties = beforeTemplate.getPropertyTemplates();
    PropertyTemplate[] afterProperties = afterTemplate.getPropertyTemplates();
    while (beforeIndex < beforeProperties.length && afterIndex < afterProperties.length) {
        int d = Integer.valueOf(afterProperties[afterIndex].hashCode()).compareTo(beforeProperties[beforeIndex].hashCode());
        if (d == 0) {
            d = afterProperties[afterIndex].getName().compareTo(beforeProperties[beforeIndex].getName());
        }
        PropertyState beforeProperty = null;
        PropertyState afterProperty = null;
        if (d < 0) {
            afterProperty = afterTemplate.getProperty(afterId, afterIndex++);
        } else if (d > 0) {
            beforeProperty = beforeTemplate.getProperty(beforeId, beforeIndex++);
        } else {
            afterProperty = afterTemplate.getProperty(afterId, afterIndex++);
            beforeProperty = beforeTemplate.getProperty(beforeId, beforeIndex++);
        }
        if (!compareProperties(beforeProperty, afterProperty, diff)) {
            return false;
        }
    }
    while (afterIndex < afterProperties.length) {
        if (!diff.propertyAdded(afterTemplate.getProperty(afterId, afterIndex++))) {
            return false;
        }
    }
    while (beforeIndex < beforeProperties.length) {
        PropertyState beforeProperty = beforeTemplate.getProperty(beforeId, beforeIndex++);
        if (!diff.propertyDeleted(beforeProperty)) {
            return false;
        }
    }
    String beforeChildName = beforeTemplate.getChildName();
    String afterChildName = afterTemplate.getChildName();
    if (afterChildName == Template.ZERO_CHILD_NODES) {
        if (beforeChildName != Template.ZERO_CHILD_NODES) {
            for (ChildNodeEntry entry : beforeTemplate.getChildNodeEntries(beforeId)) {
                if (!diff.childNodeDeleted(entry.getName(), entry.getNodeState())) {
                    return false;
                }
            }
        }
    } else if (afterChildName != Template.MANY_CHILD_NODES) {
        NodeState afterNode = afterTemplate.getChildNode(afterChildName, afterId);
        NodeState beforeNode = beforeTemplate.getChildNode(afterChildName, beforeId);
        if (!beforeNode.exists()) {
            if (!diff.childNodeAdded(afterChildName, afterNode)) {
                return false;
            }
        } else if (!fastEquals(afterNode, beforeNode)) {
            if (!diff.childNodeChanged(afterChildName, beforeNode, afterNode)) {
                return false;
            }
        }
        if (beforeChildName == Template.MANY_CHILD_NODES || (beforeChildName != Template.ZERO_CHILD_NODES && !beforeNode.exists())) {
            for (ChildNodeEntry entry : beforeTemplate.getChildNodeEntries(beforeId)) {
                if (!afterChildName.equals(entry.getName())) {
                    if (!diff.childNodeDeleted(entry.getName(), entry.getNodeState())) {
                        return false;
                    }
                }
            }
        }
    } else if (beforeChildName == Template.ZERO_CHILD_NODES) {
        for (ChildNodeEntry entry : afterTemplate.getChildNodeEntries(afterId)) {
            if (!diff.childNodeAdded(entry.getName(), entry.getNodeState())) {
                return false;
            }
        }
    } else if (beforeChildName != Template.MANY_CHILD_NODES) {
        boolean beforeChildRemoved = true;
        NodeState beforeChild = beforeTemplate.getChildNode(beforeChildName, beforeId);
        for (ChildNodeEntry entry : afterTemplate.getChildNodeEntries(afterId)) {
            String childName = entry.getName();
            NodeState afterChild = entry.getNodeState();
            if (beforeChildName.equals(childName)) {
                beforeChildRemoved = false;
                if (!fastEquals(afterChild, beforeChild) && !diff.childNodeChanged(childName, beforeChild, afterChild)) {
                    return false;
                }
            } else if (!diff.childNodeAdded(childName, afterChild)) {
                return false;
            }
        }
        if (beforeChildRemoved) {
            if (!diff.childNodeDeleted(beforeChildName, beforeChild)) {
                return false;
            }
        }
    } else {
        MapRecord afterMap = afterTemplate.getChildNodeMap(afterId);
        MapRecord beforeMap = beforeTemplate.getChildNodeMap(beforeId);
        return afterMap.compare(beforeMap, diff);
    }
    return true;
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) EmptyNodeState(org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState) AbstractNodeState(org.apache.jackrabbit.oak.spi.state.AbstractNodeState) MemoryChildNodeEntry(org.apache.jackrabbit.oak.plugins.memory.MemoryChildNodeEntry) ChildNodeEntry(org.apache.jackrabbit.oak.spi.state.ChildNodeEntry) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Aggregations

ChildNodeEntry (org.apache.jackrabbit.oak.spi.state.ChildNodeEntry)58 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)43 NodeBuilder (org.apache.jackrabbit.oak.spi.state.NodeBuilder)19 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)6 MemoryDocumentStore (org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore)6 EmptyNodeState (org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState)4 AbstractNodeState (org.apache.jackrabbit.oak.spi.state.AbstractNodeState)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 VersionGCStats (org.apache.jackrabbit.oak.plugins.document.VersionGarbageCollector.VersionGCStats)3 Clock (org.apache.jackrabbit.oak.stats.Clock)3 Map (java.util.Map)2 UUID (java.util.UUID)2 CheckForNull (javax.annotation.CheckForNull)2 Blob (org.apache.jackrabbit.oak.api.Blob)2 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)2 CONSTRAINT (org.apache.jackrabbit.oak.api.CommitFailedException.CONSTRAINT)2 JsopBuilder.prettyPrint (org.apache.jackrabbit.oak.commons.json.JsopBuilder.prettyPrint)2 MemoryChildNodeEntry (org.apache.jackrabbit.oak.plugins.memory.MemoryChildNodeEntry)2