Search in sources :

Example 51 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class NativeValueIndex method findAll.

/**
 * find.
 *
 * @param comparison The type of comparison the search is performing
 * @param docs       The documents to search for matches within
 * @param contextSet DOCUMENT ME!
 * @param axis       DOCUMENT ME!
 * @param qnames     DOCUMENT ME!
 * @param value      right hand comparison value
 * @param result     DOCUMENT ME!
 * @return DOCUMENT ME!
 * @throws TerminatedException DOCUMENT ME!
 */
private NodeSet findAll(final XQueryWatchDog watchDog, final Comparison comparison, final DocumentSet docs, final NodeSet contextSet, final int axis, final List<QName> qnames, final Indexable value, final NodeSet result) throws TerminatedException {
    final SearchCallback cb = new SearchCallback(docs, contextSet, result, axis == NodeSet.ANCESTOR);
    final int idxOp = toIndexQueryOp(comparison);
    for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext(); ) {
        final int collectionId = iter.next().getId();
        watchDog.proceed(null);
        if (qnames == null) {
            try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
                final Value searchKey = new SimpleValue(collectionId, value);
                final IndexQuery query = new IndexQuery(idxOp, searchKey);
                if (idxOp == IndexQuery.EQ) {
                    dbValues.query(query, cb);
                } else {
                    final Value prefixKey = new SimplePrefixValue(collectionId, value.getType());
                    dbValues.query(query, prefixKey, cb);
                }
            } catch (final EXistException | BTreeException | IOException e) {
                LOG.error(e.getMessage(), e);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
            }
        } else {
            for (final QName qname : qnames) {
                try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
                    // Compute a key for the value in the collection
                    final Value searchKey = new QNameValue(collectionId, qname, value, broker.getBrokerPool().getSymbols());
                    final IndexQuery query = new IndexQuery(idxOp, searchKey);
                    if (idxOp == IndexQuery.EQ) {
                        dbValues.query(query, cb);
                    } else {
                        final Value prefixKey = new QNamePrefixValue(collectionId, qname, value.getType(), broker.getBrokerPool().getSymbols());
                        dbValues.query(query, prefixKey, cb);
                    }
                } catch (final EXistException | BTreeException | IOException e) {
                    LOG.error(e.getMessage(), e);
                } catch (final LockException e) {
                    LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
                }
            }
        }
    }
    return result;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) EXistException(org.exist.EXistException) IOException(java.io.IOException) BTreeException(org.exist.storage.btree.BTreeException) AtomicValue(org.exist.xquery.value.AtomicValue) StringValue(org.exist.xquery.value.StringValue) Value(org.exist.storage.btree.Value) Collection(org.exist.collections.Collection)

Example 52 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class NativeValueIndex method getDefinedIndexes.

private List<QName> getDefinedIndexes(final DocumentSet docs) {
    final List<QName> qnames = new ArrayList<>();
    for (final Iterator<Collection> i = docs.getCollectionIterator(); i.hasNext(); ) {
        final Collection collection = i.next();
        final IndexSpec idxConf = collection.getIndexConfiguration(broker);
        if (idxConf != null) {
            qnames.addAll(idxConf.getIndexedQNames());
        }
    }
    return qnames;
}
Also used : QName(org.exist.dom.QName) Collection(org.exist.collections.Collection)

Example 53 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class NativeValueIndex method matchAll.

/**
 * Regular expression search.
 *
 * @param docs               DOCUMENT ME!
 * @param contextSet         DOCUMENT ME!
 * @param axis               DOCUMENT ME!
 * @param expr               DOCUMENT ME!
 * @param qnames             DOCUMENT ME!
 * @param type               like type argument for {@link org.exist.storage.RegexMatcher} constructor
 * @param flags              like flags argument for {@link org.exist.storage.RegexMatcher} constructor
 * @param caseSensitiveQuery DOCUMENT ME!
 * @param result             DOCUMENT ME!
 * @param collator           DOCUMENT ME!
 * @param truncation         The type of string truncation to apply
 * @param watchDog  the watchdog
 * @return DOCUMENT ME!
 * @throws TerminatedException DOCUMENT ME!
 * @throws EXistException      DOCUMENT ME!
 */
