Search in sources :

Example 6 with NamePathMapperImpl

use of org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl in project jackrabbit-oak by apache.

the class QueryEngineImpl method parseQuery.

/**
 * Parse the query.
 *
 * @param statement the statement
 * @param language the language
 * @param context the context
 * @param mappings the mappings
 * @return the list of queries, where the first is the original, and all
 *         others are alternatives (for example, a "union" query)
 */
private static List<Query> parseQuery(String statement, String language, ExecutionContext context, Map<String, String> mappings) throws ParseException {
    boolean isInternal = SQL2Parser.isInternal(statement);
    if (isInternal) {
        LOG.trace("Parsing {} statement: {}", language, statement);
    } else {
        LOG.debug("Parsing {} statement: {}", language, statement);
    }
    NamePathMapper mapper = new NamePathMapperImpl(new LocalNameMapper(context.getRoot(), mappings));
    NodeTypeInfoProvider nodeTypes = context.getNodeTypeInfoProvider();
    QueryEngineSettings settings = context.getSettings();
    QueryExecutionStats stats = settings.getQueryStatsReporter().getQueryExecution(statement, language);
    SQL2Parser parser = new SQL2Parser(mapper, nodeTypes, settings, stats);
    if (language.endsWith(NO_LITERALS)) {
        language = language.substring(0, language.length() - NO_LITERALS.length());
        parser.setAllowNumberLiterals(false);
        parser.setAllowTextLiterals(false);
    }
    ArrayList<Query> queries = new ArrayList<Query>();
    Query q;
    if (SQL2.equals(language) || JQOM.equals(language)) {
        q = parser.parse(statement, false);
    } else if (SQL.equals(language)) {
        parser.setSupportSQL1(true);
        q = parser.parse(statement, false);
    } else if (XPATH.equals(language)) {
        XPathToSQL2Converter converter = new XPathToSQL2Converter();
        String sql2 = converter.convert(statement);
        LOG.debug("XPath > SQL2: {}", sql2);
        try {
            // OAK-874: No artificial XPath selector name in wildcards
            parser.setIncludeSelectorNameInWildcardColumns(false);
            q = parser.parse(sql2, false);
        } catch (ParseException e) {
            ParseException e2 = new ParseException(statement + " converted to SQL-2 " + e.getMessage(), 0);
            e2.initCause(e);
            throw e2;
        }
    } else {
        throw new ParseException("Unsupported language: " + language, 0);
    }
    if (q.isInternal()) {
        stats.setInternal(true);
    } else {
        stats.setThreadName(Thread.currentThread().getName());
    }
    queries.add(q);
    if (settings.isSql2Optimisation()) {
        if (q.isInternal()) {
            LOG.trace("Skipping optimisation as internal query.");
        } else {
            LOG.trace("Attempting optimisation");
            Query q2 = q.buildAlternativeQuery();
            if (q2 != q) {
                LOG.debug("Alternative query available: {}", q2);
                queries.add(q2);
            }
        }
    }
    // initialising all the queries.
    for (Query query : queries) {
        try {
            query.init();
        } catch (Exception e) {
            ParseException e2 = new ParseException(query.getStatement() + ": " + e.getMessage(), 0);
            e2.initCause(e);
            throw e2;
        }
    }
    return queries;
}
Also used : NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) ArrayList(java.util.ArrayList) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) NodeTypeInfoProvider(org.apache.jackrabbit.oak.query.ast.NodeTypeInfoProvider) LocalNameMapper(org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper) ParseException(java.text.ParseException) QueryExecutionStats(org.apache.jackrabbit.oak.query.stats.QueryStatsData.QueryExecutionStats) XPathToSQL2Converter(org.apache.jackrabbit.oak.query.xpath.XPathToSQL2Converter) ParseException(java.text.ParseException)

Example 7 with NamePathMapperImpl

use of org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl in project jackrabbit-oak by apache.

the class QueryUtilTest method testEscapeForQuery.

