Search in sources :

Example 56 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class DLNStorageTest method nodeStorage.

@Test
public void nodeStorage() throws Exception {
    BrokerPool pool = BrokerPool.getInstance();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        // test element ids
        Sequence seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test/para", null);
        assertEquals(3, seq.getItemCount());
        NodeProxy comment = (NodeProxy) seq.itemAt(0);
        assertEquals("1.1", comment.getNodeId().toString());
        comment = (NodeProxy) seq.itemAt(1);
        assertEquals("1.3", comment.getNodeId().toString());
        comment = (NodeProxy) seq.itemAt(2);
        assertEquals("1.5", comment.getNodeId().toString());
        seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//a", null);
        assertEquals(1, seq.getItemCount());
        NodeProxy a = (NodeProxy) seq.itemAt(0);
        assertEquals("1.3.2", a.getNodeId().toString());
        // test attribute id
        seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//a/@href", null);
        assertEquals(1, seq.getItemCount());
        NodeProxy href = (NodeProxy) seq.itemAt(0);
        StorageAddress.toString(href);
        assertEquals("1.3.2.1", href.getNodeId().toString());
        // test Attr deserialization
        Attr attr = (Attr) href.getNode();
        StorageAddress.toString(((NodeHandle) attr));
        // test Attr fields
        assertEquals("href", attr.getNodeName());
        assertEquals("href", attr.getName());
        assertEquals("#", attr.getValue());
        // test DOMFile.getNodeValue()
        assertEquals("#", href.getStringValue());
        // test text node
        seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//b/text()", null);
        assertEquals(1, seq.getItemCount());
        NodeProxy text = (NodeProxy) seq.itemAt(0);
        assertEquals("1.5.2.1", text.getNodeId().toString());
        // test DOMFile.getNodeValue()
        assertEquals("paragraph", text.getStringValue());
        // test Text deserialization
        Text node = (Text) text.getNode();
        assertEquals("paragraph", node.getNodeValue());
        assertEquals("paragraph", node.getData());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) NodeHandle(org.exist.dom.persistent.NodeHandle) XQuery(org.exist.xquery.XQuery) Text(org.w3c.dom.Text) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) BrokerPool(org.exist.storage.BrokerPool) Attr(org.w3c.dom.Attr)

Example 57 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class BasicNodeSetTest method executeQuery.

private static Sequence executeQuery(final DBBroker broker, final String query, final int expected, final String expectedResult) throws XPathException, SAXException, PermissionDeniedException {
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final Sequence seq = xquery.execute(broker, query, null);
    assertEquals(expected, seq.getItemCount());
    if (expectedResult != null) {
        final Item item = seq.itemAt(0);
        final String value = serialize(broker, item);
        assertEquals(expectedResult, value);
    }
    return seq;
}
Also used : Item(org.exist.xquery.value.Item) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence)

Example 58 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class RecoveryTest method verify.

private void verify(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException, BTreeException, LockException {
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Serializer serializer = broker.borrowSerializer();
        try {
            try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), LockMode.READ_LOCK)) {
                assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", lockedDoc);
                final String data = serializer.serialize(lockedDoc.getDocument());
                assertNotNull(data);
            }
            try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), LockMode.READ_LOCK)) {
                assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", lockedDoc);
                final String data = serializer.serialize(lockedDoc.getDocument());
                assertNotNull(data);
            }
            final String lastSampleName = SAMPLES.getShakespeareXmlSampleNames()[SAMPLES.getShakespeareXmlSampleNames().length - 1];
            try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(lastSampleName), LockMode.READ_LOCK)) {
                assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + lastSampleName + " should not exist anymore", lockedDoc);
            }
            final XQuery xquery = pool.getXQueryService();
            assertNotNull(xquery);
            final Sequence seq = xquery.execute(broker, "//SPEECH[contains(LINE, 'king')]", null);
            assertNotNull(seq);
            for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
                final Item next = i.nextItem();
                final String value = serializer.serialize((NodeValue) next);
            }
        } finally {
            broker.returnSerializer(serializer);
        }
        try (final LockedDocument lockedBinDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), LockMode.READ_LOCK)) {
            assertNotNull("Binary document is null", lockedBinDoc);
            final BinaryDocument binDoc = (BinaryDocument) lockedBinDoc.getDocument();
            try (final InputStream is = broker.getBinaryResource(binDoc)) {
                final byte[] bdata = new byte[(int) binDoc.getContentLength()];
                is.read(bdata);
                final String data = new String(bdata);
                assertNotNull(data);
            }
        }
        final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
        assertNotNull(domDb);
        try (final Writer writer = new StringWriter()) {
            domDb.dump(writer);
        }
        final TransactionManager transact = pool.getTransactionManager();
        try (final Txn transaction = transact.beginTransaction()) {
            try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
                assertNotNull(root);
                transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(root.getURI()));
                broker.removeCollection(transaction, root);
            }
            transact.commit(transaction);
        }
    }
}
Also used : XQuery(org.exist.xquery.XQuery) InputStream(java.io.InputStream) Sequence(org.exist.xquery.value.Sequence) DOMFile(org.exist.storage.dom.DOMFile) Txn(org.exist.storage.txn.Txn) BinaryDocument(org.exist.dom.persistent.BinaryDocument) Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) StringWriter(java.io.StringWriter) TransactionManager(org.exist.storage.txn.TransactionManager) LockedDocument(org.exist.dom.persistent.LockedDocument) Collection(org.exist.collections.Collection) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Serializer(org.exist.storage.serializers.Serializer)

Example 59 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class DocTest method docAvailable_dynamicallyAvailableDocument_relativeUri.

@Test
public void docAvailable_dynamicallyAvailableDocument_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String baseUri = "http://from-dynamic-context/";
    final String docRelativeUri = "doc1";
    final String query = "fn:doc-available('" + docRelativeUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableDocument(baseUri + docRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0).toJavaObject(Boolean.class).booleanValue());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) BrokerPool(org.exist.storage.BrokerPool)

Example 60 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class DocTest method doc_dynamicallyAvailableDocument_absoluteUri.

@Test
public void doc_dynamicallyAvailableDocument_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String docUri = "http://from-dynamic-context/doc1";
    final String query = "fn:doc('" + docUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.addDynamicallyAvailableDocument(docUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0) instanceof Node);
        final Source expectedSource = Input.fromString(doc).build();
        final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
        final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11