public NodeSet matchAll(final XQueryWatchDog watchDog, final DocumentSet docs, final NodeSet contextSet, final int axis, final String expr, final List<QName> qnames, final int type, final int flags, final boolean caseSensitiveQuery, final NodeSet result, final Collator collator, final StringTruncationOperator truncation) throws TerminatedException, EXistException {
    // if the match expression starts with a char sequence, we restrict the index scan to entries starting with
    // the same sequence. Otherwise, we have to scan the whole index.
    final StringValue startTerm;
    if (type == DBBroker.MATCH_REGEXP && expr.startsWith("^") && caseSensitiveQuery == caseSensitive) {
        final StringBuilder term = new StringBuilder();
        for (int j = 1; j < expr.length(); j++) {
            if (Character.isLetterOrDigit(expr.charAt(j))) {
                term.append(expr.charAt(j));
            } else {
                break;
            }
        }
        if (term.length() > 0) {
            startTerm = new StringValue(term.toString());
            LOG.debug("Match will begin index scan at '{}'", startTerm);
        } else {
            startTerm = null;
        }
    } else if (collator == null && (type == DBBroker.MATCH_EXACT || type == DBBroker.MATCH_STARTSWITH)) {
        startTerm = new StringValue(expr);
        LOG.debug("Match will begin index scan at '{}'", startTerm);
    } else {
        startTerm = null;
    }
    // Select appropriate matcher/comparator
    final TermMatcher matcher;
    if (collator == null) {
        switch(type) {
            case DBBroker.MATCH_EXACT:
                matcher = new ExactMatcher(expr);
                break;
            case DBBroker.MATCH_CONTAINS:
                matcher = new ContainsMatcher(expr);
                break;
            case DBBroker.MATCH_STARTSWITH:
                matcher = new StartsWithMatcher(expr);
                break;
            case DBBroker.MATCH_ENDSWITH:
                matcher = new EndsWithMatcher(expr);
                break;
            default:
                matcher = new RegexMatcher(expr, flags);
        }
    } else {
        matcher = new CollatorMatcher(expr, truncation, collator);
    }
    final MatcherCallback cb = new MatcherCallback(docs, contextSet, result, matcher, axis == NodeSet.ANCESTOR);
    for (final Iterator<Collection> iter = docs.getCollectionIterator(); iter.hasNext(); ) {
        final int collectionId = iter.next().getId();
        watchDog.proceed(null);
        if (qnames == null) {
            try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
                final Value searchKey;
                if (startTerm != null) {
                    // Compute a key for the start term in the collection
                    searchKey = new SimpleValue(collectionId, startTerm);
                } else {
                    // Compute a key for an arbitrary string in the collection
                    searchKey = new SimplePrefixValue(collectionId, Type.STRING);
                }
                final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, searchKey);
                dbValues.query(query, cb);
            } catch (final IOException | BTreeException e) {
                LOG.error(e.getMessage(), e);
            } catch (final LockException e) {
                LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
            }
        } else {
            for (final QName qname : qnames) {
                try (final ManagedLock<ReentrantLock> bfileLock = lockManager.acquireBtreeReadLock(dbValues.getLockName())) {
                    final Value searchKey;
                    if (startTerm != null) {
                        searchKey = new QNameValue(collectionId, qname, startTerm, broker.getBrokerPool().getSymbols());
                    } else {
                        LOG.debug("Searching with QName prefix");
                        searchKey = new QNamePrefixValue(collectionId, qname, Type.STRING, broker.getBrokerPool().getSymbols());
                    }
                    final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, searchKey);
                    dbValues.query(query, cb);
                } catch (final IOException | BTreeException e) {
                    LOG.error(e.getMessage(), e);
                } catch (final LockException e) {
                    LOG.warn("Failed to acquire lock for '{}'", FileUtils.fileName(dbValues.getFile()), e);
                }
            }
        }
    }
    return result;
}
Also used : BTreeException(org.exist.storage.btree.BTreeException) StringValue(org.exist.xquery.value.StringValue) ReentrantLock(java.util.concurrent.locks.ReentrantLock) IndexQuery(org.exist.storage.btree.IndexQuery) QName(org.exist.dom.QName) IOException(java.io.IOException) AtomicValue(org.exist.xquery.value.AtomicValue) StringValue(org.exist.xquery.value.StringValue) Value(org.exist.storage.btree.Value) Collection(org.exist.collections.Collection)

Example 54 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class LoadXQueryModule method getModuleVariables.

private void getModuleVariables(final Module module, final IMap<AtomicValue, Sequence> variables) throws XPathException {
    for (final Iterator<QName> i = module.getGlobalVariables(); i.hasNext(); ) {
        final QName name = i.next();
        try {
            final Variable var = module.resolveVariable(name);
            variables.put(new QNameValue(context, name), var.getValue());
        } catch (final XPathException e) {
            throw new XPathException(this, ErrorCodes.FOQM0005, "Incorrect type for external variable " + name);
        }
    }
}
Also used : QName(org.exist.dom.QName)

Example 55 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class FunOnFunctions method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
        if (isCalledAs("function-lookup")) {
            final QName fname = ((QNameValue) args[0].itemAt(0)).getQName();
            final int arity = ((IntegerValue) args[1].itemAt(0)).getInt();
            FunctionCall call;
            try {
                call = NamedFunctionReference.lookupFunction(this, context, fname, arity);
            } catch (final XPathException e) {
                if (e.getErrorCode() == ErrorCodes.XPST0017) {
                    // return empty sequence for all "function not found" related errors
                    return Sequence.EMPTY_SEQUENCE;
                }
                throw e;
            }
            return call == null ? Sequence.EMPTY_SEQUENCE : new FunctionReference(call);
        } else if (isCalledAs("function-name")) {
            final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
            final QName qname = ref.getSignature().getName();
            if (qname == null || qname == InlineFunction.INLINE_FUNCTION_QNAME) {
                return Sequence.EMPTY_SEQUENCE;
            } else {
                return new QNameValue(context, qname);
            }
        } else {
            // isCalledAs("function-arity")
            final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
            return new IntegerValue(ref.getSignature().getArgumentCount());
        }
    } catch (final Exception e) {
        if (e instanceof XPathException) {
            throw (XPathException) e;
        } else {
            throw new XPathException(this, ErrorCodes.XPST0017, e.getMessage());
        }
    }
}
Also used : QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) IntegerValue(org.exist.xquery.value.IntegerValue) FunctionReference(org.exist.xquery.value.FunctionReference)

Aggregations

QName (org.exist.dom.QName)271 Test (org.junit.Test)54 Sequence (org.exist.xquery.value.Sequence)39 DBBroker (org.exist.storage.DBBroker)31 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)28 IOException (java.io.IOException)23 Document (org.w3c.dom.Document)23 DocumentSet (org.exist.dom.persistent.DocumentSet)20 Text (org.w3c.dom.Text)20 AttributesImpl (org.xml.sax.helpers.AttributesImpl)18 NameTest (org.exist.xquery.NameTest)17 XPathException (org.exist.xquery.XPathException)17 BrokerPool (org.exist.storage.BrokerPool)15 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)13 Node (org.w3c.dom.Node)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 NodeSet (org.exist.dom.persistent.NodeSet)11 SAXException (org.xml.sax.SAXException)11 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)10 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10