@Test
public void testEscapeForQuery() throws Exception {
    NamePathMapper namePathMapper = new NamePathMapperImpl(new LocalNameMapper(ImmutableMap.of(NamespaceRegistry.PREFIX_JCR, NamespaceRegistry.NAMESPACE_JCR), ImmutableMap.of("myPrefix", NamespaceRegistry.NAMESPACE_JCR)));
    String value = "'string\\value";
    assertEquals(QueryUtils.escapeForQuery("myPrefix:" + value), QueryUtil.escapeForQuery("jcr:" + value, namePathMapper));
}
Also used : NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) LocalNameMapper(org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper) Test(org.junit.Test)

Example 8 with NamePathMapperImpl

use of org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl in project jackrabbit-oak by apache.

the class NodeObserver method contentChanged.

@Override
public void contentChanged(@Nonnull NodeState root, @Nonnull CommitInfo info) {
    if (previousRoot != null) {
        try {
            long start = PERF_LOGGER.start();
            NamePathMapper namePathMapper = new NamePathMapperImpl(new GlobalNameMapper(RootFactory.createReadOnlyRoot(root)));
            Set<String> oakPropertyNames = Sets.newHashSet();
            for (String name : propertyNames) {
                String oakName = namePathMapper.getOakNameOrNull(name);
                if (oakName == null) {
                    LOG.warn("Ignoring invalid property name: {}", name);
                } else {
                    oakPropertyNames.add(oakName);
                }
            }
            NodeState before = previousRoot;
            NodeState after = root;
            EventHandler handler = new FilteredHandler(VISIBLE_FILTER, new NodeEventHandler("/", info, namePathMapper, oakPropertyNames));
            String oakPath = namePathMapper.getOakPath(path);
            if (oakPath == null) {
                LOG.warn("Cannot listen for changes on invalid path: {}", path);
                return;
            }
            for (String oakName : PathUtils.elements(oakPath)) {
                before = before.getChildNode(oakName);
                after = after.getChildNode(oakName);
                handler = handler.getChildHandler(oakName, before, after);
            }
            EventGenerator generator = new EventGenerator(before, after, handler);
            while (!generator.isDone()) {
                generator.generate();
            }
            PERF_LOGGER.end(start, 100, "Generated events (before: {}, after: {})", previousRoot, root);
        } catch (Exception e) {
            LOG.warn("Error while dispatching observation events", e);
        }
    }
    previousRoot = root;
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) GlobalNameMapper(org.apache.jackrabbit.oak.namepath.impl.GlobalNameMapper) NamePathMapper(org.apache.jackrabbit.oak.namepath.NamePathMapper) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl)

Example 9 with NamePathMapperImpl

use of org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl in project jackrabbit-oak by apache.

the class NodeTypeRegistryTest method registerNodeType.

