use of org.exist.xupdate.Modification in project exist by eXist-db.
the class AppendTest 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);
try (final Txn transaction = transact.beginTransaction()) {
// append some new element to records
for (int i = 1; i <= 50; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products\">" + " <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:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
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();
// append new element
for (int i = 1; i <= 50; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products/product[" + i + "]\">" + " <date><xu:value-of select=\"current-dateTime()\"/></date>" + " </xu:append>" + "</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.xupdate.Modification in project exist by eXist-db.
the class UpdateAttributeTest 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()) {
// append some new element to records
for (int i = 1; i <= 200; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products\">" + " <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:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] 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();
assertNotNull(transaction);
// update attributes
for (int i = 1; i <= 200; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:update select=\"/products/product[" + i + "]/@id\">" + i + "u</xu:update>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
}
// DO NOT COMMIT TRANSACTION
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class UpdateTest 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()) {
assertNotNull(transaction);
// append some new element to records
for (int i = 1; i <= 200; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:append select=\"/products\">" + " <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:append>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] 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();
assertNotNull(transaction);
// update elements
for (int i = 1; i <= 200; i++) {
final String xupdate = "<xu:modifications version=\"1.0\" xmlns:xu=\"http://www.xmldb.org/xupdate\">" + " <xu:update select=\"/products/product[" + i + "]/price\">19.99</xu:update>" + "</xu:modifications>";
proc.setBroker(broker);
proc.setDocumentSet(docs);
final Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(transaction);
modifications[0].process(transaction);
proc.reset();
}
// DO NOT COMMIT TRANSACTION
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class CustomIndexTest method xupdateRemove.
/**
* Remove nodes from different levels of the tree and check if the index is
* correctly updated.
*/
@Test
public void xupdateRemove() throws EXistException, PermissionDeniedException, XPathException, ParserConfigurationException, 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()) {
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "le8", 1);
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate = XUPDATE_START + " <xu:remove select=\"//item[@id='2']/price\"/>" + XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "892", 0);
checkIndex(broker, docs, "tab", 1);
checkIndex(broker, docs, "le8", 0);
checkIndex(broker, docs, "cab", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:remove select=\"//item[@id='3']/description/text()\"/>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "cab", 0);
checkIndex(broker, docs, "att", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:remove select=\"//item[@id='1']/@attr\"/>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "att", 0);
checkIndex(broker, docs, "cha", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:remove select=\"//item[@id='1']\"/>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "cha", 0);
transact.commit(transaction);
}
}
use of org.exist.xupdate.Modification in project exist by eXist-db.
the class CustomIndexTest method xupdateUpdate.
@Test
public void xupdateUpdate() throws EXistException, LockException, XPathException, PermissionDeniedException, SAXException, IOException, ParserConfigurationException {
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()) {
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "le8", 1);
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate = XUPDATE_START + " <xu:update select=\"//item[@id = '1']/description\">wardrobe</xu:update>" + XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "war", 1);
checkIndex(broker, docs, "cha", 0);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:update select=\"//item[@id = '1']/description/text()\">Wheelchair</xu:update>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "whe", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:update select=\"//item[@id = '1']/@attr\">abc</xu:update>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "abc", 1);
transact.commit(transaction);
}
}
Aggregations