Search in sources :

Example 1 with DOMIndexer

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

the class NativeBroker method storeTempResource.

/**
 * Store into the temporary collection of the database a given in-memory Document
 *
 * The in-memory Document is stored without a transaction and is not journalled,
 * if there is no temporary collection, this will first be created with a transaction
 *
 * @param doc The in-memory Document to store
 * @return The document stored in the temp collection
 */
@Override
public DocumentImpl storeTempResource(final org.exist.dom.memtree.DocumentImpl doc) throws EXistException, PermissionDeniedException, LockException {
    try {
        // elevate getUser() to DBA_USER
        pushSubject(pool.getSecurityManager().getSystemSubject());
        // start a transaction
        final TransactionManager transact = pool.getTransactionManager();
        // create a name for the temporary document
        final XmldbURI docName = XmldbURI.create(MessageDigester.md5(Thread.currentThread().getName() + System.currentTimeMillis(), false) + ".xml");
        // get the temp collection
        try (final Txn transaction = transact.beginTransaction();
            final ManagedCollectionLock tempCollectionLock = lockManager.acquireCollectionWriteLock(XmldbURI.TEMP_COLLECTION_URI)) {
            // if temp collection does not exist, creates temp collection (with write lock in Txn)
            final Tuple2<Boolean, Collection> createdOrExistingTemp = getOrCreateTempCollection(transaction);
            if (createdOrExistingTemp == null) {
                LOG.error("Failed to create temporary collection");
                transact.abort(transaction);
                return null;
            }
            final Collection temp = createdOrExistingTemp._2;
            // create a temporary document
            try (final ManagedDocumentLock docLock = lockManager.acquireDocumentWriteLock(temp.getURI().append(docName))) {
                final int tmpDocId = getNextResourceId(transaction);
                final Permission permission = PermissionFactory.getDefaultResourcePermission(getBrokerPool().getSecurityManager());
                permission.setMode(Permission.DEFAULT_TEMPORARY_DOCUMENT_PERM);
                final DocumentImpl targetDoc = new DocumentImpl(pool, temp, tmpDocId, docName, permission, 0, null, System.currentTimeMillis(), null, null, null);
                // index the temporary document
                final DOMIndexer indexer = new DOMIndexer(this, transaction, doc, targetDoc);
                indexer.scan();
                indexer.store();
                // store the temporary document
                temp.addDocument(transaction, this, targetDoc);
                storeXMLResource(transaction, targetDoc);
                saveCollection(transaction, temp);
                // NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
                temp.close();
                flush();
                closeDocument();
                // commit the transaction
                transact.commit(transaction);
                return targetDoc;
            }
        } catch (final Exception e) {
            LOG.error("Failed to store temporary fragment: {}", e.getMessage(), e);
        }
    } finally {
        // restore the user
        popSubject();
    }
    return null;
}
Also used : Txn(org.exist.storage.txn.Txn) TerminatedException(org.exist.xquery.TerminatedException) XMLStreamException(javax.xml.stream.XMLStreamException) SAXException(org.xml.sax.SAXException) EXistException(org.exist.EXistException) TransactionException(org.exist.storage.txn.TransactionException) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) DOMIndexer(org.exist.dom.memtree.DOMIndexer) XmldbURI(org.exist.xmldb.XmldbURI)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)1 EXistException (org.exist.EXistException)1 Collection (org.exist.collections.Collection)1 DOMIndexer (org.exist.dom.memtree.DOMIndexer)1 TransactionException (org.exist.storage.txn.TransactionException)1 TransactionManager (org.exist.storage.txn.TransactionManager)1 Txn (org.exist.storage.txn.Txn)1 XmldbURI (org.exist.xmldb.XmldbURI)1 TerminatedException (org.exist.xquery.TerminatedException)1 SAXException (org.xml.sax.SAXException)1