@Test
public void registerNodeType() throws Exception {
    registerNodeType(root, "oak6440-1.cnd");
    NodeTypeManager readOnlyNtMgr = new ReadOnlyNodeTypeManager() {

        private Root r = session.getLatestRoot();

        @Override
        protected Tree getTypes() {
            return r.getTree(NODE_TYPES_PATH);
        }
    };
    NodeTypeManager ntMgr = new ReadWriteNodeTypeManager() {

        @Override
        protected Tree getTypes() {
            return root.getTree(NODE_TYPES_PATH);
        }

        @Nonnull
        @Override
        protected Root getWriteRoot() {
            return root;
        }
    };
    ValueFactory valueFactory = new ValueFactoryImpl(root, new NamePathMapperImpl(new GlobalNameMapper(root)));
    NamespaceRegistry nsRegistry = new ReadOnlyNamespaceRegistry(root);
    DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, valueFactory, nsRegistry);
    InputStream in = NodeTypeRegistryTest.class.getResourceAsStream("oak6440-2.cnd");
    List<NodeTypeTemplate> templates;
    try {
        CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> reader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(new InputStreamReader(in, UTF_8), "oak6440-2.cnd", factory);
        templates = reader.getNodeTypeDefinitions();
    } finally {
        in.close();
    }
    for (NodeTypeTemplate t : templates) {
        NodeTypeTemplateImpl template;
        if (t instanceof NodeTypeTemplateImpl) {
            template = (NodeTypeTemplateImpl) t;
        } else {
            template = new NodeTypeTemplateImpl(new GlobalNameMapper(root), t);
        }
        template.writeTo(root.getTree(NODE_TYPES_PATH), true);
    }
    NodeTypeDefinition beforeDef = readOnlyNtMgr.getNodeType("foo");
    NodeTypeDefinition afterDef = ntMgr.getNodeType("foo");
    NodeTypeDefDiff diff = NodeTypeDefDiff.create(beforeDef, afterDef);
    assertFalse(diff.isMajor());
}
Also used : ReadOnlyNodeTypeManager(org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager) NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Root(org.apache.jackrabbit.oak.api.Root) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ValueFactoryImpl(org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) NodeTypeDefinition(javax.jcr.nodetype.NodeTypeDefinition) ReadOnlyNodeTypeManager(org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager) ValueFactory(javax.jcr.ValueFactory) ReadOnlyNamespaceRegistry(org.apache.jackrabbit.oak.plugins.name.ReadOnlyNamespaceRegistry) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) NamespaceRegistry(javax.jcr.NamespaceRegistry) ReadOnlyNamespaceRegistry(org.apache.jackrabbit.oak.plugins.name.ReadOnlyNamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) GlobalNameMapper(org.apache.jackrabbit.oak.namepath.impl.GlobalNameMapper) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) NodeTypeDefDiff(org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeDefDiff) Test(org.junit.Test)

Example 10 with NamePathMapperImpl

use of org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl in project jackrabbit-oak by apache.

the class PrivilegeManagerImplTest method testGetPrivilegeExpandedName.

@Test
public void testGetPrivilegeExpandedName() throws Exception {
    Privilege p = create(root, new NamePathMapperImpl(new GlobalNameMapper(root))).getPrivilege(Privilege.JCR_VERSION_MANAGEMENT);
    assertNotNull(p);
    assertNotEquals(Privilege.JCR_VERSION_MANAGEMENT, p.getName());
    assertEquals(PrivilegeConstants.JCR_VERSION_MANAGEMENT, p.getName());
}
Also used : GlobalNameMapper(org.apache.jackrabbit.oak.namepath.impl.GlobalNameMapper) Privilege(javax.jcr.security.Privilege) NamePathMapperImpl(org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl) AbstractSecurityTest(org.apache.jackrabbit.oak.AbstractSecurityTest) Test(org.junit.Test)

Aggregations

NamePathMapperImpl (org.apache.jackrabbit.oak.namepath.impl.NamePathMapperImpl)14 LocalNameMapper (org.apache.jackrabbit.oak.namepath.impl.LocalNameMapper)11 Test (org.junit.Test)11 NamePathMapper (org.apache.jackrabbit.oak.namepath.NamePathMapper)10 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)7 GlobalNameMapper (org.apache.jackrabbit.oak.namepath.impl.GlobalNameMapper)6 Privilege (javax.jcr.security.Privilege)4 NameMapper (org.apache.jackrabbit.oak.namepath.NameMapper)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 PrivilegeManager (org.apache.jackrabbit.api.security.authorization.PrivilegeManager)2 ValueFactoryImpl (org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl)2 AbstractAccessControlList (org.apache.jackrabbit.oak.spi.security.authorization.accesscontrol.AbstractAccessControlList)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ParseException (java.text.ParseException)1 NamespaceRegistry (javax.jcr.NamespaceRegistry)1 ValueFactory (javax.jcr.ValueFactory)1 NodeTypeDefinition (javax.jcr.nodetype.NodeTypeDefinition)1 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)1