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;
}
Aggregations