use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class ReplaceTest method doUpdate.
@Override
protected void doUpdate(final DBBroker broker, final TransactionManager transact, final MutableDocumentSet docs) throws ParserConfigurationException, IOException, SAXException, LockException, XPathException, PermissionDeniedException, EXistException {
final XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
try (final Txn transaction = transact.beginTransaction()) {
String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products\">" + " <product id=\"1\">" + " <description>Product 1</description>" + " <price>24.30</price>" + " <stock>10</stock>" + " </product>" + " </xu:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
// append some new element to records
for (int i = 1; i <= 200; i++) {
xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:insert-before select=\"/products/product[1]\">" + " <product>" + " <xu:attribute name=\"id\"><xu:value-of select=\"count(/products/product) + 1\"/></xu:attribute>" + " <description>Product " + i + "</description>" + " <price>" + (i * 2.5) + "</price>" + " <stock>" + (i * 10) + "</stock>" + " </product>" + " </xu:insert-before>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
transact.commit(transaction);
}
// the following transaction will not be committed and thus undone during recovery
final Txn transaction = transact.beginTransaction();
// replace elements
for (int i = 1; i <= 100; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:replace select=\"/products/product[" + i + "]\">" + " <product id=\"" + i + "\">" + " <description>Replaced product</description>" + " <price>" + (i * 0.75) + "</price>" + " </product>" + " </xu:replace>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
modifications[0].process(transaction);
proc.reset();
}
// DO NOT COMMIT TRANSACTION
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class ShutdownTest method storeAndShutdown.
public void storeAndShutdown() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, XPathException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
Collection test;
try (final Txn transaction = transact.beginTransaction()) {
test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(test);
broker.saveCollection(transaction, test);
// store some documents.
for (final String sampleName : SAMPLES.getShakespeareXmlSampleNames()) {
broker.storeDocument(transaction, XmldbURI.create(sampleName), new InputStreamSupplierInputSource(() -> SAMPLES.getShakespeareSample(sampleName)), MimeType.XML_TYPE, test);
}
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence result = xquery.execute(broker, "//SPEECH[contains(LINE, 'love')]", null);
assertNotNull(result);
assertEquals(187, result.getItemCount());
transact.commit(transaction);
}
try (final Txn transaction = transact.beginTransaction()) {
broker.removeCollection(transaction, test);
transact.commit(transaction);
}
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class BinaryDocumentTest method overwriteCollection.
@Test
public void overwriteCollection() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
final XmldbURI testCollectionUri = XmldbURI.create("/db/overwrite-collection-test");
final XmldbURI thingUri = testCollectionUri.append("thing");
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// create a collection
final Collection thingCollection = broker.getOrCreateCollection(transaction, thingUri);
broker.saveCollection(transaction, thingCollection);
// attempt to create a binary document with the same uri as the thingCollection (should fail)
final Collection testCollection = broker.getCollection(testCollectionUri);
try {
broker.storeDocument(transaction, thingUri.lastSegment(), new StringInputSource("binary-file".getBytes(UTF_8)), MimeType.BINARY_TYPE, testCollection);
fail("Should not have been able to overwrite Collection with Binary Document");
} catch (final EXistException e) {
assertEquals("The collection '" + testCollectionUri.getRawCollectionPath() + "' already has a sub-collection named '" + thingUri.lastSegment().toString() + "', you cannot create a Document with the same name as an existing collection.", e.getMessage());
}
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class CopyCollectionRecoveryTest method store.
private void store() 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 Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
broker.saveCollection(transaction, root);
final Collection test = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI.append("test2"));
broker.saveCollection(transaction, test);
final String sample = getSampleData();
broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test);
final Collection dest = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("destination"));
broker.saveCollection(transaction, dest);
broker.copyCollection(transaction, test, dest, XmldbURI.create("test3"));
transact.commit(transaction);
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class CopyCollectionRecoveryTest method storeAborted.
private void storeAborted() 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()))) {
Collection test2;
try (final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(root);
broker.saveCollection(transaction, root);
test2 = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI.append("test2"));
assertNotNull(test2);
broker.saveCollection(transaction, test2);
final String sample = getSampleData();
broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(sample), MimeType.XML_TYPE, test2);
transact.commit(transaction);
}
final Txn transaction = transact.beginTransaction();
final Collection dest = broker.getOrCreateCollection(transaction, XmldbURI.ROOT_COLLECTION_URI.append("destination"));
assertNotNull(dest);
broker.saveCollection(transaction, dest);
broker.copyCollection(transaction, test2, dest, XmldbURI.create("test3"));
// DO NOT COMMIT TRANSACTION
pool.getJournalManager().get().flush(true, false);
}
}
Aggregations