use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class LocalIndexQueryService method reindexDocument.
private void reindexDocument(final XmldbURI col, final String docName) throws XMLDBException {
final XmldbURI collectionPath = resolve(col);
withDb((broker, transaction) -> {
try (final LockedDocument lockedDoc = broker.getXMLResource(collectionPath.append(docName), LockMode.READ_LOCK)) {
if (lockedDoc != null) {
broker.reindexXMLResource(transaction, lockedDoc.getDocument(), DBBroker.IndexMode.STORE);
broker.sync(Sync.MAJOR);
}
return null;
}
});
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class DocUtils method getDocumentByPathFromDB.
private static Sequence getDocumentByPathFromDB(final XQueryContext context, final String path) throws XPathException, PermissionDeniedException {
// check if the loaded documents should remain locked
final LockMode lockType = context.lockDocumentsOnLoad() ? LockMode.WRITE_LOCK : LockMode.READ_LOCK;
try {
final XmldbURI baseURI = context.getBaseURI().toXmldbURI();
final XmldbURI pathUri;
if (baseURI != null && !(baseURI.equals("") || baseURI.equals("/db"))) {
// relative collection Path: add the current base URI
pathUri = baseURI.resolveCollectionPath(XmldbURI.xmldbUriFor(path, false));
} else {
pathUri = XmldbURI.xmldbUriFor(path, false);
}
// relative collection Path: add the current module call URI if applicable
final XmldbURI resourceUri = Optional.ofNullable(context.getModuleLoadPath()).filter(moduleLoadPath -> !moduleLoadPath.isEmpty()).flatMap(moduleLoadPath -> Try(() -> XmldbURI.xmldbUriFor(moduleLoadPath)).toOption()).map(moduleLoadPath -> moduleLoadPath.resolveCollectionPath(pathUri)).orElse(pathUri);
// try to open the document and acquire a lock
try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(resourceUri, lockType)) {
if (lockedDoc == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
final DocumentImpl doc = lockedDoc.getDocument();
if (!doc.getPermissions().validate(context.getSubject(), Permission.READ)) {
throw new PermissionDeniedException("Insufficient privileges to read resource " + path);
}
if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
throw new XPathException("Document " + path + " is a binary resource, not an XML document. Please consider using the function util:binary-doc() to retrieve a reference to it.");
}
return new NodeProxy(doc);
}
}
} catch (final URISyntaxException e) {
throw new XPathException(e);
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class RemoveCollectionTest method recover.
public void recover(final BrokerPool pool, final boolean checkResource) throws EXistException, PermissionDeniedException, DatabaseConfigurationException, IOException {
LockedDocument lockedDoc = null;
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
if (checkResource) {
lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI.append("hamlet.xml"), LockMode.READ_LOCK);
assertNull("Resource should have been removed", lockedDoc);
}
} finally {
if (lockedDoc != null) {
lockedDoc.close();
}
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class ConcurrentBrokerPoolTest method validateStoredDoc.
private void validateStoredDoc(final Tuple2<Path, UUID> pathUuid) throws EXistException, IOException, DatabaseConfigurationException, PermissionDeniedException, URISyntaxException {
final Path dataDir = pathUuid._1;
assertTrue(Files.exists(dataDir));
final UUID uuid = pathUuid._2;
final Properties config = new Properties();
config.put(BrokerPool.PROPERTY_DATA_DIR, dataDir);
config.put(Journal.PROPERTY_RECOVERY_JOURNAL_DIR, dataDir);
final ExistEmbeddedServer server = new ExistEmbeddedServer("validate-" + uuid.toString(), getConfigFile(getClass()), config, true, false);
server.startDb();
try {
try (final DBBroker broker = server.getBrokerPool().getBroker()) {
try (final LockedDocument doc = broker.getXMLResource(XmldbURI.DB.append(docName(uuid)), Lock.LockMode.READ_LOCK)) {
assertNotNull(doc);
final Source expected = Input.fromString(docContent(uuid)).build();
final Source actual = Input.fromNode(doc.getDocument()).build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
// ASSERT
assertFalse(diff.toString(), diff.hasDifferences());
}
}
} finally {
server.stopDb();
// clear temp files
FileUtils.deleteQuietly(dataDir);
}
}
use of org.exist.dom.persistent.LockedDocument in project exist by eXist-db.
the class SourceFactoryTest method getSourceFromXmldb.
@Test
public void getSourceFromXmldb() throws IOException, PermissionDeniedException {
final String contextPath = "xmldb:exist:///db";
final String location = "library.xqm";
final DBBroker mockBroker = createMock(DBBroker.class);
final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(contextPath).append(location)).times(2);
expect(mockBinDoc.getLastModified()).andReturn(123456789l);
/*expect*/
mockLockedDoc.close();
replay(mockBroker, mockLockedDoc, mockBinDoc);
final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
assertTrue(libSource instanceof DBSource);
assertEquals(XmldbURI.create(contextPath).append(location), ((DBSource) libSource).getDocumentPath());
verify(mockBroker, mockLockedDoc, mockBinDoc);
}
Aggregations