use of org.exist.dom.persistent.LockedDocument 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);
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class FluentBrokerAPITest method collectionThenCollectionAndDoc.
@Test
public void collectionThenCollectionAndDoc() throws PermissionDeniedException, EXistException, LockException {
final XmldbURI docUri = uri("all-test.xml");
final long collectionCreated = 1234;
final IMocksControl ctrl = createStrictControl();
ctrl.checkOrder(true);
final BrokerPool mockBrokerPool = ctrl.createMock(BrokerPool.class);
final DBBroker mockBroker = ctrl.createMock(DBBroker.class);
final Collection mockCollection = ctrl.createMock(Collection.class);
final LockedDocument mockLockedDocument = ctrl.createMock(LockedDocument.class);
final DocumentImpl mockDocument = ctrl.createMock(DocumentImpl.class);
expect(mockBrokerPool.getBroker()).andReturn(mockBroker);
expect(mockBroker.openCollection(TEST_COLLECTION_URI, READ_LOCK)).andReturn(mockCollection);
expect(mockCollection.getCreated()).andReturn(collectionCreated);
expect(mockCollection.getDocumentWithLock(mockBroker, docUri, READ_LOCK)).andReturn(mockLockedDocument);
expect(mockLockedDocument.getDocument()).andReturn(mockDocument);
expect(mockCollection.getURI()).andReturn(TEST_COLLECTION_URI);
expect(mockDocument.getFileURI()).andReturn(docUri);
// NOTE: checks that Collection lock is release before Document lock
mockCollection.close();
mockLockedDocument.close();
mockBroker.close();
ctrl.replay();
final Function<Collection, Long> collectionOp = collection -> collection.getCreated();
final BiFunction<Collection, DocumentImpl, String> collectionDocOp = (collection, doc) -> collection.getURI().append(doc.getFileURI()).toString();
final Tuple2<Long, String> result = FluentBrokerAPI.builder(mockBrokerPool).withCollection(TEST_COLLECTION_URI, READ_LOCK).execute(collectionOp).withDocument(collection -> new Tuple2<>(docUri, READ_LOCK)).execute(collectionDocOp).doAll();
assertEquals(collectionCreated, result._1.longValue());
assertEquals(TEST_COLLECTION_URI.append(docUri), result._2);
ctrl.verify();
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LargeValuesTest method restart.
/**
* Just recover.
*/
private void restart() throws EXistException, DatabaseConfigurationException, PermissionDeniedException, IOException, SAXException, LockException {
BrokerPool.FORCE_CORRUPTION = false;
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.READ_LOCK)) {
assertNotNull(root);
try (final LockedDocument lockedDoc = root.getDocumentWithLock(broker, XmldbURI.create("test.xml"), LockMode.READ_LOCK)) {
assertNotNull(lockedDoc);
final Path tempFile = Files.createTempFile("eXist", ".xml");
final Serializer serializer = broker.borrowSerializer();
try (final Writer writer = Files.newBufferedWriter(tempFile, UTF_8)) {
serializer.serialize(lockedDoc.getDocument(), writer);
} finally {
broker.returnSerializer(serializer);
}
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
root.close();
FileUtils.deleteQuietly(tempFile);
// XQuery xquery = broker.getXQueryService();
// DocumentSet docs = broker.getAllXMLResources(new DefaultDocumentSet());
// Sequence result = xquery.execute(broker, "//key/@id/string()", docs.docsToNodeSet(), AccessContext.TEST);
// assertEquals(KEY_COUNT, result.getItemCount());
// for (SequenceIterator i = result.iterate(); i.hasNext();) {
// Item item = i.nextItem();
// String s = item.getStringValue();
// assertTrue(s.length() > 0);
// if (s.length() == 0)
// break;
// }
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class ResourceTest method read2.
private void read2(final BrokerPool pool) throws EXistException, PermissionDeniedException, IOException, TriggerException {
final TransactionManager transact = pool.getTransactionManager();
byte[] data = null;
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
final XmldbURI docPath = TestConstants.TEST_COLLECTION_URI.append(DOCUMENT_NAME_URI);
try (final LockedDocument lockedDoc = broker.getXMLResource(docPath, LockMode.READ_LOCK)) {
// if document is not present, null is returned
if (lockedDoc == null) {
fail("Binary document '" + docPath + " does not exist.");
} else {
final BinaryDocument binDoc = (BinaryDocument) lockedDoc.getDocument();
try (final InputStream is = broker.getBinaryResource(transaction, binDoc)) {
data = new byte[(int) binDoc.getContentLength()];
is.read(data);
}
}
}
try (final Collection collection = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
broker.removeCollection(transaction, collection);
}
transact.commit(transaction);
}
assertEquals(0, data.length);
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class RewriteConfig method configure.
private void configure(final String controllerConfig) throws ServletException {
if (LOG.isDebugEnabled()) {
LOG.debug("Loading XQueryURLRewrite configuration from {}", controllerConfig);
}
if (controllerConfig.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
try (final DBBroker broker = urlRewrite.getBrokerPool().get(Optional.ofNullable(urlRewrite.getDefaultUser()))) {
try (final LockedDocument lockedDocument = broker.getXMLResource(XmldbURI.create(controllerConfig), LockMode.READ_LOCK)) {
final DocumentImpl doc = lockedDocument == null ? null : lockedDocument.getDocument();
if (doc != null) {
parse(doc);
}
}
} catch (final EXistException | PermissionDeniedException e) {
throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
}
} else {
try {
final Path d = Paths.get(urlRewrite.getConfig().getServletContext().getRealPath("/")).normalize();
final Path configFile = d.resolve(controllerConfig);
if (Files.isReadable(configFile)) {
final Document doc = parseConfig(configFile);
parse(doc);
}
} catch (final ParserConfigurationException | IOException | SAXException e) {
throw new ServletException("Failed to parse controller.xml: " + e.getMessage(), e);
}
}
urlRewrite.clearCaches();
}
Aggregations