Search in sources :

Example 11 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class EmbeddedOutputStream method uploadToDb.

private static void uploadToDb(final BrokerPool pool, final XmldbURL url, final Path tempFile) throws IOException {
    try (final DBBroker broker = pool.getBroker()) {
        final XmldbURI collectionUri = XmldbURI.create(url.getCollection());
        final XmldbURI documentUri = XmldbURI.create(url.getDocumentName());
        try (final Collection collection = broker.openCollection(collectionUri, Lock.LockMode.WRITE_LOCK)) {
            if (collection == null) {
                throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
            }
            if (collection.hasChildCollection(broker, documentUri)) {
                throw new IOException("Resource " + documentUri.toString() + " is a collection.");
            }
            final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
            final TransactionManager transact = pool.getTransactionManager();
            try (final Txn txn = transact.beginTransaction()) {
                broker.storeDocument(txn, documentUri, new FileInputSource(tempFile), mime, collection);
                txn.commit();
            }
        }
    } catch (final EXistException | PermissionDeniedException | LockException | SAXException e) {
        LOG.error(e);
        throw new IOException(e.getMessage(), e);
    } finally {
        if (LOG.isDebugEnabled()) {
            LOG.debug("End document upload");
        }
    }
}
Also used : Txn(org.exist.storage.txn.Txn) EXistException(org.exist.EXistException) SAXException(org.xml.sax.SAXException) DBBroker(org.exist.storage.DBBroker) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

Example 12 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class IndexerTest2 method storeDoc.

private static void storeDoc() throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager txnMgr = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().authenticate("admin", "")));
        final Txn txn = txnMgr.beginTransaction()) {
        try (final Collection collection = broker.getOrCreateCollection(txn, TestConstants.TEST_COLLECTION_URI)) {
            broker.storeDocument(txn, TestConstants.TEST_XML_URI2, new StringInputSource(XML), MimeType.XML_TYPE, collection);
            broker.flush();
            broker.saveCollection(txn, collection);
        }
        txnMgr.commit(txn);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 13 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class IndexerTest method store_preserve_ws_mixed_content_value.

private void store_preserve_ws_mixed_content_value(final boolean propValue, final String xml) throws PermissionDeniedException, IOException, EXistException, SAXException, LockException, AuthenticationException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    pool.getConfiguration().setProperty(Indexer.PROPERTY_PRESERVE_WS_MIXED_CONTENT, propValue);
    final TransactionManager txnMgr = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().authenticate("admin", "")));
        final Txn txn = txnMgr.beginTransaction()) {
        try (final Collection collection = broker.getOrCreateCollection(txn, TestConstants.TEST_COLLECTION_URI)) {
            broker.storeDocument(txn, TestConstants.TEST_XML_URI, new StringInputSource(xml), MimeType.XML_TYPE, collection);
            broker.flush();
            broker.saveCollection(txn, collection);
        }
        txnMgr.commit(txn);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool)

Example 14 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class CollectionRemovalTest method initDB.

@Before
public void initDB() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        final int worldReadable = 0744;
        final int worldForbidden = 0700;
        /*
             * Creates 3 collections: /db/test, /db/test/test2, /db/test/test2/test3 and /db/test/test2/test4,
             * and stores one document into each.
             * Collection /db/test/test2/test3 is only readable by the owner (i.e. admin user).
             */
        final List<Tuple2<XmldbURI, Integer>> collectionUriAndModes = Arrays.asList(Tuple(TestConstants.TEST_COLLECTION_URI2, worldReadable), Tuple(TestConstants.TEST_COLLECTION_URI3, worldForbidden), Tuple(TestConstants.TEST_COLLECTION_URI2.append("test4"), worldReadable));
        // creat collections
        for (final Tuple2<XmldbURI, Integer> collectionUriAndMode : collectionUriAndModes) {
            final XmldbURI collectionUri = collectionUriAndMode._1;
            final int mode = collectionUriAndMode._2;
            // create collection
            final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
            assertNotNull(collection);
            final Permission perms = collection.getPermissions();
            perms.setMode(mode);
            broker.saveCollection(transaction, collection);
            // store document
            broker.storeDocument(transaction, XmldbURI.create("document.xml"), new StringInputSource(DATA), MimeType.XML_TYPE, collection);
        }
        transact.commit(transaction);
    }
}
Also used : Txn(org.exist.storage.txn.Txn) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) Permission(org.exist.security.Permission) BrokerPool(org.exist.storage.BrokerPool) XmldbURI(org.exist.xmldb.XmldbURI)

Example 15 with TransactionManager

use of org.exist.storage.txn.TransactionManager in project exist by eXist-db.

the class LexerTest method query.

@Test
public void query() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, RecognitionException, XPathException, TokenStreamException {
    String query = "//p[. = '\u4ED6\u4E3A\u8FD9\u9879\u5DE5\u7A0B\u6295" + "\u5165\u4E86\u5341\u4E09\u5E74\u65F6\u95F4\u3002']";
    // get a BrokerPool for access to the database engine
    BrokerPool pool = BrokerPool.getInstance();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        try (final Txn transaction = transact.beginTransaction()) {
            // parse the xml source
            Collection collection = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            broker.saveCollection(transaction, collection);
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(xml), MimeType.XML_TYPE, collection);
            // TODO : unlock the collection here ?
            transact.commit(transaction);
        }
        // parse the query into the internal syntax tree
        XQueryContext context = new XQueryContext(broker.getBrokerPool());
        XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
        XQueryParser xparser = new XQueryParser(lexer);
        XQueryTreeParser treeParser = new XQueryTreeParser(context);
        xparser.xpath();
        if (xparser.foundErrors()) {
            fail(xparser.getErrorMessage());
            return;
        }
        AST ast = xparser.getAST();
        PathExpr expr = new PathExpr(context);
        treeParser.xpath(ast, expr);
        if (treeParser.foundErrors()) {
            fail(treeParser.getErrorMessage());
            return;
        }
        expr.analyze(new AnalyzeContextInfo());
        // execute the query
        Sequence result = expr.eval(null, null);
        // check results
        int count = result.getItemCount();
    }
}
Also used : AST(antlr.collections.AST) XQueryParser(org.exist.xquery.parser.XQueryParser) Txn(org.exist.storage.txn.Txn) XQueryLexer(org.exist.xquery.parser.XQueryLexer) Sequence(org.exist.xquery.value.Sequence) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Aggregations

TransactionManager (org.exist.storage.txn.TransactionManager)131 Txn (org.exist.storage.txn.Txn)128 Collection (org.exist.collections.Collection)86 DBBroker (org.exist.storage.DBBroker)70 BrokerPool (org.exist.storage.BrokerPool)61 StringInputSource (org.exist.util.StringInputSource)27 EXistException (org.exist.EXistException)21 XmldbURI (org.exist.xmldb.XmldbURI)20 Sequence (org.exist.xquery.value.Sequence)20 XQuery (org.exist.xquery.XQuery)18 InputSource (org.xml.sax.InputSource)18 DocumentImpl (org.exist.dom.persistent.DocumentImpl)17 Test (org.junit.Test)16 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 InputStream (java.io.InputStream)14 StringReader (java.io.StringReader)13 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)13 PermissionDeniedException (org.exist.security.PermissionDeniedException)13 IOException (java.io.IOException)12 Modification (org.exist.xupdate.Modification)12